mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-28 07:50:31 +05:30
Merge pull request #2729 from Isira-Seneviratne/Simplify_TimePickerPreference
Simplify TimePickerPreference.
This commit is contained in:
commit
45cbef19dd
@ -6,60 +6,39 @@ import android.util.AttributeSet
|
|||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.preference.Preference
|
import androidx.preference.Preference
|
||||||
import com.github.libretube.util.PreferenceHelper
|
import com.github.libretube.util.PreferenceHelper
|
||||||
import com.github.libretube.util.TextUtils
|
|
||||||
import com.google.android.material.timepicker.MaterialTimePicker
|
import com.google.android.material.timepicker.MaterialTimePicker
|
||||||
import com.google.android.material.timepicker.TimeFormat
|
import com.google.android.material.timepicker.TimeFormat
|
||||||
|
import java.time.LocalTime
|
||||||
|
|
||||||
class TimePickerPreference(
|
class TimePickerPreference(
|
||||||
context: Context,
|
context: Context,
|
||||||
attributeSet: AttributeSet
|
attributeSet: AttributeSet
|
||||||
) : Preference(context, attributeSet) {
|
) : Preference(context, attributeSet) {
|
||||||
override fun getSummary(): CharSequence {
|
override fun getSummary(): CharSequence {
|
||||||
val prefStr = PreferenceHelper.getString(key, "")
|
return PreferenceHelper.getString(key, DEFAULT_VALUE)
|
||||||
return if (prefStr != "") prefStr else DEFAULT_VALUE
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onClick() {
|
override fun onClick() {
|
||||||
|
val prefTime = LocalTime.parse(PreferenceHelper.getString(key, DEFAULT_VALUE))
|
||||||
val picker = MaterialTimePicker.Builder()
|
val picker = MaterialTimePicker.Builder()
|
||||||
.setInputMode(MaterialTimePicker.INPUT_MODE_CLOCK)
|
.setInputMode(MaterialTimePicker.INPUT_MODE_CLOCK)
|
||||||
.setTimeFormat(getTimeFormat())
|
.setTimeFormat(timeFormat)
|
||||||
.setHour(getHour())
|
.setHour(prefTime.hour)
|
||||||
.setMinute(getMinutes())
|
.setMinute(prefTime.minute)
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
picker.addOnPositiveButtonClickListener {
|
picker.addOnPositiveButtonClickListener {
|
||||||
val timeStr = getTimeStr(picker)
|
val timeStr = LocalTime.of(picker.hour, picker.minute).toString()
|
||||||
PreferenceHelper.putString(key, timeStr)
|
PreferenceHelper.putString(key, timeStr)
|
||||||
summary = timeStr
|
summary = timeStr
|
||||||
}
|
}
|
||||||
picker.show((context as AppCompatActivity).supportFragmentManager, null)
|
picker.show((context as AppCompatActivity).supportFragmentManager, null)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getTimeFormat(): Int {
|
private val timeFormat: Int
|
||||||
return if (is24HourFormat(context)) TimeFormat.CLOCK_24H else TimeFormat.CLOCK_12H
|
get() = if (is24HourFormat(context)) TimeFormat.CLOCK_24H else TimeFormat.CLOCK_12H
|
||||||
}
|
|
||||||
|
|
||||||
private fun getPrefStringPart(index: Int): String? {
|
|
||||||
val prefStr = PreferenceHelper.getString(key, "").split(SEPARATOR).getOrNull(index)
|
|
||||||
return if (prefStr != "") prefStr else null
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getHour(): Int {
|
|
||||||
return getPrefStringPart(0)?.toInt() ?: 0
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getMinutes(): Int {
|
|
||||||
return getPrefStringPart(1)?.toInt() ?: 0
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getTimeStr(picker: MaterialTimePicker): String {
|
|
||||||
val hour = TextUtils.toTwoDecimalsString(picker.hour)
|
|
||||||
val minute = TextUtils.toTwoDecimalsString(picker.minute)
|
|
||||||
return "$hour$SEPARATOR$minute"
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val SEPARATOR = ":"
|
|
||||||
const val DEFAULT_VALUE = "12:00"
|
const val DEFAULT_VALUE = "12:00"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,10 +22,6 @@ object TextUtils {
|
|||||||
*/
|
*/
|
||||||
const val RESERVED_CHARS = "?:\"*|/\\<>\u0000"
|
const val RESERVED_CHARS = "?:\"*|/\\<>\u0000"
|
||||||
|
|
||||||
fun toTwoDecimalsString(num: Int): String {
|
|
||||||
return if (num >= 10) num.toString() else "0$num"
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check whether an Url is valid
|
* Check whether an Url is valid
|
||||||
* @param url The url to test
|
* @param url The url to test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user