cleanups of new weekday selector

This commit is contained in:
AdrianLxM 2021-02-13 19:40:27 +01:00
parent 7c8caa659b
commit 0d601634e0
3 changed files with 59 additions and 63 deletions

View file

@ -20,7 +20,6 @@ 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,
@ -82,6 +81,5 @@ class InputWeekDay(injector: HasAndroidInjector) : Element(injector) {
setOnWeekdaysChangeListener { i: Int, selected: Boolean -> set(DayOfWeek.fromCalendarInt(i), selected) } setOnWeekdaysChangeListener { i: Int, selected: Boolean -> set(DayOfWeek.fromCalendarInt(i), selected) }
root.addView(this) root.addView(this)
} }
// TODO: remove library and dependency statement
} }
} }

View file

@ -4,7 +4,6 @@ import android.content.Context
import android.util.AttributeSet import android.util.AttributeSet
import android.view.LayoutInflater import android.view.LayoutInflater
import android.widget.Checkable import android.widget.Checkable
import android.widget.LinearLayout
import androidx.appcompat.widget.AppCompatCheckedTextView import androidx.appcompat.widget.AppCompatCheckedTextView
import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintLayout
import info.nightscout.androidaps.core.databinding.WeekdayPickerBinding import info.nightscout.androidaps.core.databinding.WeekdayPickerBinding
@ -33,33 +32,33 @@ class WeekdayPicker @JvmOverloads constructor(
} }
} }
fun setSelectedDays(list: List<Int>) { fun setSelectedDays(list: List<Int>) = with(binding) {
binding.weekdayPickerSundayStart.isChecked = list.contains(Calendar.SUNDAY) weekdayPickerSundayStart.isChecked = list.contains(Calendar.SUNDAY)
binding.weekdayPickerSundayEnd.isChecked = list.contains(Calendar.SUNDAY) weekdayPickerSundayEnd.isChecked = list.contains(Calendar.SUNDAY)
binding.weekdayPickerMonday.isChecked = list.contains(Calendar.MONDAY) weekdayPickerMonday.isChecked = list.contains(Calendar.MONDAY)
binding.weekdayPickerTuesday.isChecked = list.contains(Calendar.TUESDAY) weekdayPickerTuesday.isChecked = list.contains(Calendar.TUESDAY)
binding.weekdayPickerWednesday.isChecked = list.contains(Calendar.WEDNESDAY) weekdayPickerWednesday.isChecked = list.contains(Calendar.WEDNESDAY)
binding.weekdayPickerThursday.isChecked = list.contains(Calendar.THURSDAY) weekdayPickerThursday.isChecked = list.contains(Calendar.THURSDAY)
binding.weekdayPickerFriday.isChecked = list.contains(Calendar.FRIDAY) weekdayPickerFriday.isChecked = list.contains(Calendar.FRIDAY)
binding.weekdayPickerSaturday.isChecked = list.contains(Calendar.SATURDAY) weekdayPickerSaturday.isChecked = list.contains(Calendar.SATURDAY)
} }
private fun setupClickListeners() { private fun setupClickListeners() = with(binding) {
binding.weekdayPickerSundayStart.setupCallbackFor(Calendar.SUNDAY) weekdayPickerSundayStart.setupCallbackFor(Calendar.SUNDAY)
binding.weekdayPickerSundayEnd.setupCallbackFor(Calendar.SUNDAY) weekdayPickerSundayEnd.setupCallbackFor(Calendar.SUNDAY)
binding.weekdayPickerMonday.setupCallbackFor(Calendar.MONDAY) weekdayPickerMonday.setupCallbackFor(Calendar.MONDAY)
binding.weekdayPickerTuesday.setupCallbackFor(Calendar.TUESDAY) weekdayPickerTuesday.setupCallbackFor(Calendar.TUESDAY)
binding.weekdayPickerWednesday.setupCallbackFor(Calendar.WEDNESDAY) weekdayPickerWednesday.setupCallbackFor(Calendar.WEDNESDAY)
binding.weekdayPickerThursday.setupCallbackFor(Calendar.THURSDAY) weekdayPickerThursday.setupCallbackFor(Calendar.THURSDAY)
binding.weekdayPickerFriday.setupCallbackFor(Calendar.FRIDAY) weekdayPickerFriday.setupCallbackFor(Calendar.FRIDAY)
binding.weekdayPickerSaturday.setupCallbackFor(Calendar.SATURDAY) weekdayPickerSaturday.setupCallbackFor(Calendar.SATURDAY)
} }
fun setOnWeekdaysChangeListener(changeListener: (Int, Boolean) -> Unit) { fun setOnWeekdaysChangeListener(changeListener: (Int, Boolean) -> Unit) {
this.changeListener = changeListener this.changeListener = changeListener
} }
private fun AppCompatCheckedTextView.setupCallbackFor(day: Int) = setOnClickListener{ private fun AppCompatCheckedTextView.setupCallbackFor(day: Int) = setOnClickListener {
val checkable = it as Checkable val checkable = it as Checkable
val checked = checkable.isChecked val checked = checkable.isChecked
checkable.isChecked = !checked checkable.isChecked = !checked

View file

@ -1,40 +1,39 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linearLayout" android:id="@+id/linearLayout"
android:padding="2dp"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content"
android:padding="2dp">
<androidx.appcompat.widget.AppCompatCheckedTextView <androidx.appcompat.widget.AppCompatCheckedTextView
android:id="@+id/weekdayPickerSundayStart" android:id="@+id/weekdayPickerSundayStart"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_margin="2dp"
android:background="@drawable/weekend_circle_brackground" android:background="@drawable/weekend_circle_brackground"
android:gravity="center" android:gravity="center"
android:textAlignment="gravity"
android:padding="8dp" android:padding="8dp"
android:layout_margin="2dp"
app:layout_constraintDimensionRatio="H,1:1"
app:autoSizeTextType="uniform"
android:text="@string/sunday_short" android:text="@string/sunday_short"
app:layout_constraintTop_toTopOf="parent" android:textAlignment="gravity"
app:autoSizeTextType="uniform"
app:layout_constraintDimensionRatio="H,1:1"
app:layout_constraintEnd_toStartOf="@+id/weekdayPickerMonday" app:layout_constraintEnd_toStartOf="@+id/weekdayPickerMonday"
app:layout_constraintStart_toStartOf="parent" /> app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatCheckedTextView <androidx.appcompat.widget.AppCompatCheckedTextView
android:id="@+id/weekdayPickerMonday" android:id="@+id/weekdayPickerMonday"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_margin="2dp"
android:background="@drawable/weekday_circle_brackground" android:background="@drawable/weekday_circle_brackground"
android:gravity="center" android:gravity="center"
android:textAlignment="gravity"
android:padding="8dp" android:padding="8dp"
android:layout_margin="2dp"
app:layout_constraintDimensionRatio="H,1:1"
app:autoSizeTextType="uniform"
android:text="@string/monday_short" android:text="@string/monday_short"
android:textAlignment="gravity"
app:autoSizeTextType="uniform"
app:layout_constraintDimensionRatio="H,1:1"
app:layout_constraintEnd_toStartOf="@+id/weekdayPickerTuesday" app:layout_constraintEnd_toStartOf="@+id/weekdayPickerTuesday"
app:layout_constraintStart_toEndOf="@+id/weekdayPickerSundayStart" app:layout_constraintStart_toEndOf="@+id/weekdayPickerSundayStart"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
@ -43,15 +42,15 @@
android:id="@+id/weekdayPickerTuesday" android:id="@+id/weekdayPickerTuesday"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_margin="2dp"
android:background="@drawable/weekday_circle_brackground" android:background="@drawable/weekday_circle_brackground"
android:gravity="center" android:gravity="center"
android:textAlignment="gravity"
android:padding="8dp" android:padding="8dp"
android:layout_margin="2dp"
app:layout_constraintDimensionRatio="H,1:1"
app:autoSizeTextType="uniform"
android:text="@string/tuesday_short" android:text="@string/tuesday_short"
android:textAlignment="gravity"
app:autoSizeTextType="uniform"
app:layout_constraintBaseline_toBaselineOf="@+id/weekdayPickerWednesday" app:layout_constraintBaseline_toBaselineOf="@+id/weekdayPickerWednesday"
app:layout_constraintDimensionRatio="H,1:1"
app:layout_constraintEnd_toStartOf="@+id/weekdayPickerWednesday" app:layout_constraintEnd_toStartOf="@+id/weekdayPickerWednesday"
app:layout_constraintStart_toEndOf="@+id/weekdayPickerMonday" /> app:layout_constraintStart_toEndOf="@+id/weekdayPickerMonday" />
@ -59,14 +58,14 @@
android:id="@+id/weekdayPickerWednesday" android:id="@+id/weekdayPickerWednesday"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_margin="2dp"
android:background="@drawable/weekday_circle_brackground" android:background="@drawable/weekday_circle_brackground"
android:gravity="center" android:gravity="center"
android:textAlignment="gravity"
android:padding="8dp" android:padding="8dp"
android:layout_margin="2dp"
app:layout_constraintDimensionRatio="H,1:1"
app:autoSizeTextType="uniform"
android:text="@string/wednesday_short" android:text="@string/wednesday_short"
android:textAlignment="gravity"
app:autoSizeTextType="uniform"
app:layout_constraintDimensionRatio="H,1:1"
app:layout_constraintEnd_toStartOf="@+id/weekdayPickerThursday" app:layout_constraintEnd_toStartOf="@+id/weekdayPickerThursday"
app:layout_constraintStart_toEndOf="@+id/weekdayPickerTuesday" app:layout_constraintStart_toEndOf="@+id/weekdayPickerTuesday"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
@ -75,30 +74,30 @@
android:id="@+id/weekdayPickerThursday" android:id="@+id/weekdayPickerThursday"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_margin="2dp"
android:background="@drawable/weekday_circle_brackground" android:background="@drawable/weekday_circle_brackground"
android:gravity="center" android:gravity="center"
android:textAlignment="gravity"
android:padding="8dp" android:padding="8dp"
android:layout_margin="2dp"
app:layout_constraintDimensionRatio="H,1:1"
app:autoSizeTextType="uniform"
android:text="@string/thursday_short" android:text="@string/thursday_short"
app:layout_constraintTop_toTopOf="parent" android:textAlignment="gravity"
app:autoSizeTextType="uniform"
app:layout_constraintDimensionRatio="H,1:1"
app:layout_constraintEnd_toStartOf="@+id/weekdayPickerFriday" app:layout_constraintEnd_toStartOf="@+id/weekdayPickerFriday"
app:layout_constraintStart_toEndOf="@+id/weekdayPickerWednesday" /> app:layout_constraintStart_toEndOf="@+id/weekdayPickerWednesday"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatCheckedTextView <androidx.appcompat.widget.AppCompatCheckedTextView
android:id="@+id/weekdayPickerFriday" android:id="@+id/weekdayPickerFriday"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_margin="2dp"
android:background="@drawable/weekday_circle_brackground" android:background="@drawable/weekday_circle_brackground"
android:gravity="center" android:gravity="center"
android:textAlignment="gravity"
android:padding="8dp" android:padding="8dp"
android:layout_margin="2dp"
app:layout_constraintDimensionRatio="H,1:1"
app:autoSizeTextType="uniform"
android:text="@string/friday_short" android:text="@string/friday_short"
android:textAlignment="gravity"
app:autoSizeTextType="uniform"
app:layout_constraintDimensionRatio="H,1:1"
app:layout_constraintEnd_toStartOf="@+id/weekdayPickerSaturday" app:layout_constraintEnd_toStartOf="@+id/weekdayPickerSaturday"
app:layout_constraintStart_toEndOf="@+id/weekdayPickerThursday" app:layout_constraintStart_toEndOf="@+id/weekdayPickerThursday"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
@ -107,30 +106,30 @@
android:id="@+id/weekdayPickerSaturday" android:id="@+id/weekdayPickerSaturday"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_margin="2dp"
android:background="@drawable/weekend_circle_brackground" android:background="@drawable/weekend_circle_brackground"
android:gravity="center" android:gravity="center"
android:textAlignment="gravity"
android:padding="8dp" android:padding="8dp"
android:layout_margin="2dp"
app:layout_constraintDimensionRatio="H,1:1"
app:autoSizeTextType="uniform"
android:text="@string/saturday_short" android:text="@string/saturday_short"
app:layout_constraintTop_toTopOf="parent" android:textAlignment="gravity"
app:autoSizeTextType="uniform"
app:layout_constraintDimensionRatio="H,1:1"
app:layout_constraintEnd_toStartOf="@+id/weekdayPickerSundayEnd" app:layout_constraintEnd_toStartOf="@+id/weekdayPickerSundayEnd"
app:layout_constraintStart_toEndOf="@+id/weekdayPickerFriday" /> app:layout_constraintStart_toEndOf="@+id/weekdayPickerFriday"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatCheckedTextView <androidx.appcompat.widget.AppCompatCheckedTextView
android:id="@+id/weekdayPickerSundayEnd" android:id="@+id/weekdayPickerSundayEnd"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_margin="2dp"
android:background="@drawable/weekend_circle_brackground" android:background="@drawable/weekend_circle_brackground"
android:gravity="center" android:gravity="center"
android:textAlignment="gravity"
android:padding="8dp" android:padding="8dp"
android:layout_margin="2dp"
app:layout_constraintDimensionRatio="H,1:1"
app:autoSizeTextType="uniform"
android:text="@string/sunday_short" android:text="@string/sunday_short"
android:textAlignment="gravity"
app:autoSizeTextType="uniform"
app:layout_constraintDimensionRatio="H,1:1"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/weekdayPickerSaturday" app:layout_constraintStart_toEndOf="@+id/weekdayPickerSaturday"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />