From e0512fcfbfb9f534433c8c342ee98e0c3a53d509 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Wed, 9 Nov 2022 18:01:59 +0100 Subject: [PATCH] time picker backend --- .../libretube/constants/PreferenceKeys.kt | 3 + .../ui/views/TimePickerPreference.kt | 65 +++++++++++++++++++ .../libretube/util/NotificationHelper.kt | 1 + .../com/github/libretube/util/TextUtils.kt | 4 ++ .../{util => workers}/NotificationWorker.kt | 3 +- app/src/main/res/values/strings.xml | 5 ++ .../main/res/xml/notification_settings.xml | 18 +++++ 7 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/com/github/libretube/ui/views/TimePickerPreference.kt rename app/src/main/java/com/github/libretube/{util => workers}/NotificationWorker.kt (98%) diff --git a/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt b/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt index 3675ac171..fd840c68f 100644 --- a/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt +++ b/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt @@ -94,6 +94,9 @@ object PreferenceKeys { const val REQUIRED_NETWORK = "required_network" const val LAST_STREAM_VIDEO_ID = "last_stream_video_id" 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 diff --git a/app/src/main/java/com/github/libretube/ui/views/TimePickerPreference.kt b/app/src/main/java/com/github/libretube/ui/views/TimePickerPreference.kt new file mode 100644 index 000000000..3eabff8d8 --- /dev/null +++ b/app/src/main/java/com/github/libretube/ui/views/TimePickerPreference.kt @@ -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 = ":" + } +} diff --git a/app/src/main/java/com/github/libretube/util/NotificationHelper.kt b/app/src/main/java/com/github/libretube/util/NotificationHelper.kt index ec467e9fb..046401f99 100644 --- a/app/src/main/java/com/github/libretube/util/NotificationHelper.kt +++ b/app/src/main/java/com/github/libretube/util/NotificationHelper.kt @@ -8,6 +8,7 @@ import androidx.work.PeriodicWorkRequest import androidx.work.WorkManager import com.github.libretube.constants.NOTIFICATION_WORK_NAME import com.github.libretube.constants.PreferenceKeys +import com.github.libretube.workers.NotificationWorker import java.util.concurrent.TimeUnit object NotificationHelper { diff --git a/app/src/main/java/com/github/libretube/util/TextUtils.kt b/app/src/main/java/com/github/libretube/util/TextUtils.kt index 63b8966f1..8b8cc4008 100644 --- a/app/src/main/java/com/github/libretube/util/TextUtils.kt +++ b/app/src/main/java/com/github/libretube/util/TextUtils.kt @@ -5,4 +5,8 @@ object TextUtils { * Separator used for descriptions */ const val SEPARATOR = " • " + + fun toTwoDecimalsString(num: Int): String { + return if (num >= 10) num.toString() else "0$num" + } } diff --git a/app/src/main/java/com/github/libretube/util/NotificationWorker.kt b/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt similarity index 98% rename from app/src/main/java/com/github/libretube/util/NotificationWorker.kt rename to app/src/main/java/com/github/libretube/workers/NotificationWorker.kt index 351918cad..d31b4a9c0 100644 --- a/app/src/main/java/com/github/libretube/util/NotificationWorker.kt +++ b/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt @@ -1,4 +1,4 @@ -package com.github.libretube.util +package com.github.libretube.workers import android.app.NotificationManager 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.extensions.toID import com.github.libretube.ui.activities.MainActivity +import com.github.libretube.util.PreferenceHelper import kotlinx.coroutines.async import kotlinx.coroutines.runBlocking diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index fe3fb8831..c8aa47125 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -360,6 +360,11 @@ Confirm unsubscribing Show a confirmation dialog before unsubscribing. Play all + Time + Start time + End time + Notification time + Time span in which notifications are allowed to show. Download Service diff --git a/app/src/main/res/xml/notification_settings.xml b/app/src/main/res/xml/notification_settings.xml index 4d6568710..b0b536dd2 100644 --- a/app/src/main/res/xml/notification_settings.xml +++ b/app/src/main/res/xml/notification_settings.xml @@ -31,4 +31,22 @@ + + + + + + + + + + \ No newline at end of file