Search in sources :

Example 1 with DatabaseException

use of com.google.firebase.database.DatabaseException 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 2 with DatabaseException

use of com.google.firebase.database.DatabaseException in project BloodHub by kazijehangir.

the class RequestDetail method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_request_detail);
    mAuth = FirebaseAuth.getInstance();
    user = mAuth.getCurrentUser();
    setTitle("Donate blood");
    if (savedInstanceState == null) {
        Bundle extras = getIntent().getExtras();
        if (extras == null) {
            request = null;
        } else {
            request = extras.getString("request");
        }
    }
    db = FirebaseDatabase.getInstance().getReference().child("donations");
    if (request != null && !request.isEmpty()) {
        FirebaseDatabase.getInstance().getReference().child("bloodrequests").child(request).addListenerForSingleValueEvent(new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                BloodRequest data = dataSnapshot.getValue(BloodRequest.class);
                TextView mName = (TextView) findViewById(R.id.request_detail_name);
                mName.setText(data.name);
                TextView mNeeds = (TextView) findViewById(R.id.request_detail_needs);
                mNeeds.setText(data.quantity + " bags of " + data.blood_group);
                TextView mLocation = (TextView) findViewById(R.id.request_detail_location);
                mLocation.setText(data.location);
                TextView mWhen = (TextView) findViewById(R.id.request_detail_when);
                String date = DateFormat.getDateInstance().format(new Date(data.date));
                mWhen.setText(date);
                if (date.equals(DateFormat.getDateInstance().format(new Date()))) {
                    mWhen.setText("URGENT");
                    mWhen.setTextColor(0xFFFF0000);
                }
                TextView mDiagnosis = (TextView) findViewById(R.id.request_detail_diagnosis);
                mDiagnosis.setText(data.diagnosis);
                TextView mTransport = (TextView) findViewById(R.id.request_detail_transport);
                if (data.transport) {
                    mTransport.setText("Available");
                } else {
                    mTransport.setText("Not Available");
                }
                if (user.getUid().equals(data.userid)) {
                    isOwnRequest = true;
                    setTitle("Request Info");
                    Button donate = (Button) findViewById(R.id.request_detail_donate_button);
                    donate.setText("Open in My Requests");
                    donate.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(view.getContext(), MyRequestDetail.class);
                            intent.putExtra("request", request);
                            view.getContext().startActivity(intent);
                        }
                    });
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    } else {
        Toast.makeText(getApplicationContext(), "Error loading request", Toast.LENGTH_SHORT).show();
        finish();
    }
    Button donate = (Button) findViewById(R.id.request_detail_donate_button);
    if (!isOwnRequest) {
        donate.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                final Context context = getApplicationContext();
                Donation donation = new Donation(user.getUid(), request);
                try {
                    db.push().setValue(donation);
                    dialog = new Dialog(RequestDetail.this);
                    dialog.setTitle("Donation Confirmed");
                    dialog.setContentView(R.layout.popup_request_detail);
                    dialog.show();
                    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                    final Button submit = (Button) dialog.findViewById(R.id.request_detail_dialog_ok);
                    submit.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View view) {
                            finish();
                        }
                    });
                } catch (DatabaseException e) {
                    Toast.makeText(context, "Error occurred", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
Also used : Context(android.content.Context) Bundle(android.os.Bundle) Intent(android.content.Intent) DataSnapshot(com.google.firebase.database.DataSnapshot) TextView(android.widget.TextView) View(android.view.View) Date(java.util.Date) DatabaseError(com.google.firebase.database.DatabaseError) ColorDrawable(android.graphics.drawable.ColorDrawable) Button(android.widget.Button) Dialog(android.app.Dialog) Donation(com.jexapps.bloodhub.m_Model.Donation) TextView(android.widget.TextView) ValueEventListener(com.google.firebase.database.ValueEventListener) BloodRequest(com.jexapps.bloodhub.m_Model.BloodRequest) DatabaseException(com.google.firebase.database.DatabaseException)

Example 3 with DatabaseException

use of com.google.firebase.database.DatabaseException in project BloodHub by kazijehangir.

the class AddDonorActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    mAuth = FirebaseAuth.getInstance();
    user = mAuth.getCurrentUser();
    mEmail = user.getEmail();
    // INITIALIZE FIREBASE DB
    db = FirebaseDatabase.getInstance().getReference().child("donors");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_donor);
    setTitle("Add Donor");
    name = (AutoCompleteTextView) findViewById(R.id.name);
    bloodgroup = (Spinner) findViewById(R.id.spin);
    age = (EditText) findViewById(R.id.age);
    number = (EditText) findViewById(R.id.editText2);
    address = (EditText) findViewById(R.id.editText3);
    donorOrigin = (EditText) findViewById(R.id.editText4);
    lastDonated = (EditText) findViewById(R.id.editText);
    final EditText set = (EditText) findViewById(R.id.editText);
    set.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            AlertDialog.Builder builder = new AlertDialog.Builder(AddDonorActivity.this);
            builder.setTitle("Last donated");
            builder.setItems(options, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (options[which] == "Set date") {
                        final Dialog dialog1;
                        dialog1 = new Dialog(AddDonorActivity.this);
                        dialog1.setTitle("Set Date and Time");
                        dialog1.setContentView(R.layout.set_date);
                        dialog1.show();
                        final Button setDate = (Button) dialog1.findViewById(R.id.set_date);
                        final DatePicker datePicker = (DatePicker) dialog1.findViewById(R.id.datePicker);
                        setDate.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View view) {
                                date = datePicker.getDayOfMonth();
                                month = datePicker.getMonth();
                                year = datePicker.getYear();
                                Calendar calendar = Calendar.getInstance();
                                calendar.set(year, month, date);
                                pdate = calendar.getTime();
                                String date = DateFormat.getDateInstance().format(pdate);
                                set.setText(date);
                                dialog1.cancel();
                            }
                        });
                    } else {
                        set.setText("Never");
                    }
                }
            });
            builder.show();
        }
    });
    Button addDonor = (Button) findViewById(R.id.add_donor);
    addDonor.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            final Context context = getApplicationContext();
            dName = name.getText().toString();
            dBloodgroup = bloodgroup.getSelectedItem().toString();
            dAge = age.getText().toString();
            dNumber = number.getText().toString();
            dAddress = address.getText().toString();
            ddonorOrigin = donorOrigin.getText().toString();
            dLastDonated = lastDonated.getText().toString();
            View focusView = null;
            if (TextUtils.isEmpty(dName)) {
                name.setError("Name cannot be empty");
                focusView = name;
            } else if (TextUtils.isEmpty(dNumber)) {
                number.setError("Number cannot be empty");
            } else {
                Donor donor = new Donor(user.getUid(), dName, dBloodgroup, dNumber, dAddress, dLastDonated, ddonorOrigin, dAge);
                try {
                    db.push().setValue(donor);
                    dialog = new Dialog(AddDonorActivity.this);
                    dialog.setTitle("Add Donor");
                    dialog.setContentView(R.layout.popup_donor);
                    dialog.show();
                    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                    final Button submit = (Button) dialog.findViewById(R.id.button_ok);
                    submit.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(AddDonorActivity.this, MainActivityOrg.class);
                            startActivity(intent);
                        }
                    });
                } catch (DatabaseException e) {
                    Toast.makeText(context, "Error occurred", Toast.LENGTH_SHORT).show();
                }
            }
        // else if(dBloodgroup) {
        // bloodgroup.setError("Blood group can not be empty");
        // }
        }
    });
}
Also used : EditText(android.widget.EditText) AlertDialog(android.support.v7.app.AlertDialog) Context(android.content.Context) DialogInterface(android.content.DialogInterface) Calendar(java.util.Calendar) Intent(android.content.Intent) View(android.view.View) AutoCompleteTextView(android.widget.AutoCompleteTextView) Donor(com.jexapps.bloodhub.m_Model.Donor) ColorDrawable(android.graphics.drawable.ColorDrawable) Button(android.widget.Button) Dialog(android.app.Dialog) AlertDialog(android.support.v7.app.AlertDialog) DatePicker(android.widget.DatePicker) DatabaseException(com.google.firebase.database.DatabaseException)

Example 4 with DatabaseException

use of com.google.firebase.database.DatabaseException in project BloodHub by kazijehangir.

the class AddPatientActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    mAuth = FirebaseAuth.getInstance();
    user = mAuth.getCurrentUser();
    mEmail = user.getEmail();
    // INITIALIZE FIREBASE DB
    db = FirebaseDatabase.getInstance().getReference().child("patients");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_patient);
    setTitle("Add Patient");
    name = (AutoCompleteTextView) findViewById(R.id.name);
    age = (EditText) findViewById(R.id.age);
    bloodgroup = (Spinner) findViewById(R.id.spin);
    number = (EditText) findViewById(R.id.contact_num);
    diagnosis = (Spinner) findViewById(R.id.diagnosis);
    Button submit = (Button) findViewById(R.id.add_patient);
    submit.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            final Context context = getApplicationContext();
            pname = name.getText().toString();
            a = age.getText().toString();
            bgroup = bloodgroup.getSelectedItem().toString();
            diag = diagnosis.getSelectedItem().toString();
            num = number.getText().toString();
            Patient pat = new Patient(user.getUid(), pname, a, bgroup, num, diag);
            try {
                db.push().setValue(pat);
                dialog = new Dialog(AddPatientActivity.this);
                dialog.setTitle("Add Patient");
                dialog.setContentView(R.layout.popup_patient);
                dialog.show();
                dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                final Button submit = (Button) dialog.findViewById(R.id.button_ok);
                submit.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(AddPatientActivity.this, MainActivityOrg.class);
                        startActivity(intent);
                    }
                });
            } catch (DatabaseException e) {
                Toast.makeText(context, "Error occurred", Toast.LENGTH_SHORT).show();
            }
        }
    });
}
Also used : Context(android.content.Context) ColorDrawable(android.graphics.drawable.ColorDrawable) Button(android.widget.Button) Dialog(android.app.Dialog) Patient(com.jexapps.bloodhub.m_Model.Patient) Intent(android.content.Intent) AutoCompleteTextView(android.widget.AutoCompleteTextView) View(android.view.View) DatabaseException(com.google.firebase.database.DatabaseException)

Example 5 with DatabaseException

use of com.google.firebase.database.DatabaseException in project iosched by google.

the class FeedPresenter method initializeDataListener.

@Override
public void initializeDataListener(DatabaseReference databaseReference) {
    if (mEventListener == null) {
        mEventListener = new ChildEventListener() {

            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                try {
                    FeedMessage feedMessage = dataSnapshot.getValue(FeedMessage.class);
                    mView.addFeedMessage(feedMessage);
                } catch (DatabaseException e) {
                    mView.showErrorMessage();
                    LOGE(TAG, "Firebase error - " + e);
                }
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                try {
                    FeedMessage feedMessage = dataSnapshot.getValue(FeedMessage.class);
                    mView.updateFeedMessage(feedMessage);
                } catch (DatabaseException e) {
                    mView.showErrorMessage();
                    LOGE(TAG, "Firebase error - " + e);
                }
            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {
                try {
                    FeedMessage feedMessage = dataSnapshot.getValue(FeedMessage.class);
                    mView.removeFeedMessage(feedMessage);
                } catch (DatabaseException e) {
                    mView.showErrorMessage();
                    LOGE(TAG, "Firebase error - " + e);
                }
            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {
                try {
                    FeedMessage feedMessage = dataSnapshot.getValue(FeedMessage.class);
                    mView.updateFeedMessage(feedMessage);
                } catch (DatabaseException e) {
                    mView.showErrorMessage();
                    LOGE(TAG, "Firebase error - " + e);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                mView.showErrorMessage();
            }
        };
    }
    databaseReference.addChildEventListener(mEventListener);
}
Also used : FeedMessage(com.google.samples.apps.iosched.feed.data.FeedMessage) DatabaseError(com.google.firebase.database.DatabaseError) DataSnapshot(com.google.firebase.database.DataSnapshot) DatabaseException(com.google.firebase.database.DatabaseException) ChildEventListener(com.google.firebase.database.ChildEventListener)

Aggregations

DatabaseException (com.google.firebase.database.DatabaseException)6 DataSnapshot (com.google.firebase.database.DataSnapshot)4 DatabaseError (com.google.firebase.database.DatabaseError)4 Dialog (android.app.Dialog)3 Context (android.content.Context)3 Intent (android.content.Intent)3 ColorDrawable (android.graphics.drawable.ColorDrawable)3 View (android.view.View)3 Button (android.widget.Button)3 ValueEventListener (com.google.firebase.database.ValueEventListener)3 AutoCompleteTextView (android.widget.AutoCompleteTextView)2 DialogInterface (android.content.DialogInterface)1 Bundle (android.os.Bundle)1 AlertDialog (android.support.v7.app.AlertDialog)1 DatePicker (android.widget.DatePicker)1 EditText (android.widget.EditText)1 TextView (android.widget.TextView)1 FirebaseUser (com.google.firebase.auth.FirebaseUser)1 ChildEventListener (com.google.firebase.database.ChildEventListener)1 FeedMessage (com.google.samples.apps.iosched.feed.data.FeedMessage)1