mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 22:30:30 +05:30
Merge pull request #1798 from Bnyro/master
Option to set a notification sync start and end time
This commit is contained in:
commit
3c0e7a5f59
@ -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
|
||||||
|
@ -6,7 +6,6 @@ import android.util.Log
|
|||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import android.widget.TextView
|
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
import androidx.recyclerview.widget.ItemTouchHelper
|
import androidx.recyclerview.widget.ItemTouchHelper
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
|
@ -8,6 +8,7 @@ import com.github.libretube.R
|
|||||||
import com.github.libretube.constants.PreferenceKeys
|
import com.github.libretube.constants.PreferenceKeys
|
||||||
import com.github.libretube.ui.activities.SettingsActivity
|
import com.github.libretube.ui.activities.SettingsActivity
|
||||||
import com.github.libretube.ui.base.BasePreferenceFragment
|
import com.github.libretube.ui.base.BasePreferenceFragment
|
||||||
|
import com.github.libretube.ui.views.TimePickerPreference
|
||||||
import com.github.libretube.util.NotificationHelper
|
import com.github.libretube.util.NotificationHelper
|
||||||
|
|
||||||
class NotificationSettings : BasePreferenceFragment() {
|
class NotificationSettings : BasePreferenceFragment() {
|
||||||
@ -41,6 +42,19 @@ class NotificationSettings : BasePreferenceFragment() {
|
|||||||
updateNotificationPrefs()
|
updateNotificationPrefs()
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val notificationTime = findPreference<SwitchPreferenceCompat>(PreferenceKeys.NOTIFICATION_TIME_ENABLED)
|
||||||
|
val notificationStartTime = findPreference<TimePickerPreference>(PreferenceKeys.NOTIFICATION_START_TIME)
|
||||||
|
val notificationEndTime = findPreference<TimePickerPreference>(PreferenceKeys.NOTIFICATION_END_TIME)
|
||||||
|
listOf(notificationStartTime, notificationEndTime).forEach {
|
||||||
|
it?.isEnabled = notificationTime?.isChecked == true
|
||||||
|
}
|
||||||
|
notificationTime?.setOnPreferenceChangeListener { _, newValue ->
|
||||||
|
listOf(notificationStartTime, notificationEndTime).forEach {
|
||||||
|
it?.isEnabled = newValue as Boolean
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateNotificationPrefs() {
|
private fun updateNotificationPrefs() {
|
||||||
|
@ -0,0 +1,65 @@
|
|||||||
|
package com.github.libretube.ui.views
|
||||||
|
|
||||||
|
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 DEFAULT_VALUE
|
||||||
|
}
|
||||||
|
|
||||||
|
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 = ":"
|
||||||
|
const val DEFAULT_VALUE = "12:00"
|
||||||
|
}
|
||||||
|
}
|
@ -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 {
|
||||||
|
@ -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"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
@ -13,10 +13,14 @@ import com.github.libretube.R
|
|||||||
import com.github.libretube.api.RetrofitInstance
|
import com.github.libretube.api.RetrofitInstance
|
||||||
import com.github.libretube.api.SubscriptionHelper
|
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.constants.PreferenceKeys
|
||||||
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.ui.views.TimePickerPreference
|
||||||
|
import com.github.libretube.util.PreferenceHelper
|
||||||
import kotlinx.coroutines.async
|
import kotlinx.coroutines.async
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
|
import java.time.LocalTime
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The notification worker which checks for new streams in a certain frequency
|
* The notification worker which checks for new streams in a certain frequency
|
||||||
@ -35,12 +39,43 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun doWork(): Result {
|
override fun doWork(): Result {
|
||||||
|
if (!checkTime()) Result.success()
|
||||||
// check whether there are new streams and notify if there are some
|
// check whether there are new streams and notify if there are some
|
||||||
val result = checkForNewStreams()
|
val result = checkForNewStreams()
|
||||||
// return success if the API request succeeded
|
// return success if the API request succeeded
|
||||||
return if (result) Result.success() else Result.retry()
|
return if (result) Result.success() else Result.retry()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine whether the time is valid to notify
|
||||||
|
*/
|
||||||
|
private fun checkTime(): Boolean {
|
||||||
|
if (!PreferenceHelper.getBoolean(
|
||||||
|
PreferenceKeys.NOTIFICATION_TIME_ENABLED,
|
||||||
|
false
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
val start = getTimePickerPref(PreferenceKeys.NOTIFICATION_START_TIME)
|
||||||
|
val end = getTimePickerPref(PreferenceKeys.NOTIFICATION_END_TIME)
|
||||||
|
|
||||||
|
val currentTime = LocalTime.now()
|
||||||
|
val isOverNight = start > end
|
||||||
|
|
||||||
|
val startValid = if (isOverNight) start > currentTime else start < currentTime
|
||||||
|
val endValid = if (isOverNight) end < currentTime else start > currentTime
|
||||||
|
|
||||||
|
return (startValid && endValid)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getTimePickerPref(key: String): LocalTime {
|
||||||
|
return LocalTime.parse(
|
||||||
|
PreferenceHelper.getString(key, TimePickerPreference.DEFAULT_VALUE)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* check whether new streams are available in subscriptions
|
* check whether new streams are available in subscriptions
|
||||||
*/
|
*/
|
@ -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>
|
||||||
|
@ -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>
|
Loading…
Reference in New Issue
Block a user