diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt index 19368dff4..855810d84 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt @@ -6,7 +6,6 @@ import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import android.widget.TextView import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.LinearLayoutManager 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 index 3eabff8d8..3bb1830a2 100644 --- a/app/src/main/java/com/github/libretube/ui/views/TimePickerPreference.kt +++ b/app/src/main/java/com/github/libretube/ui/views/TimePickerPreference.kt @@ -1,6 +1,5 @@ package com.github.libretube.ui.views -import android.app.Activity import android.content.Context import android.text.format.DateFormat.is24HourFormat import android.util.AttributeSet @@ -17,7 +16,7 @@ class TimePickerPreference( ) : Preference(context, attributeSet) { override fun getSummary(): CharSequence { val prefStr = PreferenceHelper.getString(key, "") - return if (prefStr != "") prefStr else "00:00" + return if (prefStr != "") prefStr else DEFAULT_VALUE } override fun onClick() { @@ -61,5 +60,6 @@ class TimePickerPreference( companion object { const val SEPARATOR = ":" + const val DEFAULT_VALUE = "12:00" } } diff --git a/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt b/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt index d31b4a9c0..5320780e5 100644 --- a/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt +++ b/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt @@ -13,11 +13,14 @@ import com.github.libretube.R import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.SubscriptionHelper import com.github.libretube.constants.PUSH_CHANNEL_ID +import com.github.libretube.constants.PreferenceKeys import com.github.libretube.extensions.toID 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.runBlocking +import java.time.LocalTime /** * The notification worker which checks for new streams in a certain frequency @@ -36,12 +39,43 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) : } override fun doWork(): Result { + if (!checkTime()) Result.success() // check whether there are new streams and notify if there are some val result = checkForNewStreams() // return success if the API request succeeded 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 */