Treatment fragments -> kt

This commit is contained in:
Milos Kozak 2020-03-21 21:00:04 +01:00
parent 5a2821acac
commit 93e08d474c
8 changed files with 381 additions and 489 deletions

View file

@ -38,11 +38,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.TreatmentsCareportalFragment
import info.nightscout.androidaps.plugins.treatments.fragments.TreatmentsExtendedBolusesFragment
import info.nightscout.androidaps.plugins.treatments.fragments.TreatmentsProfileSwitchFragment
import info.nightscout.androidaps.plugins.treatments.fragments.TreatmentsTempTargetFragment
import info.nightscout.androidaps.plugins.treatments.fragments.*
@Module
@Suppress("unused")
@ -79,6 +75,7 @@ abstract class FragmentsModule {
@ContributesAndroidInjector abstract fun contributesTidepoolFragment(): TidepoolFragment
@ContributesAndroidInjector abstract fun contributesTreatmentsFragment(): TreatmentsFragment
@ContributesAndroidInjector abstract fun contributesTreatmentsBolusFragment(): TreatmentsBolusFragment
@ContributesAndroidInjector abstract fun contributesTreatmentsTemporaryBasalsFragment(): TreatmentsTemporaryBasalsFragment
@ContributesAndroidInjector abstract fun contributesTreatmentsTempTargetFragment(): TreatmentsTempTargetFragment
@ContributesAndroidInjector abstract fun contributesTreatmentsExtendedBolusesFragment(): TreatmentsExtendedBolusesFragment
@ContributesAndroidInjector abstract fun contributesTreatmentsCareportalFragment(): TreatmentsCareportalFragment

View file

@ -1,57 +0,0 @@
package info.nightscout.androidaps.plugins.treatments.fragments;
import android.content.Context;
import android.util.AttributeSet;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;
import java.util.ArrayList;
import java.util.List;
import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.utils.Round;
/**
* Created by Adrian on 15.04.2018.
*/
public class ProfileGraph extends GraphView {
public ProfileGraph(Context context) {
super(context);
}
public ProfileGraph(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void show(Profile profile) {
removeAllSeries();
LineGraphSeries<DataPoint> basalSeries = null;
List<DataPoint> basalArray = new ArrayList<>();
for (int hour = 0; hour < 24; hour++) {
basalArray.add(new DataPoint(hour, profile.getBasalTimeFromMidnight(new Integer(hour*60*60))));
basalArray.add(new DataPoint(hour+1, profile.getBasalTimeFromMidnight(new Integer(hour*60*60))));
}
DataPoint[] basalDataPoints = new DataPoint[basalArray.size()];
basalDataPoints = basalArray.toArray(basalDataPoints);
addSeries(basalSeries = new LineGraphSeries<>(basalDataPoints));
basalSeries.setThickness(8);
basalSeries.setDrawBackground(true);
getViewport().setXAxisBoundsManual(true);
getViewport().setMinX(0);
getViewport().setMaxX(24);
getViewport().setYAxisBoundsManual(true);
getViewport().setMinY(0);
getViewport().setMaxY(Round.ceilTo(profile.getMaxDailyBasal()*1.1, 0.5));
getGridLabelRenderer().setNumHorizontalLabels(13);
getGridLabelRenderer().setVerticalLabelsColor(basalSeries.getColor());
}
}

View file

@ -0,0 +1,38 @@
package info.nightscout.androidaps.plugins.treatments.fragments
import android.content.Context
import android.util.AttributeSet
import com.jjoe64.graphview.GraphView
import com.jjoe64.graphview.series.DataPoint
import com.jjoe64.graphview.series.LineGraphSeries
import info.nightscout.androidaps.data.Profile
import info.nightscout.androidaps.utils.Round
import java.util.*
class ProfileGraph : GraphView {
constructor(context: Context?) : super(context)
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
fun show(profile: Profile) {
removeAllSeries()
val basalArray: MutableList<DataPoint> = ArrayList()
for (hour in 0..23) {
basalArray.add(DataPoint(hour.toDouble(), profile.getBasalTimeFromMidnight(hour * 60 * 60)))
basalArray.add(DataPoint((hour + 1).toDouble(), profile.getBasalTimeFromMidnight(hour * 60 * 60)))
}
val basalDataPoints: Array<DataPoint> = Array(basalArray.size){ i-> basalArray[i]}
val basalSeries: LineGraphSeries<DataPoint> = LineGraphSeries(basalDataPoints)
addSeries(basalSeries)
basalSeries.thickness = 8
basalSeries.isDrawBackground = true
viewport.isXAxisBoundsManual = true
viewport.setMinX(0.0)
viewport.setMaxX(24.0)
viewport.isYAxisBoundsManual = true
viewport.setMinY(0.0)
viewport.setMaxY(Round.ceilTo(profile.maxDailyBasal * 1.1, 0.5))
gridLabelRenderer.numHorizontalLabels = 13
gridLabelRenderer.verticalLabelsColor = basalSeries.color
}
}

View file

@ -1,197 +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.TextView;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import javax.inject.Inject;
import dagger.android.support.DaggerFragment;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.Intervals;
import info.nightscout.androidaps.data.IobTotal;
import info.nightscout.androidaps.db.ExtendedBolus;
import info.nightscout.androidaps.db.Source;
import info.nightscout.androidaps.events.EventExtendedBolusChange;
import info.nightscout.androidaps.interfaces.ActivePluginProvider;
import info.nightscout.androidaps.plugins.bus.RxBus;
import info.nightscout.androidaps.plugins.general.nsclient.NSUpload;
import info.nightscout.androidaps.plugins.general.nsclient.UploadQueue;
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutosensCalculationFinished;
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin;
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 io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
public class TreatmentsExtendedBolusesFragment extends DaggerFragment {
@Inject ActivePluginProvider activePlugin;
private CompositeDisposable disposable = new CompositeDisposable();
private RecyclerView recyclerView;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ExtendedBolusesViewHolder> {
Intervals<ExtendedBolus> extendedBolusList;
RecyclerViewAdapter(Intervals<ExtendedBolus> extendedBolusList) {
this.extendedBolusList = extendedBolusList;
}
@NonNull
@Override
public ExtendedBolusesViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.treatments_extendedbolus_item, viewGroup, false);
return new ExtendedBolusesViewHolder(v);
}
@Override
public void onBindViewHolder(ExtendedBolusesViewHolder holder, int position) {
ExtendedBolus extendedBolus = extendedBolusList.getReversed(position);
holder.ph.setVisibility(extendedBolus.source == Source.PUMP ? View.VISIBLE : View.GONE);
holder.ns.setVisibility(NSUpload.isIdValid(extendedBolus._id) ? View.VISIBLE : View.GONE);
if (extendedBolus.isEndingEvent()) {
holder.date.setText(DateUtil.dateAndTimeString(extendedBolus.date));
holder.duration.setText(MainApp.gs(R.string.cancel));
holder.insulin.setText("");
holder.realDuration.setText("");
holder.iob.setText("");
holder.insulinSoFar.setText("");
holder.ratio.setText("");
} else {
if (extendedBolus.isInProgress()) {
holder.date.setText(DateUtil.dateAndTimeString(extendedBolus.date));
} else {
holder.date.setText(DateUtil.dateAndTimeString(extendedBolus.date) + " - " + DateUtil.timeString(extendedBolus.end()));
}
holder.duration.setText(DecimalFormatter.to0Decimal(extendedBolus.durationInMinutes) + " min");
holder.insulin.setText(DecimalFormatter.toPumpSupportedBolus(extendedBolus.insulin, activePlugin.getActivePump()) + " U");
holder.realDuration.setText(DecimalFormatter.to0Decimal(extendedBolus.getRealDuration()) + " min");
IobTotal iob = extendedBolus.iobCalc(System.currentTimeMillis());
holder.iob.setText(DecimalFormatter.to2Decimal(iob.iob) + " U");
holder.insulinSoFar.setText(DecimalFormatter.to2Decimal(extendedBolus.insulinSoFar()) + " U");
holder.ratio.setText(DecimalFormatter.to2Decimal(extendedBolus.absoluteRate()) + " U/h");
if (extendedBolus.isInProgress())
holder.date.setTextColor(ContextCompat.getColor(MainApp.instance(), R.color.colorActive));
else
holder.date.setTextColor(holder.insulin.getCurrentTextColor());
if (extendedBolus.iobCalc(System.currentTimeMillis()).iob != 0)
holder.iob.setTextColor(ContextCompat.getColor(MainApp.instance(), R.color.colorActive));
else
holder.iob.setTextColor(holder.insulin.getCurrentTextColor());
}
holder.remove.setTag(extendedBolus);
}
@Override
public int getItemCount() {
return extendedBolusList.size();
}
@Override
public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
class ExtendedBolusesViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView date;
TextView duration;
TextView insulin;
TextView realDuration;
TextView ratio;
TextView insulinSoFar;
TextView iob;
TextView remove;
TextView ph;
TextView ns;
ExtendedBolusesViewHolder(View itemView) {
super(itemView);
cv = itemView.findViewById(R.id.extendedboluses_cardview);
date = itemView.findViewById(R.id.extendedboluses_date);
duration = itemView.findViewById(R.id.extendedboluses_duration);
insulin = itemView.findViewById(R.id.extendedboluses_insulin);
realDuration = itemView.findViewById(R.id.extendedboluses_realduration);
ratio = itemView.findViewById(R.id.extendedboluses_ratio);
insulinSoFar = itemView.findViewById(R.id.extendedboluses_netinsulin);
iob = itemView.findViewById(R.id.extendedboluses_iob);
ph = itemView.findViewById(R.id.pump_sign);
ns = itemView.findViewById(R.id.ns_sign);
remove = itemView.findViewById(R.id.extendedboluses_remove);
remove.setOnClickListener(v -> {
final ExtendedBolus extendedBolus = (ExtendedBolus) v.getTag();
OKDialog.showConfirmation(getContext(), MainApp.gs(R.string.removerecord),
MainApp.gs(R.string.extended_bolus)
+ "\n" + MainApp.gs(R.string.date) + ": " + DateUtil.dateAndTimeString(extendedBolus.date), (dialog, id) -> {
final String _id = extendedBolus._id;
if (NSUpload.isIdValid(_id)) {
NSUpload.removeCareportalEntryFromNS(_id);
} else {
UploadQueue.removeID("dbAdd", _id);
}
MainApp.getDbHelper().delete(extendedBolus);
}, null);
});
remove.setPaintFlags(remove.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
}
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.treatments_extendedbolus_fragment, container, false);
recyclerView = view.findViewById(R.id.extendedboluses_recyclerview);
recyclerView.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(view.getContext());
recyclerView.setLayoutManager(llm);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(TreatmentsPlugin.getPlugin().getExtendedBolusesFromHistory());
recyclerView.setAdapter(adapter);
return view;
}
@Override
public synchronized void onResume() {
super.onResume();
disposable.add(RxBus.Companion.getINSTANCE()
.toObservable(EventExtendedBolusChange.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> updateGui(), exception -> FabricPrivacy.getInstance().logException(exception))
);
disposable.add(RxBus.Companion.getINSTANCE()
.toObservable(EventAutosensCalculationFinished.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> updateGui(), exception -> FabricPrivacy.getInstance().logException(exception))
);
updateGui();
}
@Override
public synchronized void onPause() {
super.onPause();
disposable.clear();
}
private void updateGui() {
recyclerView.swapAdapter(new RecyclerViewAdapter(TreatmentsPlugin.getPlugin().getExtendedBolusesFromHistory()), false);
}
}

View file

@ -0,0 +1,152 @@
package info.nightscout.androidaps.plugins.treatments.fragments
import android.annotation.SuppressLint
import android.content.DialogInterface
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.MainApp
import info.nightscout.androidaps.R
import info.nightscout.androidaps.data.Intervals
import info.nightscout.androidaps.db.ExtendedBolus
import info.nightscout.androidaps.db.Source
import info.nightscout.androidaps.events.EventExtendedBolusChange
import info.nightscout.androidaps.interfaces.ActivePluginProvider
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.general.nsclient.NSUpload
import info.nightscout.androidaps.plugins.general.nsclient.UploadQueue
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutosensCalculationFinished
import info.nightscout.androidaps.plugins.treatments.fragments.TreatmentsExtendedBolusesFragment.RecyclerViewAdapter.ExtendedBolusesViewHolder
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.OKDialog.showConfirmation
import info.nightscout.androidaps.utils.resources.ResourceHelper
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import kotlinx.android.synthetic.main.treatments_extendedbolus_fragment.*
import javax.inject.Inject
class TreatmentsExtendedBolusesFragment : DaggerFragment() {
private val disposable = CompositeDisposable()
@Inject lateinit var activePlugin: ActivePluginProvider
@Inject lateinit var rxBus: RxBusWrapper
@Inject lateinit var resourceHelper: ResourceHelper
@Inject lateinit var fabricPrivacy: FabricPrivacy
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.treatments_extendedbolus_fragment, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
extendedboluses_recyclerview.setHasFixedSize(true)
extendedboluses_recyclerview.layoutManager = LinearLayoutManager(view.context)
extendedboluses_recyclerview.adapter = RecyclerViewAdapter(activePlugin.activeTreatments.extendedBolusesFromHistory)
}
inner class RecyclerViewAdapter internal constructor(private var extendedBolusList: Intervals<ExtendedBolus>) : RecyclerView.Adapter<ExtendedBolusesViewHolder>() {
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ExtendedBolusesViewHolder {
val v = LayoutInflater.from(viewGroup.context).inflate(R.layout.treatments_extendedbolus_item, viewGroup, false)
return ExtendedBolusesViewHolder(v)
}
override fun onBindViewHolder(holder: ExtendedBolusesViewHolder, position: Int) {
val extendedBolus = extendedBolusList.getReversed(position)
holder.ph.visibility = if (extendedBolus.source == Source.PUMP) View.VISIBLE else View.GONE
holder.ns.visibility = if (NSUpload.isIdValid(extendedBolus._id)) View.VISIBLE else View.GONE
if (extendedBolus.isEndingEvent) {
holder.date.text = DateUtil.dateAndTimeString(extendedBolus.date)
holder.duration.text = resourceHelper.gs(R.string.cancel)
holder.insulin.text = ""
holder.realDuration.text = ""
holder.iob.text = ""
holder.insulinSoFar.text = ""
holder.ratio.text = ""
} else {
@SuppressLint("SetTextI18n")
if (extendedBolus.isInProgress) holder.date.text = DateUtil.dateAndTimeString(extendedBolus.date)
else holder.date.text = DateUtil.dateAndTimeString(extendedBolus.date) + " - " + DateUtil.timeString(extendedBolus.end())
holder.duration.text = resourceHelper.gs(R.string.format_mins, extendedBolus.durationInMinutes)
holder.insulin.text = resourceHelper.gs(R.string.formatinsulinunits, extendedBolus.insulin)
holder.realDuration.text = resourceHelper.gs(R.string.format_mins, extendedBolus.realDuration)
val iob = extendedBolus.iobCalc(System.currentTimeMillis())
holder.iob.text = resourceHelper.gs(R.string.formatinsulinunits, iob.iob)
holder.insulinSoFar.text = resourceHelper.gs(R.string.formatinsulinunits, extendedBolus.insulinSoFar())
holder.ratio.text = resourceHelper.gs(R.string.pump_basebasalrate, extendedBolus.absoluteRate())
if (extendedBolus.isInProgress) holder.date.setTextColor(resourceHelper.gc(R.color.colorActive)) else holder.date.setTextColor(holder.insulin.currentTextColor)
if (extendedBolus.iobCalc(System.currentTimeMillis()).iob != 0.0) holder.iob.setTextColor(resourceHelper.gc(R.color.colorActive)) else holder.iob.setTextColor(holder.insulin.currentTextColor)
}
holder.remove.tag = extendedBolus
}
override fun getItemCount(): Int {
return extendedBolusList.size()
}
inner class ExtendedBolusesViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var cv: CardView = itemView.findViewById(R.id.extendedboluses_cardview)
var date: TextView = itemView.findViewById(R.id.extendedboluses_date)
var duration: TextView = itemView.findViewById(R.id.extendedboluses_duration)
var insulin: TextView = itemView.findViewById(R.id.extendedboluses_insulin)
var realDuration: TextView = itemView.findViewById(R.id.extendedboluses_realduration)
var ratio: TextView = itemView.findViewById(R.id.extendedboluses_ratio)
var insulinSoFar: TextView = itemView.findViewById(R.id.extendedboluses_netinsulin)
var iob: TextView = itemView.findViewById(R.id.extendedboluses_iob)
var remove: TextView = itemView.findViewById(R.id.extendedboluses_remove)
var ph: TextView = itemView.findViewById(R.id.pump_sign)
var ns: TextView = itemView.findViewById(R.id.ns_sign)
init {
remove.setOnClickListener { v: View ->
val extendedBolus = v.tag as ExtendedBolus
context?.let {
showConfirmation(it, resourceHelper.gs(R.string.removerecord),
"""
${resourceHelper.gs(R.string.extended_bolus)}
${resourceHelper.gs(R.string.date)}: ${DateUtil.dateAndTimeString(extendedBolus.date)}
""".trimIndent(), DialogInterface.OnClickListener { _: DialogInterface, _: Int ->
val id = extendedBolus._id
if (NSUpload.isIdValid(id)) NSUpload.removeCareportalEntryFromNS(id)
else UploadQueue.removeID("dbAdd", id)
MainApp.getDbHelper().delete(extendedBolus)
}, null)
}
}
remove.paintFlags = remove.paintFlags or Paint.UNDERLINE_TEXT_FLAG
}
}
}
@Synchronized override fun onResume() {
super.onResume()
disposable.add(rxBus
.toObservable(EventExtendedBolusChange::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()
}
private fun updateGui() =
extendedboluses_recyclerview.swapAdapter(RecyclerViewAdapter(activePlugin.activeTreatments.extendedBolusesFromHistory), false)
}

View file

@ -1,225 +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.TextView;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.Intervals;
import info.nightscout.androidaps.data.IobTotal;
import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.db.Source;
import info.nightscout.androidaps.db.TemporaryBasal;
import info.nightscout.androidaps.events.EventTempBasalChange;
import info.nightscout.androidaps.plugins.bus.RxBus;
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.general.nsclient.NSUpload;
import info.nightscout.androidaps.plugins.general.nsclient.UploadQueue;
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutosensCalculationFinished;
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin;
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 io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
public class TreatmentsTemporaryBasalsFragment extends Fragment {
private CompositeDisposable disposable = new CompositeDisposable();
private RecyclerView recyclerView;
private TextView tempBasalTotalView;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.TempBasalsViewHolder> {
Intervals<TemporaryBasal> tempBasalList;
RecyclerViewAdapter(Intervals<TemporaryBasal> tempBasalList) {
this.tempBasalList = tempBasalList;
}
@NonNull
@Override
public TempBasalsViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.treatments_tempbasals_item, viewGroup, false);
return new TempBasalsViewHolder(v);
}
@Override
public void onBindViewHolder(TempBasalsViewHolder holder, int position) {
TemporaryBasal tempBasal = tempBasalList.getReversed(position);
holder.ph.setVisibility(tempBasal.source == Source.PUMP ? View.VISIBLE : View.GONE);
holder.ns.setVisibility(NSUpload.isIdValid(tempBasal._id) ? View.VISIBLE : View.GONE);
if (tempBasal.isEndingEvent()) {
holder.date.setText(DateUtil.dateAndTimeString(tempBasal.date));
holder.duration.setText(MainApp.gs(R.string.cancel));
holder.absolute.setText("");
holder.percent.setText("");
holder.realDuration.setText("");
holder.iob.setText("");
holder.netInsulin.setText("");
holder.netRatio.setText("");
holder.extendedFlag.setVisibility(View.GONE);
holder.iob.setTextColor(holder.netRatio.getCurrentTextColor());
} else {
if (tempBasal.isInProgress()) {
holder.date.setText(DateUtil.dateAndTimeString(tempBasal.date));
holder.date.setTextColor(ContextCompat.getColor(MainApp.instance(), R.color.colorActive));
} else {
holder.date.setText(DateUtil.dateAndTimeRangeString(tempBasal.date, tempBasal.end()));
holder.date.setTextColor(holder.netRatio.getCurrentTextColor());
}
holder.duration.setText(DecimalFormatter.to0Decimal(tempBasal.durationInMinutes, " min"));
if (tempBasal.isAbsolute) {
Profile profile = ConfigBuilderPlugin.getPlugin().getProfileFunction().getProfile(tempBasal.date);
if (profile != null) {
holder.absolute.setText(DecimalFormatter.to2Decimal(tempBasal.tempBasalConvertedToAbsolute(tempBasal.date, profile), " U/h"));
holder.percent.setText("");
} else {
holder.absolute.setText(MainApp.gs(R.string.noprofile));
holder.percent.setText("");
}
} else {
holder.absolute.setText("");
holder.percent.setText(DecimalFormatter.to0Decimal(tempBasal.percentRate, "%"));
}
holder.realDuration.setText(DecimalFormatter.to0Decimal(tempBasal.getRealDuration(), " min"));
long now = DateUtil.now();
IobTotal iob = new IobTotal(now);
Profile profile = ConfigBuilderPlugin.getPlugin().getProfileFunction().getProfile(now);
if (profile != null)
iob = tempBasal.iobCalc(now, profile);
holder.iob.setText(DecimalFormatter.to2Decimal(iob.basaliob, " U"));
holder.netInsulin.setText(DecimalFormatter.to2Decimal(iob.netInsulin, " U"));
holder.netRatio.setText(DecimalFormatter.to2Decimal(iob.netRatio, " U/h"));
holder.extendedFlag.setVisibility(View.GONE);
if (iob.basaliob != 0)
holder.iob.setTextColor(ContextCompat.getColor(MainApp.instance(), R.color.colorActive));
else
holder.iob.setTextColor(holder.netRatio.getCurrentTextColor());
}
holder.remove.setTag(tempBasal);
}
@Override
public int getItemCount() {
return tempBasalList.size();
}
@Override
public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
class TempBasalsViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView date;
TextView duration;
TextView absolute;
TextView percent;
TextView realDuration;
TextView netRatio;
TextView netInsulin;
TextView iob;
TextView extendedFlag;
TextView remove;
TextView ph;
TextView ns;
TempBasalsViewHolder(View itemView) {
super(itemView);
cv = itemView.findViewById(R.id.tempbasals_cardview);
date = itemView.findViewById(R.id.tempbasals_date);
duration = itemView.findViewById(R.id.tempbasals_duration);
absolute = itemView.findViewById(R.id.tempbasals_absolute);
percent = itemView.findViewById(R.id.tempbasals_percent);
realDuration = itemView.findViewById(R.id.tempbasals_realduration);
netRatio = itemView.findViewById(R.id.tempbasals_netratio);
netInsulin = itemView.findViewById(R.id.tempbasals_netinsulin);
iob = itemView.findViewById(R.id.tempbasals_iob);
extendedFlag = itemView.findViewById(R.id.tempbasals_extendedflag);
ph = itemView.findViewById(R.id.pump_sign);
ns = itemView.findViewById(R.id.ns_sign);
remove = itemView.findViewById(R.id.tempbasals_remove);
remove.setOnClickListener(v -> {
final TemporaryBasal tempBasal = (TemporaryBasal) v.getTag();
OKDialog.showConfirmation(getContext(), MainApp.gs(R.string.removerecord),
MainApp.gs(R.string.pump_tempbasal_label) + ": " + tempBasal.toStringFull() +
"\n" + MainApp.gs(R.string.date) + ": " + DateUtil.dateAndTimeString(tempBasal.date),
((dialog, id) -> {
final String _id = tempBasal._id;
if (NSUpload.isIdValid(_id)) {
NSUpload.removeCareportalEntryFromNS(_id);
} else {
UploadQueue.removeID("dbAdd", _id);
}
MainApp.getDbHelper().delete(tempBasal);
}), null);
});
remove.setPaintFlags(remove.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
}
}
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.treatments_tempbasals_fragment, container, false);
recyclerView = view.findViewById(R.id.tempbasals_recyclerview);
recyclerView.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(view.getContext());
recyclerView.setLayoutManager(llm);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(TreatmentsPlugin.getPlugin().getTemporaryBasalsFromHistory());
recyclerView.setAdapter(adapter);
tempBasalTotalView = view.findViewById(R.id.tempbasals_totaltempiob);
return view;
}
@Override
public synchronized void onResume() {
super.onResume();
disposable.add(RxBus.Companion.getINSTANCE()
.toObservable(EventTempBasalChange.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> updateGui(), exception -> FabricPrivacy.getInstance().logException(exception))
);
disposable.add(RxBus.Companion.getINSTANCE()
.toObservable(EventAutosensCalculationFinished.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> updateGui(), exception -> FabricPrivacy.getInstance().logException(exception))
);
updateGui();
}
@Override
public synchronized void onPause() {
super.onPause();
disposable.clear();
}
private void updateGui() {
recyclerView.swapAdapter(new RecyclerViewAdapter(TreatmentsPlugin.getPlugin().getTemporaryBasalsFromHistory()), false);
IobTotal tempBasalsCalculation = TreatmentsPlugin.getPlugin().getLastCalculationTempBasals();
if (tempBasalsCalculation != null)
tempBasalTotalView.setText(DecimalFormatter.to2Decimal(tempBasalsCalculation.basaliob, " U"));
}
}

View file

@ -0,0 +1,184 @@
package info.nightscout.androidaps.plugins.treatments.fragments
import android.content.DialogInterface
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.MainApp
import info.nightscout.androidaps.R
import info.nightscout.androidaps.data.Intervals
import info.nightscout.androidaps.data.IobTotal
import info.nightscout.androidaps.db.Source
import info.nightscout.androidaps.db.TemporaryBasal
import info.nightscout.androidaps.events.EventTempBasalChange
import info.nightscout.androidaps.interfaces.ActivePluginProvider
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.iob.iobCobCalculator.events.EventAutosensCalculationFinished
import info.nightscout.androidaps.plugins.treatments.fragments.TreatmentsTemporaryBasalsFragment.RecyclerViewAdapter.TempBasalsViewHolder
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.OKDialog.showConfirmation
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_tempbasals_fragment.*
import javax.inject.Inject
class TreatmentsTemporaryBasalsFragment : DaggerFragment() {
private val disposable = CompositeDisposable()
@Inject lateinit var rxBus: RxBusWrapper
@Inject lateinit var sp: SP
@Inject lateinit var resourceHelper: ResourceHelper
@Inject lateinit var fabricPrivacy: FabricPrivacy
@Inject lateinit var activePlugin: ActivePluginProvider
@Inject lateinit var profileFunction: ProfileFunction
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.treatments_tempbasals_fragment, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
tempbasals_recyclerview.setHasFixedSize(true)
tempbasals_recyclerview.layoutManager = LinearLayoutManager(view.context)
tempbasals_recyclerview.adapter = RecyclerViewAdapter(activePlugin.activeTreatments.temporaryBasalsFromHistory)
}
@Synchronized override fun onResume() {
super.onResume()
disposable.add(rxBus
.toObservable(EventTempBasalChange::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(private var tempBasalList: Intervals<TemporaryBasal>) : RecyclerView.Adapter<TempBasalsViewHolder>() {
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): TempBasalsViewHolder {
val v = LayoutInflater.from(viewGroup.context).inflate(R.layout.treatments_tempbasals_item, viewGroup, false)
return TempBasalsViewHolder(v)
}
override fun onBindViewHolder(holder: TempBasalsViewHolder, position: Int) {
val tempBasal = tempBasalList.getReversed(position)
holder.ph.visibility = if (tempBasal.source == Source.PUMP) View.VISIBLE else View.GONE
holder.ns.visibility = if (NSUpload.isIdValid(tempBasal._id)) View.VISIBLE else View.GONE
if (tempBasal.isEndingEvent) {
holder.date.text = DateUtil.dateAndTimeString(tempBasal.date)
holder.duration.text = resourceHelper.gs(R.string.cancel)
holder.absolute.text = ""
holder.percent.text = ""
holder.realDuration.text = ""
holder.iob.text = ""
holder.netInsulin.text = ""
holder.netRatio.text = ""
holder.extendedFlag.visibility = View.GONE
holder.iob.setTextColor(holder.netRatio.currentTextColor)
} else {
if (tempBasal.isInProgress) {
holder.date.text = DateUtil.dateAndTimeString(tempBasal.date)
holder.date.setTextColor(resourceHelper.gc(R.color.colorActive))
} else {
holder.date.text = DateUtil.dateAndTimeRangeString(tempBasal.date, tempBasal.end())
holder.date.setTextColor(holder.netRatio.currentTextColor)
}
holder.duration.text = resourceHelper.gs(R.string.format_mins, tempBasal.durationInMinutes)
if (tempBasal.isAbsolute) {
val profile = profileFunction.getProfile(tempBasal.date)
if (profile != null) {
holder.absolute.text = resourceHelper.gs(R.string.pump_basebasalrate, tempBasal.tempBasalConvertedToAbsolute(tempBasal.date, profile))
holder.percent.text = ""
} else {
holder.absolute.text = resourceHelper.gs(R.string.noprofile)
holder.percent.text = ""
}
} else {
holder.absolute.text = ""
holder.percent.text = resourceHelper.gs(R.string.format_percent, tempBasal.percentRate)
}
holder.realDuration.text = resourceHelper.gs(R.string.format_mins, tempBasal.realDuration)
val now = DateUtil.now()
var iob = IobTotal(now)
val profile = profileFunction.getProfile(now)
if (profile != null) iob = tempBasal.iobCalc(now, profile)
holder.iob.text = resourceHelper.gs(R.string.formatinsulinunits, iob.basaliob)
holder.netInsulin.text = resourceHelper.gs(R.string.formatinsulinunits, iob.netInsulin)
holder.netRatio.text = resourceHelper.gs(R.string.pump_basebasalrate, iob.netRatio)
holder.extendedFlag.visibility = View.GONE
if (iob.basaliob != 0.0) holder.iob.setTextColor(resourceHelper.gc(R.color.colorActive)) else holder.iob.setTextColor(holder.netRatio.currentTextColor)
}
holder.remove.tag = tempBasal
}
override fun getItemCount(): Int {
return tempBasalList.size()
}
inner class TempBasalsViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var cv: CardView = itemView.findViewById(R.id.tempbasals_cardview)
var date: TextView = itemView.findViewById(R.id.tempbasals_date)
var duration: TextView = itemView.findViewById(R.id.tempbasals_duration)
var absolute: TextView = itemView.findViewById(R.id.tempbasals_absolute)
var percent: TextView = itemView.findViewById(R.id.tempbasals_percent)
var realDuration: TextView = itemView.findViewById(R.id.tempbasals_realduration)
var netRatio: TextView = itemView.findViewById(R.id.tempbasals_netratio)
var netInsulin: TextView = itemView.findViewById(R.id.tempbasals_netinsulin)
var iob: TextView = itemView.findViewById(R.id.tempbasals_iob)
var extendedFlag: TextView = itemView.findViewById(R.id.tempbasals_extendedflag)
var remove: TextView = itemView.findViewById(R.id.tempbasals_remove)
var ph: TextView = itemView.findViewById(R.id.pump_sign)
var ns: TextView = itemView.findViewById(R.id.ns_sign)
init {
remove.setOnClickListener { v: View ->
val tempBasal = v.tag as TemporaryBasal
context?.let {
showConfirmation(it, resourceHelper.gs(R.string.removerecord),
"""
${resourceHelper.gs(R.string.pump_tempbasal_label)}: ${tempBasal.toStringFull()}
${resourceHelper.gs(R.string.date)}: ${DateUtil.dateAndTimeString(tempBasal.date)}
""".trimIndent(),
DialogInterface.OnClickListener { _: DialogInterface?, _: Int ->
val id = tempBasal._id
if (NSUpload.isIdValid(id)) NSUpload.removeCareportalEntryFromNS(id)
else UploadQueue.removeID("dbAdd", id)
MainApp.getDbHelper().delete(tempBasal)
}, null)
}
}
remove.paintFlags = remove.paintFlags or Paint.UNDERLINE_TEXT_FLAG
}
}
}
private fun updateGui() {
tempbasals_recyclerview?.swapAdapter(RecyclerViewAdapter(activePlugin.activeTreatments.temporaryBasalsFromHistory), false)
val tempBasalsCalculation = activePlugin.activeTreatments.lastCalculationTempBasals
if (tempBasalsCalculation != null) tempbasals_totaltempiob?.text = resourceHelper.gs(R.string.formatinsulinunits, tempBasalsCalculation.basaliob)
}
}

View file

@ -79,22 +79,22 @@ class SetupWizardActivity : NoSplashAppCompatActivity() {
disposable.add(rxBus
.toObservable(EventPumpStatusChanged::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ updateButtons() }) { exception: Throwable? -> fabricPrivacy.logException(exception!!) }
.subscribe({ updateButtons() }) { fabricPrivacy.logException(it) }
)
disposable.add(rxBus
.toObservable(EventNSClientStatus::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ updateButtons() }) { exception: Throwable? -> fabricPrivacy.logException(exception!!) }
.subscribe({ updateButtons() }) { fabricPrivacy.logException(it) }
)
disposable.add(rxBus
.toObservable(EventProfileNeedsUpdate::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ updateButtons() }) { exception: Throwable? -> fabricPrivacy.logException(exception!!) }
.subscribe({ updateButtons() }) { fabricPrivacy.logException(it) }
)
disposable.add(rxBus
.toObservable(EventProfileStoreChanged::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ updateButtons() }) { exception: Throwable? -> fabricPrivacy.logException(exception!!) }
.subscribe({ updateButtons() }) { fabricPrivacy.logException(it) }
)
disposable.add(rxBus
.toObservable(EventSWUpdate::class.java)
@ -102,7 +102,7 @@ class SetupWizardActivity : NoSplashAppCompatActivity() {
.subscribe({ event: EventSWUpdate ->
if (event.redraw) generateLayout()
updateButtons()
}) { exception: Throwable? -> fabricPrivacy.logException(exception!!) }
}) { fabricPrivacy.logException(it) }
)
}