From 2f05c431aa22269584dc2d610ed95df93db75a5b Mon Sep 17 00:00:00 2001 From: AdrianLxM Date: Mon, 15 Feb 2021 23:55:57 +0100 Subject: [PATCH] layout CarbDialog alarm and use timer for automation alarms --- .../general/automation/actions/ActionAlarm.kt | 18 +- .../nightscout/androidaps/utils/CarbTimer.kt | 17 +- .../nightscout/androidaps/utils/TimerUtil.kt | 25 ++ app/src/main/res/layout/dialog_carbs.xml | 234 ++++++++++-------- 4 files changed, 174 insertions(+), 120 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/utils/TimerUtil.kt diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionAlarm.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionAlarm.kt index 45480a4c3f..93411a1275 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionAlarm.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionAlarm.kt @@ -1,19 +1,20 @@ package info.nightscout.androidaps.plugins.general.automation.actions import android.content.Context -import android.content.Intent import android.widget.LinearLayout import androidx.annotation.DrawableRes import dagger.android.HasAndroidInjector import info.nightscout.androidaps.R -import info.nightscout.androidaps.activities.ErrorHelperActivity import info.nightscout.androidaps.data.PumpEnactResult import info.nightscout.androidaps.plugins.bus.RxBusWrapper import info.nightscout.androidaps.plugins.general.automation.elements.InputString import info.nightscout.androidaps.plugins.general.automation.elements.LabelWithElement import info.nightscout.androidaps.plugins.general.automation.elements.LayoutBuilder import info.nightscout.androidaps.queue.Callback +import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.JsonHelper +import info.nightscout.androidaps.utils.T +import info.nightscout.androidaps.utils.TimerUtil import info.nightscout.androidaps.utils.resources.ResourceHelper import org.json.JSONObject import javax.inject.Inject @@ -23,26 +24,23 @@ class ActionAlarm(injector: HasAndroidInjector) : Action(injector) { @Inject lateinit var resourceHelper: ResourceHelper @Inject lateinit var rxBus: RxBusWrapper @Inject lateinit var context: Context + @Inject lateinit var timerUtil: TimerUtil var text = InputString(injector) constructor(injector: HasAndroidInjector, text: String) : this(injector) { this.text = InputString(injector, text) } + override fun friendlyName(): Int = R.string.alarm override fun shortDescription(): String = resourceHelper.gs(R.string.alarm_message, text.value) @DrawableRes override fun icon(): Int = R.drawable.ic_access_alarm_24dp - override fun isValid(): Boolean = text.value.isNotEmpty() + override fun isValid(): Boolean = true // empty alarm will show app name override fun doAction(callback: Callback) { - val i = Intent(context, ErrorHelperActivity::class.java) - i.putExtra("soundid", R.raw.modern_alarm) - i.putExtra("status", text.value) - i.putExtra("title", resourceHelper.gs(R.string.alarm)) - i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - context.startActivity(i) - + timerUtil.scheduleReminder(DateUtil.now() + T.secs(10L).msecs(), text.value.takeIf { it.isNotBlank() } + ?: resourceHelper.gs(R.string.app_name)) callback.result(PumpEnactResult(injector).success(true).comment(R.string.ok))?.run() } diff --git a/app/src/main/java/info/nightscout/androidaps/utils/CarbTimer.kt b/app/src/main/java/info/nightscout/androidaps/utils/CarbTimer.kt index 77ce2b3ec3..c029b01af2 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/CarbTimer.kt +++ b/app/src/main/java/info/nightscout/androidaps/utils/CarbTimer.kt @@ -1,8 +1,5 @@ package info.nightscout.androidaps.utils -import android.content.Context -import android.content.Intent -import android.provider.AlarmClock import dagger.android.HasAndroidInjector import info.nightscout.androidaps.Constants import info.nightscout.androidaps.R @@ -21,20 +18,14 @@ import javax.inject.Singleton @Singleton class CarbTimer @Inject constructor( - private val context: Context, private val injector: HasAndroidInjector, private val resourceHelper: ResourceHelper, - private val automationPlugin: AutomationPlugin + private val automationPlugin: AutomationPlugin, + private val timerUtil: TimerUtil ) { - fun scheduleReminder(time: Long) = Intent(AlarmClock.ACTION_SET_TIMER).apply { - val length: Int = ((time - DateUtil.now()) / 1000).toInt() - flags = flags or Intent.FLAG_ACTIVITY_NEW_TASK - putExtra(AlarmClock.EXTRA_LENGTH, length) - putExtra(AlarmClock.EXTRA_SKIP_UI, true) - putExtra(AlarmClock.EXTRA_MESSAGE, resourceHelper.gs(R.string.timetoeat)) - context.startActivity(this) - } + fun scheduleReminder(time: Long, text: String? = null) = + timerUtil.scheduleReminder(time, text ?: resourceHelper.gs(R.string.timetoeat)) fun scheduleEatReminder() { val event = AutomationEvent(injector).apply { diff --git a/app/src/main/java/info/nightscout/androidaps/utils/TimerUtil.kt b/app/src/main/java/info/nightscout/androidaps/utils/TimerUtil.kt new file mode 100644 index 0000000000..4f0ce09609 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/utils/TimerUtil.kt @@ -0,0 +1,25 @@ +package info.nightscout.androidaps.utils + +import android.content.Context +import android.content.Intent +import android.provider.AlarmClock +import info.nightscout.androidaps.R +import info.nightscout.androidaps.utils.resources.ResourceHelper +import javax.inject.Inject +import javax.inject.Singleton + +@Singleton +class TimerUtil @Inject constructor( + private val context: Context, + private val resourceHelper: ResourceHelper, +) { + + fun scheduleReminder(time: Long, text: String? = null) = Intent(AlarmClock.ACTION_SET_TIMER).apply { + val length: Int = ((time - DateUtil.now()) / 1000).toInt() + flags = flags or Intent.FLAG_ACTIVITY_NEW_TASK + putExtra(AlarmClock.EXTRA_LENGTH, length) + putExtra(AlarmClock.EXTRA_SKIP_UI, true) + putExtra(AlarmClock.EXTRA_MESSAGE, text ?: resourceHelper.gs(R.string.app_name)) + context.startActivity(this) + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/dialog_carbs.xml b/app/src/main/res/layout/dialog_carbs.xml index 97f185ebdb..6b7ffdde26 100644 --- a/app/src/main/res/layout/dialog_carbs.xml +++ b/app/src/main/res/layout/dialog_carbs.xml @@ -75,121 +75,161 @@ - + android:paddingEnd="5dp"> - + - + - + - + - + + - + - + - + - + + - + - + - + - + - + - + - + + + + + + + + + + + + + + + + + + + + +