Search in sources :

Example 1 with QueueAction

use of com.google.samples.apps.iosched.session.data.QueueAction in project iosched by google.

the class SessionDetailModel method attemptReserve.

public void attemptReserve() {
    FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
    if (mQueueReference != null && currentUser != null) {
        final String requestId = generateReserveRequestId();
        QueueAction queueAction = new QueueAction(mSessionId, "reserve", requestId);
        mQueueReference.addValueEventListener(mQueueEventListener);
        mQueueReference.setValue(queueAction).addOnCompleteListener(new OnCompleteListener<Void>() {

            @Override
            public void onComplete(@NonNull Task<Void> task) {
                LOGD(TAG, "Enter queue.");
                mReservationPending = true;
                mReturnPending = false;
                DataQueryCallback<SessionDetailQueryEnum> reservationPendingCallback = mDataQueryCallbacks.get(RESERVATION_PENDING);
                reservationPendingCallback.onModelUpdated(SessionDetailModel.this, RESERVATION_PENDING);
                mSessionReservationResultReference.child(requestId).addValueEventListener(mSessionReservationResultEventListener);
            }
        }).addOnFailureListener(new OnFailureListener() {

            @Override
            public void onFailure(@NonNull Exception e) {
                LOGE(TAG, e.getMessage());
                mReservationPending = false;
                mReturnPending = false;
                DataQueryCallback<SessionDetailQueryEnum> reservationFailedCallback = mDataQueryCallbacks.get(RESERVATION_FAILED);
                reservationFailedCallback.onModelUpdated(SessionDetailModel.this, RESERVATION_FAILED);
            }
        });
    }
}
Also used : OnCompleteListener(com.google.android.gms.tasks.OnCompleteListener) Task(com.google.android.gms.tasks.Task) SessionDetailQueryEnum(com.google.samples.apps.iosched.session.SessionDetailModel.SessionDetailQueryEnum) QueueAction(com.google.samples.apps.iosched.session.data.QueueAction) NonNull(android.support.annotation.NonNull) FirebaseUser(com.google.firebase.auth.FirebaseUser) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) DatabaseException(com.google.firebase.database.DatabaseException)

Example 2 with QueueAction

use of com.google.samples.apps.iosched.session.data.QueueAction in project iosched by google.

the class SessionDetailModel method initReservationListeners.

public void initReservationListeners() {
    FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
    if (currentUser != null) {
        mQueueReference = getQueueReference(currentUser.getUid());
        mSessionReservationStatusReference = getReservationStatusReference(currentUser.getUid(), mSessionId);
        mSessionReservationResultReference = getReservationResultsReference(currentUser.getUid(), mSessionId);
        mSeatAvailabilityReference = getSeatAvailabilityReference(mSessionId);
        mSessionReservationStatusEventListener = new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                try {
                    String reservationStatus = dataSnapshot.getValue(String.class);
                    if (reservationStatus != null) {
                        mReservationStatus = reservationStatus;
                        mSessionsHelper.setReservationStatus(mSessionUri, ScheduleContract.MyReservations.fromFirebaseString(mReservationStatus), mTitle);
                        final DataQueryCallback<SessionDetailQueryEnum> reservationStatusCallback = mDataQueryCallbacks.get(RESERVATION_STATUS);
                        reservationStatusCallback.onModelUpdated(SessionDetailModel.this, RESERVATION_STATUS);
                    }
                } catch (DatabaseException e) {
                    LOGE(TAG, e.getMessage());
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                LOGE(TAG, databaseError.getMessage());
            }
        };
        mSessionReservationResultEventListener = new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                try {
                    String reservationResult = dataSnapshot.getValue(String.class);
                    if (reservationResult != null) {
                        mReservationResult = reservationResult;
                        DataQueryCallback<SessionDetailQueryEnum> reservationResultCallback = mDataQueryCallbacks.get(RESERVATION_RESULT);
                        reservationResultCallback.onModelUpdated(SessionDetailModel.this, RESERVATION_RESULT);
                        mSessionReservationResultReference.removeEventListener(this);
                    }
                } catch (DatabaseException e) {
                    LOGE(TAG, e.getMessage());
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                LOGE(TAG, databaseError.getMessage());
            }
        };
        mQueueEventListener = new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                try {
                    DataQueryCallback<SessionDetailQueryEnum> reservationPendingCallback = mDataQueryCallbacks.get(RESERVATION_PENDING);
                    QueueAction queueAction = dataSnapshot.getValue(QueueAction.class);
                    if (queueAction == null) {
                        LOGD(TAG, "Exit queue");
                        mReturnPending = false;
                        mReservationPending = false;
                        mQueueReference.removeEventListener(this);
                        reservationPendingCallback.onModelUpdated(SessionDetailModel.this, RESERVATION_PENDING);
                    }
                } catch (DatabaseException e) {
                    LOGE(TAG, e.getMessage());
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                LOGE(TAG, databaseError.getMessage());
            }
        };
        mSeatAvailabilityEventListener = new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                DataQueryCallback<SessionDetailQueryEnum> seatAvailabilityCallback = mDataQueryCallbacks.get(RESERVATION_SEAT_AVAILABILITY);
                Boolean seatsAvailable = dataSnapshot.getValue(Boolean.class);
                if (seatsAvailable != null) {
                    mSeatsAvailable = seatsAvailable;
                    seatAvailabilityCallback.onModelUpdated(SessionDetailModel.this, RESERVATION_SEAT_AVAILABILITY);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                LOGE(TAG, databaseError.getMessage());
            }
        };
        mSessionReservationStatusReference.addValueEventListener(mSessionReservationStatusEventListener);
        mSeatAvailabilityReference.addValueEventListener(mSeatAvailabilityEventListener);
    } else {
        LOGD(TAG, "Not signed in.");
        invalidateReservationCache();
        DataQueryCallback<SessionDetailQueryEnum> authCallback = mDataQueryCallbacks.get(AUTH_REGISTRATION);
        authCallback.onModelUpdated(SessionDetailModel.this, AUTH_REGISTRATION);
    }
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) SessionDetailQueryEnum(com.google.samples.apps.iosched.session.SessionDetailModel.SessionDetailQueryEnum) QueueAction(com.google.samples.apps.iosched.session.data.QueueAction) FirebaseUser(com.google.firebase.auth.FirebaseUser) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot) DatabaseException(com.google.firebase.database.DatabaseException)

Example 3 with QueueAction

use of com.google.samples.apps.iosched.session.data.QueueAction in project iosched by google.

the class SessionDetailModel method attemptReturnReservation.

public void attemptReturnReservation() {
    FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
    if (mQueueReference != null && currentUser != null) {
        final String requestId = generateReserveRequestId();
        QueueAction queueAction = new QueueAction(mSessionId, "return", requestId);
        mQueueReference.addValueEventListener(mQueueEventListener);
        mQueueReference.setValue(queueAction).addOnCompleteListener(new OnCompleteListener<Void>() {

            @Override
            public void onComplete(@NonNull Task<Void> task) {
                LOGD(TAG, "Enter queue.");
                mReservationPending = false;
                mReturnPending = true;
                DataQueryCallback<SessionDetailQueryEnum> reservationPendingCallback = mDataQueryCallbacks.get(RESERVATION_PENDING);
                reservationPendingCallback.onModelUpdated(SessionDetailModel.this, RESERVATION_PENDING);
                mSessionReservationResultReference.child(requestId).addValueEventListener(mSessionReservationResultEventListener);
            }
        }).addOnFailureListener(new OnFailureListener() {

            @Override
            public void onFailure(@NonNull Exception e) {
                LOGE(TAG, e.getMessage());
                mReservationPending = false;
                mReturnPending = false;
                DataQueryCallback<SessionDetailQueryEnum> reservationFailedCallback = mDataQueryCallbacks.get(RESERVATION_FAILED);
                reservationFailedCallback.onModelUpdated(SessionDetailModel.this, RESERVATION_FAILED);
            }
        });
    }
}
Also used : OnCompleteListener(com.google.android.gms.tasks.OnCompleteListener) Task(com.google.android.gms.tasks.Task) SessionDetailQueryEnum(com.google.samples.apps.iosched.session.SessionDetailModel.SessionDetailQueryEnum) QueueAction(com.google.samples.apps.iosched.session.data.QueueAction) NonNull(android.support.annotation.NonNull) FirebaseUser(com.google.firebase.auth.FirebaseUser) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) DatabaseException(com.google.firebase.database.DatabaseException)

Aggregations

FirebaseUser (com.google.firebase.auth.FirebaseUser)3 DatabaseException (com.google.firebase.database.DatabaseException)3 SessionDetailQueryEnum (com.google.samples.apps.iosched.session.SessionDetailModel.SessionDetailQueryEnum)3 QueueAction (com.google.samples.apps.iosched.session.data.QueueAction)3 NonNull (android.support.annotation.NonNull)2 OnCompleteListener (com.google.android.gms.tasks.OnCompleteListener)2 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)2 Task (com.google.android.gms.tasks.Task)2 DataSnapshot (com.google.firebase.database.DataSnapshot)1 DatabaseError (com.google.firebase.database.DatabaseError)1 ValueEventListener (com.google.firebase.database.ValueEventListener)1