From c7e261d0e6686c0e7e431d254c2081973e7e88f2 Mon Sep 17 00:00:00 2001 From: AdrianLxM Date: Sat, 13 Feb 2021 18:30:35 +0100 Subject: [PATCH] weekday picker working without view --- .../automation/elements/InputWeekDay.kt | 21 +++--- .../androidaps/utils/ui/WeekdayPicker.kt | 68 +++++++++++++++++ .../src/main/res/color/weekday_background.xml | 5 ++ .../src/main/res/color/weekend_background.xml | 5 ++ .../drawable/weekday_circle_brackground.xml | 7 ++ core/src/main/res/layout/weekday_picker.xml | 74 +++++++++++++++++++ core/src/main/res/values/colors.xml | 5 ++ core/src/main/res/values/strings.xml | 9 +++ 8 files changed, 182 insertions(+), 12 deletions(-) create mode 100644 core/src/main/java/info/nightscout/androidaps/utils/ui/WeekdayPicker.kt create mode 100644 core/src/main/res/color/weekday_background.xml create mode 100644 core/src/main/res/color/weekend_background.xml create mode 100644 core/src/main/res/drawable/weekday_circle_brackground.xml create mode 100644 core/src/main/res/layout/weekday_picker.xml diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputWeekDay.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputWeekDay.kt index 1f2531d95b..dda616de14 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputWeekDay.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputWeekDay.kt @@ -1,12 +1,10 @@ package info.nightscout.androidaps.plugins.general.automation.elements -import android.view.View -import android.view.ViewGroup import android.widget.LinearLayout import androidx.annotation.StringRes -import com.dpro.widgets.WeekdaysPicker import dagger.android.HasAndroidInjector import info.nightscout.androidaps.R +import info.nightscout.androidaps.utils.ui.WeekdayPicker import java.util.* class InputWeekDay(injector: HasAndroidInjector) : Element(injector) { @@ -22,6 +20,7 @@ class InputWeekDay(injector: HasAndroidInjector) : Element(injector) { get() = shortNames[ordinal] companion object { + private val calendarInts = intArrayOf( Calendar.MONDAY, Calendar.TUESDAY, @@ -56,7 +55,7 @@ class InputWeekDay(injector: HasAndroidInjector) : Element(injector) { for (day in DayOfWeek.values()) set(day, false) } - fun setAll(value:Boolean) { + fun setAll(value: Boolean) { for (day in DayOfWeek.values()) set(day, value) } @@ -78,13 +77,11 @@ class InputWeekDay(injector: HasAndroidInjector) : Element(injector) { } override fun addToLayout(root: LinearLayout) { - val weekdaysPicker = WeekdaysPicker(root.context) - weekdaysPicker.setEditable(true) - weekdaysPicker.selectedDays = getSelectedDays() - weekdaysPicker.setOnWeekdaysChangeListener { _: View?, i: Int, list: List -> set(DayOfWeek.fromCalendarInt(i), list.contains(i)) } - weekdaysPicker.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) - weekdaysPicker.sundayFirstDay = Calendar.getInstance().firstDayOfWeek == Calendar.SUNDAY - weekdaysPicker.redrawDays() - root.addView(weekdaysPicker) + WeekdayPicker(root.context).apply { + setSelectedDays(getSelectedDays()) + setOnWeekdaysChangeListener { i: Int, selected: Boolean -> set(DayOfWeek.fromCalendarInt(i), selected) } + root.addView(this) + } + // TODO: remove library and dependency statement } } diff --git a/core/src/main/java/info/nightscout/androidaps/utils/ui/WeekdayPicker.kt b/core/src/main/java/info/nightscout/androidaps/utils/ui/WeekdayPicker.kt new file mode 100644 index 0000000000..0ac104a070 --- /dev/null +++ b/core/src/main/java/info/nightscout/androidaps/utils/ui/WeekdayPicker.kt @@ -0,0 +1,68 @@ +package info.nightscout.androidaps.utils.ui + +import android.content.Context +import android.util.AttributeSet +import android.view.LayoutInflater +import android.widget.Checkable +import android.widget.LinearLayout +import androidx.appcompat.widget.AppCompatCheckedTextView +import info.nightscout.androidaps.core.databinding.WeekdayPickerBinding +import info.nightscout.androidaps.utils.extensions.toVisibility +import java.util.* + +class WeekdayPicker @JvmOverloads constructor( + context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 +) : LinearLayout(context, attrs, defStyleAttr) { + + private var changeListener: ((Int, Boolean) -> Unit)? = null + + private var binding: WeekdayPickerBinding + + init { + val inflater = LayoutInflater.from(context) + binding = WeekdayPickerBinding.inflate(inflater, this, true) + determineBeginOfWeek() + setupClickListeners() + } + + private fun determineBeginOfWeek() { + (Calendar.getInstance().firstDayOfWeek == Calendar.SUNDAY).let { + binding.weekdayPickerSundayStart.visibility = it.toVisibility() + binding.weekdayPickerSundayEnd.visibility = it.not().toVisibility() + } + } + + fun setSelectedDays(list: List) { + binding.weekdayPickerSundayStart.isChecked = list.contains(Calendar.SUNDAY) + binding.weekdayPickerSundayEnd.isChecked = list.contains(Calendar.SUNDAY) + binding.weekdayPickerMonday.isChecked = list.contains(Calendar.MONDAY) + binding.weekdayPickerTuesday.isChecked = list.contains(Calendar.TUESDAY) + binding.weekdayPickerWednesday.isChecked = list.contains(Calendar.WEDNESDAY) + binding.weekdayPickerThursday.isChecked = list.contains(Calendar.THURSDAY) + binding.weekdayPickerFriday.isChecked = list.contains(Calendar.FRIDAY) + binding.weekdayPickerSaturday.isChecked = list.contains(Calendar.SATURDAY) + } + + private fun setupClickListeners() { + binding.weekdayPickerSundayStart.setupCallbackFor(Calendar.SUNDAY) + binding.weekdayPickerSundayEnd.setupCallbackFor(Calendar.SUNDAY) + binding.weekdayPickerMonday.setupCallbackFor(Calendar.MONDAY) + binding.weekdayPickerTuesday.setupCallbackFor(Calendar.TUESDAY) + binding.weekdayPickerWednesday.setupCallbackFor(Calendar.WEDNESDAY) + binding.weekdayPickerThursday.setupCallbackFor(Calendar.THURSDAY) + binding.weekdayPickerFriday.setupCallbackFor(Calendar.FRIDAY) + binding.weekdayPickerSaturday.setupCallbackFor(Calendar.SATURDAY) + } + + fun setOnWeekdaysChangeListener(changeListener: (Int, Boolean) -> Unit) { + this.changeListener = changeListener + } + + private fun AppCompatCheckedTextView.setupCallbackFor(day: Int) = setOnClickListener{ + val checkable = it as Checkable + val checked = checkable.isChecked + checkable.isChecked = !checked + changeListener?.invoke(day, !checked) + } + +} \ No newline at end of file diff --git a/core/src/main/res/color/weekday_background.xml b/core/src/main/res/color/weekday_background.xml new file mode 100644 index 0000000000..b345f818d2 --- /dev/null +++ b/core/src/main/res/color/weekday_background.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/core/src/main/res/color/weekend_background.xml b/core/src/main/res/color/weekend_background.xml new file mode 100644 index 0000000000..1de73d0e17 --- /dev/null +++ b/core/src/main/res/color/weekend_background.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/core/src/main/res/drawable/weekday_circle_brackground.xml b/core/src/main/res/drawable/weekday_circle_brackground.xml new file mode 100644 index 0000000000..861dda73c2 --- /dev/null +++ b/core/src/main/res/drawable/weekday_circle_brackground.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/core/src/main/res/layout/weekday_picker.xml b/core/src/main/res/layout/weekday_picker.xml new file mode 100644 index 0000000000..82acbb829b --- /dev/null +++ b/core/src/main/res/layout/weekday_picker.xml @@ -0,0 +1,74 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/core/src/main/res/values/colors.xml b/core/src/main/res/values/colors.xml index 7842897b59..6bd15fb852 100644 --- a/core/src/main/res/values/colors.xml +++ b/core/src/main/res/values/colors.xml @@ -41,6 +41,11 @@ #00d2d2 #ffffff + #8000FF00 + #1000FF00 + #800000FF + #100000FF + #FF8C00 diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index e42bd8e553..06a686a867 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -315,6 +315,15 @@ Please reboot your phone or restart AndroidAPS from the System Settings \notherwise Android APS will not have logging (important to track and verify that the algorithms are working correctly)! + + M + T + W + T + F + S + S + %1$d day %1$d days