layout CarbDialog alarm and use timer for automation alarms

This commit is contained in:
AdrianLxM 2021-02-15 23:55:57 +01:00
parent b9bf699942
commit 2f05c431aa
4 changed files with 174 additions and 120 deletions

View file

@ -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()
}

View file

@ -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 {

View file

@ -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)
}
}

View file

@ -75,121 +75,161 @@
</LinearLayout>
<LinearLayout
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:paddingEnd="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:width="120dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="@string/time_offset"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textStyle="bold" />
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<info.nightscout.androidaps.utils.ui.MinutesNumberPicker
android:id="@+id/time"
android:layout_width="130dp"
android:layout_height="40dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="start"
android:minWidth="45dp"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:text="@string/unit_minute_short"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:width="120dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="@string/time_offset"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textStyle="bold" />
<CheckBox
android:id="@+id/alarmCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:checked="false"
android:padding="2dp" />
<CheckBox
android:id="@+id/alarmCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:checked="false"
android:padding="2dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ic_access_alarm_24dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="8dp"
android:src="@drawable/ic_access_alarm_24dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<info.nightscout.androidaps.utils.ui.MinutesNumberPicker
android:id="@+id/time"
android:layout_width="130dp"
android:layout_height="40dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:width="120dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="@string/careportal_newnstreatment_duration_label"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="start"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:text="@string/unit_minute_short"
android:textAppearance="?android:attr/textAppearanceSmall" />
<info.nightscout.androidaps.utils.ui.NumberPicker
android:id="@+id/duration"
android:layout_width="130dp"
android:layout_height="40dp" />
</LinearLayout>
</TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="start"
android:minWidth="45dp"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:text="@string/shorthour"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:width="120dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="@string/careportal_newnstreatment_duration_label"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:width="120dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="@string/treatments_wizard_carbs_label"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textStyle="bold" />
</LinearLayout>
<info.nightscout.androidaps.utils.ui.NumberPicker
android:id="@+id/carbs"
android:layout_width="130dp"
android:layout_height="40dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="start"
android:minWidth="45dp"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:text="@string/shortgramm"
android:textAppearance="?android:attr/textAppearanceSmall" />
<info.nightscout.androidaps.utils.ui.NumberPicker
android:id="@+id/duration"
android:layout_width="130dp"
android:layout_height="40dp" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="start"
android:minWidth="45dp"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:text="@string/shorthour"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:width="120dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="@string/treatments_wizard_carbs_label"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<info.nightscout.androidaps.utils.ui.NumberPicker
android:id="@+id/carbs"
android:layout_width="130dp"
android:layout_height="40dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="start"
android:minWidth="45dp"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:text="@string/shortgramm"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</TableRow>
</TableLayout>
<LinearLayout
android:layout_width="match_parent"