ComboFragment -> kt

This commit is contained in:
Milos Kozak 2022-04-14 10:39:32 +02:00
parent 2ac4cbd1db
commit 36999ebd87
4 changed files with 256 additions and 308 deletions

View file

@ -47,10 +47,8 @@ class VirtualPumpFragment : DaggerFragment() {
// onDestroyView.
private val binding get() = _binding!!
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
_binding = VirtualpumpFragmentBinding.inflate(inflater, container, false)
return binding.root
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View =
VirtualpumpFragmentBinding.inflate(inflater, container, false).also { _binding = it }.root
@Synchronized
override fun onResume() {

View file

@ -1,302 +0,0 @@
package info.nightscout.androidaps.plugins.pump.combo;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import javax.inject.Inject;
import dagger.android.support.DaggerFragment;
import info.nightscout.androidaps.combo.R;
import info.nightscout.androidaps.interfaces.CommandQueue;
import info.nightscout.androidaps.plugins.bus.RxBus;
import info.nightscout.androidaps.plugins.pump.combo.data.ComboErrorUtil;
import info.nightscout.androidaps.plugins.pump.combo.events.EventComboPumpUpdateGUI;
import info.nightscout.androidaps.plugins.pump.combo.ruffyscripter.PumpState;
import info.nightscout.androidaps.plugins.pump.combo.ruffyscripter.history.Bolus;
import info.nightscout.androidaps.queue.Callback;
import info.nightscout.androidaps.queue.events.EventQueueChanged;
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.FabricPrivacy;
import info.nightscout.androidaps.utils.resources.ResourceHelper;
import info.nightscout.androidaps.utils.rx.AapsSchedulers;
import io.reactivex.rxjava3.disposables.CompositeDisposable;
public class ComboFragment extends DaggerFragment {
@Inject ComboPlugin comboPlugin;
@Inject CommandQueue commandQueue;
@Inject ResourceHelper rh;
@Inject RxBus rxBus;
@Inject DateUtil dateUtil;
@Inject FabricPrivacy fabricPrivacy;
@Inject AapsSchedulers aapsSchedulers;
@Inject ComboErrorUtil errorUtil;
private final CompositeDisposable disposable = new CompositeDisposable();
private TextView stateView;
private TextView activityView;
private TextView batteryView;
private TextView reservoirView;
private TextView lastConnectionView;
private TextView lastBolusView;
private TextView baseBasalRate;
private TextView tempBasalText;
private Button refreshButton;
private TextView bolusCount;
private TextView tbrCount;
private View errorCountDelimiter;
private LinearLayout errorCountLayout;
private TextView errorCountLabel;
private TextView errorCountDots;
private TextView errorCountValue;
private TextView serialNumber;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.combopump_fragment, container, false);
stateView = view.findViewById(R.id.combo_state);
activityView = view.findViewById(R.id.combo_activity);
batteryView = view.findViewById(R.id.combo_pumpstate_battery);
reservoirView = view.findViewById(R.id.combo_insulinstate);
lastBolusView = view.findViewById(R.id.combo_last_bolus);
lastConnectionView = view.findViewById(R.id.combo_lastconnection);
baseBasalRate = view.findViewById(R.id.combo_base_basal_rate);
tempBasalText = view.findViewById(R.id.combo_temp_basal);
bolusCount = view.findViewById(R.id.combo_bolus_count);
tbrCount = view.findViewById(R.id.combo_tbr_count);
serialNumber = view.findViewById(R.id.serial_number);
errorCountDelimiter = view.findViewById(R.id.combo_connection_error_delimiter);
errorCountLayout = view.findViewById(R.id.combo_connection_error_layout);
errorCountLabel = view.findViewById(R.id.combo_connection_error_label);
errorCountDots = view.findViewById(R.id.combo_connection_error_dots);
errorCountValue = view.findViewById(R.id.combo_connection_error_value);
refreshButton = view.findViewById(R.id.combo_refresh_button);
refreshButton.setOnClickListener(v -> {
refreshButton.setEnabled(false);
commandQueue.readStatus(rh.gs(R.string.user_request), new Callback() {
@Override
public void run() {
runOnUiThread(() -> refreshButton.setEnabled(true));
}
});
});
return view;
}
@Override
public synchronized void onResume() {
super.onResume();
disposable.add(rxBus
.toObservable(EventComboPumpUpdateGUI.class)
.observeOn(aapsSchedulers.getMain())
.subscribe(event -> updateGui(), fabricPrivacy::logException)
);
disposable.add(rxBus
.toObservable(EventQueueChanged.class)
.observeOn(aapsSchedulers.getMain())
.subscribe(event -> updateGui(), fabricPrivacy::logException)
);
updateGui();
}
@Override
public synchronized void onPause() {
super.onPause();
disposable.clear();
}
private void runOnUiThread(Runnable action) {
FragmentActivity activity = getActivity();
if (activity != null) {
activity.runOnUiThread(action);
}
}
public void updateGui() {
// state
stateView.setText(comboPlugin.getStateSummary());
PumpState ps = comboPlugin.getPump().state;
if (ps.insulinState == PumpState.EMPTY || ps.batteryState == PumpState.EMPTY
|| ps.activeAlert != null && ps.activeAlert.errorCode != null) {
stateView.setTextColor(rh.gac(getContext(), R.attr.warningColor));
stateView.setTypeface(null, Typeface.BOLD);
} else if (comboPlugin.getPump().state.suspended
|| ps.activeAlert != null && ps.activeAlert.warningCode != null) {
stateView.setTextColor(rh.gac(getContext(), R.attr.omniYellowColor));
stateView.setTypeface(null, Typeface.BOLD);
} else {
stateView.setTextColor(rh.gac(getContext(), R.attr.defaultTextColor));
stateView.setTypeface(null, Typeface.NORMAL);
}
// activity
String activity = comboPlugin.getPump().activity;
if (activity != null) {
activityView.setTextColor(rh.gac(getContext(), R.attr.defaultTextColor));
activityView.setTextSize(14);
activityView.setText(activity);
} else if (commandQueue.size() > 0) {
activityView.setTextColor(rh.gac(getContext(), R.attr.defaultTextColor));
activityView.setTextSize(14);
activityView.setText("");
} else if (comboPlugin.isInitialized()) {
activityView.setTextColor(rh.gac(getContext(), R.attr.defaultTextColor));
activityView.setTextSize(20);
activityView.setText("{fa-bed}");
} else {
activityView.setTextColor(rh.gac(getContext(), R.attr.warningColor));
activityView.setTextSize(14);
activityView.setText(rh.gs(R.string.pump_unreachable));
}
if (comboPlugin.isInitialized()) {
// battery
batteryView.setTextSize(20);
if (ps.batteryState == PumpState.EMPTY) {
batteryView.setText("{fa-battery-empty}");
batteryView.setTextColor(rh.gac(getContext(), R.attr.warningColor));
} else if (ps.batteryState == PumpState.LOW) {
batteryView.setText("{fa-battery-quarter}");
batteryView.setTextColor(rh.gac(getContext(), R.attr.omniYellowColor));
} else {
batteryView.setText("{fa-battery-full}");
batteryView.setTextColor(Color.WHITE);
}
// reservoir
int reservoirLevel = comboPlugin.getPump().reservoirLevel;
if (reservoirLevel != -1) {
reservoirView.setText(reservoirLevel + " " + rh.gs(R.string.insulin_unit_shortname));
} else if (ps.insulinState == PumpState.LOW) {
reservoirView.setText(rh.gs(R.string.combo_reservoir_low));
} else if (ps.insulinState == PumpState.EMPTY) {
reservoirView.setText(rh.gs(R.string.combo_reservoir_empty));
} else {
reservoirView.setText(rh.gs(R.string.combo_reservoir_normal));
}
if (ps.insulinState == PumpState.UNKNOWN) {
reservoirView.setTextColor(rh.gac(getContext(), R.attr.defaultTextColor));
reservoirView.setTypeface(null, Typeface.NORMAL);
} else if (ps.insulinState == PumpState.LOW) {
reservoirView.setTextColor(rh.gac(getContext(), R.attr.omniYellowColor));
reservoirView.setTypeface(null, Typeface.BOLD);
} else if (ps.insulinState == PumpState.EMPTY) {
reservoirView.setTextColor(rh.gac(getContext(), R.attr.warningColor));
reservoirView.setTypeface(null, Typeface.BOLD);
} else {
reservoirView.setTextColor(rh.gac(getContext(), R.attr.defaultTextColor));
reservoirView.setTypeface(null, Typeface.NORMAL);
}
// last connection
String minAgo = dateUtil.minAgo(rh, comboPlugin.getPump().lastSuccessfulCmdTime);
long min = (System.currentTimeMillis() - comboPlugin.getPump().lastSuccessfulCmdTime) / 1000 / 60;
if (comboPlugin.getPump().lastSuccessfulCmdTime + 60 * 1000 > System.currentTimeMillis()) {
lastConnectionView.setText(R.string.combo_pump_connected_now);
lastConnectionView.setTextColor(rh.gac(getContext(), R.attr.defaultTextColor));
} else if (comboPlugin.getPump().lastSuccessfulCmdTime + 30 * 60 * 1000 < System.currentTimeMillis()) {
lastConnectionView.setText(rh.gs(R.string.combo_no_pump_connection, min));
lastConnectionView.setTextColor(rh.gac(getContext(), R.attr.warningColor));
} else {
lastConnectionView.setText(minAgo);
lastConnectionView.setTextColor(rh.gac(getContext(), R.attr.defaultTextColor));
}
// last bolus
Bolus bolus = comboPlugin.getPump().lastBolus;
if (bolus != null) {
long agoMsc = System.currentTimeMillis() - bolus.timestamp;
double bolusMinAgo = agoMsc / 60d / 1000d;
String unit = rh.gs(R.string.insulin_unit_shortname);
String ago;
if ((agoMsc < 60 * 1000)) {
ago = rh.gs(R.string.combo_pump_connected_now);
} else if (bolusMinAgo < 60) {
ago = dateUtil.minAgo(rh, bolus.timestamp);
} else {
ago = dateUtil.hourAgo(bolus.timestamp, rh);
}
lastBolusView.setText(rh.gs(R.string.combo_last_bolus, bolus.amount, unit, ago));
} else {
lastBolusView.setText("");
}
// base basal rate
baseBasalRate.setText(rh.gs(R.string.pump_basebasalrate, comboPlugin.getBaseBasalRate()));
// TBR
String tbrStr = "";
if (ps.tbrPercent != -1 && ps.tbrPercent != 100) {
long minSinceRead = (System.currentTimeMillis() - comboPlugin.getPump().state.timestamp) / 1000 / 60;
long remaining = ps.tbrRemainingDuration - minSinceRead;
if (remaining >= 0) {
tbrStr = rh.gs(R.string.combo_tbr_remaining, ps.tbrPercent, remaining);
}
}
tempBasalText.setText(tbrStr);
// stats
bolusCount.setText(String.valueOf(comboPlugin.getBolusesDelivered()));
tbrCount.setText(String.valueOf(comboPlugin.getTbrsSet()));
serialNumber.setText(comboPlugin.serialNumber());
updateErrorDisplay(false);
} else {
updateErrorDisplay(true);
}
}
private void updateErrorDisplay(boolean forceHide) {
int errorCount = -1;
if (!forceHide) {
ComboErrorUtil.DisplayType displayType = errorUtil.getDisplayType();
if (displayType== ComboErrorUtil.DisplayType.ON_ERROR || displayType== ComboErrorUtil.DisplayType.ALWAYS) {
int errorCountInternal = errorUtil.getErrorCount();
if (errorCountInternal>0) {
errorCount = errorCountInternal;
} else if (displayType== ComboErrorUtil.DisplayType.ALWAYS) {
errorCount = 0;
}
}
}
if (errorCount >=0) {
errorCountDelimiter.setVisibility(View.VISIBLE);
errorCountLayout.setVisibility(View.VISIBLE);
errorCountLabel.setVisibility(View.VISIBLE);
errorCountDots.setVisibility(View.VISIBLE);
errorCountValue.setVisibility(View.VISIBLE);
errorCountValue.setText(errorCount==0 ?
"-" :
""+errorCount);
} else {
errorCountDelimiter.setVisibility(View.GONE);
errorCountLayout.setVisibility(View.GONE);
errorCountLabel.setVisibility(View.GONE);
errorCountDots.setVisibility(View.GONE);
errorCountValue.setVisibility(View.GONE);
}
}
}

View file

@ -0,0 +1,254 @@
package info.nightscout.androidaps.plugins.pump.combo
import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.Typeface
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import dagger.android.support.DaggerFragment
import info.nightscout.androidaps.combo.R
import info.nightscout.androidaps.combo.databinding.CombopumpFragmentBinding
import info.nightscout.androidaps.extensions.runOnUiThread
import info.nightscout.androidaps.interfaces.CommandQueue
import info.nightscout.androidaps.plugins.bus.RxBus
import info.nightscout.androidaps.plugins.pump.combo.data.ComboErrorUtil
import info.nightscout.androidaps.plugins.pump.combo.data.ComboErrorUtil.DisplayType
import info.nightscout.androidaps.plugins.pump.combo.events.EventComboPumpUpdateGUI
import info.nightscout.androidaps.plugins.pump.combo.ruffyscripter.PumpState
import info.nightscout.androidaps.queue.Callback
import info.nightscout.androidaps.queue.events.EventQueueChanged
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.rx.AapsSchedulers
import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.kotlin.plusAssign
import javax.inject.Inject
class ComboFragment : DaggerFragment() {
@Inject lateinit var comboPlugin: ComboPlugin
@Inject lateinit var commandQueue: CommandQueue
@Inject lateinit var rh: ResourceHelper
@Inject lateinit var rxBus: RxBus
@Inject lateinit var dateUtil: DateUtil
@Inject lateinit var fabricPrivacy: FabricPrivacy
@Inject lateinit var aapsSchedulers: AapsSchedulers
@Inject lateinit var errorUtil: ComboErrorUtil
private val disposable = CompositeDisposable()
private var _binding: CombopumpFragmentBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View =
CombopumpFragmentBinding.inflate(inflater, container, false).also { _binding = it }.root
@Synchronized override fun onResume() {
super.onResume()
disposable += rxBus
.toObservable(EventComboPumpUpdateGUI::class.java)
.observeOn(aapsSchedulers.main)
.subscribe({ updateGui() }, fabricPrivacy::logException)
disposable += rxBus
.toObservable(EventQueueChanged::class.java)
.observeOn(aapsSchedulers.main)
.subscribe({ updateGui() }, fabricPrivacy::logException)
binding.comboRefreshButton.setOnClickListener {
binding.comboRefreshButton.isEnabled = false
commandQueue.readStatus(rh.gs(R.string.user_request), object : Callback() {
override fun run() {
runOnUiThread { binding.comboRefreshButton.isEnabled = true }
}
})
}
updateGui()
}
@Synchronized override fun onPause() {
super.onPause()
disposable.clear()
}
@SuppressLint("SetTextI18n")
fun updateGui() {
_binding ?: return
// state
binding.comboState.text = comboPlugin.stateSummary
val ps = comboPlugin.pump.state
if (ps.insulinState == PumpState.EMPTY || ps.batteryState == PumpState.EMPTY || ps.activeAlert != null && ps.activeAlert.errorCode != null) {
binding.comboState.setTextColor(rh.gac(context, R.attr.warningColor))
binding.comboState.setTypeface(null, Typeface.BOLD)
} else if (comboPlugin.pump.state.suspended
|| ps.activeAlert != null && ps.activeAlert.warningCode != null
) {
binding.comboState.setTextColor(rh.gac(context, R.attr.omniYellowColor))
binding.comboState.setTypeface(null, Typeface.BOLD)
} else {
binding.comboState.setTextColor(rh.gac(context, R.attr.defaultTextColor))
binding.comboState.setTypeface(null, Typeface.NORMAL)
}
// activity
val activity = comboPlugin.pump.activity
when {
activity != null -> {
binding.comboActivity.setTextColor(rh.gac(context, R.attr.defaultTextColor))
binding.comboActivity.textSize = 14f
binding.comboActivity.text = activity
}
commandQueue.size() > 0 -> {
binding.comboActivity.setTextColor(rh.gac(context, R.attr.defaultTextColor))
binding.comboActivity.textSize = 14f
binding.comboActivity.text = ""
}
comboPlugin.isInitialized() -> {
binding.comboActivity.setTextColor(rh.gac(context, R.attr.defaultTextColor))
binding.comboActivity.textSize = 20f
binding.comboActivity.text = "{fa-bed}"
}
else -> {
binding.comboActivity.setTextColor(rh.gac(context, R.attr.warningColor))
binding.comboActivity.textSize = 14f
binding.comboActivity.text = rh.gs(R.string.pump_unreachable)
}
}
if (comboPlugin.isInitialized()) {
// battery
binding.comboPumpstateBattery.textSize = 20f
when (ps.batteryState) {
PumpState.EMPTY -> {
binding.comboPumpstateBattery.text = "{fa-battery-empty}"
binding.comboPumpstateBattery.setTextColor(rh.gac(context, R.attr.warningColor))
}
PumpState.LOW -> {
binding.comboPumpstateBattery.text = "{fa-battery-quarter}"
binding.comboPumpstateBattery.setTextColor(rh.gac(context, R.attr.omniYellowColor))
}
else -> {
binding.comboPumpstateBattery.text = "{fa-battery-full}"
binding.comboPumpstateBattery.setTextColor(Color.WHITE)
}
}
// reservoir
val reservoirLevel = comboPlugin.pump.reservoirLevel
when {
reservoirLevel != -1 -> binding.comboInsulinstate.text = reservoirLevel.toString() + " " + rh.gs(R.string.insulin_unit_shortname)
ps.insulinState == PumpState.LOW -> binding.comboInsulinstate.text = rh.gs(R.string.combo_reservoir_low)
ps.insulinState == PumpState.EMPTY -> binding.comboInsulinstate.text = rh.gs(R.string.combo_reservoir_empty)
else -> binding.comboInsulinstate.text = rh.gs(R.string.combo_reservoir_normal)
}
when (ps.insulinState) {
PumpState.UNKNOWN -> {
binding.comboInsulinstate.setTextColor(rh.gac(context, R.attr.defaultTextColor))
binding.comboInsulinstate.setTypeface(null, Typeface.NORMAL)
}
PumpState.LOW -> {
binding.comboInsulinstate.setTextColor(rh.gac(context, R.attr.omniYellowColor))
binding.comboInsulinstate.setTypeface(null, Typeface.BOLD)
}
PumpState.EMPTY -> {
binding.comboInsulinstate.setTextColor(rh.gac(context, R.attr.warningColor))
binding.comboInsulinstate.setTypeface(null, Typeface.BOLD)
}
else -> {
binding.comboInsulinstate.setTextColor(rh.gac(context, R.attr.defaultTextColor))
binding.comboInsulinstate.setTypeface(null, Typeface.NORMAL)
}
}
// last connection
val minAgo = dateUtil.minAgo(rh, comboPlugin.pump.lastSuccessfulCmdTime)
val min = (System.currentTimeMillis() - comboPlugin.pump.lastSuccessfulCmdTime) / 1000 / 60
when {
comboPlugin.pump.lastSuccessfulCmdTime + 60 * 1000 > System.currentTimeMillis() -> {
binding.comboLastconnection.setText(R.string.combo_pump_connected_now)
binding.comboLastconnection.setTextColor(rh.gac(context, R.attr.defaultTextColor))
}
comboPlugin.pump.lastSuccessfulCmdTime + 30 * 60 * 1000 < System.currentTimeMillis() -> {
binding.comboLastconnection.text = rh.gs(R.string.combo_no_pump_connection, min)
binding.comboLastconnection.setTextColor(rh.gac(context, R.attr.warningColor))
}
else -> {
binding.comboLastconnection.text = minAgo
binding.comboLastconnection.setTextColor(rh.gac(context, R.attr.defaultTextColor))
}
}
// last bolus
val bolus = comboPlugin.pump.lastBolus
if (bolus != null) {
val agoMsc = System.currentTimeMillis() - bolus.timestamp
val bolusMinAgo = agoMsc / 60.0 / 1000.0
val unit = rh.gs(R.string.insulin_unit_shortname)
val ago: String = when {
agoMsc < 60 * 1000 -> rh.gs(R.string.combo_pump_connected_now)
bolusMinAgo < 60 -> dateUtil.minAgo(rh, bolus.timestamp)
else -> dateUtil.hourAgo(bolus.timestamp, rh)
}
binding.comboLastBolus.text = rh.gs(R.string.combo_last_bolus, bolus.amount, unit, ago)
} else {
binding.comboLastBolus.text = ""
}
// base basal rate
binding.comboBaseBasalRate.text = rh.gs(R.string.pump_basebasalrate, comboPlugin.baseBasalRate)
// TBR
var tbrStr = ""
if (ps.tbrPercent != -1 && ps.tbrPercent != 100) {
val minSinceRead = (System.currentTimeMillis() - comboPlugin.pump.state.timestamp) / 1000 / 60
val remaining = ps.tbrRemainingDuration - minSinceRead
if (remaining >= 0) {
tbrStr = rh.gs(R.string.combo_tbr_remaining, ps.tbrPercent, remaining)
}
}
binding.comboTempBasal.text = tbrStr
// stats
binding.comboBolusCount.text = comboPlugin.bolusesDelivered.toString()
binding.comboTbrCount.text = comboPlugin.tbrsSet.toString()
binding.serialNumber.text = comboPlugin.serialNumber()
updateErrorDisplay(false)
} else {
updateErrorDisplay(true)
}
}
private fun updateErrorDisplay(forceHide: Boolean) {
var errorCount = -1
if (!forceHide) {
val displayType = errorUtil.displayType
if (displayType === DisplayType.ON_ERROR || displayType === DisplayType.ALWAYS) {
val errorCountInternal = errorUtil.errorCount
if (errorCountInternal > 0) {
errorCount = errorCountInternal
} else if (displayType === DisplayType.ALWAYS) {
errorCount = 0
}
}
}
if (errorCount >= 0) {
binding.comboConnectionErrorValue.visibility = View.VISIBLE
binding.comboConnectionErrorLayout.visibility = View.VISIBLE
binding.comboConnectionErrorValue.visibility = View.VISIBLE
binding.comboConnectionErrorValue.text = if (errorCount == 0) "-" else "" + errorCount
} else {
binding.comboConnectionErrorValue.visibility = View.GONE
binding.comboConnectionErrorLayout.visibility = View.GONE
binding.comboConnectionErrorValue.visibility = View.GONE
}
}
}

View file

@ -479,7 +479,6 @@
android:orientation="horizontal">
<TextView
android:id="@+id/combo_connection_error_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.5"
@ -489,7 +488,6 @@
android:textSize="14sp" />
<TextView
android:id="@+id/combo_connection_error_dots"
android:layout_width="5dp"
android:layout_height="wrap_content"
android:layout_weight="0"