Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Rework pod history view #161

Open
wants to merge 9 commits into
base: omnipod_eros_dev
Choose a base branch
from
8 changes: 7 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@ android {
configurations.all {
resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
}

buildFeatures {
dataBinding true
}

}

allprojects {
Expand Down Expand Up @@ -259,7 +264,7 @@ dependencies {
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.biometric:biometric:1.0.1'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.recyclerview:recyclerview:${recyclerview_version}'
implementation 'androidx.gridlayout:gridlayout:1.0.0'
implementation 'androidx.percentlayout:percentlayout:1.0.0'
implementation "androidx.preference:preference-ktx:1.1.1"
Expand All @@ -283,6 +288,7 @@ dependencies {
implementation 'com.madgag.spongycastle:core:1.58.0.0'
// Omnipod wizard
implementation(name: "com.atech-software.android.library.wizardpager-1.1.4", ext: "aar")
implementation "androidx.paging:paging-runtime-ktx:${paging_version}"
implementation("com.google.android:flexbox:0.3.0") {
exclude group: "com.android.support"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1891,6 +1891,36 @@ public List<OmnipodHistoryRecord> getAllOmnipodHistoryRecordsFromTimeStamp(long
}


public List<OmnipodHistoryRecord> getFilteredOmnipodHistoryRecords(long predecessor, long limit, String podSerial, boolean ascending) {
try {
Dao<OmnipodHistoryRecord, Long> daoPodHistory = getDaoPodHistory();
List<OmnipodHistoryRecord> podHistories;
QueryBuilder<OmnipodHistoryRecord, Long> queryBuilder = daoPodHistory.queryBuilder();
queryBuilder.orderBy("date", ascending);
queryBuilder.limit(limit);
if (predecessor > 0 || podSerial != null) {
Where where = queryBuilder.where();
if (predecessor > 0) {
if (ascending) {
where.gt("date", predecessor);
} else {
where.lt("date", predecessor);
}
}
if (podSerial != null) {
where.eq("podSerial", podSerial);
}
}
PreparedQuery<OmnipodHistoryRecord> preparedQuery = queryBuilder.prepare();
podHistories = daoPodHistory.query(preparedQuery);
return podHistories;
} catch (SQLException e) {
aapsLogger.error("Unhandled exception", e);
}
return new ArrayList<>();
}


// Copied from xDrip+
String calculateDirection(BgReading bgReading) {
// Rework to get bgreaings from internal DB and calculate on that base
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.j256.ormlite.dao.CloseableIterator;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.sql.SQLException;
import java.util.List;
Expand Down Expand Up @@ -99,6 +100,10 @@ public class DatabaseHelperProvider implements DatabaseHelperInterface {
return MainApp.getDbHelper().getAllOmnipodHistoryRecordsFromTimeStamp(timestamp, ascending);
}

@NotNull @Override public List<OmnipodHistoryRecord> getFilteredOmnipodHistoryRecords(long until, long limit, @Nullable String podSerial, boolean ascending) {
return MainApp.getDbHelper().getFilteredOmnipodHistoryRecords(until,limit,podSerial,ascending);
}

@NotNull @Override public List<TDD> getTDDsForLastXDays(int days) {
return MainApp.getDbHelper().getTDDsForLastXDays(days);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard.initpod.InitPod
import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard.pages.InitPodRefreshAction
import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard.pages.PodInfoFragment
import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard.removepod.RemoveActionFragment
import info.nightscout.androidaps.plugins.pump.omnipod.ui.PodHistoryDetailFragment
import info.nightscout.androidaps.plugins.pump.omnipod.ui.PodHistoryListFragment

@Module
@Suppress("unused")
Expand All @@ -30,6 +32,8 @@ abstract class OmnipodModule {
@ContributesAndroidInjector abstract fun initActionFragment(): InitActionFragment
@ContributesAndroidInjector abstract fun removeActionFragment(): RemoveActionFragment
@ContributesAndroidInjector abstract fun podInfoFragment(): PodInfoFragment
@ContributesAndroidInjector abstract fun podHistoryListFragment(): PodHistoryListFragment
@ContributesAndroidInjector abstract fun podHistoryDetailFragment(): PodHistoryDetailFragment

// Service
@ContributesAndroidInjector
Expand Down
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ buildscript {
activityVersion = '1.2.0-alpha06'
fragmentVersion = '1.3.0-alpha07'
ormLiteVersion = "4.46"
paging_version = "2.1.2"
recyclerview_version = "1.1.0"
}
repositories {
google()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

import java.io.Serializable;

import info.nightscout.androidaps.plugins.pump.common.utils.DateTimeUtil;

/**
* Created by andy on 30.11.2019.
*/
@DatabaseTable(tableName = "PodHistory")
public class OmnipodHistoryRecord implements DbObjectBase, Comparable<OmnipodHistoryRecord> {
public class OmnipodHistoryRecord implements DbObjectBase, Comparable<OmnipodHistoryRecord>, Serializable {

@DatabaseField(id = true)
public long date;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ interface DatabaseHelperInterface {
fun getTemporaryBasalsDataFromTime(mills: Long, ascending: Boolean): List<TemporaryBasal>
fun getCareportalEventFromTimestamp(timestamp: Long): CareportalEvent?
fun getAllOmnipodHistoryRecordsFromTimestamp(timestamp: Long, ascending: Boolean): List<OmnipodHistoryRecord>
fun getFilteredOmnipodHistoryRecords(until: Long, limit: Long, podSerial: String?, ascending: Boolean): List<OmnipodHistoryRecord>
fun getTDDsForLastXDays(days: Int): List<TDD>
fun getProfileSwitchData(from: Long, ascending: Boolean): List<ProfileSwitch>
}
6 changes: 6 additions & 0 deletions omnipod/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ android {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildFeatures {
dataBinding true
}
}

allprojects {
Expand Down Expand Up @@ -91,10 +94,13 @@ dependencies {
implementation 'net.danlew:android.joda:2.10.6'
implementation "com.google.code.gson:gson:2.8.6"
implementation(name: "com.atech-software.android.library.wizardpager-1.1.4", ext: "aar")
implementation "androidx.paging:paging-runtime-ktx:${paging_version}"
// Omnipod - end

implementation "com.google.dagger:dagger-android:$dagger_version"
implementation "com.google.dagger:dagger-android-support:$dagger_version"
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.recyclerview:recyclerview:${recyclerview_version}'
annotationProcessor "com.google.dagger:dagger-compiler:$dagger_version"
annotationProcessor "com.google.dagger:dagger-android-processor:$dagger_version"
kapt "com.google.dagger:dagger-android-processor:$dagger_version"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package info.nightscout.androidaps.plugins.pump.omnipod.data

import androidx.paging.ItemKeyedDataSource
import info.nightscout.androidaps.db.OmnipodHistoryRecord
import info.nightscout.androidaps.interfaces.DatabaseHelperInterface
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.logging.LTag
import java.lang.Exception

class OmnipodHistoryRecordDataSource(val databaseHelper: DatabaseHelperInterface, val aapsLogger: AAPSLogger) : ItemKeyedDataSource<Long, OmnipodHistoryRecord>() {

override fun getKey(item: OmnipodHistoryRecord): Long {
return item.date
}

override fun loadInitial(params: LoadInitialParams<Long>, callback: LoadInitialCallback<OmnipodHistoryRecord>) {
aapsLogger.info(LTag.PUMP,"Fetching initial data")
try {
val results = databaseHelper.getFilteredOmnipodHistoryRecords(
params.requestedInitialKey ?: 0,
params.requestedLoadSize.toLong(),
null,
false)
aapsLogger.info(LTag.PUMP, "Got %d results", results.size)
callback.onResult(results)
} catch (e : Exception) {
aapsLogger.error(LTag.PUMP,e.toString())
}
}

override fun loadAfter(params: LoadParams<Long>, callback: LoadCallback<OmnipodHistoryRecord>) {
val results = databaseHelper.getFilteredOmnipodHistoryRecords(
params.key,
params.requestedLoadSize.toLong(),
null,
false)
callback.onResult(results)
}

override fun loadBefore(params: LoadParams<Long>, callback: LoadCallback<OmnipodHistoryRecord>) {
val results = databaseHelper.getFilteredOmnipodHistoryRecords(
params.key,
params.requestedLoadSize.toLong(),
null,
true)
callback.onResult(results)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package info.nightscout.androidaps.plugins.pump.omnipod.event

import info.nightscout.androidaps.db.OmnipodHistoryRecord
import info.nightscout.androidaps.events.Event

data class EventOmnipodHistoryItemClicked(val record: OmnipodHistoryRecord) : Event()
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package info.nightscout.androidaps.plugins.pump.omnipod.ui

import android.graphics.Color
import android.graphics.PorterDuff
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import info.nightscout.androidaps.data.Profile
import info.nightscout.androidaps.db.OmnipodHistoryRecord
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.logging.LTag
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.pump.common.data.TempBasalPair
import info.nightscout.androidaps.plugins.pump.common.defs.PumpType
import info.nightscout.androidaps.plugins.pump.common.utils.ProfileUtil
import info.nightscout.androidaps.plugins.pump.omnipod.R
import info.nightscout.androidaps.plugins.pump.omnipod.definition.PodHistoryEntryType
import info.nightscout.androidaps.plugins.pump.omnipod.event.EventOmnipodHistoryItemClicked
import info.nightscout.androidaps.plugins.pump.omnipod.util.AapsOmnipodUtil
import info.nightscout.androidaps.utils.resources.ResourceHelper

class OmnipodHistoryRecordViewHolder(view: View, val aapsLogger: AAPSLogger, val aapsOmnipodUtil: AapsOmnipodUtil, val resourceHelper: ResourceHelper, val rxBus: RxBusWrapper) : RecyclerView.ViewHolder(view) {

private val time: TextView = view.findViewById(R.id.omnipod_history_time)
private val type: TextView = view.findViewById(R.id.omnipod_history_source)
private val value: TextView = view.findViewById(R.id.omnipod_history_description)
private val layout: LinearLayout = view.findViewById(R.id.omnipod_history_item_layout)
private val standardBackgroundColorFilter = view.background.colorFilter

private var record: OmnipodHistoryRecord? = null

init {
itemView.setOnClickListener {
val record = this.record
record?.let {
rxBus.send(EventOmnipodHistoryItemClicked(record))
}
}
}

fun bind(record: OmnipodHistoryRecord?) {
this.record = record
record?.let {
time.text = record.dateTimeString
type.setText(PodHistoryEntryType.getByCode(record.podEntryTypeCode).resourceId)
value.text = description(record)

when {
!record.isSuccess ->
layout.background.setColorFilter(Color.parseColor("#80ff0000"), PorterDuff.Mode.OVERLAY)
PodHistoryEntryType.getByCode(record.podEntryTypeCode) == PodHistoryEntryType.PAIR_AND_PRIME ->
layout.background.setColorFilter(Color.parseColor("#8000ff00"), PorterDuff.Mode.OVERLAY)
else ->
layout.background.colorFilter = standardBackgroundColorFilter
}
}
}

private fun description(record: OmnipodHistoryRecord) : String {
if (record.isSuccess) {
return when (PodHistoryEntryType.getByCode(record.podEntryTypeCode)) {
PodHistoryEntryType.SET_TEMPORARY_BASAL -> {
val tempBasalPair: TempBasalPair = aapsOmnipodUtil.gsonInstance.fromJson(record.data, TempBasalPair::class.java)
resourceHelper.gs(R.string.omnipod_cmd_tbr_value, tempBasalPair.insulinRate, tempBasalPair.durationMinutes)
}

PodHistoryEntryType.FILL_CANNULA_SET_BASAL_PROFILE,
PodHistoryEntryType.SET_BASAL_SCHEDULE -> profileValue(record.data)

PodHistoryEntryType.SET_BOLUS -> {
if (record.data.contains(";")) {
val splitVal = record.data.split(";".toRegex()).toTypedArray()
resourceHelper.gs(R.string.omnipod_cmd_bolus_value_with_carbs, java.lang.Double.valueOf(splitVal[0]), java.lang.Double.valueOf(splitVal[1]))
} else {
resourceHelper.gs(R.string.omnipod_cmd_bolus_value, java.lang.Double.valueOf(record.data))
}
}

PodHistoryEntryType.PAIR_AND_PRIME -> {
resourceHelper.gs(R.string.omnipod_history_new_pod_serial,record.podSerial)
}

PodHistoryEntryType.GET_POD_STATUS,
PodHistoryEntryType.GET_POD_INFO,
PodHistoryEntryType.SET_TIME,
PodHistoryEntryType.CANCEL_TEMPORARY_BASAL,
PodHistoryEntryType.CANCEL_TEMPORARY_BASAL_BY_DRIVER,
PodHistoryEntryType.CONFIGURE_ALERTS,
PodHistoryEntryType.CANCEL_BOLUS,
PodHistoryEntryType.DEACTIVATE_POD,
PodHistoryEntryType.RESET_POD_STATE,
PodHistoryEntryType.ACKNOWLEDGE_ALERTS,
PodHistoryEntryType.SUSPEND_DELIVERY,
PodHistoryEntryType.RESUME_DELIVERY,
PodHistoryEntryType.UNKNOWN_ENTRY_TYPE -> ""
else -> ""
}
} else {
return record.data
}
}

private fun profileValue(data: String?) : String {
if (data != null) {
aapsLogger.debug(LTag.PUMP, "Profile json:\n$data")
try {
val profileValuesArray: Array<Profile.ProfileValue> = aapsOmnipodUtil.getGsonInstance().fromJson(data, Array<Profile.ProfileValue>::class.java)
return ProfileUtil.getBasalProfilesDisplayable(profileValuesArray, PumpType.Insulet_Omnipod)
} catch (e: Exception) {
aapsLogger.error(LTag.PUMP, "Problem parsing Profile json. Ex: {}, Data:\n{}", e.message, data)
return ""
}
} else {
return ""
}
}

companion object {

fun create(parent: ViewGroup, aapsLogger: AAPSLogger, aapsOmnipodUtil: AapsOmnipodUtil, resourceHelper: ResourceHelper, rxBus: RxBusWrapper) : OmnipodHistoryRecordViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.omnipod_pod_history_item, parent, false)
return OmnipodHistoryRecordViewHolder(view, aapsLogger, aapsOmnipodUtil, resourceHelper, rxBus)
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package info.nightscout.androidaps.plugins.pump.omnipod.ui

import android.view.ViewGroup
import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.DiffUtil
import info.nightscout.androidaps.db.OmnipodHistoryRecord
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.pump.omnipod.util.AapsOmnipodUtil
import info.nightscout.androidaps.utils.resources.ResourceHelper
import javax.inject.Inject

class OmnipodHistoryRecordsAdapter @Inject constructor() : PagedListAdapter<OmnipodHistoryRecord, OmnipodHistoryRecordViewHolder>(DIFF_CALLBACK) {

@Inject lateinit var aapsLogger: AAPSLogger
@Inject lateinit var resourceHelper: ResourceHelper
@Inject lateinit var aapsOmnipodUtil: AapsOmnipodUtil
@Inject lateinit var rxBus: RxBusWrapper

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): OmnipodHistoryRecordViewHolder {
return OmnipodHistoryRecordViewHolder.create(parent ,aapsLogger, aapsOmnipodUtil, resourceHelper, rxBus)
}

override fun onBindViewHolder(holder: OmnipodHistoryRecordViewHolder, position: Int) {
val record: OmnipodHistoryRecord = getItem(position)!!
holder.bind(record)
}

companion object {

private val DIFF_CALLBACK = object : DiffUtil.ItemCallback<OmnipodHistoryRecord>() {

override fun areItemsTheSame(oldItem: OmnipodHistoryRecord, newItem: OmnipodHistoryRecord) =
oldItem.compareTo(newItem) == 0

override fun areContentsTheSame(oldItem: OmnipodHistoryRecord, newItem: OmnipodHistoryRecord) =
areItemsTheSame(oldItem,newItem)

}

}

}
Loading