Check dawn on start in case the periodic work failed

This commit is contained in:
FooIbar authored and tarsin committed 2023-02-27 20:53:19 +08:00
1 parent c4c654079d
commit 0a93763c48
4 files changed
+59 -30

No files matched your search

@@ -34,6 +34,7 @@ import com.hippo.ehviewer.client.EhSSLSocketFactory
import com.hippo.ehviewer.client.EhTagDatabase
import com.hippo.ehviewer.client.data.GalleryDetail
import com.hippo.ehviewer.coil.MergeInterceptor
import com.hippo.ehviewer.dailycheck.checkDawn
import com.hippo.ehviewer.dao.buildMainDB
import com.hippo.ehviewer.download.DownloadManager
import com.hippo.ehviewer.ui.CommonOperations
@@ -100,6 +101,11 @@ class EhApplication : Application(), DefaultLifecycleObserver, ImageLoaderFactor
launchIO {
cleanupDownload()
}
if (Settings.requestNews) {
launchIO {
checkDawn()
}
}
}
mIdGenerator.setNextId(Settings.getInt(KEY_GLOBAL_STUFF_NEXT_ID, 0))
}
@@ -95,6 +95,7 @@ object Settings {
const val KEY_DOMAIN_FRONTING = "domain_fronting"
const val KEY_BYPASS_VPN = "bypass_vpn"
const val KEY_LIST_THUMB_SIZE = "list_tile_size"
private const val KEY_LAST_DAWN_DAY = "last_dawn_day"
private const val DEFAULT_HIDE_HV_EVENTS = false
private const val KEY_SHOW_COMMENTS = "show_gallery_comments"
private const val DEFAULT_SHOW_COMMENTS = true
@@ -293,6 +294,9 @@ object Settings {
sSettingsPre.edit().putString(key, value.toString()).apply()
}
var lastDawnDay: Long
get() = sSettingsPre.getLong(KEY_LAST_DAWN_DAY, 0)
set(value) = sSettingsPre.edit().putLong(KEY_LAST_DAWN_DAY, value).apply()
val displayName: String?
get() = getString(KEY_DISPLAY_NAME, DEFAULT_DISPLAY_NAME)
@@ -47,6 +47,8 @@ import com.hippo.ehviewer.client.parser.SignInParser
import com.hippo.ehviewer.client.parser.TorrentParser
import com.hippo.ehviewer.client.parser.VoteCommentParser
import com.hippo.ehviewer.client.parser.VoteTagParser
import com.hippo.ehviewer.dailycheck.showEventNotification
import com.hippo.ehviewer.dailycheck.today
import com.hippo.network.StatusCodeException
import com.hippo.util.ExceptionUtils
import kotlinx.coroutines.async
@@ -320,6 +322,10 @@ object EhEngine {
code = response.code
headers = response.headers
body = response.body.string()
EventPaneParser.parse(body!!)?.let {
Settings.lastDawnDay = today
showEventNotification(it)
}
return GalleryDetailParser.parse(body!!)
}
} catch (e: Throwable) {
@@ -20,49 +20,23 @@ import com.hippo.ehviewer.Settings
import com.hippo.ehviewer.client.EhEngine
import eu.kanade.tachiyomi.util.lang.withIOContext
import java.time.Duration
import java.time.Instant
import java.time.LocalDateTime
import java.time.temporal.ChronoUnit
private val signedIn
get() = EhApplication.ehCookieStore.hasSignedIn()
private const val CHANNEL_ID = "DailyCheckNotification"
class DailyCheckWork(val context: Context, workerParams: WorkerParameters) :
class DailyCheckWork(context: Context, workerParams: WorkerParameters) :
CoroutineWorker(context, workerParams) {
override suspend fun doWork(): Result = withIOContext {
runCatching {
if (signedIn) {
EhEngine.getNews(true)?.let { showEventPane(it) }
}
}.onFailure {
it.printStackTrace()
checkDawn().onFailure {
return@withIOContext Result.retry()
}
Result.success()
}
@SuppressLint("MissingPermission")
private fun showEventPane(html: String) {
if (Settings.hideHvEvents && html.contains("You have encountered a monster!")) {
return
}
val notificationManager = NotificationManagerCompat.from(context)
val chan = NotificationChannelCompat
.Builder(CHANNEL_ID, NotificationManagerCompat.IMPORTANCE_LOW)
.setName(CHANNEL_ID)
.build()
notificationManager.createNotificationChannel(chan)
val msg = NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_monochrome)
.setContentText(Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY))
.setStyle(NotificationCompat.BigTextStyle())
.build()
runCatching {
notificationManager.notify(1, msg)
}.onFailure {
it.printStackTrace()
}
}
}
val schedHour
@@ -103,3 +77,42 @@ fun updateDailyCheckWork(context: Context) {
WorkManager.getInstance(context).cancelUniqueWork(workName)
}
}
val today
get() = Instant.now().truncatedTo(ChronoUnit.DAYS).epochSecond
suspend fun checkDawn() = runCatching {
if (signedIn && Settings.lastDawnDay != today) {
EhEngine.getNews(true)?.let {
Settings.lastDawnDay = today
showEventNotification(it)
}
}
}.onFailure {
it.printStackTrace()
}
@SuppressLint("MissingPermission")
fun showEventNotification(html: String) {
if (Settings.hideHvEvents && html.contains("You have encountered a monster!")) {
return
}
val context = EhApplication.application
val notificationManager = NotificationManagerCompat.from(context)
val chan = NotificationChannelCompat
.Builder(CHANNEL_ID, NotificationManagerCompat.IMPORTANCE_LOW)
.setName(CHANNEL_ID)
.build()
notificationManager.createNotificationChannel(chan)
val msg = NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_monochrome)
.setContentText(Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY))
.setStyle(NotificationCompat.BigTextStyle())
.build()
runCatching {
notificationManager.notify(1, msg)
}.onFailure {
it.printStackTrace()
}
}