Search in sources :

Example 11 with MedicineReminder

use of m.co.rh.id.a_medic_log.base.entity.MedicineReminder in project a-medic-log by rh-id.

the class MedicineItemSV method createView.

@Override
protected View createView(Activity activity, ViewGroup container) {
    View rootLayout = activity.getLayoutInflater().inflate(R.layout.item_medicine, container, false);
    rootLayout.setOnClickListener(this);
    TextView nameText = rootLayout.findViewById(R.id.text_name);
    TextView descriptionText = rootLayout.findViewById(R.id.text_description);
    TextView lastIntakeText = rootLayout.findViewById(R.id.text_last_intake);
    Button editButton = rootLayout.findViewById(R.id.button_edit);
    editButton.setOnClickListener(this);
    Button deleteButton = rootLayout.findViewById(R.id.button_delete);
    deleteButton.setOnClickListener(this);
    Button moreAction = rootLayout.findViewById(R.id.button_more_action);
    moreAction.setOnClickListener(this);
    GridLayout containerReminder = rootLayout.findViewById(R.id.container_reminder);
    mRxDisposer.add("createView_onMedicineStateChanged", mMedicineStateSubject.getSubject().debounce(16, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe(medicineState -> {
        nameText.setText(medicineState.getMedicineName());
        descriptionText.setText(medicineState.getMedicineDescription());
        if (medicineState.getMedicineId() != null) {
            moreAction.setVisibility(View.VISIBLE);
        } else {
            moreAction.setVisibility(View.GONE);
        }
        queryLastMedicineIntake(medicineState.getMedicineId());
        containerReminder.removeAllViews();
        ArrayList<MedicineReminder> medicineReminders = medicineState.getMedicineReminderList();
        if (medicineReminders != null && !medicineReminders.isEmpty()) {
            containerReminder.setVisibility(View.VISIBLE);
            for (MedicineReminder medicineReminder : medicineReminders) {
                MaterialTextView materialTextView = new MaterialTextView(activity);
                materialTextView.setText(mTimeFormat.format(medicineReminder.startDateTime));
                GridLayout.LayoutParams params = new GridLayout.LayoutParams();
                params.width = GridLayout.LayoutParams.WRAP_CONTENT;
                params.height = GridLayout.LayoutParams.WRAP_CONTENT;
                params.columnSpec = GridLayout.spec(GridLayout.UNDEFINED, 1.0f);
                materialTextView.setLayoutParams(params);
                materialTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
                int tintColor = activity.getResources().getColor(R.color.daynight_black_white);
                Drawable icon;
                if (medicineReminder.reminderEnabled) {
                    icon = AppCompatResources.getDrawable(activity, R.drawable.ic_timer_black);
                } else {
                    icon = AppCompatResources.getDrawable(activity, R.drawable.ic_timer_off_black);
                }
                icon = DrawableCompat.wrap(icon);
                DrawableCompat.setTint(icon, tintColor);
                materialTextView.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
                containerReminder.addView(materialTextView);
            }
        } else {
            containerReminder.setVisibility(View.GONE);
        }
    }));
    mRxDisposer.add("createView_onLastMedicineIntakeChanged", mLastMedicineIntakeSubject.getSubject().debounce(100, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe(medicineIntakeOpt -> {
        if (medicineIntakeOpt.isPresent()) {
            Date takenDateTime = medicineIntakeOpt.get().takenDateTime;
            lastIntakeText.setText(mDateTimeFormat.format(takenDateTime));
            lastIntakeText.setVisibility(View.VISIBLE);
        } else {
            lastIntakeText.setText(null);
            lastIntakeText.setVisibility(View.GONE);
        }
    }));
    mRxDisposer.add("createView_onMedicineIntakeAdded", mMedicineIntakeChangeNotifier.getAddedMedicineIntake().observeOn(Schedulers.from(mExecutorService)).subscribe(medicineIntake -> {
        if (medicineIntake.medicineId != null && medicineIntake.medicineId.equals(mMedicineStateSubject.getValue().getMedicineId())) {
            queryLastMedicineIntake(medicineIntake.medicineId);
        }
    }));
    mRxDisposer.add("createView_onMedicineIntakeUpdated", mMedicineIntakeChangeNotifier.getUpdatedMedicineIntake().observeOn(Schedulers.from(mExecutorService)).subscribe(updateMedicineIntakeEvent -> {
        MedicineIntake medicineIntake = updateMedicineIntakeEvent.getAfter();
        if (medicineIntake.medicineId != null && medicineIntake.medicineId.equals(mMedicineStateSubject.getValue().getMedicineId())) {
            queryLastMedicineIntake(medicineIntake.medicineId);
        }
    }));
    mRxDisposer.add("createView_onMedicineIntakeDeleted", mMedicineIntakeChangeNotifier.getDeletedMedicineIntake().observeOn(Schedulers.from(mExecutorService)).subscribe(medicineIntake -> {
        if (medicineIntake.medicineId != null && medicineIntake.medicineId.equals(mMedicineStateSubject.getValue().getMedicineId())) {
            queryLastMedicineIntake(medicineIntake.medicineId);
        }
    }));
    return rootLayout;
}
Also used : AppCompatResources(androidx.appcompat.content.res.AppCompatResources) Date(java.util.Date) MedicineIntake(m.co.rh.id.a_medic_log.base.entity.MedicineIntake) SimpleDateFormat(java.text.SimpleDateFormat) DrawableCompat(androidx.core.graphics.drawable.DrawableCompat) Drawable(android.graphics.drawable.Drawable) MedicineIntakeChangeNotifier(m.co.rh.id.a_medic_log.app.provider.notifier.MedicineIntakeChangeNotifier) MedicineReminder(m.co.rh.id.a_medic_log.base.entity.MedicineReminder) MenuItem(android.view.MenuItem) ArrayList(java.util.ArrayList) Schedulers(io.reactivex.rxjava3.schedulers.Schedulers) View(android.view.View) Button(android.widget.Button) GridLayout(androidx.gridlayout.widget.GridLayout) QueryMedicineCmd(m.co.rh.id.a_medic_log.app.provider.command.QueryMedicineCmd) DateFormat(java.text.DateFormat) ExecutorService(java.util.concurrent.ExecutorService) OptionalBehaviorSubject(m.co.rh.id.a_medic_log.base.rx.OptionalBehaviorSubject) SerialBehaviorSubject(m.co.rh.id.a_medic_log.base.rx.SerialBehaviorSubject) StatefulView(m.co.rh.id.anavigator.StatefulView) Provider(m.co.rh.id.aprovider.Provider) MedicineState(m.co.rh.id.a_medic_log.base.state.MedicineState) ViewGroup(android.view.ViewGroup) PopupMenu(androidx.appcompat.widget.PopupMenu) TimeUnit(java.util.concurrent.TimeUnit) AndroidSchedulers(io.reactivex.rxjava3.android.schedulers.AndroidSchedulers) MaterialTextView(com.google.android.material.textview.MaterialTextView) TextView(android.widget.TextView) StatefulViewProvider(m.co.rh.id.a_medic_log.app.provider.StatefulViewProvider) RequireComponent(m.co.rh.id.anavigator.component.RequireComponent) TypedValue(android.util.TypedValue) RxDisposer(m.co.rh.id.a_medic_log.app.rx.RxDisposer) R(m.co.rh.id.a_medic_log.R) Activity(android.app.Activity) MaterialTextView(com.google.android.material.textview.MaterialTextView) ArrayList(java.util.ArrayList) Drawable(android.graphics.drawable.Drawable) View(android.view.View) StatefulView(m.co.rh.id.anavigator.StatefulView) MaterialTextView(com.google.android.material.textview.MaterialTextView) TextView(android.widget.TextView) Date(java.util.Date) GridLayout(androidx.gridlayout.widget.GridLayout) Button(android.widget.Button) MedicineIntake(m.co.rh.id.a_medic_log.base.entity.MedicineIntake) MaterialTextView(com.google.android.material.textview.MaterialTextView) TextView(android.widget.TextView) MedicineReminder(m.co.rh.id.a_medic_log.base.entity.MedicineReminder)

Example 12 with MedicineReminder

use of m.co.rh.id.a_medic_log.base.entity.MedicineReminder in project a-medic-log by rh-id.

the class QueryNoteCmd method queryMedicineStateList.

private void queryMedicineStateList(NoteState noteState) {
    Long noteId = noteState.getNoteId();
    List<Medicine> medicineList = mMedicineDao.findMedicinesByNoteId(noteId);
    if (!medicineList.isEmpty()) {
        ArrayList<MedicineState> medicineStates = new ArrayList<>();
        for (Medicine medicine : medicineList) {
            List<MedicineReminder> medicineReminders = mMedicineDao.findMedicineRemindersByMedicineId(medicine.id);
            MedicineState medicineState = new MedicineState();
            medicineState.updateMedicine(medicine);
            medicineState.updateMedicineReminderList(medicineReminders);
            medicineStates.add(medicineState);
        }
        noteState.updateMedicineStates(medicineStates);
    }
}
Also used : Medicine(m.co.rh.id.a_medic_log.base.entity.Medicine) MedicineState(m.co.rh.id.a_medic_log.base.state.MedicineState) ArrayList(java.util.ArrayList) MedicineReminder(m.co.rh.id.a_medic_log.base.entity.MedicineReminder)

Example 13 with MedicineReminder

use of m.co.rh.id.a_medic_log.base.entity.MedicineReminder in project a-medic-log by rh-id.

the class AppNotificationHandler method disableMedicineReminder.

public void disableMedicineReminder(Intent intent) {
    Serializable serializable = intent.getSerializableExtra(KEY_INT_REQUEST_ID);
    if (serializable instanceof Integer) {
        mExecutorService.get().execute(() -> {
            mLock.lock();
            try {
                AndroidNotification androidNotification = mAndroidNotificationRepo.get().findByRequestId((int) serializable);
                if (androidNotification != null && androidNotification.groupKey.equals(GROUP_KEY_MEDICINE_REMINDER)) {
                    MedicineReminder medicineReminder = mMedicineDao.get().findMedicineReminderById(androidNotification.refId);
                    medicineReminder.reminderEnabled = false;
                    medicineReminder = mUpdateMedicineReminderCmd.get().execute(medicineReminder).blockingGet();
                    cancelNotificationSync(medicineReminder);
                    mMedicineReminderEventHandler.get().cancelMedicineReminderNotificationWork(Collections.singletonList(medicineReminder));
                }
            } catch (Exception e) {
                mLogger.get().d(TAG, "Failed to disable medicine reminder: " + e.getMessage(), e);
            } finally {
                mLock.unlock();
            }
        });
    }
}
Also used : Serializable(java.io.Serializable) AndroidNotification(m.co.rh.id.a_medic_log.base.entity.AndroidNotification) MedicineReminder(m.co.rh.id.a_medic_log.base.entity.MedicineReminder)

Example 14 with MedicineReminder

use of m.co.rh.id.a_medic_log.base.entity.MedicineReminder in project a-medic-log by rh-id.

the class AppNotificationHandler method processNotification.

public void processNotification(Intent intent) {
    Serializable serializable = intent.getSerializableExtra(KEY_INT_REQUEST_ID);
    if (serializable instanceof Integer) {
        mExecutorService.get().execute(() -> {
            mLock.lock();
            try {
                AndroidNotification androidNotification = mAndroidNotificationRepo.get().findByRequestId((int) serializable);
                if (androidNotification != null && androidNotification.groupKey.equals(GROUP_KEY_MEDICINE_REMINDER)) {
                    MedicineReminder medicineReminder = mMedicineDao.get().findMedicineReminderById(androidNotification.refId);
                    mMedicineReminderSubject.onNext(medicineReminder);
                    cancelNotificationSync(medicineReminder);
                }
            } catch (Exception e) {
                mLogger.get().d(TAG, "Failed to process notification: " + e.getMessage(), e);
            } finally {
                mLock.unlock();
            }
        });
    }
}
Also used : Serializable(java.io.Serializable) AndroidNotification(m.co.rh.id.a_medic_log.base.entity.AndroidNotification) MedicineReminder(m.co.rh.id.a_medic_log.base.entity.MedicineReminder)

Example 15 with MedicineReminder

use of m.co.rh.id.a_medic_log.base.entity.MedicineReminder in project a-medic-log by rh-id.

the class MedicineReminderEventHandler method startMedicineReminderNotificationWork.

public void startMedicineReminderNotificationWork(List<MedicineReminder> medicineReminders) {
    if (!medicineReminders.isEmpty()) {
        for (MedicineReminder medicineReminder : medicineReminders) {
            if (medicineReminder.reminderEnabled) {
                long initialDelay = calculateInitialDelayMs(medicineReminder.startDateTime);
                String tag = calculateTag(medicineReminder);
                OneTimeWorkRequest notificationWorkRequest = new OneTimeWorkRequest.Builder(MedicineReminderNotificationWorker.class).setInitialDelay(initialDelay, TimeUnit.MILLISECONDS).addTag(tag).setInputData(calculateInputData(medicineReminder)).build();
                mWorkManager.enqueue(notificationWorkRequest);
            }
        }
    }
}
Also used : MedicineReminder(m.co.rh.id.a_medic_log.base.entity.MedicineReminder) OneTimeWorkRequest(androidx.work.OneTimeWorkRequest)

Aggregations

MedicineReminder (m.co.rh.id.a_medic_log.base.entity.MedicineReminder)26 Serializable (java.io.Serializable)8 Activity (android.app.Activity)7 View (android.view.View)7 ViewGroup (android.view.ViewGroup)7 Button (android.widget.Button)7 AndroidSchedulers (io.reactivex.rxjava3.android.schedulers.AndroidSchedulers)7 R (m.co.rh.id.a_medic_log.R)7 StatefulViewProvider (m.co.rh.id.a_medic_log.app.provider.StatefulViewProvider)7 RxDisposer (m.co.rh.id.a_medic_log.app.rx.RxDisposer)7 MedicineState (m.co.rh.id.a_medic_log.base.state.MedicineState)7 StatefulView (m.co.rh.id.anavigator.StatefulView)7 RequireComponent (m.co.rh.id.anavigator.component.RequireComponent)7 Provider (m.co.rh.id.aprovider.Provider)7 MenuItem (android.view.MenuItem)6 Medicine (m.co.rh.id.a_medic_log.base.entity.Medicine)6 Context (android.content.Context)5 Editable (android.text.Editable)5 TextWatcher (android.text.TextWatcher)5 EditText (android.widget.EditText)5