Fix request news

This commit is contained in:
FooIbar committed 2024-01-06 20:41:30 +08:00
1 parent 28410befa3
commit fe4d004083
3 files changed
+7 -17

No files matched your search

@@ -154,8 +154,7 @@ object Settings : DataStorePreferences(null) {
var securityDelay by intPref("require_unlock_delay", 0)
var clipboardTextHashCode by intPref("clipboard_text_hash_code", 0)
var searchCategory by intPref("search_pref", EhUtils.ALL_CATEGORY)
var requestNewsTimerHour by intPref("request_news_timer_hour", -1)
var requestNewsTimerMinute by intPref("request_news_timer_minute", -1)
var requestNewsTime by intPref("request_news_time", 0).observed { updateWhenRequestNewsChanges() }
var updateIntervalDays by intPref("update_interval_days", 7)
var recentToplist by stringPref("recent_toplist", "11")
var userAgent by stringPref("user_agent", CHROME_USER_AGENT)
@@ -48,18 +48,12 @@ class DailyCheckWork(context: Context, workerParams: WorkerParameters) :
}
}
val schedHour
get() = Settings.requestNewsTimerHour.takeUnless { it == -1 } ?: 0
val schedMinute
get() = Settings.requestNewsTimerMinute.takeUnless { it == -1 } ?: 0
private fun getDailyCheckWorkRequest(): PeriodicWorkRequest {
val now = Clock.System.now()
val timeZone = TimeZone.currentSystemDefault()
val whenToWork = LocalDateTime(
now.toLocalDateTime(timeZone).date,
LocalTime(schedHour, schedMinute),
LocalTime.fromSecondOfDay(Settings.requestNewsTime),
).toInstant(timeZone)
val initialDelay = (whenToWork - now).run { if (isNegative()) plus(1.days) else this }
val constraints = Constraints.Builder()
@@ -77,7 +71,7 @@ fun updateDailyCheckWork(context: Context) {
if (Settings.requestNews) {
WorkManager.getInstance(context).enqueueUniquePeriodicWork(
WORK_NAME,
ExistingPeriodicWorkPolicy.UPDATE,
ExistingPeriodicWorkPolicy.CANCEL_AND_REENQUEUE,
getDailyCheckWorkRequest(),
)
} else {
@@ -39,9 +39,6 @@ import com.hippo.ehviewer.client.EhTagDatabase
import com.hippo.ehviewer.client.EhUrl
import com.hippo.ehviewer.client.EhUtils
import com.hippo.ehviewer.client.parser.HomeParser
import com.hippo.ehviewer.dailycheck.schedHour
import com.hippo.ehviewer.dailycheck.schedMinute
import com.hippo.ehviewer.dailycheck.updateDailyCheckWork
import com.hippo.ehviewer.ui.destinations.FilterScreenDestination
import com.hippo.ehviewer.ui.destinations.MyTagsScreenDestination
import com.hippo.ehviewer.ui.destinations.SignInScreenDestination
@@ -59,6 +56,7 @@ import io.ktor.http.Url
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.datetime.LocalTime
import moe.tarsin.coroutines.runSuspendCatching
@Destination
@@ -325,10 +323,9 @@ fun EhScreen(navigator: DestinationsNavigator) {
val pickerTitle = stringResource(id = R.string.settings_eh_request_news_timepicker)
Preference(title = pickerTitle) {
coroutineScope.launch {
val (hour, minute) = dialogState.showTimePicker(pickerTitle, schedHour, schedMinute)
Settings.requestNewsTimerHour = hour
Settings.requestNewsTimerMinute = minute
updateDailyCheckWork(context)
val time = LocalTime.fromSecondOfDay(Settings.requestNewsTime)
val (hour, minute) = dialogState.showTimePicker(pickerTitle, time.hour, time.minute)
Settings.requestNewsTime = LocalTime(hour, minute).toSecondOfDay()
}
}
}