From 7d62d789767f6a889d7eec2ecce8d15a1f4da4d6 Mon Sep 17 00:00:00 2001 From: Jay Date: Sun, 10 May 2020 18:09:15 -0400 Subject: [PATCH] Check for app updates on foreground Figured to leave the check for updates option incase people don't use the app often (there is no disabling foreground though) --- .../data/preference/PreferencesHelper.kt | 2 ++ .../kanade/tachiyomi/ui/main/MainActivity.kt | 33 ++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt index 5e270c251e..0a82278890 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt @@ -253,6 +253,8 @@ class PreferencesHelper(val context: Context) { fun lastExtCheck() = rxPrefs.getLong("last_ext_check", 0) + fun lastAppCheck() = flowPrefs.getLong("last_app_check", 0) + fun unreadBadgeType() = rxPrefs.getInteger("unread_badge_type", 2) fun hideStartReadingButton() = rxPrefs.getBoolean("hide_reading_button", false) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt index ec32004feb..5a7dcbd467 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt @@ -21,6 +21,7 @@ import androidx.appcompat.graphics.drawable.DrawerArrowDrawable import androidx.core.content.ContextCompat import androidx.core.graphics.ColorUtils import androidx.core.view.GestureDetectorCompat +import androidx.lifecycle.lifecycleScope import com.afollestad.materialdialogs.MaterialDialog import com.bluelinelabs.conductor.Conductor import com.bluelinelabs.conductor.Controller @@ -40,6 +41,8 @@ import eu.kanade.tachiyomi.data.download.DownloadServiceListener import eu.kanade.tachiyomi.data.library.LibraryUpdateService import eu.kanade.tachiyomi.data.notification.NotificationReceiver import eu.kanade.tachiyomi.data.preference.getOrDefault +import eu.kanade.tachiyomi.data.updater.UpdateChecker +import eu.kanade.tachiyomi.data.updater.UpdateResult import eu.kanade.tachiyomi.extension.api.ExtensionGithubApi import eu.kanade.tachiyomi.ui.base.activity.BaseActivity import eu.kanade.tachiyomi.ui.base.controller.BaseController @@ -50,6 +53,7 @@ import eu.kanade.tachiyomi.ui.recent_updates.RecentChaptersController import eu.kanade.tachiyomi.ui.recently_read.RecentlyReadController import eu.kanade.tachiyomi.ui.recents.RecentsController import eu.kanade.tachiyomi.ui.security.SecureActivityDelegate +import eu.kanade.tachiyomi.ui.setting.AboutController import eu.kanade.tachiyomi.ui.setting.SettingsController import eu.kanade.tachiyomi.ui.setting.SettingsMainController import eu.kanade.tachiyomi.ui.source.SourceController @@ -71,6 +75,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.delay import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import timber.log.Timber import uy.kohesive.injekt.injectLazy import java.util.Date @@ -96,6 +101,9 @@ open class MainActivity : BaseActivity(), DownloadServiceListener { private val hideBottomNav get() = router.backstackSize > 1 && router.backstack[1].controller() !is DialogController + private val updateChecker by lazy { UpdateChecker.getUpdateChecker() } + private val isUpdaterEnabled = BuildConfig.INCLUDE_UPDATER + fun setUndoSnackBar(snackBar: Snackbar?, extraViewToCheck: View? = null) { this.snackBar = snackBar canDismissSnackBar = false @@ -348,7 +356,7 @@ open class MainActivity : BaseActivity(), DownloadServiceListener { override fun onResume() { super.onResume() - // setting in case someone comes from the search activity to main + getAppUpdates() getExtensionUpdates() DownloadService.callListeners() showDLQueueTutorial() @@ -384,6 +392,29 @@ open class MainActivity : BaseActivity(), DownloadServiceListener { snackBar?.dismiss() } + private fun getAppUpdates() { + if (isUpdaterEnabled && + Date().time >= preferences.lastAppCheck().get() + TimeUnit.DAYS.toMillis(1)) { + lifecycleScope.launch(Dispatchers.IO) { + try { + val result = updateChecker.checkForUpdate() + preferences.lastAppCheck().set(Date().time) + if (result is UpdateResult.NewUpdate<*>) { + val body = result.release.info + val url = result.release.downloadLink + + // Create confirmation window + withContext(Dispatchers.Main) { + AboutController.NewUpdateDialogController(body, url).showDialog(router) + } + } + } catch (error: Exception) { + Timber.e(error) + } + } + } + } + private fun getExtensionUpdates() { if (Date().time >= preferences.lastExtCheck().getOrDefault() + TimeUnit.HOURS.toMillis(6)) { GlobalScope.launch(Dispatchers.IO) {