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]
companion object {
private val calendarInts = intArrayOf(
Calendar.MONDAY,
Calendar.TUESDAY,
@ -82,6 +81,5 @@ class InputWeekDay(injector: HasAndroidInjector) : Element(injector) {
setOnWeekdaysChangeListener { i: Int, selected: Boolean -> set(DayOfWeek.fromCalendarInt(i), selected) }
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.view.LayoutInflater
import android.widget.Checkable
import android.widget.LinearLayout
import androidx.appcompat.widget.AppCompatCheckedTextView
import androidx.constraintlayout.widget.ConstraintLayout
import info.nightscout.androidaps.core.databinding.WeekdayPickerBinding
@ -33,33 +32,33 @@ class WeekdayPicker @JvmOverloads constructor(
}
}
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)
fun setSelectedDays(list: List<Int>) = with(binding) {
weekdayPickerSundayStart.isChecked = list.contains(Calendar.SUNDAY)
weekdayPickerSundayEnd.isChecked = list.contains(Calendar.SUNDAY)
weekdayPickerMonday.isChecked = list.contains(Calendar.MONDAY)
weekdayPickerTuesday.isChecked = list.contains(Calendar.TUESDAY)
weekdayPickerWednesday.isChecked = list.contains(Calendar.WEDNESDAY)
weekdayPickerThursday.isChecked = list.contains(Calendar.THURSDAY)
weekdayPickerFriday.isChecked = list.contains(Calendar.FRIDAY)
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)
private fun setupClickListeners() = with(binding) {
weekdayPickerSundayStart.setupCallbackFor(Calendar.SUNDAY)
weekdayPickerSundayEnd.setupCallbackFor(Calendar.SUNDAY)
weekdayPickerMonday.setupCallbackFor(Calendar.MONDAY)
weekdayPickerTuesday.setupCallbackFor(Calendar.TUESDAY)
weekdayPickerWednesday.setupCallbackFor(Calendar.WEDNESDAY)
weekdayPickerThursday.setupCallbackFor(Calendar.THURSDAY)
weekdayPickerFriday.setupCallbackFor(Calendar.FRIDAY)
weekdayPickerSaturday.setupCallbackFor(Calendar.SATURDAY)
}
fun setOnWeekdaysChangeListener(changeListener: (Int, Boolean) -> Unit) {
this.changeListener = changeListener
}
private fun AppCompatCheckedTextView.setupCallbackFor(day: Int) = setOnClickListener{
private fun AppCompatCheckedTextView.setupCallbackFor(day: Int) = setOnClickListener {
val checkable = it as Checkable
val checked = checkable.isChecked
checkable.isChecked = !checked

View file

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