-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3202 from TimVroom/develop/automation-day
Add day automation
- Loading branch information
Showing
8 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
plugins/automation/src/main/kotlin/app/aaps/plugins/automation/triggers/TriggerDay.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package app.aaps.plugins.automation.triggers | ||
|
||
import android.widget.LinearLayout | ||
import app.aaps.core.interfaces.logging.LTag | ||
import app.aaps.core.ui.elements.WeekDay | ||
import app.aaps.core.utils.JsonHelper | ||
import app.aaps.plugins.automation.R | ||
import app.aaps.plugins.automation.elements.InputWeekDay | ||
import app.aaps.plugins.automation.elements.LayoutBuilder | ||
import app.aaps.plugins.automation.elements.StaticLabel | ||
import dagger.android.HasAndroidInjector | ||
import org.json.JSONObject | ||
import java.util.Calendar | ||
import java.util.Objects | ||
import java.util.Optional | ||
|
||
class TriggerDay(injector: HasAndroidInjector) : Trigger(injector) { | ||
|
||
val days = InputWeekDay() | ||
|
||
constructor(injector: HasAndroidInjector, triggerDay: TriggerDay) : this(injector) { | ||
if (days.weekdays.size >= 0) | ||
System.arraycopy(triggerDay.days.weekdays, 0, days.weekdays, 0, triggerDay.days.weekdays.size) | ||
} | ||
|
||
override fun shouldRun(): Boolean { | ||
val scheduledDayOfWeek = Calendar.getInstance()[Calendar.DAY_OF_WEEK] | ||
if (days.isSet(Objects.requireNonNull(WeekDay.DayOfWeek.fromCalendarInt(scheduledDayOfWeek)))) { | ||
aapsLogger.debug(LTag.AUTOMATION, "Ready for execution: " + friendlyDescription()) | ||
return true | ||
} | ||
aapsLogger.debug(LTag.AUTOMATION, "NOT ready for execution: " + friendlyDescription()) | ||
return false | ||
} | ||
|
||
override fun dataJSON(): JSONObject { | ||
val data = JSONObject() | ||
for (i in days.weekdays.indices) { | ||
data.put(WeekDay.DayOfWeek.entries[i].name, days.weekdays[i]) | ||
} | ||
return data | ||
} | ||
|
||
override fun fromJSON(data: String): Trigger { | ||
val o = JSONObject(data) | ||
for (i in days.weekdays.indices) { | ||
days.weekdays[i] = JsonHelper.safeGetBoolean(o, WeekDay.DayOfWeek.entries[i].name) | ||
} | ||
|
||
return this | ||
} | ||
|
||
override fun friendlyName(): Int = R.string.day | ||
|
||
override fun friendlyDescription(): String { | ||
val sb = StringBuilder() | ||
sb.append(rh.gs(R.string.every)) | ||
sb.append(" ") | ||
var counter = 0 | ||
for (i in days.getSelectedDays()) { | ||
if (counter++ > 0) sb.append(",") | ||
sb.append(rh.gs(Objects.requireNonNull(WeekDay.DayOfWeek.fromCalendarInt(i)).shortName)) | ||
} | ||
return if (counter == 0) rh.gs(R.string.never) else sb.toString() | ||
} | ||
|
||
override fun icon(): Optional<Int> = Optional.of(R.drawable.ic_calendar_days) | ||
|
||
override fun duplicate(): Trigger = TriggerDay(injector, this) | ||
|
||
override fun generateDialog(root: LinearLayout) { | ||
LayoutBuilder() | ||
.add(StaticLabel(rh, R.string.day, this)) | ||
.add(days) | ||
.build(root) | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
plugins/automation/src/main/res/drawable/ic_calendar_days.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="22dp" | ||
android:height="24dp" | ||
android:viewportWidth="22" | ||
android:viewportHeight="24"> | ||
<path | ||
android:fillColor="#FFFFFFFF" | ||
android:pathData="m7.933,2.383c0,-0.551 -0.441,-0.995 -0.99,-0.995 -0.549,0 -0.99,0.444 -0.99,0.995L5.953,4.042L4.303,4.042c-1.456,0 -2.64,1.19 -2.64,2.653L1.663,7.358 1.663,9.348 1.663,19.96c0,1.463 1.184,2.653 2.64,2.653L17.505,22.613c1.456,0 2.64,-1.19 2.64,-2.653L20.145,9.348 20.145,7.358 20.145,6.695c0,-1.463 -1.184,-2.653 -2.64,-2.653L15.854,4.042L15.854,2.383c0,-0.551 -0.441,-0.995 -0.99,-0.995 -0.549,0 -0.99,0.444 -0.99,0.995v1.658h-5.941zM3.643,9.348L6.943,9.348L6.943,11.669L3.643,11.669ZM3.643,13.659h3.3v2.653L3.643,16.312ZM8.924,13.659h3.96v2.653L8.924,16.312ZM14.864,13.659h3.3v2.653h-3.3zM18.165,11.669L14.864,11.669L14.864,9.348h3.3zM18.165,18.302v1.658c0,0.365 -0.297,0.663 -0.66,0.663h-2.64v-2.321zM12.884,18.302v2.321L8.924,20.623v-2.321zM6.943,18.302v2.321h-2.64c-0.363,0 -0.66,-0.298 -0.66,-0.663L3.643,18.302ZM12.884,11.669L8.924,11.669L8.924,9.348h3.96z" | ||
android:strokeWidth="0.0413548"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
plugins/automation/src/test/kotlin/app/aaps/plugins/automation/triggers/TriggerDayTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package app.aaps.plugins.automation.triggers | ||
|
||
import app.aaps.core.ui.elements.WeekDay | ||
import com.google.common.truth.Truth.assertThat | ||
import org.json.JSONObject | ||
import org.junit.jupiter.api.Test | ||
import org.skyscreamer.jsonassert.JSONAssert | ||
|
||
class TriggerDayTest : TriggerTestBase() { | ||
|
||
@Test fun shouldRunTest() { | ||
var t = TriggerDay(injector) | ||
t.days[WeekDay.DayOfWeek.MONDAY] = true | ||
assertThat(t.shouldRun()).isFalse() | ||
|
||
// scheduled 1 min before | ||
t = TriggerDay(injector) | ||
t.days[WeekDay.DayOfWeek.SUNDAY] = true | ||
assertThat(t.shouldRun()).isTrue() | ||
} | ||
|
||
private var dayJson = | ||
"{\"data\":{\"WEDNESDAY\":false,\"MONDAY\":false,\"THURSDAY\":false,\"SUNDAY\":true,\"TUESDAY\":false,\"FRIDAY\":false,\"SATURDAY\":false,\"type\":\"TriggerRecurringDay\"}" | ||
|
||
@Test | ||
fun toJSONTest() { | ||
val t = TriggerDay(injector) | ||
t.days[WeekDay.DayOfWeek.SUNDAY] = true | ||
JSONAssert.assertEquals(dayJson, t.toJSON(), true) | ||
} | ||
|
||
@Test | ||
fun fromJSONTest() { | ||
val t = TriggerDay(injector) | ||
val t2 = TriggerDummy(injector).instantiate(JSONObject(t.toJSON())) as TriggerDay | ||
assertThat(t2.days.getSelectedDays()).contains(WeekDay.DayOfWeek.SUNDAY) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters