Search in sources :

Example 1 with Donation

use of com.jexapps.bloodhub.m_Model.Donation in project BloodHub by kazijehangir.

the class DonationsFragment method fetchData.

// Getting data from database
public ArrayList<BloodRequest> fetchData() {
    donations = new ArrayList<BloodRequest>();
    FirebaseAuth mAuth = FirebaseAuth.getInstance();
    FirebaseUser user = mAuth.getCurrentUser();
    db.orderByChild("userid").equalTo(user.getUid()).addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot child : dataSnapshot.getChildren()) {
                Donation donation = child.getValue(Donation.class);
                if (donation != null) {
                    FirebaseDatabase.getInstance().getReference().child("bloodrequests").child(donation.requestid).addListenerForSingleValueEvent(new ValueEventListener() {

                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            BloodRequest request = dataSnapshot.getValue(BloodRequest.class);
                            if (request != null) {
                                donations.add(request);
                                numDonations.setText("LIVES SAVED: " + donations.size());
                                mAdapter.notifyDataSetChanged();
                            }
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {
                        }
                    });
                }
                FirebaseDatabase.getInstance().getReference().child("bloodrequests").child(donation.requestid).addListenerForSingleValueEvent(new ValueEventListener() {

                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        BloodRequest request = dataSnapshot.getValue(BloodRequest.class);
                        donations.add(request);
                        numDonations.setText("LIVES SAVED: " + donations.size());
                        mAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                    }
                });
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
    return donations;
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) Donation(com.jexapps.bloodhub.m_Model.Donation) FirebaseUser(com.google.firebase.auth.FirebaseUser) ValueEventListener(com.google.firebase.database.ValueEventListener) BloodRequest(com.jexapps.bloodhub.m_Model.BloodRequest) DataSnapshot(com.google.firebase.database.DataSnapshot) FirebaseAuth(com.google.firebase.auth.FirebaseAuth)

Example 2 with Donation

use of com.jexapps.bloodhub.m_Model.Donation 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 Donation

use of com.jexapps.bloodhub.m_Model.Donation in project BloodHub by kazijehangir.

the class MyRequestDetail method fetchData.

public void fetchData() {
    donations = new ArrayList<Donation>();
    keys = new ArrayList<String>();
    users = new ArrayList<User>();
    final DatabaseReference dbUsers = FirebaseDatabase.getInstance().getReference().child("users");
    // dbBloodRequests.orderByChild("userid").equalTo(user.getUid()).addValueEventListener(new ValueEventListener() {
    // @Override
    // public void onDataChange(DataSnapshot dataSnapshot) {
    // requests.clear();
    // //                keys.clear();
    // for (DataSnapshot child: dataSnapshot.getChildren()) {
    // requests.add(child.getKey());
    // }
    // }
    // @Override
    // public void onCancelled(DatabaseError databaseError) {
    // }
    // });
    db.orderByChild("requestid").equalTo(request).addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot child : dataSnapshot.getChildren()) {
                Donation donation = child.getValue(Donation.class);
                dbUsers.child(donation.userid).addValueEventListener(new ValueEventListener() {

                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        User user = dataSnapshot.getValue(User.class);
                        users.add(user);
                        mAdapter.notifyDataSetChanged();
                    }

                    // mAdapter.;
                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                    }
                });
            }
            mAdapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}
Also used : User(com.jexapps.bloodhub.m_Model.User) FirebaseUser(com.google.firebase.auth.FirebaseUser) DatabaseError(com.google.firebase.database.DatabaseError) DatabaseReference(com.google.firebase.database.DatabaseReference) Donation(com.jexapps.bloodhub.m_Model.Donation) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Aggregations

DataSnapshot (com.google.firebase.database.DataSnapshot)3 DatabaseError (com.google.firebase.database.DatabaseError)3 ValueEventListener (com.google.firebase.database.ValueEventListener)3 Donation (com.jexapps.bloodhub.m_Model.Donation)3 FirebaseUser (com.google.firebase.auth.FirebaseUser)2 BloodRequest (com.jexapps.bloodhub.m_Model.BloodRequest)2 Dialog (android.app.Dialog)1 Context (android.content.Context)1 Intent (android.content.Intent)1 ColorDrawable (android.graphics.drawable.ColorDrawable)1 Bundle (android.os.Bundle)1 View (android.view.View)1 Button (android.widget.Button)1 TextView (android.widget.TextView)1 FirebaseAuth (com.google.firebase.auth.FirebaseAuth)1 DatabaseException (com.google.firebase.database.DatabaseException)1 DatabaseReference (com.google.firebase.database.DatabaseReference)1 User (com.jexapps.bloodhub.m_Model.User)1 Date (java.util.Date)1