Search in sources :

Example 1 with BloodRequest

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

the class MyRequestDetail method onCreateView.

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mAuth = FirebaseAuth.getInstance();
    user = mAuth.getCurrentUser();
    if (savedInstanceState == null) {
        Bundle extras = getIntent().getExtras();
        if (extras == null) {
            request = null;
        } else {
            request = extras.getString("request");
        }
    }
    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.name);
                mName.setText(data.name);
                TextView mTime = (TextView) findViewById(R.id.time);
                String date = DateFormat.getDateInstance().format(new Date(data.date));
                mTime.setText(date);
                TextView mLocation = (TextView) findViewById(R.id.location);
                mLocation.setText(data.location);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }
    View rootView = inflater.inflate(R.layout.fragment_my_request_details, container, false);
    db = FirebaseDatabase.getInstance().getReference().child("donations");
    fetchData();
    mRecyclerView = (RecyclerView) rootView.findViewById(R.id.respondent_list_recycler_view);
    mRecyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    mRecyclerView.addItemDecoration(new RecycleMarginDecoration(this));
    mAdapter = new RespondentListDataAdapter(users, this);
    mRecyclerView.setAdapter(mAdapter);
    return rootView;
}
Also used : RespondentListDataAdapter(com.jexapps.bloodhub.m_UI.RespondentListDataAdapter) Bundle(android.os.Bundle) DataSnapshot(com.google.firebase.database.DataSnapshot) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) Date(java.util.Date) DefaultItemAnimator(android.support.v7.widget.DefaultItemAnimator) DatabaseError(com.google.firebase.database.DatabaseError) TextView(android.widget.TextView) ValueEventListener(com.google.firebase.database.ValueEventListener) BloodRequest(com.jexapps.bloodhub.m_Model.BloodRequest)

Example 2 with BloodRequest

use of com.jexapps.bloodhub.m_Model.BloodRequest 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 3 with BloodRequest

use of com.jexapps.bloodhub.m_Model.BloodRequest 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 4 with BloodRequest

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

Example 5 with BloodRequest

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

the class AddRequestActivity 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("bloodrequests");
    mStorageRef = FirebaseStorage.getInstance().getReference().child("bloodrequests");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_request);
    setTitle("Add Request");
    name = (AutoCompleteTextView) findViewById(R.id.name);
    bloodgroup = (Spinner) findViewById(R.id.spin);
    quantity = (Spinner) findViewById(R.id.spin1);
    number = (EditText) findViewById(R.id.contact_num);
    location = (AutoCompleteTextView) findViewById(R.id.loc);
    when = (EditText) findViewById(R.id.editText);
    diagnosis = (Spinner) findViewById(R.id.diagnosis);
    transport_group = (RadioGroup) findViewById(R.id.transport);
    image = (TextView) findViewById(R.id.image_text);
    String[] hospitals = getResources().getStringArray(R.array.organizations_array);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, hospitals);
    location.setAdapter(adapter);
    final EditText set = (EditText) findViewById(R.id.editText);
    set.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            dialog = new Dialog(AddRequestActivity.this);
            dialog.setTitle("Set Date and Time");
            dialog.setContentView(R.layout.set_date);
            dialog.show();
            final Button setDate = (Button) dialog.findViewById(R.id.set_date);
            final DatePicker datePicker = (DatePicker) dialog.findViewById(R.id.datePicker);
            datePicker.setMinDate(System.currentTimeMillis() - 1000);
            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);
                    dialog.cancel();
                }
            });
        }
    });
    Button image_btn = (Button) findViewById(R.id.upload_image_button);
    image_btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            final Context context = getApplicationContext();
            Intent intent = new Intent(Intent.ACTION_PICK);
            intent.setType("image/*");
            startActivityForResult(intent, GALLERY_INTENT);
        }
    });
    Button submit = (Button) findViewById(R.id.submit_button);
    submit.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            pname = name.getText().toString();
            bgroup = bloodgroup.getSelectedItem().toString();
            quan = quantity.getSelectedItem().toString();
            diag = diagnosis.getSelectedItem().toString();
            num = number.getText().toString();
            loc = location.getText().toString();
            transport_btn = (RadioButton) findViewById(transport_group.getCheckedRadioButtonId());
            String transport_text = (String) transport_btn.getText();
            if (transport_text.equals("Available")) {
                transport = true;
            } else if (transport_text.equals("Not Available")) {
                transport = false;
            }
            new_request = db.push();
            String address = loc + ", Pakistan";
            new GetCoordinates().execute(address.replace(" ", "+"));
            if (image_file != null) {
                mStorageRef.child(new_request.getKey()).putFile(image_file).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        // Toast.makeText(getApplicationContext(),"Image uploaded",
                        // Toast.LENGTH_SHORT).show();
                        String regToken = FirebaseInstanceId.getInstance().getToken();
                        BloodRequest request = new BloodRequest(user.getUid(), pname, bgroup, quan, num, loc, lat, lng, diag, pdate.getTime(), transport, regToken);
                        new_request.setValue(request);
                        request_added = true;
                        dialog = new Dialog(AddRequestActivity.this);
                        dialog.setContentView(R.layout.popup_submit);
                        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(AddRequestActivity.this, MainActivity.class);
                                startActivity(intent);
                            }
                        });
                    }
                }).addOnFailureListener(new OnFailureListener() {

                    @Override
                    public void onFailure(@NonNull Exception exception) {
                        Toast.makeText(getApplicationContext(), "Error uploading image", Toast.LENGTH_SHORT).show();
                    }
                });
            } else {
                String regToken = FirebaseInstanceId.getInstance().getToken();
                BloodRequest request = new BloodRequest(user.getUid(), pname, bgroup, quan, num, loc, lat, lng, diag, pdate.getTime(), transport, regToken);
                new_request.setValue(request);
                request_added = true;
                dialog = new Dialog(AddRequestActivity.this);
                dialog.setContentView(R.layout.popup_submit);
                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(AddRequestActivity.this, MainActivity.class);
                        startActivity(intent);
                    }
                });
            }
        }
    });
}
Also used : UploadTask(com.google.firebase.storage.UploadTask) RadioButton(android.widget.RadioButton) Button(android.widget.Button) Dialog(android.app.Dialog) NonNull(android.support.annotation.NonNull) BloodRequest(com.jexapps.bloodhub.m_Model.BloodRequest) EditText(android.widget.EditText) Context(android.content.Context) Calendar(java.util.Calendar) Intent(android.content.Intent) RadioButton(android.widget.RadioButton) View(android.view.View) AutoCompleteTextView(android.widget.AutoCompleteTextView) TextView(android.widget.TextView) ColorDrawable(android.graphics.drawable.ColorDrawable) DatePicker(android.widget.DatePicker) ArrayAdapter(android.widget.ArrayAdapter) OnFailureListener(com.google.android.gms.tasks.OnFailureListener)

Aggregations

BloodRequest (com.jexapps.bloodhub.m_Model.BloodRequest)11 Date (java.util.Date)8 DataSnapshot (com.google.firebase.database.DataSnapshot)7 DatabaseError (com.google.firebase.database.DatabaseError)7 ValueEventListener (com.google.firebase.database.ValueEventListener)7 TextView (android.widget.TextView)4 Intent (android.content.Intent)3 Bundle (android.os.Bundle)3 View (android.view.View)3 Calendar (java.util.Calendar)3 Dialog (android.app.Dialog)2 Context (android.content.Context)2 ColorDrawable (android.graphics.drawable.ColorDrawable)2 DefaultItemAnimator (android.support.v7.widget.DefaultItemAnimator)2 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)2 Button (android.widget.Button)2 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)2 FirebaseAuth (com.google.firebase.auth.FirebaseAuth)2 FirebaseUser (com.google.firebase.auth.FirebaseUser)2 Donation (com.jexapps.bloodhub.m_Model.Donation)2