From 93e2b88d41143b7485172a29bb40b7ed5478a0ac Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 23 Oct 2021 17:29:13 -0400 Subject: [PATCH] Minor cleanup --- .../data/updater/AppUpdateChecker.kt | 24 +++++---- .../extension/api/ExtensionGithubApi.kt | 6 +++ .../ui/base/controller/ConductorExtensions.kt | 4 ++ .../kanade/tachiyomi/ui/main/MainActivity.kt | 54 +++++++------------ 4 files changed, 43 insertions(+), 45 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/AppUpdateChecker.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/AppUpdateChecker.kt index 37488a753..2e638fcc7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/AppUpdateChecker.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/AppUpdateChecker.kt @@ -10,24 +10,22 @@ import eu.kanade.tachiyomi.network.parseAs import eu.kanade.tachiyomi.util.lang.withIOContext import uy.kohesive.injekt.injectLazy import java.util.Date +import java.util.concurrent.TimeUnit class AppUpdateChecker { private val networkService: NetworkHelper by injectLazy() private val preferences: PreferencesHelper by injectLazy() - private val repo: String by lazy { - if (BuildConfig.PREVIEW) { - "tachiyomiorg/tachiyomi-preview" - } else { - "tachiyomiorg/tachiyomi" - } - } - suspend fun checkForUpdate(context: Context): AppUpdateResult { + // Limit checks to once a day at most + if (Date().time < preferences.lastAppCheck().get() + TimeUnit.DAYS.toMillis(1)) { + return AppUpdateResult.NoNewUpdate + } + return withIOContext { val result = networkService.client - .newCall(GET("https://api.github.com/repos/$repo/releases/latest")) + .newCall(GET("https://api.github.com/repos/$GITHUB_REPO/releases/latest")) .await() .parseAs() .let { @@ -64,3 +62,11 @@ class AppUpdateChecker { } } } + +val GITHUB_REPO: String by lazy { + if (BuildConfig.PREVIEW) { + "tachiyomiorg/tachiyomi-preview" + } else { + "tachiyomiorg/tachiyomi" + } +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/extension/api/ExtensionGithubApi.kt b/app/src/main/java/eu/kanade/tachiyomi/extension/api/ExtensionGithubApi.kt index d712a564d..308b3782d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/extension/api/ExtensionGithubApi.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/extension/api/ExtensionGithubApi.kt @@ -13,6 +13,7 @@ import eu.kanade.tachiyomi.util.lang.withIOContext import kotlinx.serialization.Serializable import uy.kohesive.injekt.injectLazy import java.util.Date +import java.util.concurrent.TimeUnit internal class ExtensionGithubApi { @@ -30,6 +31,11 @@ internal class ExtensionGithubApi { } suspend fun checkForUpdates(context: Context): List { + // Limit checks to once a day at most + if (Date().time < preferences.lastExtCheck().get() + TimeUnit.DAYS.toMillis(1)) { + return emptyList() + } + val extensions = findExtensions() preferences.lastExtCheck().set(Date().time) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/base/controller/ConductorExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/base/controller/ConductorExtensions.kt index d225bec56..bc7ef0b6f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/base/controller/ConductorExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/base/controller/ConductorExtensions.kt @@ -9,6 +9,10 @@ import com.bluelinelabs.conductor.RouterTransaction import eu.kanade.tachiyomi.ui.main.MainActivity import eu.kanade.tachiyomi.util.system.openInBrowser +fun Router.setRoot(controller: Controller, id: Int) { + setRoot(controller.withFadeTransaction().tag(id.toString())) +} + fun Router.popControllerWithTag(tag: String): Boolean { val controller = getControllerWithTag(tag) if (controller != null) { 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 19d9e2126..70e5e83db 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 @@ -45,6 +45,7 @@ import eu.kanade.tachiyomi.ui.base.controller.FabController import eu.kanade.tachiyomi.ui.base.controller.NoAppBarElevationController import eu.kanade.tachiyomi.ui.base.controller.RootController import eu.kanade.tachiyomi.ui.base.controller.TabbedController +import eu.kanade.tachiyomi.ui.base.controller.setRoot import eu.kanade.tachiyomi.ui.base.controller.withFadeTransaction import eu.kanade.tachiyomi.ui.browse.BrowseController import eu.kanade.tachiyomi.ui.browse.source.browse.BrowseSourceController @@ -69,8 +70,6 @@ import kotlinx.coroutines.flow.drop import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach import logcat.LogPriority -import java.util.Date -import java.util.concurrent.TimeUnit class MainActivity : BaseViewBindingActivity() { @@ -153,11 +152,11 @@ class MainActivity : BaseViewBindingActivity() { val currentRoot = router.backstack.firstOrNull() if (currentRoot?.tag()?.toIntOrNull() != id) { when (id) { - R.id.nav_library -> setRoot(LibraryController(), id) - R.id.nav_updates -> setRoot(UpdatesController(), id) - R.id.nav_history -> setRoot(HistoryController(), id) - R.id.nav_browse -> setRoot(BrowseController(), id) - R.id.nav_more -> setRoot(MoreController(), id) + R.id.nav_library -> router.setRoot(LibraryController(), id) + R.id.nav_updates -> router.setRoot(UpdatesController(), id) + R.id.nav_history -> router.setRoot(HistoryController(), id) + R.id.nav_browse -> router.setRoot(BrowseController(), id) + R.id.nav_more -> router.setRoot(MoreController(), id) } } else if (!isHandlingShortcut) { when (id) { @@ -322,37 +321,24 @@ class MainActivity : BaseViewBindingActivity() { override fun onResume() { super.onResume() - checkForExtensionUpdates() - if (BuildConfig.INCLUDE_UPDATER) { - checkForAppUpdates() - } + checkForUpdates() } - private fun checkForAppUpdates() { - // Limit checks to once a day at most - if (Date().time < preferences.lastAppCheck().get() + TimeUnit.DAYS.toMillis(1)) { - return - } - + private fun checkForUpdates() { lifecycleScope.launchIO { - try { - val result = AppUpdateChecker().checkForUpdate(this@MainActivity) - if (result is AppUpdateResult.NewUpdate) { - NewUpdateDialogController(result).showDialog(router) + // App updates + if (BuildConfig.INCLUDE_UPDATER) { + try { + val result = AppUpdateChecker().checkForUpdate(this@MainActivity) + if (result is AppUpdateResult.NewUpdate) { + NewUpdateDialogController(result).showDialog(router) + } + } catch (e: Exception) { + logcat(LogPriority.ERROR, e) } - } catch (e: Exception) { - logcat(LogPriority.ERROR, e) } - } - } - private fun checkForExtensionUpdates() { - // Limit checks to once a day at most - if (Date().time < preferences.lastExtCheck().get() + TimeUnit.DAYS.toMillis(1)) { - return - } - - lifecycleScope.launchIO { + // Extension updates try { val pendingUpdates = ExtensionGithubApi().checkForUpdates(this@MainActivity) preferences.extensionUpdatesCount().set(pendingUpdates.size) @@ -502,10 +488,6 @@ class MainActivity : BaseViewBindingActivity() { } } - private fun setRoot(controller: Controller, id: Int) { - router.setRoot(controller.withFadeTransaction().tag(id.toString())) - } - private fun syncActivityViewWithController(to: Controller?, from: Controller? = null, isPush: Boolean = true) { if (from is DialogController || to is DialogController) { return