weekday picker working without view
This commit is contained in:
parent
7d80176c75
commit
c7e261d0e6
8 changed files with 182 additions and 12 deletions
|
@ -1,12 +1,10 @@
|
||||||
package info.nightscout.androidaps.plugins.general.automation.elements
|
package info.nightscout.androidaps.plugins.general.automation.elements
|
||||||
|
|
||||||
import android.view.View
|
|
||||||
import android.view.ViewGroup
|
|
||||||
import android.widget.LinearLayout
|
import android.widget.LinearLayout
|
||||||
import androidx.annotation.StringRes
|
import androidx.annotation.StringRes
|
||||||
import com.dpro.widgets.WeekdaysPicker
|
|
||||||
import dagger.android.HasAndroidInjector
|
import dagger.android.HasAndroidInjector
|
||||||
import info.nightscout.androidaps.R
|
import info.nightscout.androidaps.R
|
||||||
|
import info.nightscout.androidaps.utils.ui.WeekdayPicker
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
class InputWeekDay(injector: HasAndroidInjector) : Element(injector) {
|
class InputWeekDay(injector: HasAndroidInjector) : Element(injector) {
|
||||||
|
@ -22,6 +20,7 @@ class InputWeekDay(injector: HasAndroidInjector) : Element(injector) {
|
||||||
get() = shortNames[ordinal]
|
get() = shortNames[ordinal]
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|
||||||
private val calendarInts = intArrayOf(
|
private val calendarInts = intArrayOf(
|
||||||
Calendar.MONDAY,
|
Calendar.MONDAY,
|
||||||
Calendar.TUESDAY,
|
Calendar.TUESDAY,
|
||||||
|
@ -56,7 +55,7 @@ class InputWeekDay(injector: HasAndroidInjector) : Element(injector) {
|
||||||
for (day in DayOfWeek.values()) set(day, false)
|
for (day in DayOfWeek.values()) set(day, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setAll(value:Boolean) {
|
fun setAll(value: Boolean) {
|
||||||
for (day in DayOfWeek.values()) set(day, value)
|
for (day in DayOfWeek.values()) set(day, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,13 +77,11 @@ class InputWeekDay(injector: HasAndroidInjector) : Element(injector) {
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun addToLayout(root: LinearLayout) {
|
override fun addToLayout(root: LinearLayout) {
|
||||||
val weekdaysPicker = WeekdaysPicker(root.context)
|
WeekdayPicker(root.context).apply {
|
||||||
weekdaysPicker.setEditable(true)
|
setSelectedDays(getSelectedDays())
|
||||||
weekdaysPicker.selectedDays = getSelectedDays()
|
setOnWeekdaysChangeListener { i: Int, selected: Boolean -> set(DayOfWeek.fromCalendarInt(i), selected) }
|
||||||
weekdaysPicker.setOnWeekdaysChangeListener { _: View?, i: Int, list: List<Int?> -> set(DayOfWeek.fromCalendarInt(i), list.contains(i)) }
|
root.addView(this)
|
||||||
weekdaysPicker.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
|
}
|
||||||
weekdaysPicker.sundayFirstDay = Calendar.getInstance().firstDayOfWeek == Calendar.SUNDAY
|
// TODO: remove library and dependency statement
|
||||||
weekdaysPicker.redrawDays()
|
|
||||||
root.addView(weekdaysPicker)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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<Int>) {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
5
core/src/main/res/color/weekday_background.xml
Normal file
5
core/src/main/res/color/weekday_background.xml
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<selector xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
<item android:state_checked="true" android:color="@color/weekdaySelected"/>
|
||||||
|
<item android:color="@color/weekdayNotSelected"/>
|
||||||
|
</selector>
|
5
core/src/main/res/color/weekend_background.xml
Normal file
5
core/src/main/res/color/weekend_background.xml
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<selector xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
<item android:state_checked="true" android:color="@color/weekendSelected"/>
|
||||||
|
<item android:color="@color/weekendNotSelected"/>
|
||||||
|
</selector>
|
|
@ -0,0 +1,7 @@
|
||||||
|
<shape xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:shape="oval">
|
||||||
|
<solid android:color="@color/weekdaySelected"/>
|
||||||
|
<stroke
|
||||||
|
android:width="2dp"
|
||||||
|
android:color="@color/weekday_background"/>
|
||||||
|
</shape>
|
74
core/src/main/res/layout/weekday_picker.xml
Normal file
74
core/src/main/res/layout/weekday_picker.xml
Normal file
|
@ -0,0 +1,74 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:orientation="horizontal">
|
||||||
|
|
||||||
|
<androidx.appcompat.widget.AppCompatCheckedTextView
|
||||||
|
android:id="@+id/weekdayPickerSundayStart"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:text="@string/sunday_short"
|
||||||
|
app:autoSizeTextType="uniform" />
|
||||||
|
|
||||||
|
<androidx.appcompat.widget.AppCompatCheckedTextView
|
||||||
|
android:id="@+id/weekdayPickerMonday"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:text="@string/monday_short"
|
||||||
|
android:gravity="center"
|
||||||
|
android:background="@drawable/weekday_circle_brackground"
|
||||||
|
app:autoSizeTextType="uniform" />
|
||||||
|
|
||||||
|
<androidx.appcompat.widget.AppCompatCheckedTextView
|
||||||
|
android:id="@+id/weekdayPickerTuesday"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:text="@string/tuesday_short"
|
||||||
|
app:autoSizeTextType="uniform" />
|
||||||
|
|
||||||
|
<androidx.appcompat.widget.AppCompatCheckedTextView
|
||||||
|
android:id="@+id/weekdayPickerWednesday"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:text="@string/wednesday_short"
|
||||||
|
app:autoSizeTextType="uniform" />
|
||||||
|
|
||||||
|
<androidx.appcompat.widget.AppCompatCheckedTextView
|
||||||
|
android:id="@+id/weekdayPickerThursday"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:text="@string/thursday_short"
|
||||||
|
app:autoSizeTextType="uniform" />
|
||||||
|
|
||||||
|
<androidx.appcompat.widget.AppCompatCheckedTextView
|
||||||
|
android:id="@+id/weekdayPickerFriday"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:text="@string/friday_short"
|
||||||
|
app:autoSizeTextType="uniform" />
|
||||||
|
|
||||||
|
<androidx.appcompat.widget.AppCompatCheckedTextView
|
||||||
|
android:id="@+id/weekdayPickerSaturday"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:text="@string/saturday_short"
|
||||||
|
app:autoSizeTextType="uniform" />
|
||||||
|
|
||||||
|
<androidx.appcompat.widget.AppCompatCheckedTextView
|
||||||
|
android:id="@+id/weekdayPickerSundayEnd"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:text="@string/sunday_short"
|
||||||
|
app:autoSizeTextType="uniform" />
|
||||||
|
|
||||||
|
</LinearLayout>
|
|
@ -41,6 +41,11 @@
|
||||||
<color name="zt">#00d2d2</color>
|
<color name="zt">#00d2d2</color>
|
||||||
<color name="white">#ffffff</color>
|
<color name="white">#ffffff</color>
|
||||||
|
|
||||||
|
<color name="weekdaySelected">#8000FF00</color>
|
||||||
|
<color name="weekdayNotSelected">#1000FF00</color>
|
||||||
|
<color name="weekendSelected">#800000FF</color>
|
||||||
|
<color name="weekendNotSelected">#100000FF</color>
|
||||||
|
|
||||||
<!-- CareportalEvent-->
|
<!-- CareportalEvent-->
|
||||||
<color name="notificationAnnouncement">#FF8C00</color>
|
<color name="notificationAnnouncement">#FF8C00</color>
|
||||||
|
|
||||||
|
|
|
@ -315,6 +315,15 @@
|
||||||
<!-- Permissions -->
|
<!-- Permissions -->
|
||||||
<string name="alert_dialog_storage_permission_text">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)!</string>
|
<string name="alert_dialog_storage_permission_text">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)!</string>
|
||||||
|
|
||||||
|
<!-- WeekdayPicker -->
|
||||||
|
<string name="monday_short">M</string>
|
||||||
|
<string name="tuesday_short">T</string>
|
||||||
|
<string name="wednesday_short">W</string>
|
||||||
|
<string name="thursday_short">T</string>
|
||||||
|
<string name="friday_short">F</string>
|
||||||
|
<string name="saturday_short">S</string>
|
||||||
|
<string name="sunday_short">S</string>
|
||||||
|
|
||||||
<plurals name="days">
|
<plurals name="days">
|
||||||
<item quantity="one">%1$d day</item>
|
<item quantity="one">%1$d day</item>
|
||||||
<item quantity="other">%1$d days</item>
|
<item quantity="other">%1$d days</item>
|
||||||
|
|
Loading…
Reference in a new issue