add logic

This commit is contained in:
Bnyro 2022-11-09 18:15:21 +01:00
parent e0512fcfbf
commit 49bbe312d1
3 changed files with 36 additions and 3 deletions

View File

@ -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

View File

@ -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"
}
}

View File

@ -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
*/