Search in sources :

Example 61 with ValueEventListener

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

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

the class LoginActivity method setView.

/**
 * Attempts to sign in or register the account specified by the login form.
 * If there are form errors (invalid email, missing fields, etc.), the
 * errors are presented and no actual login attempt is made.
 */
private void setView(String userId, final String email) {
    final String uid = userId;
    final Context context = getApplicationContext();
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    FirebaseDatabase.getInstance().getReference().child("users").child(uid).addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            User user = dataSnapshot.getValue(User.class);
            String account_type = user.account_type;
            showProgress(false);
            if (account_type.equals("individual")) {
                Intent intent;
                intent = new Intent(LoginActivity.this, MainActivity.class);
                intent.putExtra("mEmail", email);
                startActivity(intent);
            } else if (account_type.equals("organization")) {
                Intent intent;
                intent = new Intent(LoginActivity.this, MainActivityOrg.class);
                intent.putExtra("mEmail", email);
                startActivity(intent);
            }
        }

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

Example 63 with ValueEventListener

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

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

the class UserProfile method onStart.

@Override
public void onStart() {
    super.onStart();
    final TextView name = (TextView) findViewById(R.id.name);
    final TextView bgroup = (TextView) findViewById(R.id.blood_g);
    final TextView number = (TextView) findViewById(R.id.con_num);
    // add value event listener to the user
    ValueEventListener userListener = new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            User curruser = dataSnapshot.getValue(User.class);
            name.setText(curruser.username);
            bgroup.setText("Blood group: " + curruser.blood_group);
            number.setText("Number : " + curruser.number);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    };
    mUserReference.addValueEventListener(userListener);
}
Also used : User(com.jexapps.bloodhub.m_Model.User) FirebaseUser(com.google.firebase.auth.FirebaseUser) DatabaseError(com.google.firebase.database.DatabaseError) TextView(android.widget.TextView) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 65 with ValueEventListener

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

the class RequestListFragment method fetchData.

// Getting data from database
public void fetchData() {
    requests = new ArrayList<BloodRequest>();
    keys = new ArrayList<String>();
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    Date startDate = cal.getTime();
    // Date endDate = new Date();
    db.orderByChild("date").startAt(startDate.getTime()).limitToFirst(15).addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot child : dataSnapshot.getChildren()) {
                BloodRequest request = child.getValue(BloodRequest.class);
                requests.add(request);
                keys.add(child.getKey());
            }
            mAdapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
    return;
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) Calendar(java.util.Calendar) ValueEventListener(com.google.firebase.database.ValueEventListener) BloodRequest(com.jexapps.bloodhub.m_Model.BloodRequest) DataSnapshot(com.google.firebase.database.DataSnapshot) Date(java.util.Date)

Aggregations

DataSnapshot (com.google.firebase.database.DataSnapshot)211 ValueEventListener (com.google.firebase.database.ValueEventListener)211 DatabaseError (com.google.firebase.database.DatabaseError)210 DatabaseReference (com.google.firebase.database.DatabaseReference)62 View (android.view.View)47 Intent (android.content.Intent)43 TextView (android.widget.TextView)30 FirebaseDatabase (com.google.firebase.database.FirebaseDatabase)24 RecyclerView (android.support.v7.widget.RecyclerView)20 FirebaseUser (com.google.firebase.auth.FirebaseUser)20 HashMap (java.util.HashMap)20 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)19 Bundle (android.os.Bundle)16 ImageView (android.widget.ImageView)15 ArrayList (java.util.ArrayList)15 User (com.jexapps.bloodhub.m_Model.User)11 Map (java.util.Map)11 Date (java.util.Date)10 Query (com.google.firebase.database.Query)9 User (com.polito.mad17.madmax.entities.User)9