SelectPatientsFragment.java 6.47 KB
package com.sw.laryngoscope.FHIR.fragment;


import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sw.laryngoscope.R;
import com.sw.laryngoscope.FHIR.adapter.SelectPatientsAdapter;
import com.sw.laryngoscope.databinding.FgPatientFragmentBinding;
import com.sw.laryngoscope.FHIR.model.PatientModel;
import com.sw.laryngoscope.FHIR.viewModel.SharedViewModel;

import java.util.ArrayList;
import java.util.HashMap;

public class SelectPatientsFragment extends Fragment implements SelectPatientsAdapter.OnPatientClickListener{
    private String practitionerID;
    private SelectPatientsFragment thisFrag; // a reference to this fragment
//    private Toolbar toolbar;
    private SharedViewModel sharedViewModel;
//    private RecyclerView recyclerView;
    private ArrayList<PatientModel> selectedPatients = new ArrayList<>();
//    private TextView title;
//    private TextView loadingTextView;
//    private ImageButton backButton;
//    private ProgressBar loadingSpinner;

    private FgPatientFragmentBinding binding;

    /**
     * Constructor
     * @param practitionerID the Health Practitioner's ID
     */
    public SelectPatientsFragment(String practitionerID) {
        this.practitionerID = practitionerID;
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        binding = FgPatientFragmentBinding.inflate(inflater, container, false);

        return binding.getRoot();
    }

    public void onViewCreatred(@NonNull View view,@Nullable Bundle savedInstanceState){

        sharedViewModel = new ViewModelProvider(requireActivity()).get(SharedViewModel.class);

        // find all the graphical components
//        recyclerView = root.findViewById(R.id.select_patients_recycler_view);
//        backButton = root.findViewById(R.id.btn_back);
//        toolbar = root.findViewById(R.id.select_patients_toolbar);
//        title = toolbar.findViewById(R.id.toolbar_title);
//        loadingTextView = root.findViewById(R.id.select_patients_txt_loading);
//        loadingSpinner = root.findViewById(R.id.select_patients_progressBar);

        binding.selectPatientsTxtLoading.setVisibility(View.VISIBLE);
        binding.selectPatientsProgressBar.setVisibility(View.VISIBLE);

        sharedViewModel.setPractitionerID(practitionerID);
        setUpToolBar();

        //update UI when there's new patient under Health Practitioner
        sharedViewModel.getAllPatients().observe(getViewLifecycleOwner(),patientUpdatedObserver);

        thisFrag = this;
    }

    /**
     * This observer observes for changes in patient list
     * ( The list that contains all patients under Health Practitioner )
     * It displays a loading screen when the app is still fetching data from the server,
     * It immediately updates the UI to display the patients when the system completes
     * the data fetching process.
     */
    private Observer<HashMap<String,PatientModel>> patientUpdatedObserver = new Observer<HashMap<String,PatientModel >>() {
        @Override
        public void onChanged( HashMap < String, PatientModel > patientHashMap) {
            if (patientHashMap.size() == 0) {
                binding.selectPatientsTxtLoading.setText(R.string.zero_patients_message);
            }
            else{
                binding.selectPatientsProgressBar.setVisibility(View.GONE);
                binding.selectPatientsTxtLoading.setVisibility(View.GONE);
                binding.selectPatientsRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
                binding.selectPatientsRecyclerView.setAdapter( new SelectPatientsAdapter(patientHashMap,thisFrag, selectedPatients));
            }
        }
    };

    private void setUpToolBar() {
        binding.selectPatientsToolbar.toolbarTitle.setText(R.string.title_patient_selection);
        binding.selectPatientsToolbar.getRoot().inflateMenu(R.menu.select_patients_menu);
        Toolbar.OnMenuItemClickListener menuItemClickListener = item -> {
            if (selectedPatients.size() > 0) {
                // save list into view model
                sharedViewModel.setSelectedPatients(selectedPatients);
//                MainActivity main = (MainActivity) getActivity();
//                Objects.requireNonNull(main).findFragment(MainActivity.home_fragment);
            } else {
                String message = "Please select AT LEAST 1 patient";
                Context context = getContext();
                Toast toast = Toast.makeText(context, message, Toast.LENGTH_LONG);
                toast.show();
            }
            return true;
        };
        binding.selectPatientsToolbar.getRoot().setOnMenuItemClickListener(menuItemClickListener);

        binding.selectPatientsToolbar.btnBack.setOnClickListener(v -> {
//            MainActivity main = (MainActivity) getActivity();
//            Objects.requireNonNull(main).findFragment(MainActivity.login_fragment);
        });
    }

    public void onPatientClick(boolean checked, PatientModel patient) {
        PatientModel patientWithSameID = null;
        boolean found = false;
        for (PatientModel p : selectedPatients){
            if(p.getPatientID().equals(patient.getPatientID())){
                found = true;
                patientWithSameID = p;
                break;
            }
        }
        if (checked) {
            if (!found) {
                selectedPatients.add(patient);
            }
        } else if (!checked) {
            if(found){
                selectedPatients.remove(patientWithSameID);
            }else{
                selectedPatients.remove(patient);
            }
        }
        updateToolbar();
    }

    /**
     * This method updates the tool bar to display the total number of patients that is currently
     * being selected
     */
    private void updateToolbar() {
        String text = selectedPatients.size() + " Patients Selected";
        // Log.d("Select_Patient_Fragment","current sellected patient : " + selected_patients.toString());
        binding.selectPatientsToolbar.toolbarTitle.setText(text);
    }

}