break aps -> automation dependency

This commit is contained in:
Milos Kozak 2023-09-17 23:22:28 +02:00
parent f717d7a0b2
commit bedccac4e3
36 changed files with 183 additions and 175 deletions

View file

@ -0,0 +1,92 @@
package info.nightscout.core.ui.elements
import android.widget.LinearLayout
import androidx.annotation.StringRes
import info.nightscout.core.ui.R
import java.util.Calendar
import java.util.Date
open class WeekDay {
enum class DayOfWeek {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
fun toCalendarInt(): Int {
return calendarInts[ordinal]
}
@get:StringRes val shortName: Int
get() = shortNames[ordinal]
companion object {
private val calendarInts = intArrayOf(
Calendar.MONDAY,
Calendar.TUESDAY,
Calendar.WEDNESDAY,
Calendar.THURSDAY,
Calendar.FRIDAY,
Calendar.SATURDAY,
Calendar.SUNDAY
)
private val shortNames = intArrayOf(
R.string.weekday_monday_short,
R.string.weekday_tuesday_short,
R.string.weekday_wednesday_short,
R.string.weekday_thursday_short,
R.string.weekday_friday_short,
R.string.weekday_saturday_short,
R.string.weekday_sunday_short
)
fun fromCalendarInt(day: Int): DayOfWeek {
for (i in calendarInts.indices) {
if (calendarInts[i] == day) return values()[i]
}
throw IllegalStateException("Invalid day")
}
}
}
val weekdays = BooleanArray(DayOfWeek.values().size)
var view: WeekdayPicker? = null
init {
for (day in DayOfWeek.values()) set(day, false)
}
fun setAll(value: Boolean) {
for (day in DayOfWeek.values()) set(day, value)
}
operator fun set(day: DayOfWeek, value: Boolean): WeekDay {
weekdays[day.ordinal] = value
return this
}
fun isSet(day: DayOfWeek): Boolean = weekdays[day.ordinal]
fun isSet(timestamp: Long): Boolean {
val scheduledDayOfWeek = Calendar.getInstance().also { it.time = Date(timestamp) }
return isSet(DayOfWeek.fromCalendarInt(scheduledDayOfWeek[Calendar.DAY_OF_WEEK]))
}
fun getSelectedDays(): List<Int> {
val selectedDays: MutableList<Int> = ArrayList()
for (i in weekdays.indices) {
val day = DayOfWeek.values()[i]
val selected = weekdays[i]
if (selected) selectedDays.add(day.toCalendarInt())
}
return selectedDays
}
fun addToLayout(root: LinearLayout) {
view = WeekdayPicker(root.context).apply {
setSelectedDays(getSelectedDays())
setOnWeekdaysChangeListener { i: Int, selected: Boolean -> set(DayOfWeek.fromCalendarInt(i), selected) }
}
root.addView(
view
)
}
}

View file

@ -1,13 +1,13 @@
package info.nightscout.automation.ui package info.nightscout.core.ui.elements
import android.content.Context import android.content.Context
import android.util.AttributeSet import android.util.AttributeSet
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View
import android.widget.Checkable import android.widget.Checkable
import androidx.appcompat.widget.AppCompatCheckedTextView import androidx.appcompat.widget.AppCompatCheckedTextView
import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintLayout
import info.nightscout.automation.databinding.WeekdayPickerBinding import info.nightscout.core.ui.databinding.WeekdayPickerBinding
import info.nightscout.shared.extensions.toVisibility
import java.util.Calendar import java.util.Calendar
class WeekdayPicker constructor( class WeekdayPicker constructor(
@ -25,6 +25,8 @@ class WeekdayPicker constructor(
setupClickListeners() setupClickListeners()
} }
fun Boolean.toVisibility() = if (this) View.VISIBLE else View.GONE
private fun determineBeginOfWeek() { private fun determineBeginOfWeek() {
(Calendar.getInstance().firstDayOfWeek == Calendar.SUNDAY).let { (Calendar.getInstance().firstDayOfWeek == Calendar.SUNDAY).let {
binding.weekdayPickerSundayStart.visibility = it.toVisibility() binding.weekdayPickerSundayStart.visibility = it.toVisibility()

View file

@ -11,7 +11,7 @@
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_margin="2dp" android:layout_margin="2dp"
android:background="@drawable/weekend_circle_brackground" android:background="@drawable/weekend_circle_background"
android:gravity="center" android:gravity="center"
android:padding="8dp" android:padding="8dp"
android:text="@string/sunday_short" android:text="@string/sunday_short"
@ -27,7 +27,7 @@
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_margin="2dp" android:layout_margin="2dp"
android:background="@drawable/weekday_circle_brackground" android:background="@drawable/weekday_circle_background"
android:gravity="center" android:gravity="center"
android:padding="8dp" android:padding="8dp"
android:text="@string/monday_short" android:text="@string/monday_short"
@ -43,7 +43,7 @@
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_margin="2dp" android:layout_margin="2dp"
android:background="@drawable/weekday_circle_brackground" android:background="@drawable/weekday_circle_background"
android:gravity="center" android:gravity="center"
android:padding="8dp" android:padding="8dp"
android:text="@string/tuesday_short" android:text="@string/tuesday_short"
@ -59,7 +59,7 @@
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_margin="2dp" android:layout_margin="2dp"
android:background="@drawable/weekday_circle_brackground" android:background="@drawable/weekday_circle_background"
android:gravity="center" android:gravity="center"
android:padding="8dp" android:padding="8dp"
android:text="@string/wednesday_short" android:text="@string/wednesday_short"
@ -75,7 +75,7 @@
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_margin="2dp" android:layout_margin="2dp"
android:background="@drawable/weekday_circle_brackground" android:background="@drawable/weekday_circle_background"
android:gravity="center" android:gravity="center"
android:padding="8dp" android:padding="8dp"
android:text="@string/thursday_short" android:text="@string/thursday_short"
@ -91,7 +91,7 @@
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_margin="2dp" android:layout_margin="2dp"
android:background="@drawable/weekday_circle_brackground" android:background="@drawable/weekday_circle_background"
android:gravity="center" android:gravity="center"
android:padding="8dp" android:padding="8dp"
android:text="@string/friday_short" android:text="@string/friday_short"
@ -107,7 +107,7 @@
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_margin="2dp" android:layout_margin="2dp"
android:background="@drawable/weekend_circle_brackground" android:background="@drawable/weekend_circle_background"
android:gravity="center" android:gravity="center"
android:padding="8dp" android:padding="8dp"
android:text="@string/saturday_short" android:text="@string/saturday_short"
@ -123,7 +123,7 @@
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_margin="2dp" android:layout_margin="2dp"
android:background="@drawable/weekend_circle_brackground" android:background="@drawable/weekend_circle_background"
android:gravity="center" android:gravity="center"
android:padding="8dp" android:padding="8dp"
android:text="@string/sunday_short" android:text="@string/sunday_short"

View file

@ -630,4 +630,23 @@
<string name="cleanup_db_confirm_sync">Do you want to cleanup the database?\nIt will remove tracked changes and historic data older than 3 months.\nDoing it will speedup full synchronization dramatically.</string> <string name="cleanup_db_confirm_sync">Do you want to cleanup the database?\nIt will remove tracked changes and historic data older than 3 months.\nDoing it will speedup full synchronization dramatically.</string>
<string name="cleared_entries">Cleared entries</string> <string name="cleared_entries">Cleared entries</string>
<!-- Weekday-->
<string name="weekday_sunday_short">Sun</string>
<string name="weekday_saturday_short">Sat</string>
<string name="weekday_friday_short">Fri</string>
<string name="weekday_thursday_short">Thu</string>
<string name="weekday_wednesday_short">Wed</string>
<string name="weekday_tuesday_short">Tue</string>
<string name="weekday_monday_short">Mon</string>
<!-- WeekdayPicker -->
<string name="monday_short">M</string>
<string name="tuesday_short">T</string>
<string name="wednesday_short">W</string>
<string name="thursday_short">T</string>
<string name="friday_short">F</string>
<string name="saturday_short">S</string>
<string name="sunday_short">S</string>
</resources> </resources>

View file

@ -23,7 +23,6 @@ dependencies {
implementation project(':core:utils') implementation project(':core:utils')
implementation project(':core:ui') implementation project(':core:ui')
implementation project(':core:validators') implementation project(':core:validators')
implementation project(':plugins:automation')
testImplementation project(':app-wear-shared:shared-tests') testImplementation project(':app-wear-shared:shared-tests')

View file

@ -19,9 +19,9 @@ import android.widget.TableRow
import android.widget.TextView import android.widget.TextView
import dagger.android.HasAndroidInjector import dagger.android.HasAndroidInjector
import dagger.android.support.DaggerFragment import dagger.android.support.DaggerFragment
import info.nightscout.automation.elements.InputWeekDay
import info.nightscout.core.profile.ProfileSealed import info.nightscout.core.profile.ProfileSealed
import info.nightscout.core.ui.dialogs.OKDialog import info.nightscout.core.ui.dialogs.OKDialog
import info.nightscout.core.ui.elements.WeekDay
import info.nightscout.core.utils.fabric.FabricPrivacy import info.nightscout.core.utils.fabric.FabricPrivacy
import info.nightscout.database.entities.UserEntry import info.nightscout.database.entities.UserEntry
import info.nightscout.database.entities.ValueWithUnit import info.nightscout.database.entities.ValueWithUnit
@ -112,7 +112,7 @@ class AutotuneFragment : DaggerFragment() {
if (autotunePlugin.calculationRunning) if (autotunePlugin.calculationRunning)
days.view?.setSelectedDays(days.getSelectedDays()) days.view?.setSelectedDays(days.getSelectedDays())
else { else {
days.set(InputWeekDay.DayOfWeek.fromCalendarInt(i), selected) days.set(WeekDay.DayOfWeek.fromCalendarInt(i), selected)
resetParam(false) resetParam(false)
updateGui() updateGui()
} }

View file

@ -1,10 +1,12 @@
@file:Suppress("SpellCheckingInspection")
package info.nightscout.plugins.general.autotune package info.nightscout.plugins.general.autotune
import android.view.View import android.view.View
import dagger.android.HasAndroidInjector import dagger.android.HasAndroidInjector
import info.nightscout.automation.elements.InputWeekDay
import info.nightscout.core.extensions.pureProfileFromJson import info.nightscout.core.extensions.pureProfileFromJson
import info.nightscout.core.profile.ProfileSealed import info.nightscout.core.profile.ProfileSealed
import info.nightscout.core.ui.elements.WeekDay
import info.nightscout.database.entities.UserEntry import info.nightscout.database.entities.UserEntry
import info.nightscout.database.entities.ValueWithUnit import info.nightscout.database.entities.ValueWithUnit
import info.nightscout.interfaces.Config import info.nightscout.interfaces.Config
@ -38,7 +40,6 @@ import org.json.JSONObject
import java.util.TimeZone import java.util.TimeZone
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
import kotlin.collections.ArrayList
/* /*
* adaptation from oref0 autotune developed by philoul on 2022 (complete refactoring of AutotunePlugin initialised by Rumen Georgiev on 1/29/2018.) * adaptation from oref0 autotune developed by philoul on 2022 (complete refactoring of AutotunePlugin initialised by Rumen Georgiev on 1/29/2018.)
@ -87,7 +88,7 @@ class AutotunePlugin @Inject constructor(
@Volatile var tunedProfile: ATProfile? = null @Volatile var tunedProfile: ATProfile? = null
private var preppedGlucose: PreppedGlucose? = null private var preppedGlucose: PreppedGlucose? = null
private lateinit var profile: Profile private lateinit var profile: Profile
val days = InputWeekDay() val days = WeekDay()
val autotuneStartHour: Int = 4 val autotuneStartHour: Int = 4
override fun aapsAutotune(daysBack: Int, autoSwitch: Boolean, profileToTune: String, weekDays: BooleanArray?) { override fun aapsAutotune(daysBack: Int, autoSwitch: Boolean, profileToTune: String, weekDays: BooleanArray?) {
@ -104,10 +105,9 @@ class AutotunePlugin @Inject constructor(
val calcDays = calcDays(daysBack) val calcDays = calcDays(daysBack)
val sb = StringBuilder() val sb = StringBuilder()
sb.append("Selected days: ") sb.append("Selected days: ")
var counter = 0 for ((counter, i) in days.getSelectedDays().withIndex()) {
for (i in days.getSelectedDays()) { if (counter > 0) sb.append(",")
if (counter++ > 0) sb.append(",") sb.append(WeekDay.DayOfWeek.fromCalendarInt(i))
sb.append(InputWeekDay.DayOfWeek.fromCalendarInt(i))
} }
log(sb.toString()) log(sb.toString())
tunedProfile = null tunedProfile = null
@ -143,8 +143,8 @@ class AutotunePlugin @Inject constructor(
// Today at 4 AM // Today at 4 AM
var endTime = MidnightTime.calc(lastRun) + autotuneStartHour * 60 * 60 * 1000L var endTime = MidnightTime.calc(lastRun) + autotuneStartHour * 60 * 60 * 1000L
if (endTime > lastRun) endTime -= 24 * 60 * 60 * 1000L // Check if 4 AM is before now if (endTime > lastRun) endTime -= 24 * 60 * 60 * 1000L // Check if 4 AM is before now
val starttime = endTime - daysBack * 24 * 60 * 60 * 1000L val startTime = endTime - daysBack * 24 * 60 * 60 * 1000L
autotuneFS.exportSettings(settings(lastRun, daysBack, starttime, endTime)) autotuneFS.exportSettings(settings(lastRun, daysBack, startTime, endTime))
tunedProfile = ATProfile(profile, localInsulin, injector).also { tunedProfile = ATProfile(profile, localInsulin, injector).also {
it.profileName = rh.gs(info.nightscout.core.ui.R.string.autotune_tunedprofile_name) it.profileName = rh.gs(info.nightscout.core.ui.R.string.autotune_tunedprofile_name)
} }
@ -165,7 +165,7 @@ class AutotunePlugin @Inject constructor(
} }
var currentCalcDay = 0 var currentCalcDay = 0
for (i in 0 until daysBack) { for (i in 0 until daysBack) {
val from = starttime + i * 24 * 60 * 60 * 1000L // get 24 hours BG values from 4 AM to 4 AM next day val from = startTime + i * 24 * 60 * 60 * 1000L // get 24 hours BG values from 4 AM to 4 AM next day
val to = from + 24 * 60 * 60 * 1000L val to = from + 24 * 60 * 60 * 1000L
if (days.isSet(from)) { if (days.isSet(from)) {
currentCalcDay++ currentCalcDay++
@ -234,9 +234,9 @@ class AutotunePlugin @Inject constructor(
ValueWithUnit.SimpleString(tunedP.profileName) ValueWithUnit.SimpleString(tunedP.profileName)
) )
updateButtonVisibility = View.GONE updateButtonVisibility = View.GONE
tunedP.profileStore(circadian)?.let { profilestore -> tunedP.profileStore(circadian)?.let { profileStore ->
if (profileFunction.createProfileSwitch( if (profileFunction.createProfileSwitch(
profilestore, profileStore,
profileName = tunedP.profileName, profileName = tunedP.profileName,
durationInMinutes = 0, durationInMinutes = 0,
percentage = 100, percentage = 100,
@ -301,13 +301,13 @@ class AutotunePlugin @Inject constructor(
return strResult return strResult
} }
private fun settings(runDate: Long, nbDays: Int, firstloopstart: Long, lastloopend: Long): String { private fun settings(runDate: Long, nbDays: Int, firstLoopStart: Long, lastLoopEnd: Long): String {
var jsonString = "" var jsonString = ""
val jsonSettings = JSONObject() val jsonSettings = JSONObject()
val insulinInterface = activePlugin.activeInsulin val insulinInterface = activePlugin.activeInsulin
val utcOffset = T.msecs(TimeZone.getDefault().getOffset(dateUtil.now()).toLong()).hours() val utcOffset = T.msecs(TimeZone.getDefault().getOffset(dateUtil.now()).toLong()).hours()
val startDateString = dateUtil.toISOString(firstloopstart).substring(0, 10) val startDateString = dateUtil.toISOString(firstLoopStart).substring(0, 10)
val endDateString = dateUtil.toISOString(lastloopend - 24 * 60 * 60 * 1000L).substring(0, 10) val endDateString = dateUtil.toISOString(lastLoopEnd - 24 * 60 * 60 * 1000L).substring(0, 10)
val nsUrl = sp.getString(info.nightscout.core.utils.R.string.key_nsclientinternal_url, "") val nsUrl = sp.getString(info.nightscout.core.utils.R.string.key_nsclientinternal_url, "")
val optCategorizeUam = if (sp.getBoolean(info.nightscout.core.utils.R.string.key_autotune_categorize_uam_as_basal, false)) "-c=true" else "" val optCategorizeUam = if (sp.getBoolean(info.nightscout.core.utils.R.string.key_autotune_categorize_uam_as_basal, false)) "-c=true" else ""
val optInsulinCurve = if (sp.getBoolean(info.nightscout.core.utils.R.string.key_autotune_tune_insulin_curve, false)) "-i=true" else "" val optInsulinCurve = if (sp.getBoolean(info.nightscout.core.utils.R.string.key_autotune_tune_insulin_curve, false)) "-i=true" else ""
@ -330,7 +330,7 @@ class AutotunePlugin @Inject constructor(
jsonSettings.put("categorize_uam_as_basal", sp.getBoolean(info.nightscout.core.utils.R.string.key_autotune_categorize_uam_as_basal, false)) jsonSettings.put("categorize_uam_as_basal", sp.getBoolean(info.nightscout.core.utils.R.string.key_autotune_categorize_uam_as_basal, false))
jsonSettings.put("tune_insulin_curve", false) jsonSettings.put("tune_insulin_curve", false)
val peaktime: Int = insulinInterface.peak val peakTime: Int = insulinInterface.peak
if (insulinInterface.id === Insulin.InsulinType.OREF_ULTRA_RAPID_ACTING) if (insulinInterface.id === Insulin.InsulinType.OREF_ULTRA_RAPID_ACTING)
jsonSettings.put("curve", "ultra-rapid") jsonSettings.put("curve", "ultra-rapid")
else if (insulinInterface.id === Insulin.InsulinType.OREF_RAPID_ACTING) else if (insulinInterface.id === Insulin.InsulinType.OREF_RAPID_ACTING)
@ -338,11 +338,11 @@ class AutotunePlugin @Inject constructor(
else if (insulinInterface.id === Insulin.InsulinType.OREF_LYUMJEV) { else if (insulinInterface.id === Insulin.InsulinType.OREF_LYUMJEV) {
jsonSettings.put("curve", "ultra-rapid") jsonSettings.put("curve", "ultra-rapid")
jsonSettings.put("useCustomPeakTime", true) jsonSettings.put("useCustomPeakTime", true)
jsonSettings.put("insulinPeakTime", peaktime) jsonSettings.put("insulinPeakTime", peakTime)
} else if (insulinInterface.id === Insulin.InsulinType.OREF_FREE_PEAK) { } else if (insulinInterface.id === Insulin.InsulinType.OREF_FREE_PEAK) {
jsonSettings.put("curve", if (peaktime > 55) "rapid-acting" else "ultra-rapid") jsonSettings.put("curve", if (peakTime > 55) "rapid-acting" else "ultra-rapid")
jsonSettings.put("useCustomPeakTime", true) jsonSettings.put("useCustomPeakTime", true)
jsonSettings.put("insulinPeakTime", peaktime) jsonSettings.put("insulinPeakTime", peakTime)
} }
jsonString = jsonSettings.toString(4).replace("\\/", "/") jsonString = jsonSettings.toString(4).replace("\\/", "/")
} catch (e: JSONException) { } catch (e: JSONException) {
@ -392,7 +392,7 @@ class AutotunePlugin @Inject constructor(
} }
} }
for (i in days.weekdays.indices) { for (i in days.weekdays.indices) {
json.put(InputWeekDay.DayOfWeek.values()[i].name, days.weekdays[i]) json.put(WeekDay.DayOfWeek.values()[i].name, days.weekdays[i])
} }
json.put("result", result) json.put("result", result)
json.put("updateButtonVisibility", updateButtonVisibility) json.put("updateButtonVisibility", updateButtonVisibility)
@ -429,7 +429,7 @@ class AutotunePlugin @Inject constructor(
} }
} }
for (i in days.weekdays.indices) for (i in days.weekdays.indices)
days.weekdays[i] = JsonHelper.safeGetBoolean(json, InputWeekDay.DayOfWeek.values()[i].name,true) days.weekdays[i] = JsonHelper.safeGetBoolean(json, WeekDay.DayOfWeek.values()[i].name,true)
result = JsonHelper.safeGetString(json, "result", "") result = JsonHelper.safeGetString(json, "result", "")
updateButtonVisibility = JsonHelper.safeGetInt(json, "updateButtonVisibility") updateButtonVisibility = JsonHelper.safeGetInt(json, "updateButtonVisibility")
lastRunSuccess = true lastRunSuccess = true
@ -441,10 +441,10 @@ class AutotunePlugin @Inject constructor(
fun calcDays(daysBack:Int): Int { fun calcDays(daysBack:Int): Int {
var endTime = MidnightTime.calc(dateUtil.now()) + autotuneStartHour * 60 * 60 * 1000L var endTime = MidnightTime.calc(dateUtil.now()) + autotuneStartHour * 60 * 60 * 1000L
if (endTime > dateUtil.now()) endTime -= T.days(1).msecs() // Check if 4 AM is before now if (endTime > dateUtil.now()) endTime -= T.days(1).msecs() // Check if 4 AM is before now
val starttime = endTime - daysBack * T.days(1).msecs() val startTime = endTime - daysBack * T.days(1).msecs()
var result = 0 var result = 0
for (i in 0 until daysBack) { for (i in 0 until daysBack) {
if (days.isSet(starttime + i * T.days(1).msecs())) if (days.isSet(startTime + i * T.days(1).msecs()))
result++ result++
} }
return result return result

View file

@ -8,6 +8,7 @@ import info.nightscout.automation.elements.InputProfileName
import info.nightscout.automation.elements.InputWeekDay import info.nightscout.automation.elements.InputWeekDay
import info.nightscout.automation.elements.LabelWithElement import info.nightscout.automation.elements.LabelWithElement
import info.nightscout.automation.elements.LayoutBuilder import info.nightscout.automation.elements.LayoutBuilder
import info.nightscout.core.ui.elements.WeekDay
import info.nightscout.interfaces.autotune.Autotune import info.nightscout.interfaces.autotune.Autotune
import info.nightscout.interfaces.plugin.ActivePlugin import info.nightscout.interfaces.plugin.ActivePlugin
import info.nightscout.interfaces.profile.ProfileFunction import info.nightscout.interfaces.profile.ProfileFunction
@ -28,7 +29,7 @@ class ActionRunAutotune(injector: HasAndroidInjector) : Action(injector) {
@Inject lateinit var activePlugin: ActivePlugin @Inject lateinit var activePlugin: ActivePlugin
@Inject lateinit var sp: SP @Inject lateinit var sp: SP
var defaultValue = 0 private var defaultValue = 0
private var inputProfileName = InputProfileName(rh, activePlugin, "", true) private var inputProfileName = InputProfileName(rh, activePlugin, "", true)
private var daysBack = InputDuration(0, InputDuration.TimeUnit.DAYS) private var daysBack = InputDuration(0, InputDuration.TimeUnit.DAYS)
private val days = InputWeekDay().also { it.setAll(true) } private val days = InputWeekDay().also { it.setAll(true) }
@ -77,7 +78,7 @@ class ActionRunAutotune(injector: HasAndroidInjector) : Action(injector) {
.put("profileToTune", inputProfileName.value) .put("profileToTune", inputProfileName.value)
.put("tunedays", daysBack.value) .put("tunedays", daysBack.value)
for (i in days.weekdays.indices) { for (i in days.weekdays.indices) {
data.put(InputWeekDay.DayOfWeek.values()[i].name, days.weekdays[i]) data.put(WeekDay.DayOfWeek.values()[i].name, days.weekdays[i])
} }
return JSONObject() return JSONObject()
.put("type", this.javaClass.simpleName) .put("type", this.javaClass.simpleName)
@ -88,7 +89,7 @@ class ActionRunAutotune(injector: HasAndroidInjector) : Action(injector) {
override fun fromJSON(data: String): Action { override fun fromJSON(data: String): Action {
val o = JSONObject(data) val o = JSONObject(data)
for (i in days.weekdays.indices) for (i in days.weekdays.indices)
days.weekdays[i] = JsonHelper.safeGetBoolean(o, InputWeekDay.DayOfWeek.values()[i].name,true) days.weekdays[i] = JsonHelper.safeGetBoolean(o, WeekDay.DayOfWeek.values()[i].name,true)
inputProfileName.value = JsonHelper.safeGetString(o, "profileToTune", "") inputProfileName.value = JsonHelper.safeGetString(o, "profileToTune", "")
defaultValue = JsonHelper.safeGetInt(o, "tunedays") defaultValue = JsonHelper.safeGetInt(o, "tunedays")
if (defaultValue == 0) if (defaultValue == 0)

View file

@ -10,7 +10,7 @@ import androidx.annotation.StringRes
import info.nightscout.automation.R import info.nightscout.automation.R
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
class Comparator(private val rh: ResourceHelper) : Element() { class Comparator(private val rh: ResourceHelper) : Element {
enum class Compare { enum class Compare {
IS_LESSER, IS_LESSER,

View file

@ -10,7 +10,7 @@ import androidx.annotation.StringRes
import info.nightscout.automation.R import info.nightscout.automation.R
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
class ComparatorConnect(private val rh: ResourceHelper) : Element() { class ComparatorConnect(private val rh: ResourceHelper) : Element {
enum class Compare { enum class Compare {
ON_CONNECT, ON_DISCONNECT; ON_CONNECT, ON_DISCONNECT;

View file

@ -9,7 +9,7 @@ import android.widget.Spinner
import androidx.annotation.StringRes import androidx.annotation.StringRes
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
class ComparatorExists(private val rh: ResourceHelper, var value: Compare = Compare.EXISTS) : Element() { class ComparatorExists(private val rh: ResourceHelper, var value: Compare = Compare.EXISTS) : Element {
enum class Compare { enum class Compare {
EXISTS, NOT_EXISTS; EXISTS, NOT_EXISTS;

View file

@ -2,8 +2,7 @@ package info.nightscout.automation.elements
import android.widget.LinearLayout import android.widget.LinearLayout
abstract class Element { interface Element {
abstract fun addToLayout(root: LinearLayout)
fun addToLayout(root: LinearLayout)
} }

View file

@ -7,7 +7,7 @@ import info.nightscout.interfaces.GlucoseUnit
import info.nightscout.interfaces.profile.ProfileFunction import info.nightscout.interfaces.profile.ProfileFunction
import java.text.DecimalFormat import java.text.DecimalFormat
class InputBg(profileFunction: ProfileFunction) : Element() { class InputBg(profileFunction: ProfileFunction) : Element {
var units = GlucoseUnit.MGDL var units = GlucoseUnit.MGDL
var value = 0.0 var value = 0.0

View file

@ -4,7 +4,7 @@ import android.view.Gravity
import android.widget.Button import android.widget.Button
import android.widget.LinearLayout import android.widget.LinearLayout
class InputButton() : Element() { class InputButton() : Element {
var text: String? = null var text: String? = null
var runnable: Runnable? = null var runnable: Runnable? = null

View file

@ -11,7 +11,7 @@ import androidx.annotation.StringRes
import info.nightscout.database.entities.TherapyEvent import info.nightscout.database.entities.TherapyEvent
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
class InputCarePortalMenu(private val rh: ResourceHelper) : Element() { class InputCarePortalMenu(private val rh: ResourceHelper) : Element {
enum class EventType(val therapyEventType: TherapyEvent.Type) { enum class EventType(val therapyEventType: TherapyEvent.Type) {
NOTE(TherapyEvent.Type.NOTE), NOTE(TherapyEvent.Type.NOTE),

View file

@ -17,7 +17,7 @@ import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.utils.DateUtil import info.nightscout.shared.utils.DateUtil
import java.util.Calendar import java.util.Calendar
class InputDateTime(private val rh: ResourceHelper, private val dateUtil: DateUtil, var value: Long = dateUtil.now()) : Element() { class InputDateTime(private val rh: ResourceHelper, private val dateUtil: DateUtil, var value: Long = dateUtil.now()) : Element {
override fun addToLayout(root: LinearLayout) { override fun addToLayout(root: LinearLayout) {
val px = rh.dpToPx(10) val px = rh.dpToPx(10)

View file

@ -12,7 +12,7 @@ import info.nightscout.core.ui.elements.NumberPicker
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
import java.text.DecimalFormat import java.text.DecimalFormat
class InputDelta(private val rh: ResourceHelper) : Element() { class InputDelta(private val rh: ResourceHelper) : Element {
enum class DeltaType { enum class DeltaType {
DELTA, SHORT_AVERAGE, LONG_AVERAGE; DELTA, SHORT_AVERAGE, LONG_AVERAGE;

View file

@ -5,7 +5,7 @@ import android.widget.LinearLayout
import info.nightscout.core.ui.elements.NumberPicker import info.nightscout.core.ui.elements.NumberPicker
import java.text.DecimalFormat import java.text.DecimalFormat
class InputDouble() : Element() { class InputDouble() : Element {
var value = 0.0 var value = 0.0
private var minValue = 0.0 private var minValue = 0.0

View file

@ -8,7 +8,7 @@ import android.widget.LinearLayout
import android.widget.Spinner import android.widget.Spinner
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
class InputDropdownMenu(private val rh: ResourceHelper) : Element() { class InputDropdownMenu(private val rh: ResourceHelper) : Element {
private var itemList: ArrayList<CharSequence> = ArrayList() private var itemList: ArrayList<CharSequence> = ArrayList()
var value: String = "" var value: String = ""

View file

@ -9,7 +9,7 @@ import java.text.DecimalFormat
class InputDuration( class InputDuration(
var value: Int = 0, var value: Int = 0,
var unit: TimeUnit = TimeUnit.MINUTES, var unit: TimeUnit = TimeUnit.MINUTES,
) : Element() { ) : Element {
enum class TimeUnit { enum class TimeUnit {
MINUTES, HOURS, DAYS MINUTES, HOURS, DAYS

View file

@ -5,7 +5,7 @@ import android.widget.LinearLayout
import info.nightscout.core.ui.elements.NumberPicker import info.nightscout.core.ui.elements.NumberPicker
import java.text.DecimalFormat import java.text.DecimalFormat
class InputInsulin() : Element() { class InputInsulin() : Element {
var value = 0.0 var value = 0.0

View file

@ -10,7 +10,7 @@ import androidx.annotation.StringRes
import info.nightscout.automation.R import info.nightscout.automation.R
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
class InputLocationMode(private val rh: ResourceHelper) : Element() { class InputLocationMode(private val rh: ResourceHelper) : Element {
enum class Mode { enum class Mode {
INSIDE, OUTSIDE, GOING_IN, GOING_OUT; INSIDE, OUTSIDE, GOING_IN, GOING_OUT;

View file

@ -5,7 +5,7 @@ import android.widget.LinearLayout
import info.nightscout.core.ui.elements.NumberPicker import info.nightscout.core.ui.elements.NumberPicker
import java.text.DecimalFormat import java.text.DecimalFormat
class InputPercent() : Element() { class InputPercent() : Element {
var value: Double = 100.0 var value: Double = 100.0

View file

@ -9,7 +9,7 @@ import android.widget.Spinner
import info.nightscout.interfaces.plugin.ActivePlugin import info.nightscout.interfaces.plugin.ActivePlugin
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
class InputProfileName(private val rh: ResourceHelper, private val activePlugin: ActivePlugin, val name: String = "", private val addActive: Boolean = false) : Element() { class InputProfileName(private val rh: ResourceHelper, private val activePlugin: ActivePlugin, val name: String = "", private val addActive: Boolean = false) : Element {
var value: String = name var value: String = name

View file

@ -7,7 +7,7 @@ import android.view.ViewGroup
import android.widget.EditText import android.widget.EditText
import android.widget.LinearLayout import android.widget.LinearLayout
class InputString(var value: String = "") : Element() { class InputString(var value: String = "") : Element {
private val textWatcher: TextWatcher = object : TextWatcher { private val textWatcher: TextWatcher = object : TextWatcher {
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}

View file

@ -8,7 +8,7 @@ import info.nightscout.interfaces.GlucoseUnit
import info.nightscout.interfaces.profile.ProfileFunction import info.nightscout.interfaces.profile.ProfileFunction
import java.text.DecimalFormat import java.text.DecimalFormat
class InputTempTarget(profileFunction: ProfileFunction) : Element() { class InputTempTarget(profileFunction: ProfileFunction) : Element {
var units: GlucoseUnit = GlucoseUnit.MGDL var units: GlucoseUnit = GlucoseUnit.MGDL
var value = 0.0 var value = 0.0

View file

@ -19,7 +19,7 @@ import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.utils.DateUtil import info.nightscout.shared.utils.DateUtil
import java.util.Calendar import java.util.Calendar
class InputTime(private val rh: ResourceHelper, private val dateUtil: DateUtil) : Element() { class InputTime(private val rh: ResourceHelper, private val dateUtil: DateUtil) : Element {
var value: Int = getMinSinceMidnight(dateUtil.now()) var value: Int = getMinSinceMidnight(dateUtil.now())

View file

@ -19,7 +19,7 @@ import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.utils.DateUtil import info.nightscout.shared.utils.DateUtil
import java.util.Calendar import java.util.Calendar
class InputTimeRange(private val rh: ResourceHelper, private val dateUtil: DateUtil) : Element() { class InputTimeRange(private val rh: ResourceHelper, private val dateUtil: DateUtil) : Element {
var start: Int = getMinSinceMidnight(dateUtil.now()) var start: Int = getMinSinceMidnight(dateUtil.now())
var end: Int = getMinSinceMidnight(dateUtil.now()) var end: Int = getMinSinceMidnight(dateUtil.now())

View file

@ -1,94 +1,5 @@
package info.nightscout.automation.elements package info.nightscout.automation.elements
import android.widget.LinearLayout import info.nightscout.core.ui.elements.WeekDay
import androidx.annotation.StringRes
import info.nightscout.automation.ui.WeekdayPicker
import info.nightscout.automation.R
import java.util.Calendar
import java.util.Date
import kotlin.collections.ArrayList
class InputWeekDay : Element() { class InputWeekDay : WeekDay(), Element
enum class DayOfWeek {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
fun toCalendarInt(): Int {
return calendarInts[ordinal]
}
@get:StringRes val shortName: Int
get() = shortNames[ordinal]
companion object {
private val calendarInts = intArrayOf(
Calendar.MONDAY,
Calendar.TUESDAY,
Calendar.WEDNESDAY,
Calendar.THURSDAY,
Calendar.FRIDAY,
Calendar.SATURDAY,
Calendar.SUNDAY
)
private val shortNames = intArrayOf(
R.string.weekday_monday_short,
R.string.weekday_tuesday_short,
R.string.weekday_wednesday_short,
R.string.weekday_thursday_short,
R.string.weekday_friday_short,
R.string.weekday_saturday_short,
R.string.weekday_sunday_short
)
fun fromCalendarInt(day: Int): DayOfWeek {
for (i in calendarInts.indices) {
if (calendarInts[i] == day) return values()[i]
}
throw IllegalStateException("Invalid day")
}
}
}
val weekdays = BooleanArray(DayOfWeek.values().size)
var view: WeekdayPicker? = null
init {
for (day in DayOfWeek.values()) set(day, false)
}
fun setAll(value: Boolean) {
for (day in DayOfWeek.values()) set(day, value)
}
operator fun set(day: DayOfWeek, value: Boolean): InputWeekDay {
weekdays[day.ordinal] = value
return this
}
fun isSet(day: DayOfWeek): Boolean = weekdays[day.ordinal]
fun isSet(timestamp: Long): Boolean {
val scheduledDayOfWeek = Calendar.getInstance().also { it.time = Date(timestamp) }
return isSet(DayOfWeek.fromCalendarInt(scheduledDayOfWeek[Calendar.DAY_OF_WEEK]))
}
fun getSelectedDays(): List<Int> {
val selectedDays: MutableList<Int> = ArrayList()
for (i in weekdays.indices) {
val day = DayOfWeek.values()[i]
val selected = weekdays[i]
if (selected) selectedDays.add(day.toCalendarInt())
}
return selectedDays
}
override fun addToLayout(root: LinearLayout) {
view = WeekdayPicker(root.context).apply {
setSelectedDays(getSelectedDays())
setOnWeekdaysChangeListener { i: Int, selected: Boolean -> set(DayOfWeek.fromCalendarInt(i), selected) }
}
root.addView(
view
)
}
}

View file

@ -11,7 +11,7 @@ class LabelWithElement(
var textPre: String = "", var textPre: String = "",
var textPost: String = "", var textPost: String = "",
var element: Element? = null, var element: Element? = null,
) : Element() { ) : Element {
override fun addToLayout(root: LinearLayout) { // container layout override fun addToLayout(root: LinearLayout) { // container layout
// text view pre element // text view pre element

View file

@ -7,7 +7,7 @@ import android.widget.TextView
import info.nightscout.automation.triggers.Trigger import info.nightscout.automation.triggers.Trigger
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
class StaticLabel(private val rh: ResourceHelper) : Element() { class StaticLabel(private val rh: ResourceHelper) : Element {
var label = "" var label = ""
var trigger: Trigger? = null var trigger: Trigger? = null

View file

@ -8,6 +8,7 @@ import info.nightscout.automation.elements.InputTime
import info.nightscout.automation.elements.InputWeekDay import info.nightscout.automation.elements.InputWeekDay
import info.nightscout.automation.elements.LayoutBuilder import info.nightscout.automation.elements.LayoutBuilder
import info.nightscout.automation.elements.StaticLabel import info.nightscout.automation.elements.StaticLabel
import info.nightscout.core.ui.elements.WeekDay
import info.nightscout.core.utils.MidnightUtils import info.nightscout.core.utils.MidnightUtils
import info.nightscout.interfaces.utils.JsonHelper import info.nightscout.interfaces.utils.JsonHelper
import info.nightscout.interfaces.utils.MidnightTime import info.nightscout.interfaces.utils.MidnightTime
@ -35,7 +36,7 @@ class TriggerRecurringTime(injector: HasAndroidInjector) : Trigger(injector) {
override fun shouldRun(): Boolean { override fun shouldRun(): Boolean {
val currentMinSinceMidnight = getMinSinceMidnight(dateUtil.now()) val currentMinSinceMidnight = getMinSinceMidnight(dateUtil.now())
val scheduledDayOfWeek = Calendar.getInstance()[Calendar.DAY_OF_WEEK] val scheduledDayOfWeek = Calendar.getInstance()[Calendar.DAY_OF_WEEK]
if (days.isSet(Objects.requireNonNull(InputWeekDay.DayOfWeek.fromCalendarInt(scheduledDayOfWeek)))) { if (days.isSet(Objects.requireNonNull(WeekDay.DayOfWeek.fromCalendarInt(scheduledDayOfWeek)))) {
if (currentMinSinceMidnight >= time.value && currentMinSinceMidnight - time.value < 5) { if (currentMinSinceMidnight >= time.value && currentMinSinceMidnight - time.value < 5) {
aapsLogger.debug(LTag.AUTOMATION, "Ready for execution: " + friendlyDescription()) aapsLogger.debug(LTag.AUTOMATION, "Ready for execution: " + friendlyDescription())
return true return true
@ -49,7 +50,7 @@ class TriggerRecurringTime(injector: HasAndroidInjector) : Trigger(injector) {
val data = JSONObject() val data = JSONObject()
.put("time", time.value) .put("time", time.value)
for (i in days.weekdays.indices) { for (i in days.weekdays.indices) {
data.put(InputWeekDay.DayOfWeek.values()[i].name, days.weekdays[i]) data.put(WeekDay.DayOfWeek.values()[i].name, days.weekdays[i])
} }
return data return data
} }
@ -57,7 +58,7 @@ class TriggerRecurringTime(injector: HasAndroidInjector) : Trigger(injector) {
override fun fromJSON(data: String): Trigger { override fun fromJSON(data: String): Trigger {
val o = JSONObject(data) val o = JSONObject(data)
for (i in days.weekdays.indices) for (i in days.weekdays.indices)
days.weekdays[i] = JsonHelper.safeGetBoolean(o, InputWeekDay.DayOfWeek.values()[i].name) days.weekdays[i] = JsonHelper.safeGetBoolean(o, WeekDay.DayOfWeek.values()[i].name)
if (o.has("hour")) { if (o.has("hour")) {
// do conversion from 2.5.1 format // do conversion from 2.5.1 format
val hour = JsonHelper.safeGetInt(o, "hour") val hour = JsonHelper.safeGetInt(o, "hour")
@ -78,7 +79,7 @@ class TriggerRecurringTime(injector: HasAndroidInjector) : Trigger(injector) {
var counter = 0 var counter = 0
for (i in days.getSelectedDays()) { for (i in days.getSelectedDays()) {
if (counter++ > 0) sb.append(",") if (counter++ > 0) sb.append(",")
sb.append(rh.gs(Objects.requireNonNull(InputWeekDay.DayOfWeek.fromCalendarInt(i)).shortName)) sb.append(rh.gs(Objects.requireNonNull(WeekDay.DayOfWeek.fromCalendarInt(i)).shortName))
} }
sb.append(" ") sb.append(" ")
sb.append(dateUtil.timeString(toMills(time.value))) sb.append(dateUtil.timeString(toMills(time.value)))

View file

@ -65,13 +65,6 @@
<string name="time_range">Time range</string> <string name="time_range">Time range</string>
<string name="timerange_value">Time is between %1$s and %2$s</string> <string name="timerange_value">Time is between %1$s and %2$s</string>
<string name="between">Between </string> <string name="between">Between </string>
<string name="weekday_sunday_short">Sun</string>
<string name="weekday_saturday_short">Sat</string>
<string name="weekday_friday_short">Fri</string>
<string name="weekday_thursday_short">Thu</string>
<string name="weekday_wednesday_short">Wed</string>
<string name="weekday_tuesday_short">Tue</string>
<string name="weekday_monday_short">Mon</string>
<string name="delete_short">DEL</string> <string name="delete_short">DEL</string>
<string name="add_short">ADD</string> <string name="add_short">ADD</string>
<string name="copy_short">COPY</string> <string name="copy_short">COPY</string>
@ -123,15 +116,6 @@
<string name="add_automation">Add rule</string> <string name="add_automation">Add rule</string>
<string name="stop_processing">Stop processing</string> <string name="stop_processing">Stop processing</string>
<!-- WeekdayPicker -->
<string name="monday_short">M</string>
<string name="tuesday_short">T</string>
<string name="wednesday_short">W</string>
<string name="thursday_short">T</string>
<string name="friday_short">F</string>
<string name="saturday_short">S</string>
<string name="sunday_short">S</string>
<!-- Reminders--> <!-- Reminders-->
<string name="time_to_eat">Time to eat!\nRun Bolus wizard and do calculation again.</string> <string name="time_to_eat">Time to eat!\nRun Bolus wizard and do calculation again.</string>
<string name="time_to_bolus">Time to bolus!\nRun Bolus wizard and do calculation again.</string> <string name="time_to_bolus">Time to bolus!\nRun Bolus wizard and do calculation again.</string>