.
This commit is contained in:
@@ -0,0 +1,346 @@
|
||||
package com.ssb.simplitend.medreminder;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.DatePickerDialog;
|
||||
import android.app.TimePickerDialog;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.CompoundButton;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.content.res.AppCompatResources;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.navigation.Navigation;
|
||||
|
||||
import com.ssb.simplitend.R;
|
||||
import com.ssb.simplitend.apputils.AppUtil;
|
||||
import com.ssb.simplitend.databinding.AddReminderFragmentBinding;
|
||||
import com.ssb.simplitend.medreminder.mvvm.Reminder;
|
||||
import com.ssb.simplitend.welcome.fragments.contacts.mvvm.Contact;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Locale;
|
||||
|
||||
public class AddReminderFragment extends Fragment implements CompoundButton.OnCheckedChangeListener {
|
||||
|
||||
// view binding
|
||||
protected AddReminderFragmentBinding binding;
|
||||
|
||||
/*
|
||||
week day selection states
|
||||
true -> selected
|
||||
false -> un-selected
|
||||
*/
|
||||
boolean[] week_state;
|
||||
|
||||
// arguments keys
|
||||
public static final String REMINDER_KEY = "reminder_key";
|
||||
|
||||
// model
|
||||
private Reminder reminder;
|
||||
|
||||
public AddReminderFragment() {
|
||||
// required empty const.
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||
binding = AddReminderFragmentBinding.inflate(inflater, container, false);
|
||||
|
||||
initViews();
|
||||
|
||||
clickEvents();
|
||||
|
||||
return binding.getRoot();
|
||||
}
|
||||
|
||||
private void clickEvents() {
|
||||
|
||||
binding.getTime.setOnClickListener(v -> {
|
||||
AppUtil.closeKeyboard(requireActivity());
|
||||
getTime();
|
||||
});
|
||||
|
||||
binding.getDate.setOnClickListener(v -> {
|
||||
AppUtil.closeKeyboard(requireActivity());
|
||||
getDate();
|
||||
});
|
||||
|
||||
binding.addReminder.setOnClickListener(v -> {
|
||||
if (reminder == null){
|
||||
Navigation.findNavController(v).popBackStack(R.id.reminderFragment, false, true);
|
||||
}else{
|
||||
AppUtil.showSOSDecision(requireContext(), getString(R.string.make_changes),
|
||||
getString(R.string.yes), getString(R.string.no),
|
||||
v1 -> {
|
||||
// yes click
|
||||
AppUtil.showAnimateDBS(requireContext(),
|
||||
getString(R.string.changes_successful), R.raw.done_anim_primary,
|
||||
3000, v3 -> {
|
||||
// here v3 is null
|
||||
Navigation.findNavController(v).popBackStack(R.id.reminderFragment, false, true);
|
||||
});
|
||||
}, v2 -> {
|
||||
// no click
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
private void initViews() {
|
||||
|
||||
// checking if intent to EDIT or ADD reminder
|
||||
Bundle bundle = getArguments();
|
||||
if (bundle != null) setLayoutDetails(reminder = (Reminder) bundle.getSerializable(REMINDER_KEY));
|
||||
|
||||
// scrolling instruction edit text
|
||||
binding.instructions.setOnTouchListener((v, event) -> {
|
||||
if (binding.instructions.hasFocus()) {
|
||||
v.getParent().requestDisallowInterceptTouchEvent(true);
|
||||
if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_SCROLL) {
|
||||
v.getParent().requestDisallowInterceptTouchEvent(false);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
// load into spinner
|
||||
loadMedType();
|
||||
loadFrequency();
|
||||
|
||||
setUpWeekSelection();
|
||||
|
||||
binding.everydayCheck.setOnCheckedChangeListener(this);
|
||||
|
||||
}
|
||||
|
||||
private void setLayoutDetails(Reminder reminder){
|
||||
if (reminder != null){
|
||||
// intent is to edit the reminder
|
||||
binding.title.setText(getString(R.string.edit_reminder));
|
||||
binding.medicName.setText(reminder.dosage_name);
|
||||
binding.quantity.setText(reminder.quantity);
|
||||
binding.getTime.setText(reminder.time);
|
||||
}
|
||||
}
|
||||
|
||||
// every-dau toggle listener
|
||||
@Override
|
||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
||||
for (int i = 0; i < 7; i++) {
|
||||
setSelectionState(i, week_state[i] = isChecked);
|
||||
}
|
||||
}
|
||||
|
||||
// set selection and un-selection of week day
|
||||
private void setUpWeekSelection() {
|
||||
|
||||
week_state = new boolean[7];
|
||||
|
||||
// week day selections
|
||||
binding.sun.setOnClickListener(v -> setSelectionState(0, week_state[0] = !week_state[0]));
|
||||
binding.mon.setOnClickListener(v -> setSelectionState(1, week_state[1] = !week_state[1]));
|
||||
binding.tue.setOnClickListener(v -> setSelectionState(2, week_state[2] = !week_state[2]));
|
||||
binding.wed.setOnClickListener(v -> setSelectionState(3, week_state[3] = !week_state[3]));
|
||||
binding.thu.setOnClickListener(v -> setSelectionState(4, week_state[4] = !week_state[4]));
|
||||
binding.fri.setOnClickListener(v -> setSelectionState(5, week_state[5] = !week_state[5]));
|
||||
binding.sat.setOnClickListener(v -> setSelectionState(6, week_state[6] = !week_state[6]));
|
||||
|
||||
}
|
||||
|
||||
private void setSelectionState(int position, boolean selection) {
|
||||
if (selection) {
|
||||
// selection has to be made
|
||||
switch (position) {
|
||||
case 0:
|
||||
// sun
|
||||
binding.sun.setBackgroundTintList(AppCompatResources.getColorStateList(requireContext(), R.color.color_primary));
|
||||
binding.sun.setTextColor(AppCompatResources.getColorStateList(requireContext(), R.color.white_bg));
|
||||
break;
|
||||
case 1:
|
||||
// mon
|
||||
binding.mon.setBackgroundTintList(AppCompatResources.getColorStateList(requireContext(), R.color.color_primary));
|
||||
binding.mon.setTextColor(AppCompatResources.getColorStateList(requireContext(), R.color.white_bg));
|
||||
break;
|
||||
case 2:
|
||||
// tue
|
||||
binding.tue.setBackgroundTintList(AppCompatResources.getColorStateList(requireContext(), R.color.color_primary));
|
||||
binding.tue.setTextColor(AppCompatResources.getColorStateList(requireContext(), R.color.white_bg));
|
||||
break;
|
||||
case 3:
|
||||
// wed
|
||||
binding.wed.setBackgroundTintList(AppCompatResources.getColorStateList(requireContext(), R.color.color_primary));
|
||||
binding.wed.setTextColor(AppCompatResources.getColorStateList(requireContext(), R.color.white_bg));
|
||||
break;
|
||||
case 4:
|
||||
// thu
|
||||
binding.thu.setBackgroundTintList(AppCompatResources.getColorStateList(requireContext(), R.color.color_primary));
|
||||
binding.thu.setTextColor(AppCompatResources.getColorStateList(requireContext(), R.color.white_bg));
|
||||
break;
|
||||
case 5:
|
||||
// fri
|
||||
binding.fri.setBackgroundTintList(AppCompatResources.getColorStateList(requireContext(), R.color.color_primary));
|
||||
binding.fri.setTextColor(AppCompatResources.getColorStateList(requireContext(), R.color.white_bg));
|
||||
break;
|
||||
case 6:
|
||||
// sat
|
||||
binding.sat.setBackgroundTintList(AppCompatResources.getColorStateList(requireContext(), R.color.color_primary));
|
||||
binding.sat.setTextColor(AppCompatResources.getColorStateList(requireContext(), R.color.white_bg));
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
// un-selection has to be made
|
||||
switch (position) {
|
||||
case 0:
|
||||
// sun
|
||||
binding.sun.setBackgroundTintList(null);
|
||||
binding.sun.setTextColor(AppCompatResources.getColorStateList(requireContext(), R.color.black));
|
||||
break;
|
||||
case 1:
|
||||
// mon
|
||||
binding.mon.setBackgroundTintList(null);
|
||||
binding.mon.setTextColor(AppCompatResources.getColorStateList(requireContext(), R.color.black));
|
||||
break;
|
||||
case 2:
|
||||
// tue
|
||||
binding.tue.setBackgroundTintList(null);
|
||||
binding.tue.setTextColor(AppCompatResources.getColorStateList(requireContext(), R.color.black));
|
||||
break;
|
||||
case 3:
|
||||
// wed
|
||||
binding.wed.setBackgroundTintList(null);
|
||||
binding.wed.setTextColor(AppCompatResources.getColorStateList(requireContext(), R.color.black));
|
||||
break;
|
||||
case 4:
|
||||
// thu
|
||||
binding.thu.setBackgroundTintList(null);
|
||||
binding.thu.setTextColor(AppCompatResources.getColorStateList(requireContext(), R.color.black));
|
||||
break;
|
||||
case 5:
|
||||
// fri
|
||||
binding.fri.setBackgroundTintList(null);
|
||||
binding.fri.setTextColor(AppCompatResources.getColorStateList(requireContext(), R.color.black));
|
||||
break;
|
||||
case 6:
|
||||
// sat
|
||||
binding.sat.setBackgroundTintList(null);
|
||||
binding.sat.setTextColor(AppCompatResources.getColorStateList(requireContext(), R.color.black));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// checking if all days are selected or not
|
||||
// thus, updating the Everyday switch accordingly
|
||||
boolean isEveryDay = true;
|
||||
|
||||
for (int i = 0; i < 7; i++) {
|
||||
if (!week_state[i]) {
|
||||
// some day is not selected thus not everyday selection
|
||||
isEveryDay = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
binding.everydayCheck.setOnCheckedChangeListener(null);
|
||||
binding.everydayCheck.setChecked(isEveryDay);
|
||||
binding.everydayCheck.setOnCheckedChangeListener(this);
|
||||
}
|
||||
|
||||
// shows date picker to pick date and set to tv
|
||||
private void getDate() {
|
||||
|
||||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
|
||||
DatePickerDialog dpd = new DatePickerDialog(requireContext());
|
||||
|
||||
dpd.setOnDateSetListener((view, year, month, dayOfMonth) -> {
|
||||
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.set(Calendar.YEAR, year);
|
||||
cal.set(Calendar.MONTH, month);
|
||||
cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
|
||||
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy", Locale.getDefault());
|
||||
|
||||
String selected_time = sdf.format(cal.getTime());
|
||||
binding.getDate.setText(selected_time);
|
||||
|
||||
});
|
||||
|
||||
dpd.show();
|
||||
}
|
||||
|
||||
// TODO: 05-07-2023 for lower version
|
||||
|
||||
}
|
||||
|
||||
// shows time picker to pick time and set to textview
|
||||
private void getTime() {
|
||||
|
||||
Calendar calendar = Calendar.getInstance(Locale.getDefault());
|
||||
|
||||
TimePickerDialog tpd = new TimePickerDialog(requireContext(),
|
||||
(TimePickerDialog.OnTimeSetListener) (view, hourOfDay, minute) -> {
|
||||
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
|
||||
cal.set(Calendar.MINUTE, minute);
|
||||
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a", Locale.getDefault());
|
||||
|
||||
String selected_time = sdf.format(cal.getTime());
|
||||
binding.getTime.setText(selected_time);
|
||||
|
||||
}, calendar.getTime().getHours(), calendar.getTime().getMinutes(), false);
|
||||
|
||||
tpd.show();
|
||||
|
||||
}
|
||||
|
||||
// loading freq. into its resp. spinner
|
||||
private void loadFrequency() {
|
||||
|
||||
// static data
|
||||
ArrayList<String> medTypeList = new ArrayList<>();
|
||||
medTypeList.add("1 time, Daily");
|
||||
medTypeList.add("2 time, Daily");
|
||||
medTypeList.add("3 time, Daily");
|
||||
|
||||
binding.frequencySpinner.setLifecycleOwner(this);
|
||||
|
||||
binding.frequencySpinner.setItems(medTypeList);
|
||||
|
||||
binding.frequencySpinner.setDismissWhenNotifiedItemSelected(true);
|
||||
|
||||
binding.frequencySpinner.setIsFocusable(true);
|
||||
|
||||
}
|
||||
|
||||
// loads med types into its resp. apinner
|
||||
private void loadMedType() {
|
||||
|
||||
// static data
|
||||
ArrayList<String> medTypeList = new ArrayList<>();
|
||||
medTypeList.add("Lorem ipsum");
|
||||
medTypeList.add("Lorem ipsum");
|
||||
medTypeList.add("Lorem ipsum");
|
||||
|
||||
binding.medicationsSpinner.setLifecycleOwner(this);
|
||||
|
||||
binding.medicationsSpinner.setItems(medTypeList);
|
||||
|
||||
binding.medicationsSpinner.setDismissWhenNotifiedItemSelected(true);
|
||||
|
||||
binding.medicationsSpinner.setIsFocusable(true);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user