DanaRHistoryActivity refactor

This commit is contained in:
Milos Kozak 2019-12-21 16:15:35 +01:00
parent 2e687fe687
commit 7176a8654b
3 changed files with 306 additions and 362 deletions

View file

@ -1,6 +1,5 @@
package info.nightscout.androidaps.plugins.pump.danaR
import android.content.Intent
import android.os.Bundle
import android.os.Handler
@ -11,6 +10,7 @@ import androidx.fragment.app.Fragment
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.R
import info.nightscout.androidaps.activities.TDDStatsActivity
import info.nightscout.androidaps.dialogs.ProfileViewerDialog
import info.nightscout.androidaps.events.EventExtendedBolusChange
import info.nightscout.androidaps.events.EventPumpStatusChanged
import info.nightscout.androidaps.events.EventTempBasalChange
@ -24,9 +24,12 @@ import info.nightscout.androidaps.plugins.pump.danaR.activities.DanaRUserOptions
import info.nightscout.androidaps.plugins.pump.danaR.events.EventDanaRNewStatus
import info.nightscout.androidaps.plugins.pump.danaRKorean.DanaRKoreanPlugin
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin
import info.nightscout.androidaps.dialogs.ProfileViewerDialog
import info.nightscout.androidaps.queue.events.EventQueueChanged
import info.nightscout.androidaps.utils.*
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.SetWarnColor
import info.nightscout.androidaps.utils.T
import info.nightscout.androidaps.utils.plusAssign
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import kotlinx.android.synthetic.main.danar_fragment.*
@ -59,8 +62,10 @@ class DanaRFragment : Fragment() {
danar_history.setOnClickListener { startActivity(Intent(context, DanaRHistoryActivity::class.java)) }
danar_viewprofile.setOnClickListener {
fragmentManager?.let { fragmentManager ->
val profile = DanaRPump.getInstance().createConvertedProfile()?.getDefaultProfile() ?: return@let
val profileName = DanaRPump.getInstance().createConvertedProfile()?.getDefaultProfileName() ?: return@let
val profile = DanaRPump.getInstance().createConvertedProfile()?.getDefaultProfile()
?: return@let
val profileName = DanaRPump.getInstance().createConvertedProfile()?.getDefaultProfileName()
?: return@let
val args = Bundle()
args.putLong("time", DateUtil.now())
args.putInt("mode", ProfileViewerDialog.Mode.CUSTOM_PROFILE.ordinal)
@ -87,37 +92,37 @@ class DanaRFragment : Fragment() {
super.onResume()
loopHandler.postDelayed(refreshLoop, T.mins(1).msecs())
disposable += RxBus
.toObservable(EventDanaRNewStatus::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ updateGUI() }, { FabricPrivacy.logException(it) })
.toObservable(EventDanaRNewStatus::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ updateGUI() }, { FabricPrivacy.logException(it) })
disposable += RxBus
.toObservable(EventExtendedBolusChange::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ updateGUI() }, { FabricPrivacy.logException(it) })
.toObservable(EventExtendedBolusChange::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ updateGUI() }, { FabricPrivacy.logException(it) })
disposable += RxBus
.toObservable(EventTempBasalChange::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ updateGUI() }, { FabricPrivacy.logException(it) })
.toObservable(EventTempBasalChange::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ updateGUI() }, { FabricPrivacy.logException(it) })
disposable += RxBus
.toObservable(EventQueueChanged::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ updateGUI() }, { FabricPrivacy.logException(it) })
.toObservable(EventQueueChanged::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ updateGUI() }, { FabricPrivacy.logException(it) })
disposable += RxBus
.toObservable(EventPumpStatusChanged::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
when {
it.sStatus == EventPumpStatusChanged.Status.CONNECTING -> danar_btconnection?.text = "{fa-bluetooth-b spin} " + it.sSecondsElapsed + "s"
it.sStatus == EventPumpStatusChanged.Status.CONNECTED -> danar_btconnection?.text = "{fa-bluetooth}"
it.sStatus == EventPumpStatusChanged.Status.DISCONNECTED -> danar_btconnection?.text = "{fa-bluetooth-b}"
}
if (it.getStatus() != "") {
dana_pumpstatus?.text = it.getStatus()
dana_pumpstatuslayout?.visibility = View.VISIBLE
} else {
dana_pumpstatuslayout?.visibility = View.GONE
}
}, { FabricPrivacy.logException(it) })
.toObservable(EventPumpStatusChanged::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
when {
it.sStatus == EventPumpStatusChanged.Status.CONNECTING -> danar_btconnection?.text = "{fa-bluetooth-b spin} " + it.sSecondsElapsed + "s"
it.sStatus == EventPumpStatusChanged.Status.CONNECTED -> danar_btconnection?.text = "{fa-bluetooth}"
it.sStatus == EventPumpStatusChanged.Status.DISCONNECTED -> danar_btconnection?.text = "{fa-bluetooth-b}"
}
if (it.getStatus() != "") {
dana_pumpstatus?.text = it.getStatus()
dana_pumpstatuslayout?.visibility = View.VISIBLE
} else {
dana_pumpstatuslayout?.visibility = View.GONE
}
}, { FabricPrivacy.logException(it) })
updateGUI()
}
@ -156,14 +161,14 @@ class DanaRFragment : Fragment() {
// DanaRPlugin, DanaRKoreanPlugin
if (ConfigBuilderPlugin.getPlugin().activePump!!.isFakingTempsByExtendedBoluses) {
danar_tempbasal.text = TreatmentsPlugin.getPlugin()
.getRealTempBasalFromHistory(System.currentTimeMillis())?.toStringFull() ?: ""
.getRealTempBasalFromHistory(System.currentTimeMillis())?.toStringFull() ?: ""
} else {
// v2 plugin
danar_tempbasal.text = TreatmentsPlugin.getPlugin()
.getTempBasalFromHistory(System.currentTimeMillis())?.toStringFull() ?: ""
.getTempBasalFromHistory(System.currentTimeMillis())?.toStringFull() ?: ""
}
danar_extendedbolus.text = TreatmentsPlugin.getPlugin()
.getExtendedBolusFromHistory(System.currentTimeMillis())?.toString() ?: ""
.getExtendedBolusFromHistory(System.currentTimeMillis())?.toString() ?: ""
danar_reservoir.text = MainApp.gs(R.string.reservoirvalue, pump.reservoirRemainingUnits, 300)
SetWarnColor.setColorInverse(danar_reservoir, pump.reservoirRemainingUnits, 50.0, 20.0)
danar_battery.text = "{fa-battery-" + pump.batteryRemaining / 25 + "}"

View file

@ -1,327 +0,0 @@
package info.nightscout.androidaps.plugins.pump.danaR.activities;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.activities.NoSplashAppCompatActivity;
import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.db.DanaRHistoryRecord;
import info.nightscout.androidaps.events.EventPumpStatusChanged;
import info.nightscout.androidaps.interfaces.PluginType;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.bus.RxBus;
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
import info.nightscout.androidaps.plugins.pump.danaR.comm.RecordTypes;
import info.nightscout.androidaps.plugins.pump.danaR.events.EventDanaRSyncStatus;
import info.nightscout.androidaps.plugins.pump.danaRKorean.DanaRKoreanPlugin;
import info.nightscout.androidaps.plugins.pump.danaRS.DanaRSPlugin;
import info.nightscout.androidaps.queue.Callback;
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.DecimalFormatter;
import info.nightscout.androidaps.utils.FabricPrivacy;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
public class DanaRHistoryActivity extends NoSplashAppCompatActivity {
private static Logger log = LoggerFactory.getLogger(L.PUMP);
private CompositeDisposable disposable = new CompositeDisposable();
Spinner historyTypeSpinner;
TextView statusView;
Button reloadButton;
RecyclerView recyclerView;
LinearLayoutManager llm;
static byte showingType = RecordTypes.RECORD_TYPE_ALARM;
List<DanaRHistoryRecord> historyList = new ArrayList<>();
public static class TypeList {
public byte type;
String name;
TypeList(byte type, String name) {
this.type = type;
this.name = name;
}
@NonNull
@Override
public String toString() {
return name;
}
}
public DanaRHistoryActivity() {
super();
}
@Override
protected void onResume() {
super.onResume();
disposable.add(RxBus.INSTANCE
.toObservable(EventPumpStatusChanged.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> statusView.setText(event.getStatus()), FabricPrivacy::logException)
);
disposable.add(RxBus.INSTANCE
.toObservable(EventDanaRSyncStatus.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> {
if (L.isEnabled(L.PUMP))
log.debug("EventDanaRSyncStatus: " + event.getMessage());
statusView.setText(event.getMessage());
}, FabricPrivacy::logException)
);
}
@Override
protected void onPause() {
super.onPause();
disposable.clear();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.danar_historyactivity);
historyTypeSpinner = findViewById(R.id.danar_history_spinner);
statusView = findViewById(R.id.danar_history_status);
reloadButton = findViewById(R.id.danar_history_reload);
recyclerView = findViewById(R.id.danar_history_recyclerview);
recyclerView.setHasFixedSize(true);
llm = new LinearLayoutManager(this);
recyclerView.setLayoutManager(llm);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(historyList);
recyclerView.setAdapter(adapter);
statusView.setVisibility(View.GONE);
boolean isKorean = DanaRKoreanPlugin.getPlugin().isEnabled(PluginType.PUMP);
boolean isRS = DanaRSPlugin.getPlugin().isEnabled(PluginType.PUMP);
// Types
ArrayList<TypeList> typeList = new ArrayList<>();
typeList.add(new TypeList(RecordTypes.RECORD_TYPE_ALARM, MainApp.gs(R.string.danar_history_alarm)));
typeList.add(new TypeList(RecordTypes.RECORD_TYPE_BASALHOUR, MainApp.gs(R.string.danar_history_basalhours)));
typeList.add(new TypeList(RecordTypes.RECORD_TYPE_BOLUS, MainApp.gs(R.string.danar_history_bolus)));
typeList.add(new TypeList(RecordTypes.RECORD_TYPE_CARBO, MainApp.gs(R.string.danar_history_carbohydrates)));
typeList.add(new TypeList(RecordTypes.RECORD_TYPE_DAILY, MainApp.gs(R.string.danar_history_dailyinsulin)));
typeList.add(new TypeList(RecordTypes.RECORD_TYPE_GLUCOSE, MainApp.gs(R.string.danar_history_glucose)));
if (!isKorean && !isRS) {
typeList.add(new TypeList(RecordTypes.RECORD_TYPE_ERROR, MainApp.gs(R.string.danar_history_errors)));
}
if (isRS)
typeList.add(new TypeList(RecordTypes.RECORD_TYPE_PRIME, MainApp.gs(R.string.danar_history_prime)));
if (!isKorean) {
typeList.add(new TypeList(RecordTypes.RECORD_TYPE_REFILL, MainApp.gs(R.string.danar_history_refill)));
typeList.add(new TypeList(RecordTypes.RECORD_TYPE_SUSPEND, MainApp.gs(R.string.danar_history_syspend)));
}
ArrayAdapter<TypeList> spinnerAdapter = new ArrayAdapter<>(this,
R.layout.spinner_centered, typeList);
historyTypeSpinner.setAdapter(spinnerAdapter);
reloadButton.setOnClickListener(v -> {
final TypeList selected = (TypeList) historyTypeSpinner.getSelectedItem();
runOnUiThread(() -> {
reloadButton.setVisibility(View.GONE);
statusView.setVisibility(View.VISIBLE);
});
clearCardView();
ConfigBuilderPlugin.getPlugin().getCommandQueue().loadHistory(selected.type, new Callback() {
@Override
public void run() {
loadDataFromDB(selected.type);
runOnUiThread(() -> {
reloadButton.setVisibility(View.VISIBLE);
statusView.setVisibility(View.GONE);
});
}
});
});
historyTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TypeList selected = (TypeList) historyTypeSpinner.getSelectedItem();
loadDataFromDB(selected.type);
showingType = selected.type;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
clearCardView();
}
});
}
public static class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.HistoryViewHolder> {
List<DanaRHistoryRecord> historyList;
RecyclerViewAdapter(List<DanaRHistoryRecord> historyList) {
this.historyList = historyList;
}
@NonNull
@Override
public HistoryViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.danar_history_item, viewGroup, false);
return new HistoryViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull HistoryViewHolder holder, int position) {
DanaRHistoryRecord record = historyList.get(position);
holder.time.setText(DateUtil.dateAndTimeString(record.recordDate));
holder.value.setText(DecimalFormatter.to2Decimal(record.recordValue));
holder.stringvalue.setText(record.stringRecordValue);
holder.bolustype.setText(record.bolusType);
holder.duration.setText(DecimalFormatter.to0Decimal(record.recordDuration));
holder.alarm.setText(record.recordAlarm);
switch (showingType) {
case RecordTypes.RECORD_TYPE_ALARM:
holder.time.setVisibility(View.VISIBLE);
holder.value.setVisibility(View.VISIBLE);
holder.stringvalue.setVisibility(View.GONE);
holder.bolustype.setVisibility(View.GONE);
holder.duration.setVisibility(View.GONE);
holder.dailybasal.setVisibility(View.GONE);
holder.dailybolus.setVisibility(View.GONE);
holder.dailytotal.setVisibility(View.GONE);
holder.alarm.setVisibility(View.VISIBLE);
break;
case RecordTypes.RECORD_TYPE_BOLUS:
holder.time.setVisibility(View.VISIBLE);
holder.value.setVisibility(View.VISIBLE);
holder.stringvalue.setVisibility(View.GONE);
holder.bolustype.setVisibility(View.VISIBLE);
holder.duration.setVisibility(View.VISIBLE);
holder.dailybasal.setVisibility(View.GONE);
holder.dailybolus.setVisibility(View.GONE);
holder.dailytotal.setVisibility(View.GONE);
holder.alarm.setVisibility(View.GONE);
break;
case RecordTypes.RECORD_TYPE_DAILY:
holder.dailybasal.setText(DecimalFormatter.to2Decimal(record.recordDailyBasal) + "U");
holder.dailybolus.setText(DecimalFormatter.to2Decimal(record.recordDailyBolus) + "U");
holder.dailytotal.setText(DecimalFormatter.to2Decimal(record.recordDailyBolus + record.recordDailyBasal) + "U");
holder.time.setText(DateUtil.dateString(record.recordDate));
holder.time.setVisibility(View.VISIBLE);
holder.value.setVisibility(View.GONE);
holder.stringvalue.setVisibility(View.GONE);
holder.bolustype.setVisibility(View.GONE);
holder.duration.setVisibility(View.GONE);
holder.dailybasal.setVisibility(View.VISIBLE);
holder.dailybolus.setVisibility(View.VISIBLE);
holder.dailytotal.setVisibility(View.VISIBLE);
holder.alarm.setVisibility(View.GONE);
break;
case RecordTypes.RECORD_TYPE_GLUCOSE:
holder.value.setText(Profile.toUnitsString(record.recordValue, record.recordValue * Constants.MGDL_TO_MMOLL, ProfileFunctions.getSystemUnits()));
// rest is the same
case RecordTypes.RECORD_TYPE_CARBO:
case RecordTypes.RECORD_TYPE_BASALHOUR:
case RecordTypes.RECORD_TYPE_ERROR:
case RecordTypes.RECORD_TYPE_PRIME:
case RecordTypes.RECORD_TYPE_REFILL:
case RecordTypes.RECORD_TYPE_TB:
holder.time.setVisibility(View.VISIBLE);
holder.value.setVisibility(View.VISIBLE);
holder.stringvalue.setVisibility(View.GONE);
holder.bolustype.setVisibility(View.GONE);
holder.duration.setVisibility(View.GONE);
holder.dailybasal.setVisibility(View.GONE);
holder.dailybolus.setVisibility(View.GONE);
holder.dailytotal.setVisibility(View.GONE);
holder.alarm.setVisibility(View.GONE);
break;
case RecordTypes.RECORD_TYPE_SUSPEND:
holder.time.setVisibility(View.VISIBLE);
holder.value.setVisibility(View.GONE);
holder.stringvalue.setVisibility(View.VISIBLE);
holder.bolustype.setVisibility(View.GONE);
holder.duration.setVisibility(View.GONE);
holder.dailybasal.setVisibility(View.GONE);
holder.dailybolus.setVisibility(View.GONE);
holder.dailytotal.setVisibility(View.GONE);
holder.alarm.setVisibility(View.GONE);
break;
}
}
@Override
public int getItemCount() {
return historyList.size();
}
@Override
public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
static class HistoryViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView time;
TextView value;
TextView bolustype;
TextView stringvalue;
TextView duration;
TextView dailybasal;
TextView dailybolus;
TextView dailytotal;
TextView alarm;
HistoryViewHolder(View itemView) {
super(itemView);
cv = itemView.findViewById(R.id.danar_history_cardview);
time = itemView.findViewById(R.id.danar_history_time);
value = itemView.findViewById(R.id.danar_history_value);
bolustype = itemView.findViewById(R.id.danar_history_bolustype);
stringvalue = itemView.findViewById(R.id.danar_history_stringvalue);
duration = itemView.findViewById(R.id.danar_history_duration);
dailybasal = itemView.findViewById(R.id.danar_history_dailybasal);
dailybolus = itemView.findViewById(R.id.danar_history_dailybolus);
dailytotal = itemView.findViewById(R.id.danar_history_dailytotal);
alarm = itemView.findViewById(R.id.danar_history_alarm);
}
}
}
private void loadDataFromDB(byte type) {
historyList = MainApp.getDbHelper().getDanaRHistoryRecordsByType(type);
runOnUiThread(() -> recyclerView.swapAdapter(new RecyclerViewAdapter(historyList), false));
}
private void clearCardView() {
historyList = new ArrayList<>();
runOnUiThread(() -> recyclerView.swapAdapter(new RecyclerViewAdapter(historyList), false));
}
}

View file

@ -0,0 +1,266 @@
package info.nightscout.androidaps.plugins.pump.danaR.activities
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.TextView
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import info.nightscout.androidaps.Constants
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.R
import info.nightscout.androidaps.activities.NoSplashAppCompatActivity
import info.nightscout.androidaps.data.Profile
import info.nightscout.androidaps.db.DanaRHistoryRecord
import info.nightscout.androidaps.events.EventPumpStatusChanged
import info.nightscout.androidaps.interfaces.PluginType
import info.nightscout.androidaps.logging.L
import info.nightscout.androidaps.plugins.bus.RxBus.toObservable
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions
import info.nightscout.androidaps.plugins.pump.danaR.comm.RecordTypes
import info.nightscout.androidaps.plugins.pump.danaR.events.EventDanaRSyncStatus
import info.nightscout.androidaps.plugins.pump.danaRKorean.DanaRKoreanPlugin
import info.nightscout.androidaps.plugins.pump.danaRS.DanaRSPlugin
import info.nightscout.androidaps.queue.Callback
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.DecimalFormatter
import info.nightscout.androidaps.utils.FabricPrivacy
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import kotlinx.android.synthetic.main.danar_historyactivity.*
import org.slf4j.LoggerFactory
import java.util.*
class DanaRHistoryActivity : NoSplashAppCompatActivity() {
private val log = LoggerFactory.getLogger(L.PUMP)
private val disposable = CompositeDisposable()
private var showingType = RecordTypes.RECORD_TYPE_ALARM
private var historyList: List<DanaRHistoryRecord> = ArrayList()
class TypeList internal constructor(var type: Byte, var name: String) {
override fun toString(): String = name
}
override fun onResume() {
super.onResume()
disposable.add(toObservable(EventPumpStatusChanged::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ danar_history_status.text = it.getStatus() }) { FabricPrivacy.logException(it) }
)
disposable.add(toObservable(EventDanaRSyncStatus::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
if (L.isEnabled(L.PUMP))
log.debug("EventDanaRSyncStatus: " + it.message)
danar_history_status.setText(it.message)
}) { FabricPrivacy.logException(it) }
)
}
override fun onPause() {
super.onPause()
disposable.clear()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.danar_historyactivity)
danar_history_recyclerview.setHasFixedSize(true)
danar_history_recyclerview.setLayoutManager(LinearLayoutManager(this))
danar_history_recyclerview.setAdapter(RecyclerViewAdapter(historyList))
danar_history_status.visibility = View.GONE
val isKorean = DanaRKoreanPlugin.getPlugin().isEnabled(PluginType.PUMP)
val isRS = DanaRSPlugin.getPlugin().isEnabled(PluginType.PUMP)
// Types
val typeList = ArrayList<TypeList>()
typeList.add(TypeList(RecordTypes.RECORD_TYPE_ALARM, MainApp.gs(R.string.danar_history_alarm)))
typeList.add(TypeList(RecordTypes.RECORD_TYPE_BASALHOUR, MainApp.gs(R.string.danar_history_basalhours)))
typeList.add(TypeList(RecordTypes.RECORD_TYPE_BOLUS, MainApp.gs(R.string.danar_history_bolus)))
typeList.add(TypeList(RecordTypes.RECORD_TYPE_CARBO, MainApp.gs(R.string.danar_history_carbohydrates)))
typeList.add(TypeList(RecordTypes.RECORD_TYPE_DAILY, MainApp.gs(R.string.danar_history_dailyinsulin)))
typeList.add(TypeList(RecordTypes.RECORD_TYPE_GLUCOSE, MainApp.gs(R.string.danar_history_glucose)))
if (!isKorean && !isRS) {
typeList.add(TypeList(RecordTypes.RECORD_TYPE_ERROR, MainApp.gs(R.string.danar_history_errors)))
}
if (isRS) typeList.add(TypeList(RecordTypes.RECORD_TYPE_PRIME, MainApp.gs(R.string.danar_history_prime)))
if (!isKorean) {
typeList.add(TypeList(RecordTypes.RECORD_TYPE_REFILL, MainApp.gs(R.string.danar_history_refill)))
typeList.add(TypeList(RecordTypes.RECORD_TYPE_SUSPEND, MainApp.gs(R.string.danar_history_syspend)))
}
danar_history_spinner.adapter = ArrayAdapter(this, R.layout.spinner_centered, typeList)
danar_history_reload.setOnClickListener {
val selected = danar_history_spinner.selectedItem as TypeList
runOnUiThread {
danar_history_reload.setVisibility(View.GONE)
danar_history_status.setVisibility(View.VISIBLE)
}
clearCardView()
ConfigBuilderPlugin.getPlugin().commandQueue.loadHistory(selected.type, object : Callback() {
override fun run() {
loadDataFromDB(selected.type)
runOnUiThread {
danar_history_reload.setVisibility(View.VISIBLE)
danar_history_status.setVisibility(View.GONE)
}
}
})
}
danar_history_spinner.setOnItemSelectedListener(object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>?, view: View, position: Int, id: Long) {
val selected = danar_history_spinner.selectedItem as TypeList
loadDataFromDB(selected.type)
showingType = selected.type
}
override fun onNothingSelected(parent: AdapterView<*>?) {
clearCardView()
}
})
}
inner class RecyclerViewAdapter internal constructor(var historyList: List<DanaRHistoryRecord>) : RecyclerView.Adapter<RecyclerViewAdapter.HistoryViewHolder>() {
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): HistoryViewHolder =
HistoryViewHolder(LayoutInflater.from(viewGroup.context).inflate(R.layout.danar_history_item, viewGroup, false))
override fun onBindViewHolder(holder: HistoryViewHolder, position: Int) {
val record = historyList[position]
holder.time.text = DateUtil.dateAndTimeString(record.recordDate)
holder.value.text = DecimalFormatter.to2Decimal(record.recordValue)
holder.stringvalue.text = record.stringRecordValue
holder.bolustype.text = record.bolusType
holder.duration.text = DecimalFormatter.to0Decimal(record.recordDuration.toDouble())
holder.alarm.text = record.recordAlarm
when (showingType) {
RecordTypes.RECORD_TYPE_ALARM -> {
holder.time.visibility = View.VISIBLE
holder.value.visibility = View.VISIBLE
holder.stringvalue.visibility = View.GONE
holder.bolustype.visibility = View.GONE
holder.duration.visibility = View.GONE
holder.dailybasal.visibility = View.GONE
holder.dailybolus.visibility = View.GONE
holder.dailytotal.visibility = View.GONE
holder.alarm.visibility = View.VISIBLE
}
RecordTypes.RECORD_TYPE_BOLUS -> {
holder.time.visibility = View.VISIBLE
holder.value.visibility = View.VISIBLE
holder.stringvalue.visibility = View.GONE
holder.bolustype.visibility = View.VISIBLE
holder.duration.visibility = View.VISIBLE
holder.dailybasal.visibility = View.GONE
holder.dailybolus.visibility = View.GONE
holder.dailytotal.visibility = View.GONE
holder.alarm.visibility = View.GONE
}
RecordTypes.RECORD_TYPE_DAILY -> {
holder.dailybasal.text = MainApp.gs(R.string.formatinsulinunits, record.recordDailyBasal)
holder.dailybolus.text = MainApp.gs(R.string.formatinsulinunits, record.recordDailyBolus)
holder.dailytotal.text = MainApp.gs(R.string.formatinsulinunits, record.recordDailyBolus + record.recordDailyBasal)
holder.time.text = DateUtil.dateString(record.recordDate)
holder.time.visibility = View.VISIBLE
holder.value.visibility = View.GONE
holder.stringvalue.visibility = View.GONE
holder.bolustype.visibility = View.GONE
holder.duration.visibility = View.GONE
holder.dailybasal.visibility = View.VISIBLE
holder.dailybolus.visibility = View.VISIBLE
holder.dailytotal.visibility = View.VISIBLE
holder.alarm.visibility = View.GONE
}
RecordTypes.RECORD_TYPE_GLUCOSE -> {
holder.value.text = Profile.toUnitsString(record.recordValue, record.recordValue * Constants.MGDL_TO_MMOLL, ProfileFunctions.getSystemUnits())
holder.time.visibility = View.VISIBLE
holder.value.visibility = View.VISIBLE
holder.stringvalue.visibility = View.GONE
holder.bolustype.visibility = View.GONE
holder.duration.visibility = View.GONE
holder.dailybasal.visibility = View.GONE
holder.dailybolus.visibility = View.GONE
holder.dailytotal.visibility = View.GONE
holder.alarm.visibility = View.GONE
}
RecordTypes.RECORD_TYPE_CARBO, RecordTypes.RECORD_TYPE_BASALHOUR, RecordTypes.RECORD_TYPE_ERROR, RecordTypes.RECORD_TYPE_PRIME, RecordTypes.RECORD_TYPE_REFILL, RecordTypes.RECORD_TYPE_TB -> {
holder.time.visibility = View.VISIBLE
holder.value.visibility = View.VISIBLE
holder.stringvalue.visibility = View.GONE
holder.bolustype.visibility = View.GONE
holder.duration.visibility = View.GONE
holder.dailybasal.visibility = View.GONE
holder.dailybolus.visibility = View.GONE
holder.dailytotal.visibility = View.GONE
holder.alarm.visibility = View.GONE
}
RecordTypes.RECORD_TYPE_SUSPEND -> {
holder.time.visibility = View.VISIBLE
holder.value.visibility = View.GONE
holder.stringvalue.visibility = View.VISIBLE
holder.bolustype.visibility = View.GONE
holder.duration.visibility = View.GONE
holder.dailybasal.visibility = View.GONE
holder.dailybolus.visibility = View.GONE
holder.dailytotal.visibility = View.GONE
holder.alarm.visibility = View.GONE
}
}
}
override fun getItemCount(): Int {
return historyList.size
}
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
super.onAttachedToRecyclerView(recyclerView)
}
inner class HistoryViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var cv: CardView = itemView.findViewById(R.id.danar_history_cardview)
var time: TextView = itemView.findViewById(R.id.danar_history_time)
var value: TextView
var bolustype: TextView
var stringvalue: TextView
var duration: TextView
var dailybasal: TextView
var dailybolus: TextView
var dailytotal: TextView
var alarm: TextView
init {
value = itemView.findViewById(R.id.danar_history_value)
bolustype = itemView.findViewById(R.id.danar_history_bolustype)
stringvalue = itemView.findViewById(R.id.danar_history_stringvalue)
duration = itemView.findViewById(R.id.danar_history_duration)
dailybasal = itemView.findViewById(R.id.danar_history_dailybasal)
dailybolus = itemView.findViewById(R.id.danar_history_dailybolus)
dailytotal = itemView.findViewById(R.id.danar_history_dailytotal)
alarm = itemView.findViewById(R.id.danar_history_alarm)
}
}
}
private fun loadDataFromDB(type: Byte) {
historyList = MainApp.getDbHelper().getDanaRHistoryRecordsByType(type)
runOnUiThread { danar_history_recyclerview?.swapAdapter(RecyclerViewAdapter(historyList), false) }
}
private fun clearCardView() {
historyList = ArrayList()
runOnUiThread { danar_history_recyclerview?.swapAdapter(RecyclerViewAdapter(historyList), false) }
}
}