time picker backend

This commit is contained in:
Bnyro 2022-11-09 18:01:59 +01:00
parent 5b89ef9069
commit e0512fcfbf
7 changed files with 98 additions and 1 deletions

View File

@ -94,6 +94,9 @@ object PreferenceKeys {
const val REQUIRED_NETWORK = "required_network" const val REQUIRED_NETWORK = "required_network"
const val LAST_STREAM_VIDEO_ID = "last_stream_video_id" const val LAST_STREAM_VIDEO_ID = "last_stream_video_id"
const val IGNORED_NOTIFICATION_CHANNELS = "ignored_notification_channels" const val IGNORED_NOTIFICATION_CHANNELS = "ignored_notification_channels"
const val NOTIFICATION_TIME_ENABLED = "notification_time"
const val NOTIFICATION_START_TIME = "notification_start_time"
const val NOTIFICATION_END_TIME = "notification_end_time"
/** /**
* Advanced * Advanced

View File

@ -0,0 +1,65 @@
package com.github.libretube.ui.views
import android.app.Activity
import android.content.Context
import android.text.format.DateFormat.is24HourFormat
import android.util.AttributeSet
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.Preference
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.TimeFormat
class TimePickerPreference(
context: Context,
attributeSet: AttributeSet
) : Preference(context, attributeSet) {
override fun getSummary(): CharSequence {
val prefStr = PreferenceHelper.getString(key, "")
return if (prefStr != "") prefStr else "00:00"
}
override fun onClick() {
val picker = MaterialTimePicker.Builder()
.setInputMode(MaterialTimePicker.INPUT_MODE_CLOCK)
.setTimeFormat(getTimeFormat())
.setHour(getHour())
.setMinute(getMinutes())
.build()
picker.addOnPositiveButtonClickListener {
val timeStr = getTimeStr(picker)
PreferenceHelper.putString(key, timeStr)
summary = timeStr
}
picker.show((context as AppCompatActivity).supportFragmentManager, null)
}
private fun getTimeFormat(): Int {
return 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 {
const val SEPARATOR = ":"
}
}

View File

@ -8,6 +8,7 @@ import androidx.work.PeriodicWorkRequest
import androidx.work.WorkManager import androidx.work.WorkManager
import com.github.libretube.constants.NOTIFICATION_WORK_NAME import com.github.libretube.constants.NOTIFICATION_WORK_NAME
import com.github.libretube.constants.PreferenceKeys import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.workers.NotificationWorker
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
object NotificationHelper { object NotificationHelper {

View File

@ -5,4 +5,8 @@ object TextUtils {
* Separator used for descriptions * Separator used for descriptions
*/ */
const val SEPARATOR = "" const val SEPARATOR = ""
fun toTwoDecimalsString(num: Int): String {
return if (num >= 10) num.toString() else "0$num"
}
} }

View File

@ -1,4 +1,4 @@
package com.github.libretube.util package com.github.libretube.workers
import android.app.NotificationManager import android.app.NotificationManager
import android.app.PendingIntent import android.app.PendingIntent
@ -15,6 +15,7 @@ import com.github.libretube.api.SubscriptionHelper
import com.github.libretube.constants.PUSH_CHANNEL_ID import com.github.libretube.constants.PUSH_CHANNEL_ID
import com.github.libretube.extensions.toID import com.github.libretube.extensions.toID
import com.github.libretube.ui.activities.MainActivity import com.github.libretube.ui.activities.MainActivity
import com.github.libretube.util.PreferenceHelper
import kotlinx.coroutines.async import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking

View File

@ -360,6 +360,11 @@
<string name="confirm_unsubscribing">Confirm unsubscribing</string> <string name="confirm_unsubscribing">Confirm unsubscribing</string>
<string name="confirm_unsubscribing_summary">Show a confirmation dialog before unsubscribing.</string> <string name="confirm_unsubscribing_summary">Show a confirmation dialog before unsubscribing.</string>
<string name="play_all">Play all</string> <string name="play_all">Play all</string>
<string name="time">Time</string>
<string name="start_time">Start time</string>
<string name="end_time">End time</string>
<string name="notification_time">Notification time</string>
<string name="notification_time_summary">Time span in which notifications are allowed to show.</string>
<!-- Notification channel strings --> <!-- Notification channel strings -->
<string name="download_channel_name">Download Service</string> <string name="download_channel_name">Download Service</string>

View File

@ -31,4 +31,22 @@
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory app:title="@string/time">
<SwitchPreferenceCompat
app:title="@string/notification_time"
android:summary="@string/notification_time_summary"
app:key="notification_time"
/>
<com.github.libretube.ui.views.TimePickerPreference
app:key="notification_start_time"
app:title="@string/start_time" />
<com.github.libretube.ui.views.TimePickerPreference
app:key="notification_end_time"
app:title="@string/end_time" />
</PreferenceCategory>
</PreferenceScreen> </PreferenceScreen>