Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package app.aaps.annotations

@Target(AnnotationTarget.PROPERTY)
@Retention(AnnotationRetention.RUNTIME)
annotation class DisplayAsDate
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app.aaps.core.interfaces.aps

import android.text.Spanned
import app.aaps.annotations.DisplayAsDate
import app.aaps.core.data.model.GV
import app.aaps.core.interfaces.constraints.Constraint
import org.json.JSONObject
Expand All @@ -9,6 +10,7 @@ interface APSResult {

fun with(result: RT): APSResult

@DisplayAsDate
var date: Long
var reason: String
var rate: Double
Expand All @@ -18,6 +20,7 @@ interface APSResult {
var usePercent: Boolean
var carbsReq: Int
var carbsReqWithin: Int
@DisplayAsDate
var deliverAt: Long
var targetBG: Double
var hasPredictions: Boolean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package app.aaps.core.interfaces.aps

import app.aaps.annotations.DisplayAsDate
import kotlinx.serialization.Serializable

@Suppress("SpellCheckingInspection")
@Serializable
data class IobTotal(
@DisplayAsDate
val time: Long,
var iob: Double = 0.0,
var activity: Double = 0.0,
Expand All @@ -14,6 +16,7 @@ data class IobTotal(
var hightempinsulin: Double = 0.0,

// oref1
@DisplayAsDate
var lastBolusTime: Long = 0,
var iobWithZeroTemp: IobTotal? = null,
var netInsulin: Double = 0.0, // for calculations from temp basals only
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package app.aaps.core.interfaces.aps

import app.aaps.annotations.DisplayAsDate
import kotlinx.serialization.Serializable

@Serializable
Expand All @@ -9,7 +10,9 @@ data class MealData(
var mealCOB: Double = 0.0,
var slopeFromMaxDeviation: Double = 0.0,
var slopeFromMinDeviation: Double = 999.0,
@DisplayAsDate
var lastBolusTime: Long = 0,
@DisplayAsDate
var lastCarbTime: Long = 0L,
var usedMinCarbsImpact: Double = 0.0
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import android.view.ViewGroup
import androidx.core.view.MenuCompat
import androidx.core.view.MenuProvider
import androidx.lifecycle.Lifecycle
import app.aaps.annotations.DisplayAsDate
import app.aaps.core.interfaces.logging.AAPSLogger
import app.aaps.core.interfaces.plugin.ActivePlugin
import app.aaps.core.interfaces.resources.ResourceHelper
Expand All @@ -31,6 +32,7 @@ import io.reactivex.rxjava3.kotlin.plusAssign
import org.apache.commons.lang3.ClassUtils
import javax.inject.Inject
import kotlin.reflect.full.declaredMemberProperties
import kotlin.reflect.full.findAnnotation

class OpenAPSFragment : DaggerFragment(), MenuProvider {

Expand Down Expand Up @@ -125,13 +127,13 @@ class OpenAPSFragment : DaggerFragment(), MenuProvider {
if (_binding == null) return
val openAPSPlugin = activePlugin.activeAPS
openAPSPlugin.lastAPSResult?.let { lastAPSResult ->
binding.result.text = lastAPSResult.rawData().dataClassToHtml()
binding.result.text = lastAPSResult.rawData().dataClassToHtmlDisplayAsDate(dateUtil)
binding.request.text = lastAPSResult.resultAsSpanned()
binding.glucosestatus.text = lastAPSResult.glucoseStatus?.dataClassToHtml(listOf("glucose", "delta", "shortAvgDelta", "longAvgDelta"))
binding.currenttemp.text = lastAPSResult.currentTemp?.dataClassToHtml()
binding.iobdata.text = rh.gs(R.string.array_of_elements, lastAPSResult.iobData?.size) + "\n" + lastAPSResult.iob?.dataClassToHtml()
binding.profile.text = lastAPSResult.oapsProfile?.dataClassToHtml() ?: lastAPSResult.oapsProfileAutoIsf?.dataClassToHtml()
binding.mealdata.text = lastAPSResult.mealData?.dataClassToHtml()
binding.mealdata.text = lastAPSResult.mealData?.dataClassToHtmlDisplayAsDate(dateUtil)
binding.scriptdebugdata.text = lastAPSResult.scriptDebug?.joinToString("\n")
binding.constraints.text = lastAPSResult.inputConstraints?.getReasons()
binding.autosensdata.text = lastAPSResult.autosensResult?.dataClassToHtml()
Expand Down Expand Up @@ -181,6 +183,54 @@ class OpenAPSFragment : DaggerFragment(), MenuProvider {
}
}.toString()
)
private fun Any.dataClassToHtmlDisplayAsDate(dateUtil: DateUtil): Spanned {
return HtmlHelper.fromHtml(
StringBuilder().also { sb ->
this::class.declaredMemberProperties.forEach { property ->
property.call(this)?.let { value ->
// Check for @DisplayAsDate annotation
if (property.findAnnotation<DisplayAsDate>() != null && value is Long) {
if (value > 0) { // Good practice to check for valid (positive) timestamps
val formattedDate = dateUtil.dateAndTimeString(value)
sb.append(property.name.bold(), ": "/*, value.toString(), " ("*/, formattedDate/*, ")"*/, br)
} else {
// Handle invalid/default timestamps (e.g., -1 for dateCreated)
sb.append(property.name.bold(), ": ", value.toString(), " (invalid Timestamp)", br)
}
} else if (ClassUtils.isPrimitiveOrWrapper(value::class.java)) {
sb.append(property.name.bold(), ": ", value.toString(), br)
} else if (value is StringBuilder) {
sb.append(property.name.bold(), ": ", value.toString(), br)
}
}
}
}.toString()
)
}

private fun Any.dataClassToHtmlDisplayAsDate(properties: List<String>, dateUtil: DateUtil): Spanned {
return HtmlHelper.fromHtml(
StringBuilder().also { sb ->
properties.forEach { propertyName ->
val property = this::class.declaredMemberProperties.firstOrNull { it.name == propertyName }
property?.call(this)?.let { value ->
if (property.findAnnotation<DisplayAsDate>() != null && value is Long) {
if (value > 0) { // Good practice for timestamps
val formattedDate = dateUtil.dateAndTimeString(value)
sb.append(property.name.bold(), ": "/*, value.toString(), " ("*/, formattedDate/*, ")"*/, br)
} else {
sb.append(propertyName.bold(), ": ", value.toString(), " (invalid Timestamp)", br)
}
} else if (ClassUtils.isPrimitiveOrWrapper(value::class.java)) {
sb.append(propertyName.bold(), ": ", value.toString(), br)
} else if (value is StringBuilder) {
sb.append(propertyName.bold(), ": ", value.toString(), br)
}
}
}
}.toString()
)
}

private fun String.bold(): String = "<b>$this</b>"
private val br = "<br>"
Expand Down