Merge pull request #332 from nightscout/adrian/own-weekday-view

Own weekday picker (library is crashing in dev)
This commit is contained in:
Milos Kozak 2021-02-13 22:16:00 +01:00 committed by GitHub
commit 400f69d4b4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 244 additions and 15 deletions

View file

@ -241,9 +241,6 @@ dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs') implementation fileTree(include: ['*.jar'], dir: 'libs')
// https://github.com/DavidProdinger/Weekdays-Selector (used outdated 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1')
implementation(name: 'weekdaysselector-1.1.1', ext: 'aar')
testImplementation "junit:junit:$junit_version" testImplementation "junit:junit:$junit_version"
testImplementation 'org.json:json:20201115' testImplementation 'org.json:json:20201115'
testImplementation "org.mockito:mockito-core:${mockitoVersion}" testImplementation "org.mockito:mockito-core:${mockitoVersion}"

Binary file not shown.

View file

@ -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) {
@ -78,13 +76,10 @@ 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
weekdaysPicker.redrawDays()
root.addView(weekdaysPicker)
} }
} }

View file

@ -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 androidx.appcompat.widget.AppCompatCheckedTextView
import androidx.constraintlayout.widget.ConstraintLayout
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
) : ConstraintLayout(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>) = 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() = 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 {
val checkable = it as Checkable
val checked = checkable.isChecked
checkable.isChecked = !checked
changeListener?.invoke(day, !checked)
}
}

View 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/daySelected"/>
<item android:color="@android:color/transparent"/>
</selector>

View file

@ -0,0 +1,7 @@
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/day_selection_background"/>
<stroke
android:width="4dp"
android:color="@color/weekdayOutline"/>
</shape>

View file

@ -0,0 +1,7 @@
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/day_selection_background"/>
<stroke
android:width="4dp"
android:color="@color/weekendOutline"/>
</shape>

View file

@ -0,0 +1,137 @@
<?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"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
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:padding="8dp"
android:text="@string/sunday_short"
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_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:padding="8dp"
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" />
<androidx.appcompat.widget.AppCompatCheckedTextView
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:padding="8dp"
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" />
<androidx.appcompat.widget.AppCompatCheckedTextView
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:padding="8dp"
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" />
<androidx.appcompat.widget.AppCompatCheckedTextView
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:padding="8dp"
android:text="@string/thursday_short"
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_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:padding="8dp"
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" />
<androidx.appcompat.widget.AppCompatCheckedTextView
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:padding="8dp"
android:text="@string/saturday_short"
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_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:padding="8dp"
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" />
</androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -41,6 +41,10 @@
<color name="zt">#00d2d2</color> <color name="zt">#00d2d2</color>
<color name="white">#ffffff</color> <color name="white">#ffffff</color>
<color name="daySelected">#D000FF00</color>
<color name="weekdayOutline">#1ea3e5</color>
<color name="weekendOutline">#1e88e5</color>
<!-- CareportalEvent--> <!-- CareportalEvent-->
<color name="notificationAnnouncement">#FF8C00</color> <color name="notificationAnnouncement">#FF8C00</color>

View file

@ -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>