From bedccac4e37ba90feb302d2610058891b7d436be Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 17 Sep 2023 23:22:28 +0200 Subject: [PATCH] break aps -> automation dependency --- .../nightscout/core/ui/elements/WeekDay.kt | 92 ++++++++++++++++++ .../core/ui/elements}/WeekdayPicker.kt | 8 +- .../res/color/day_selection_background.xml | 0 .../drawable/weekday_circle_background.xml | 0 .../drawable/weekend_circle_background.xml | 0 .../src/main/res/layout/weekday_picker.xml | 16 ++-- core/ui/src/main/res/values/strings.xml | 19 ++++ plugins/aps/build.gradle | 1 - .../general/autotune/AutotuneFragment.kt | 4 +- .../general/autotune/AutotunePlugin.kt | 46 ++++----- .../automation/actions/ActionRunAutotune.kt | 7 +- .../automation/elements/Comparator.kt | 2 +- .../automation/elements/ComparatorConnect.kt | 2 +- .../automation/elements/ComparatorExists.kt | 2 +- .../nightscout/automation/elements/Element.kt | 5 +- .../nightscout/automation/elements/InputBg.kt | 2 +- .../automation/elements/InputButton.kt | 2 +- .../elements/InputCarePortalMenu.kt | 2 +- .../automation/elements/InputDateTime.kt | 2 +- .../automation/elements/InputDelta.kt | 2 +- .../automation/elements/InputDouble.kt | 2 +- .../automation/elements/InputDropdownMenu.kt | 2 +- .../automation/elements/InputDuration.kt | 2 +- .../automation/elements/InputInsulin.kt | 2 +- .../automation/elements/InputLocationMode.kt | 2 +- .../automation/elements/InputPercent.kt | 2 +- .../automation/elements/InputProfileName.kt | 2 +- .../automation/elements/InputString.kt | 2 +- .../automation/elements/InputTempTarget.kt | 2 +- .../automation/elements/InputTime.kt | 2 +- .../automation/elements/InputTimeRange.kt | 2 +- .../automation/elements/InputWeekDay.kt | 93 +------------------ .../automation/elements/LabelWithElement.kt | 2 +- .../automation/elements/StaticLabel.kt | 2 +- .../triggers/TriggerRecurringTime.kt | 9 +- .../src/main/res/values/strings.xml | 16 ---- 36 files changed, 183 insertions(+), 175 deletions(-) create mode 100644 core/ui/src/main/java/info/nightscout/core/ui/elements/WeekDay.kt rename {plugins/automation/src/main/java/info/nightscout/automation/ui => core/ui/src/main/java/info/nightscout/core/ui/elements}/WeekdayPicker.kt (92%) rename {plugins/automation => core/ui}/src/main/res/color/day_selection_background.xml (100%) rename plugins/automation/src/main/res/drawable/weekday_circle_brackground.xml => core/ui/src/main/res/drawable/weekday_circle_background.xml (100%) rename plugins/automation/src/main/res/drawable/weekend_circle_brackground.xml => core/ui/src/main/res/drawable/weekend_circle_background.xml (100%) rename {plugins/automation => core/ui}/src/main/res/layout/weekday_picker.xml (91%) diff --git a/core/ui/src/main/java/info/nightscout/core/ui/elements/WeekDay.kt b/core/ui/src/main/java/info/nightscout/core/ui/elements/WeekDay.kt new file mode 100644 index 0000000000..5281ffd220 --- /dev/null +++ b/core/ui/src/main/java/info/nightscout/core/ui/elements/WeekDay.kt @@ -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 { + val selectedDays: MutableList = 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 + ) + } +} diff --git a/plugins/automation/src/main/java/info/nightscout/automation/ui/WeekdayPicker.kt b/core/ui/src/main/java/info/nightscout/core/ui/elements/WeekdayPicker.kt similarity index 92% rename from plugins/automation/src/main/java/info/nightscout/automation/ui/WeekdayPicker.kt rename to core/ui/src/main/java/info/nightscout/core/ui/elements/WeekdayPicker.kt index 4b86c01ac5..f4b098c10b 100644 --- a/plugins/automation/src/main/java/info/nightscout/automation/ui/WeekdayPicker.kt +++ b/core/ui/src/main/java/info/nightscout/core/ui/elements/WeekdayPicker.kt @@ -1,13 +1,13 @@ -package info.nightscout.automation.ui +package info.nightscout.core.ui.elements import android.content.Context import android.util.AttributeSet import android.view.LayoutInflater +import android.view.View import android.widget.Checkable import androidx.appcompat.widget.AppCompatCheckedTextView import androidx.constraintlayout.widget.ConstraintLayout -import info.nightscout.automation.databinding.WeekdayPickerBinding -import info.nightscout.shared.extensions.toVisibility +import info.nightscout.core.ui.databinding.WeekdayPickerBinding import java.util.Calendar class WeekdayPicker constructor( @@ -25,6 +25,8 @@ class WeekdayPicker constructor( setupClickListeners() } + fun Boolean.toVisibility() = if (this) View.VISIBLE else View.GONE + private fun determineBeginOfWeek() { (Calendar.getInstance().firstDayOfWeek == Calendar.SUNDAY).let { binding.weekdayPickerSundayStart.visibility = it.toVisibility() diff --git a/plugins/automation/src/main/res/color/day_selection_background.xml b/core/ui/src/main/res/color/day_selection_background.xml similarity index 100% rename from plugins/automation/src/main/res/color/day_selection_background.xml rename to core/ui/src/main/res/color/day_selection_background.xml diff --git a/plugins/automation/src/main/res/drawable/weekday_circle_brackground.xml b/core/ui/src/main/res/drawable/weekday_circle_background.xml similarity index 100% rename from plugins/automation/src/main/res/drawable/weekday_circle_brackground.xml rename to core/ui/src/main/res/drawable/weekday_circle_background.xml diff --git a/plugins/automation/src/main/res/drawable/weekend_circle_brackground.xml b/core/ui/src/main/res/drawable/weekend_circle_background.xml similarity index 100% rename from plugins/automation/src/main/res/drawable/weekend_circle_brackground.xml rename to core/ui/src/main/res/drawable/weekend_circle_background.xml diff --git a/plugins/automation/src/main/res/layout/weekday_picker.xml b/core/ui/src/main/res/layout/weekday_picker.xml similarity index 91% rename from plugins/automation/src/main/res/layout/weekday_picker.xml rename to core/ui/src/main/res/layout/weekday_picker.xml index f20fec423d..3e6ab00ff0 100644 --- a/plugins/automation/src/main/res/layout/weekday_picker.xml +++ b/core/ui/src/main/res/layout/weekday_picker.xml @@ -11,7 +11,7 @@ android:layout_width="0dp" android:layout_height="0dp" android:layout_margin="2dp" - android:background="@drawable/weekend_circle_brackground" + android:background="@drawable/weekend_circle_background" android:gravity="center" android:padding="8dp" android:text="@string/sunday_short" @@ -27,7 +27,7 @@ android:layout_width="0dp" android:layout_height="0dp" android:layout_margin="2dp" - android:background="@drawable/weekday_circle_brackground" + android:background="@drawable/weekday_circle_background" android:gravity="center" android:padding="8dp" android:text="@string/monday_short" @@ -43,7 +43,7 @@ android:layout_width="0dp" android:layout_height="0dp" android:layout_margin="2dp" - android:background="@drawable/weekday_circle_brackground" + android:background="@drawable/weekday_circle_background" android:gravity="center" android:padding="8dp" android:text="@string/tuesday_short" @@ -59,7 +59,7 @@ android:layout_width="0dp" android:layout_height="0dp" android:layout_margin="2dp" - android:background="@drawable/weekday_circle_brackground" + android:background="@drawable/weekday_circle_background" android:gravity="center" android:padding="8dp" android:text="@string/wednesday_short" @@ -75,7 +75,7 @@ android:layout_width="0dp" android:layout_height="0dp" android:layout_margin="2dp" - android:background="@drawable/weekday_circle_brackground" + android:background="@drawable/weekday_circle_background" android:gravity="center" android:padding="8dp" android:text="@string/thursday_short" @@ -91,7 +91,7 @@ android:layout_width="0dp" android:layout_height="0dp" android:layout_margin="2dp" - android:background="@drawable/weekday_circle_brackground" + android:background="@drawable/weekday_circle_background" android:gravity="center" android:padding="8dp" android:text="@string/friday_short" @@ -107,7 +107,7 @@ android:layout_width="0dp" android:layout_height="0dp" android:layout_margin="2dp" - android:background="@drawable/weekend_circle_brackground" + android:background="@drawable/weekend_circle_background" android:gravity="center" android:padding="8dp" android:text="@string/saturday_short" @@ -123,7 +123,7 @@ android:layout_width="0dp" android:layout_height="0dp" android:layout_margin="2dp" - android:background="@drawable/weekend_circle_brackground" + android:background="@drawable/weekend_circle_background" android:gravity="center" android:padding="8dp" android:text="@string/sunday_short" diff --git a/core/ui/src/main/res/values/strings.xml b/core/ui/src/main/res/values/strings.xml index ec3ae4661b..5965f8ec64 100644 --- a/core/ui/src/main/res/values/strings.xml +++ b/core/ui/src/main/res/values/strings.xml @@ -630,4 +630,23 @@ 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. Cleared entries + + Sun + Sat + Fri + Thu + Wed + Tue + Mon + + + M + T + W + T + F + S + S + + diff --git a/plugins/aps/build.gradle b/plugins/aps/build.gradle index d191d8afe0..05bda637bf 100644 --- a/plugins/aps/build.gradle +++ b/plugins/aps/build.gradle @@ -23,7 +23,6 @@ dependencies { implementation project(':core:utils') implementation project(':core:ui') implementation project(':core:validators') - implementation project(':plugins:automation') testImplementation project(':app-wear-shared:shared-tests') diff --git a/plugins/aps/src/main/java/info/nightscout/plugins/general/autotune/AutotuneFragment.kt b/plugins/aps/src/main/java/info/nightscout/plugins/general/autotune/AutotuneFragment.kt index 53f21fb92e..85e90953af 100644 --- a/plugins/aps/src/main/java/info/nightscout/plugins/general/autotune/AutotuneFragment.kt +++ b/plugins/aps/src/main/java/info/nightscout/plugins/general/autotune/AutotuneFragment.kt @@ -19,9 +19,9 @@ import android.widget.TableRow import android.widget.TextView import dagger.android.HasAndroidInjector import dagger.android.support.DaggerFragment -import info.nightscout.automation.elements.InputWeekDay import info.nightscout.core.profile.ProfileSealed import info.nightscout.core.ui.dialogs.OKDialog +import info.nightscout.core.ui.elements.WeekDay import info.nightscout.core.utils.fabric.FabricPrivacy import info.nightscout.database.entities.UserEntry import info.nightscout.database.entities.ValueWithUnit @@ -112,7 +112,7 @@ class AutotuneFragment : DaggerFragment() { if (autotunePlugin.calculationRunning) days.view?.setSelectedDays(days.getSelectedDays()) else { - days.set(InputWeekDay.DayOfWeek.fromCalendarInt(i), selected) + days.set(WeekDay.DayOfWeek.fromCalendarInt(i), selected) resetParam(false) updateGui() } diff --git a/plugins/aps/src/main/java/info/nightscout/plugins/general/autotune/AutotunePlugin.kt b/plugins/aps/src/main/java/info/nightscout/plugins/general/autotune/AutotunePlugin.kt index 4d4449c814..46bd06c7ff 100644 --- a/plugins/aps/src/main/java/info/nightscout/plugins/general/autotune/AutotunePlugin.kt +++ b/plugins/aps/src/main/java/info/nightscout/plugins/general/autotune/AutotunePlugin.kt @@ -1,10 +1,12 @@ +@file:Suppress("SpellCheckingInspection") + package info.nightscout.plugins.general.autotune import android.view.View import dagger.android.HasAndroidInjector -import info.nightscout.automation.elements.InputWeekDay import info.nightscout.core.extensions.pureProfileFromJson import info.nightscout.core.profile.ProfileSealed +import info.nightscout.core.ui.elements.WeekDay import info.nightscout.database.entities.UserEntry import info.nightscout.database.entities.ValueWithUnit import info.nightscout.interfaces.Config @@ -38,7 +40,6 @@ import org.json.JSONObject import java.util.TimeZone import javax.inject.Inject 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.) @@ -87,7 +88,7 @@ class AutotunePlugin @Inject constructor( @Volatile var tunedProfile: ATProfile? = null private var preppedGlucose: PreppedGlucose? = null private lateinit var profile: Profile - val days = InputWeekDay() + val days = WeekDay() val autotuneStartHour: Int = 4 override fun aapsAutotune(daysBack: Int, autoSwitch: Boolean, profileToTune: String, weekDays: BooleanArray?) { @@ -104,10 +105,9 @@ class AutotunePlugin @Inject constructor( val calcDays = calcDays(daysBack) val sb = StringBuilder() sb.append("Selected days: ") - var counter = 0 - for (i in days.getSelectedDays()) { - if (counter++ > 0) sb.append(",") - sb.append(InputWeekDay.DayOfWeek.fromCalendarInt(i)) + for ((counter, i) in days.getSelectedDays().withIndex()) { + if (counter > 0) sb.append(",") + sb.append(WeekDay.DayOfWeek.fromCalendarInt(i)) } log(sb.toString()) tunedProfile = null @@ -143,8 +143,8 @@ class AutotunePlugin @Inject constructor( // Today at 4 AM var endTime = MidnightTime.calc(lastRun) + autotuneStartHour * 60 * 60 * 1000L if (endTime > lastRun) endTime -= 24 * 60 * 60 * 1000L // Check if 4 AM is before now - val starttime = endTime - daysBack * 24 * 60 * 60 * 1000L - autotuneFS.exportSettings(settings(lastRun, daysBack, starttime, endTime)) + val startTime = endTime - daysBack * 24 * 60 * 60 * 1000L + autotuneFS.exportSettings(settings(lastRun, daysBack, startTime, endTime)) tunedProfile = ATProfile(profile, localInsulin, injector).also { it.profileName = rh.gs(info.nightscout.core.ui.R.string.autotune_tunedprofile_name) } @@ -165,7 +165,7 @@ class AutotunePlugin @Inject constructor( } var currentCalcDay = 0 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 if (days.isSet(from)) { currentCalcDay++ @@ -234,9 +234,9 @@ class AutotunePlugin @Inject constructor( ValueWithUnit.SimpleString(tunedP.profileName) ) updateButtonVisibility = View.GONE - tunedP.profileStore(circadian)?.let { profilestore -> + tunedP.profileStore(circadian)?.let { profileStore -> if (profileFunction.createProfileSwitch( - profilestore, + profileStore, profileName = tunedP.profileName, durationInMinutes = 0, percentage = 100, @@ -301,13 +301,13 @@ class AutotunePlugin @Inject constructor( 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 = "" val jsonSettings = JSONObject() val insulinInterface = activePlugin.activeInsulin val utcOffset = T.msecs(TimeZone.getDefault().getOffset(dateUtil.now()).toLong()).hours() - val startDateString = dateUtil.toISOString(firstloopstart).substring(0, 10) - val endDateString = dateUtil.toISOString(lastloopend - 24 * 60 * 60 * 1000L).substring(0, 10) + val startDateString = dateUtil.toISOString(firstLoopStart).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 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 "" @@ -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("tune_insulin_curve", false) - val peaktime: Int = insulinInterface.peak + val peakTime: Int = insulinInterface.peak if (insulinInterface.id === Insulin.InsulinType.OREF_ULTRA_RAPID_ACTING) jsonSettings.put("curve", "ultra-rapid") 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) { jsonSettings.put("curve", "ultra-rapid") jsonSettings.put("useCustomPeakTime", true) - jsonSettings.put("insulinPeakTime", peaktime) + jsonSettings.put("insulinPeakTime", peakTime) } 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("insulinPeakTime", peaktime) + jsonSettings.put("insulinPeakTime", peakTime) } jsonString = jsonSettings.toString(4).replace("\\/", "/") } catch (e: JSONException) { @@ -392,7 +392,7 @@ class AutotunePlugin @Inject constructor( } } 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("updateButtonVisibility", updateButtonVisibility) @@ -429,7 +429,7 @@ class AutotunePlugin @Inject constructor( } } 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", "") updateButtonVisibility = JsonHelper.safeGetInt(json, "updateButtonVisibility") lastRunSuccess = true @@ -441,10 +441,10 @@ class AutotunePlugin @Inject constructor( fun calcDays(daysBack:Int): Int { 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 - val starttime = endTime - daysBack * T.days(1).msecs() + val startTime = endTime - daysBack * T.days(1).msecs() var result = 0 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++ } return result diff --git a/plugins/automation/src/main/java/info/nightscout/automation/actions/ActionRunAutotune.kt b/plugins/automation/src/main/java/info/nightscout/automation/actions/ActionRunAutotune.kt index f386c222ae..c2dceab6dc 100644 --- a/plugins/automation/src/main/java/info/nightscout/automation/actions/ActionRunAutotune.kt +++ b/plugins/automation/src/main/java/info/nightscout/automation/actions/ActionRunAutotune.kt @@ -8,6 +8,7 @@ import info.nightscout.automation.elements.InputProfileName import info.nightscout.automation.elements.InputWeekDay import info.nightscout.automation.elements.LabelWithElement import info.nightscout.automation.elements.LayoutBuilder +import info.nightscout.core.ui.elements.WeekDay import info.nightscout.interfaces.autotune.Autotune import info.nightscout.interfaces.plugin.ActivePlugin import info.nightscout.interfaces.profile.ProfileFunction @@ -28,7 +29,7 @@ class ActionRunAutotune(injector: HasAndroidInjector) : Action(injector) { @Inject lateinit var activePlugin: ActivePlugin @Inject lateinit var sp: SP - var defaultValue = 0 + private var defaultValue = 0 private var inputProfileName = InputProfileName(rh, activePlugin, "", true) private var daysBack = InputDuration(0, InputDuration.TimeUnit.DAYS) private val days = InputWeekDay().also { it.setAll(true) } @@ -77,7 +78,7 @@ class ActionRunAutotune(injector: HasAndroidInjector) : Action(injector) { .put("profileToTune", inputProfileName.value) .put("tunedays", daysBack.value) 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() .put("type", this.javaClass.simpleName) @@ -88,7 +89,7 @@ class ActionRunAutotune(injector: HasAndroidInjector) : Action(injector) { override fun fromJSON(data: String): Action { val o = JSONObject(data) 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", "") defaultValue = JsonHelper.safeGetInt(o, "tunedays") if (defaultValue == 0) diff --git a/plugins/automation/src/main/java/info/nightscout/automation/elements/Comparator.kt b/plugins/automation/src/main/java/info/nightscout/automation/elements/Comparator.kt index 81bf666597..6fba8d942e 100644 --- a/plugins/automation/src/main/java/info/nightscout/automation/elements/Comparator.kt +++ b/plugins/automation/src/main/java/info/nightscout/automation/elements/Comparator.kt @@ -10,7 +10,7 @@ import androidx.annotation.StringRes import info.nightscout.automation.R import info.nightscout.shared.interfaces.ResourceHelper -class Comparator(private val rh: ResourceHelper) : Element() { +class Comparator(private val rh: ResourceHelper) : Element { enum class Compare { IS_LESSER, diff --git a/plugins/automation/src/main/java/info/nightscout/automation/elements/ComparatorConnect.kt b/plugins/automation/src/main/java/info/nightscout/automation/elements/ComparatorConnect.kt index 61f4314796..63edc159ee 100644 --- a/plugins/automation/src/main/java/info/nightscout/automation/elements/ComparatorConnect.kt +++ b/plugins/automation/src/main/java/info/nightscout/automation/elements/ComparatorConnect.kt @@ -10,7 +10,7 @@ import androidx.annotation.StringRes import info.nightscout.automation.R import info.nightscout.shared.interfaces.ResourceHelper -class ComparatorConnect(private val rh: ResourceHelper) : Element() { +class ComparatorConnect(private val rh: ResourceHelper) : Element { enum class Compare { ON_CONNECT, ON_DISCONNECT; diff --git a/plugins/automation/src/main/java/info/nightscout/automation/elements/ComparatorExists.kt b/plugins/automation/src/main/java/info/nightscout/automation/elements/ComparatorExists.kt index 3b9ac38f53..d3c38abe32 100644 --- a/plugins/automation/src/main/java/info/nightscout/automation/elements/ComparatorExists.kt +++ b/plugins/automation/src/main/java/info/nightscout/automation/elements/ComparatorExists.kt @@ -9,7 +9,7 @@ import android.widget.Spinner import androidx.annotation.StringRes 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 { EXISTS, NOT_EXISTS; diff --git a/plugins/automation/src/main/java/info/nightscout/automation/elements/Element.kt b/plugins/automation/src/main/java/info/nightscout/automation/elements/Element.kt index 0e980bf2d6..0f4e512b51 100644 --- a/plugins/automation/src/main/java/info/nightscout/automation/elements/Element.kt +++ b/plugins/automation/src/main/java/info/nightscout/automation/elements/Element.kt @@ -2,8 +2,7 @@ package info.nightscout.automation.elements import android.widget.LinearLayout -abstract class Element { - - abstract fun addToLayout(root: LinearLayout) +interface Element { + fun addToLayout(root: LinearLayout) } \ No newline at end of file diff --git a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputBg.kt b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputBg.kt index 0723a9d595..7fe302f257 100644 --- a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputBg.kt +++ b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputBg.kt @@ -7,7 +7,7 @@ import info.nightscout.interfaces.GlucoseUnit import info.nightscout.interfaces.profile.ProfileFunction import java.text.DecimalFormat -class InputBg(profileFunction: ProfileFunction) : Element() { +class InputBg(profileFunction: ProfileFunction) : Element { var units = GlucoseUnit.MGDL var value = 0.0 diff --git a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputButton.kt b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputButton.kt index c51dc518c9..3841de824e 100644 --- a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputButton.kt +++ b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputButton.kt @@ -4,7 +4,7 @@ import android.view.Gravity import android.widget.Button import android.widget.LinearLayout -class InputButton() : Element() { +class InputButton() : Element { var text: String? = null var runnable: Runnable? = null diff --git a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputCarePortalMenu.kt b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputCarePortalMenu.kt index 1a53741d1a..5da10bfe52 100644 --- a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputCarePortalMenu.kt +++ b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputCarePortalMenu.kt @@ -11,7 +11,7 @@ import androidx.annotation.StringRes import info.nightscout.database.entities.TherapyEvent 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) { NOTE(TherapyEvent.Type.NOTE), diff --git a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputDateTime.kt b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputDateTime.kt index ae34ae264f..e7c4472eca 100644 --- a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputDateTime.kt +++ b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputDateTime.kt @@ -17,7 +17,7 @@ import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.utils.DateUtil 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) { val px = rh.dpToPx(10) diff --git a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputDelta.kt b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputDelta.kt index 49dab52cc9..7c4c4303e4 100644 --- a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputDelta.kt +++ b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputDelta.kt @@ -12,7 +12,7 @@ import info.nightscout.core.ui.elements.NumberPicker import info.nightscout.shared.interfaces.ResourceHelper import java.text.DecimalFormat -class InputDelta(private val rh: ResourceHelper) : Element() { +class InputDelta(private val rh: ResourceHelper) : Element { enum class DeltaType { DELTA, SHORT_AVERAGE, LONG_AVERAGE; diff --git a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputDouble.kt b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputDouble.kt index cb35f082d1..2d24edc5c7 100644 --- a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputDouble.kt +++ b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputDouble.kt @@ -5,7 +5,7 @@ import android.widget.LinearLayout import info.nightscout.core.ui.elements.NumberPicker import java.text.DecimalFormat -class InputDouble() : Element() { +class InputDouble() : Element { var value = 0.0 private var minValue = 0.0 diff --git a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputDropdownMenu.kt b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputDropdownMenu.kt index a6fd55f701..e623313100 100644 --- a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputDropdownMenu.kt +++ b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputDropdownMenu.kt @@ -8,7 +8,7 @@ import android.widget.LinearLayout import android.widget.Spinner import info.nightscout.shared.interfaces.ResourceHelper -class InputDropdownMenu(private val rh: ResourceHelper) : Element() { +class InputDropdownMenu(private val rh: ResourceHelper) : Element { private var itemList: ArrayList = ArrayList() var value: String = "" diff --git a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputDuration.kt b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputDuration.kt index dcbef5b52a..686f72a34b 100644 --- a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputDuration.kt +++ b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputDuration.kt @@ -9,7 +9,7 @@ import java.text.DecimalFormat class InputDuration( var value: Int = 0, var unit: TimeUnit = TimeUnit.MINUTES, -) : Element() { +) : Element { enum class TimeUnit { MINUTES, HOURS, DAYS diff --git a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputInsulin.kt b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputInsulin.kt index 9286b5b3f3..aa64712d91 100644 --- a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputInsulin.kt +++ b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputInsulin.kt @@ -5,7 +5,7 @@ import android.widget.LinearLayout import info.nightscout.core.ui.elements.NumberPicker import java.text.DecimalFormat -class InputInsulin() : Element() { +class InputInsulin() : Element { var value = 0.0 diff --git a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputLocationMode.kt b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputLocationMode.kt index 6b52fb3015..78258a4bc3 100644 --- a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputLocationMode.kt +++ b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputLocationMode.kt @@ -10,7 +10,7 @@ import androidx.annotation.StringRes import info.nightscout.automation.R import info.nightscout.shared.interfaces.ResourceHelper -class InputLocationMode(private val rh: ResourceHelper) : Element() { +class InputLocationMode(private val rh: ResourceHelper) : Element { enum class Mode { INSIDE, OUTSIDE, GOING_IN, GOING_OUT; diff --git a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputPercent.kt b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputPercent.kt index 97737b5d98..86c5083054 100644 --- a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputPercent.kt +++ b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputPercent.kt @@ -5,7 +5,7 @@ import android.widget.LinearLayout import info.nightscout.core.ui.elements.NumberPicker import java.text.DecimalFormat -class InputPercent() : Element() { +class InputPercent() : Element { var value: Double = 100.0 diff --git a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputProfileName.kt b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputProfileName.kt index c1e89152dc..6e1f62365c 100644 --- a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputProfileName.kt +++ b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputProfileName.kt @@ -9,7 +9,7 @@ import android.widget.Spinner import info.nightscout.interfaces.plugin.ActivePlugin 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 diff --git a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputString.kt b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputString.kt index 05c08d56c4..9894e47ef2 100644 --- a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputString.kt +++ b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputString.kt @@ -7,7 +7,7 @@ import android.view.ViewGroup import android.widget.EditText import android.widget.LinearLayout -class InputString(var value: String = "") : Element() { +class InputString(var value: String = "") : Element { private val textWatcher: TextWatcher = object : TextWatcher { override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} diff --git a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputTempTarget.kt b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputTempTarget.kt index 58d1ac103c..f06cecb96d 100644 --- a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputTempTarget.kt +++ b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputTempTarget.kt @@ -8,7 +8,7 @@ import info.nightscout.interfaces.GlucoseUnit import info.nightscout.interfaces.profile.ProfileFunction import java.text.DecimalFormat -class InputTempTarget(profileFunction: ProfileFunction) : Element() { +class InputTempTarget(profileFunction: ProfileFunction) : Element { var units: GlucoseUnit = GlucoseUnit.MGDL var value = 0.0 diff --git a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputTime.kt b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputTime.kt index e3600294f8..4a3c6be6a7 100644 --- a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputTime.kt +++ b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputTime.kt @@ -19,7 +19,7 @@ import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.utils.DateUtil 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()) diff --git a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputTimeRange.kt b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputTimeRange.kt index 3a2cb243a6..4c7c43d560 100644 --- a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputTimeRange.kt +++ b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputTimeRange.kt @@ -19,7 +19,7 @@ import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.utils.DateUtil 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 end: Int = getMinSinceMidnight(dateUtil.now()) diff --git a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputWeekDay.kt b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputWeekDay.kt index cf13f3b8a2..a8269d8f93 100644 --- a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputWeekDay.kt +++ b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputWeekDay.kt @@ -1,94 +1,5 @@ package info.nightscout.automation.elements -import android.widget.LinearLayout -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 +import info.nightscout.core.ui.elements.WeekDay -class InputWeekDay : 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 { - val selectedDays: MutableList = 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 - ) - } -} +class InputWeekDay : WeekDay(), Element diff --git a/plugins/automation/src/main/java/info/nightscout/automation/elements/LabelWithElement.kt b/plugins/automation/src/main/java/info/nightscout/automation/elements/LabelWithElement.kt index fa73ca5dc3..b52476b965 100644 --- a/plugins/automation/src/main/java/info/nightscout/automation/elements/LabelWithElement.kt +++ b/plugins/automation/src/main/java/info/nightscout/automation/elements/LabelWithElement.kt @@ -11,7 +11,7 @@ class LabelWithElement( var textPre: String = "", var textPost: String = "", var element: Element? = null, -) : Element() { +) : Element { override fun addToLayout(root: LinearLayout) { // container layout // text view pre element diff --git a/plugins/automation/src/main/java/info/nightscout/automation/elements/StaticLabel.kt b/plugins/automation/src/main/java/info/nightscout/automation/elements/StaticLabel.kt index c1d308e8b3..21d218ffed 100644 --- a/plugins/automation/src/main/java/info/nightscout/automation/elements/StaticLabel.kt +++ b/plugins/automation/src/main/java/info/nightscout/automation/elements/StaticLabel.kt @@ -7,7 +7,7 @@ import android.widget.TextView import info.nightscout.automation.triggers.Trigger import info.nightscout.shared.interfaces.ResourceHelper -class StaticLabel(private val rh: ResourceHelper) : Element() { +class StaticLabel(private val rh: ResourceHelper) : Element { var label = "" var trigger: Trigger? = null diff --git a/plugins/automation/src/main/java/info/nightscout/automation/triggers/TriggerRecurringTime.kt b/plugins/automation/src/main/java/info/nightscout/automation/triggers/TriggerRecurringTime.kt index fa7cb76dad..805cf24fa4 100644 --- a/plugins/automation/src/main/java/info/nightscout/automation/triggers/TriggerRecurringTime.kt +++ b/plugins/automation/src/main/java/info/nightscout/automation/triggers/TriggerRecurringTime.kt @@ -8,6 +8,7 @@ import info.nightscout.automation.elements.InputTime import info.nightscout.automation.elements.InputWeekDay import info.nightscout.automation.elements.LayoutBuilder import info.nightscout.automation.elements.StaticLabel +import info.nightscout.core.ui.elements.WeekDay import info.nightscout.core.utils.MidnightUtils import info.nightscout.interfaces.utils.JsonHelper import info.nightscout.interfaces.utils.MidnightTime @@ -35,7 +36,7 @@ class TriggerRecurringTime(injector: HasAndroidInjector) : Trigger(injector) { override fun shouldRun(): Boolean { val currentMinSinceMidnight = getMinSinceMidnight(dateUtil.now()) 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) { aapsLogger.debug(LTag.AUTOMATION, "Ready for execution: " + friendlyDescription()) return true @@ -49,7 +50,7 @@ class TriggerRecurringTime(injector: HasAndroidInjector) : Trigger(injector) { val data = JSONObject() .put("time", time.value) 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 } @@ -57,7 +58,7 @@ class TriggerRecurringTime(injector: HasAndroidInjector) : Trigger(injector) { override fun fromJSON(data: String): Trigger { val o = JSONObject(data) 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")) { // do conversion from 2.5.1 format val hour = JsonHelper.safeGetInt(o, "hour") @@ -78,7 +79,7 @@ class TriggerRecurringTime(injector: HasAndroidInjector) : Trigger(injector) { var counter = 0 for (i in days.getSelectedDays()) { 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(dateUtil.timeString(toMills(time.value))) diff --git a/plugins/automation/src/main/res/values/strings.xml b/plugins/automation/src/main/res/values/strings.xml index a4df0e3e1a..a80edb2723 100644 --- a/plugins/automation/src/main/res/values/strings.xml +++ b/plugins/automation/src/main/res/values/strings.xml @@ -65,13 +65,6 @@ Time range Time is between %1$s and %2$s Between - Sun - Sat - Fri - Thu - Wed - Tue - Mon DEL ADD COPY @@ -123,15 +116,6 @@ Add rule Stop processing - - M - T - W - T - F - S - S - Time to eat!\nRun Bolus wizard and do calculation again. Time to bolus!\nRun Bolus wizard and do calculation again.