TreatmentsBolusFragment -> kt

This commit is contained in:
Milos Kozak 2019-12-30 09:23:02 +01:00
parent 7d255d014d
commit 9ceeaa3136
3 changed files with 196 additions and 265 deletions

View file

@ -29,6 +29,7 @@ import info.nightscout.androidaps.plugins.pump.medtronic.MedtronicFragment
import info.nightscout.androidaps.plugins.pump.virtual.VirtualPumpFragment
import info.nightscout.androidaps.plugins.source.BGSourceFragment
import info.nightscout.androidaps.plugins.treatments.TreatmentsFragment
import info.nightscout.androidaps.plugins.treatments.fragments.TreatmentsBolusFragment
import info.nightscout.androidaps.plugins.treatments.fragments.TreatmentsProfileSwitchFragment
@Module
@ -60,6 +61,7 @@ abstract class FragmentsModule {
@ContributesAndroidInjector abstract fun contributesTidepoolFragment(): TidepoolFragment
@ContributesAndroidInjector abstract fun contributesTreatmentsFragment(): TreatmentsFragment
@ContributesAndroidInjector abstract fun contributesTreatmentsProfileSwitchFragment(): TreatmentsProfileSwitchFragment
@ContributesAndroidInjector abstract fun contributesTreatmentsBolusFragment(): TreatmentsBolusFragment
@ContributesAndroidInjector abstract fun contributesVirtualPumpFragment(): VirtualPumpFragment
@ContributesAndroidInjector abstract fun contributesBolusProgressDialog(): BolusProgressDialog

View file

@ -1,265 +0,0 @@
package info.nightscout.androidaps.plugins.treatments.fragments;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.Iob;
import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.db.Source;
import info.nightscout.androidaps.events.EventTreatmentChange;
import info.nightscout.androidaps.plugins.bus.RxBus;
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
import info.nightscout.androidaps.plugins.general.nsclient.NSUpload;
import info.nightscout.androidaps.plugins.general.nsclient.UploadQueue;
import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientRestart;
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutosensCalculationFinished;
import info.nightscout.androidaps.plugins.treatments.Treatment;
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin;
import info.nightscout.androidaps.plugins.treatments.dialogs.WizardInfoDialog;
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.DecimalFormatter;
import info.nightscout.androidaps.utils.FabricPrivacy;
import info.nightscout.androidaps.utils.OKDialog;
import info.nightscout.androidaps.utils.SP;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import static info.nightscout.androidaps.utils.DateUtil.now;
public class TreatmentsBolusFragment extends Fragment {
private CompositeDisposable disposable = new CompositeDisposable();
private RecyclerView recyclerView;
private TextView iobTotal;
private TextView activityTotal;
private Button deleteFutureTreatments;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.TreatmentsViewHolder> {
List<Treatment> treatments;
RecyclerViewAdapter(List<Treatment> treatments) {
this.treatments = treatments;
}
@NonNull
@Override
public TreatmentsViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.treatments_bolus_item, viewGroup, false);
return new TreatmentsViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull TreatmentsViewHolder holder, int position) {
Profile profile = ProfileFunctions.getInstance().getProfile();
if (profile == null)
return;
Treatment t = treatments.get(position);
holder.date.setText(DateUtil.dateAndTimeString(t.date));
holder.insulin.setText(MainApp.gs(R.string.formatinsulinunits, t.insulin));
holder.carbs.setText(MainApp.gs(R.string.format_carbs, (int) t.carbs));
Iob iob = t.iobCalc(System.currentTimeMillis(), profile.getDia());
holder.iob.setText(MainApp.gs(R.string.formatinsulinunits, iob.iobContrib));
holder.mealOrCorrection.setText(t.isSMB ? "SMB" : t.mealBolus ? MainApp.gs(R.string.mealbolus) : MainApp.gs(R.string.correctionbous));
holder.ph.setVisibility(t.source == Source.PUMP ? View.VISIBLE : View.GONE);
holder.ns.setVisibility(NSUpload.isIdValid(t._id) ? View.VISIBLE : View.GONE);
holder.invalid.setVisibility(t.isValid ? View.GONE : View.VISIBLE);
if (iob.iobContrib != 0)
holder.iob.setTextColor(ContextCompat.getColor(MainApp.instance(), R.color.colorActive));
else
holder.iob.setTextColor(holder.carbs.getCurrentTextColor());
if (t.date > now())
holder.date.setTextColor(ContextCompat.getColor(MainApp.instance(), R.color.colorScheduled));
else
holder.date.setTextColor(holder.carbs.getCurrentTextColor());
holder.remove.setTag(t);
holder.calculation.setTag(t);
holder.calculation.setVisibility(t.getBoluscalc() == null ? View.INVISIBLE : View.VISIBLE);
}
@Override
public int getItemCount() {
return treatments.size();
}
@Override
public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
public class TreatmentsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
CardView cv;
TextView date;
TextView insulin;
TextView carbs;
TextView iob;
TextView mealOrCorrection;
TextView remove;
TextView calculation;
TextView ph;
TextView ns;
TextView invalid;
TreatmentsViewHolder(View itemView) {
super(itemView);
cv = itemView.findViewById(R.id.treatments_cardview);
date = itemView.findViewById(R.id.treatments_date);
insulin = itemView.findViewById(R.id.treatments_insulin);
carbs = itemView.findViewById(R.id.treatments_carbs);
iob = itemView.findViewById(R.id.treatments_iob);
mealOrCorrection = itemView.findViewById(R.id.treatments_mealorcorrection);
ph = itemView.findViewById(R.id.pump_sign);
ns = itemView.findViewById(R.id.ns_sign);
invalid = itemView.findViewById(R.id.invalid_sign);
calculation = itemView.findViewById(R.id.treatments_calculation);
calculation.setOnClickListener(this);
calculation.setPaintFlags(calculation.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
remove = itemView.findViewById(R.id.treatments_remove);
remove.setOnClickListener(this);
remove.setPaintFlags(remove.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
}
@Override
public void onClick(View v) {
final Treatment treatment = (Treatment) v.getTag();
if (treatment == null)
return;
switch (v.getId()) {
case R.id.treatments_remove:
OKDialog.showConfirmation(getContext(), MainApp.gs(R.string.removerecord),
MainApp.gs(R.string.configbuilder_insulin) + ": " + MainApp.gs(R.string.formatinsulinunits, treatment.insulin) +
"\n" + MainApp.gs(R.string.carbs) + ": " + MainApp.gs(R.string.format_carbs, (int) treatment.carbs) +
"\n" + MainApp.gs(R.string.date) + ": " + DateUtil.dateAndTimeString(treatment.date),
(dialog, id) -> {
final String _id = treatment._id;
if (treatment.source == Source.PUMP) {
treatment.isValid = false;
TreatmentsPlugin.getPlugin().getService().update(treatment);
} else {
if (NSUpload.isIdValid(_id)) {
NSUpload.removeCareportalEntryFromNS(_id);
} else {
UploadQueue.removeID("dbAdd", _id);
}
TreatmentsPlugin.getPlugin().getService().delete(treatment);
}
updateGui();
}, null);
break;
case R.id.treatments_calculation:
FragmentManager manager = getFragmentManager();
// try to fix https://fabric.io/nightscout3/android/apps/info.nightscout.androidaps/issues/5aca7a1536c7b23527eb4be7?time=last-seven-days
// https://stackoverflow.com/questions/14860239/checking-if-state-is-saved-before-committing-a-fragmenttransaction
if (manager.isStateSaved())
return;
if (treatment.getBoluscalc() != null) {
WizardInfoDialog wizardDialog = new WizardInfoDialog();
wizardDialog.setData(treatment.getBoluscalc());
wizardDialog.show(manager, "WizardInfoDialog");
}
break;
}
}
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.treatments_bolus_fragment, container, false);
recyclerView = view.findViewById(R.id.treatments_recyclerview);
recyclerView.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(view.getContext());
recyclerView.setLayoutManager(llm);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(TreatmentsPlugin.getPlugin().getTreatmentsFromHistory());
recyclerView.setAdapter(adapter);
iobTotal = view.findViewById(R.id.treatments_iobtotal);
activityTotal = view.findViewById(R.id.treatments_iobactivitytotal);
Button refreshFromNS = view.findViewById(R.id.treatments_reshreshfromnightscout);
refreshFromNS.setOnClickListener(v -> OKDialog.showConfirmation(getContext(), MainApp.gs(R.string.refresheventsfromnightscout) + "?", () -> {
TreatmentsPlugin.getPlugin().getService().resetTreatments();
RxBus.Companion.getINSTANCE().send(new EventNSClientRestart());
}));
deleteFutureTreatments = view.findViewById(R.id.treatments_delete_future_treatments);
deleteFutureTreatments.setOnClickListener(v -> {
OKDialog.showConfirmation(getContext(), MainApp.gs(R.string.overview_treatment_label), MainApp.gs(R.string.deletefuturetreatments) + "?", () -> {
final List<Treatment> futureTreatments = TreatmentsPlugin.getPlugin().getService()
.getTreatmentDataFromTime(now() + 1000, true);
for (Treatment treatment : futureTreatments) {
final String _id = treatment._id;
if (NSUpload.isIdValid(_id)) {
NSUpload.removeCareportalEntryFromNS(_id);
} else {
UploadQueue.removeID("dbAdd", _id);
}
TreatmentsPlugin.getPlugin().getService().delete(treatment);
}
updateGui();
});
});
boolean nsUploadOnly = SP.getBoolean(R.string.key_ns_upload_only, false);
if (nsUploadOnly)
refreshFromNS.setVisibility(View.GONE);
return view;
}
@Override
public synchronized void onResume() {
super.onResume();
disposable.add(RxBus.Companion.getINSTANCE()
.toObservable(EventTreatmentChange.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> updateGui(), FabricPrivacy::logException)
);
disposable.add(RxBus.Companion.getINSTANCE()
.toObservable(EventAutosensCalculationFinished.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> updateGui(), FabricPrivacy::logException)
);
updateGui();
}
@Override
public synchronized void onPause() {
super.onPause();
disposable.clear();
}
private void updateGui() {
recyclerView.swapAdapter(new RecyclerViewAdapter(TreatmentsPlugin.getPlugin().getTreatmentsFromHistory()), false);
if (TreatmentsPlugin.getPlugin().getLastCalculationTreatments() != null) {
iobTotal.setText(DecimalFormatter.to2Decimal(TreatmentsPlugin.getPlugin().getLastCalculationTreatments().iob) + " " + MainApp.gs(R.string.insulin_unit_shortname));
activityTotal.setText(DecimalFormatter.to3Decimal(TreatmentsPlugin.getPlugin().getLastCalculationTreatments().activity) + " " + MainApp.gs(R.string.insulin_unit_shortname));
}
if (!TreatmentsPlugin.getPlugin().getService().getTreatmentDataFromTime(now() + 1000, true).isEmpty()) {
deleteFutureTreatments.setVisibility(View.VISIBLE);
} else {
deleteFutureTreatments.setVisibility(View.GONE);
}
}
}

View file

@ -0,0 +1,194 @@
package info.nightscout.androidaps.plugins.treatments.fragments
import android.graphics.Paint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import dagger.android.support.DaggerFragment
import info.nightscout.androidaps.R
import info.nightscout.androidaps.db.Source
import info.nightscout.androidaps.events.EventTreatmentChange
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction
import info.nightscout.androidaps.plugins.general.nsclient.NSUpload
import info.nightscout.androidaps.plugins.general.nsclient.UploadQueue
import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientRestart
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutosensCalculationFinished
import info.nightscout.androidaps.plugins.treatments.Treatment
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin
import info.nightscout.androidaps.plugins.treatments.dialogs.WizardInfoDialog
import info.nightscout.androidaps.plugins.treatments.fragments.TreatmentsBolusFragment.RecyclerViewAdapter.TreatmentsViewHolder
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.OKDialog
import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.sharedPreferences.SP
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import kotlinx.android.synthetic.main.treatments_bolus_fragment.*
import javax.inject.Inject
class TreatmentsBolusFragment : DaggerFragment() {
private val disposable = CompositeDisposable()
@Inject lateinit var rxBus: RxBusWrapper
@Inject lateinit var sp: SP
@Inject lateinit var resourceHelper: ResourceHelper
@Inject lateinit var treatmentsPlugin: TreatmentsPlugin
@Inject lateinit var profileFunction: ProfileFunction
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.treatments_bolus_fragment, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
treatments_recyclerview.setHasFixedSize(true)
treatments_recyclerview.layoutManager = LinearLayoutManager(view.context)
treatments_recyclerview.adapter = RecyclerViewAdapter(treatmentsPlugin.treatmentsFromHistory)
treatments_reshreshfromnightscout.setOnClickListener {
activity?.let { activity ->
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.refresheventsfromnightscout) + "?", Runnable {
treatmentsPlugin.service.resetTreatments()
rxBus.send(EventNSClientRestart())
})
}
}
treatments_delete_future_treatments.setOnClickListener {
activity?.let { activity ->
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.overview_treatment_label), resourceHelper.gs(R.string.deletefuturetreatments) + "?", Runnable {
val futureTreatments = treatmentsPlugin.service.getTreatmentDataFromTime(DateUtil.now() + 1000, true)
for (treatment in futureTreatments) {
if (NSUpload.isIdValid(treatment._id))
NSUpload.removeCareportalEntryFromNS(treatment._id)
else
UploadQueue.removeID("dbAdd", treatment._id)
treatmentsPlugin.service.delete(treatment)
}
updateGui()
})
}
}
val nsUploadOnly = sp.getBoolean(R.string.key_ns_upload_only, false)
if (nsUploadOnly) treatments_reshreshfromnightscout.visibility = View.GONE
}
@Synchronized override fun onResume() {
super.onResume()
disposable.add(rxBus
.toObservable(EventTreatmentChange::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ updateGui() }) { FabricPrivacy.logException(it) }
)
disposable.add(rxBus
.toObservable(EventAutosensCalculationFinished::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ updateGui() }) { FabricPrivacy.logException(it) }
)
updateGui()
}
@Synchronized override fun onPause() {
super.onPause()
disposable.clear()
}
inner class RecyclerViewAdapter internal constructor(var treatments: List<Treatment>) : RecyclerView.Adapter<TreatmentsViewHolder>() {
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): TreatmentsViewHolder {
val v = LayoutInflater.from(viewGroup.context).inflate(R.layout.treatments_bolus_item, viewGroup, false)
return TreatmentsViewHolder(v)
}
override fun onBindViewHolder(holder: TreatmentsViewHolder, position: Int) {
val profile = profileFunction.getProfile() ?: return
val t = treatments[position]
holder.date.text = DateUtil.dateAndTimeString(t.date)
holder.insulin.text = resourceHelper.gs(R.string.formatinsulinunits, t.insulin)
holder.carbs.text = resourceHelper.gs(R.string.format_carbs, t.carbs.toInt())
val iob = t.iobCalc(System.currentTimeMillis(), profile.dia)
holder.iob.text = resourceHelper.gs(R.string.formatinsulinunits, iob.iobContrib)
holder.mealOrCorrection.text = if (t.isSMB) "SMB" else if (t.mealBolus) resourceHelper.gs(R.string.mealbolus) else resourceHelper.gs(R.string.correctionbous)
holder.ph.visibility = if (t.source == Source.PUMP) View.VISIBLE else View.GONE
holder.ns.visibility = if (NSUpload.isIdValid(t._id)) View.VISIBLE else View.GONE
holder.invalid.visibility = if (t.isValid) View.GONE else View.VISIBLE
if (iob.iobContrib != 0.0) holder.iob.setTextColor(resourceHelper.gc(R.color.colorActive)) else holder.iob.setTextColor(holder.carbs.currentTextColor)
if (t.date > DateUtil.now()) holder.date.setTextColor(resourceHelper.gc(R.color.colorScheduled)) else holder.date.setTextColor(holder.carbs.currentTextColor)
holder.remove.tag = t
holder.calculation.tag = t
holder.calculation.visibility = if (t.getBoluscalc() == null) View.INVISIBLE else View.VISIBLE
}
override fun getItemCount(): Int {
return treatments.size
}
inner class TreatmentsViewHolder internal constructor(itemView: View) : RecyclerView.ViewHolder(itemView) {
var cv: CardView = itemView.findViewById(R.id.treatments_cardview)
var date: TextView = itemView.findViewById(R.id.treatments_date)
var insulin: TextView = itemView.findViewById(R.id.treatments_insulin)
var carbs: TextView = itemView.findViewById(R.id.treatments_carbs)
var iob: TextView = itemView.findViewById(R.id.treatments_iob)
var mealOrCorrection: TextView = itemView.findViewById(R.id.treatments_mealorcorrection)
var remove: TextView = itemView.findViewById(R.id.treatments_remove)
var calculation: TextView = itemView.findViewById(R.id.treatments_calculation)
var ph: TextView = itemView.findViewById(R.id.pump_sign)
var ns: TextView = itemView.findViewById(R.id.ns_sign)
var invalid: TextView = itemView.findViewById(R.id.invalid_sign)
init {
calculation.setOnClickListener {
val treatment = it.tag as Treatment
fragmentManager?.let { fragmentManager ->
if (treatment.getBoluscalc() != null) {
val wizardDialog = WizardInfoDialog()
wizardDialog.setData(treatment.getBoluscalc()!!)
wizardDialog.show(fragmentManager, "WizardInfoDialog")
}
}
}
calculation.paintFlags = calculation.paintFlags or Paint.UNDERLINE_TEXT_FLAG
remove.setOnClickListener {
val treatment = it.tag as Treatment
activity?.let { activity ->
val text = resourceHelper.gs(R.string.configbuilder_insulin) + ": " +
resourceHelper.gs(R.string.formatinsulinunits, treatment.insulin) + "\n" +
resourceHelper.gs(R.string.carbs) + ": " + resourceHelper.gs(R.string.format_carbs, treatment.carbs.toInt()) + "\n" +
resourceHelper.gs(R.string.date) + ": " + DateUtil.dateAndTimeString(treatment.date)
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.removerecord), text, Runnable {
if (treatment.source == Source.PUMP) {
treatment.isValid = false
treatmentsPlugin.service.update(treatment)
} else {
if (NSUpload.isIdValid(treatment._id))
NSUpload.removeCareportalEntryFromNS(treatment._id)
else
UploadQueue.removeID("dbAdd", treatment._id)
treatmentsPlugin.service.delete(treatment)
}
updateGui()
})
}
}
remove.paintFlags = remove.paintFlags or Paint.UNDERLINE_TEXT_FLAG
}
}
}
private fun updateGui() {
treatments_recyclerview?.swapAdapter(RecyclerViewAdapter(treatmentsPlugin.treatmentsFromHistory), false)
if (treatmentsPlugin.lastCalculationTreatments != null) {
treatments_iobtotal?.text = resourceHelper.gs(R.string.formatinsulinunits, treatmentsPlugin.lastCalculationTreatments.iob)
treatments_iobactivitytotal?.text = resourceHelper.gs(R.string.formatinsulinunits, treatmentsPlugin.lastCalculationTreatments.activity)
}
if (treatmentsPlugin.service.getTreatmentDataFromTime(DateUtil.now() + 1000, true).isNotEmpty())
treatments_delete_future_treatments?.visibility = View.VISIBLE
else
treatments_delete_future_treatments?.visibility = View.GONE
}
}