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


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


import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelStoreOwner;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sw.laryngoscope.R;
import com.sw.laryngoscope.FHIR.adapter.SelectPatientsAdapter;
import com.sw.laryngoscope.activity.BaseFragment;
import com.sw.laryngoscope.activity.HomeActivity;
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 BaseFragment<FgPatientFragmentBinding> implements SelectPatientsAdapter.OnPatientClickListener{
    private String practitionerID;
    private SelectPatientsFragment thisFrag; // a reference to this fragment
    private SharedViewModel sharedViewModel;
    private ArrayList<PatientModel> selectedPatients = new ArrayList<>();
    private HomeActivity homeActivity;

    @Override
    protected FgPatientFragmentBinding getViewBinding(LayoutInflater inflater, ViewGroup container) {
        return FgPatientFragmentBinding.inflate(inflater,container,false);
    }

    @Override
    protected void init() {
//        sharedViewModel = new ViewModelProvider(new ViewModelStoreOwner(this)).get(SharedViewModel.class);
        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(getActivity(),patientUpdatedObserver);

        thisFrag = this;
    }

    @Override
    protected void setListener() {

    }

//    public void onViewCreatred(@NonNull View view,@Nullable Bundle savedInstanceState){
//
//
//
//        // 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);
//
//
//    }

    /**
     * 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);
    }

    @Override
    public void backMainAct() {

    }

    @Override
    public boolean startEnable() {
        return false;
    }
}