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)
This commit is contained in:
Jay 2020-05-10 18:09:15 -04:00
parent 4b50692cee
commit 7d62d78976
2 changed files with 34 additions and 1 deletions

View File

@ -253,6 +253,8 @@ class PreferencesHelper(val context: Context) {
fun lastExtCheck() = rxPrefs.getLong("last_ext_check", 0) 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 unreadBadgeType() = rxPrefs.getInteger("unread_badge_type", 2)
fun hideStartReadingButton() = rxPrefs.getBoolean("hide_reading_button", false) fun hideStartReadingButton() = rxPrefs.getBoolean("hide_reading_button", false)

View File

@ -21,6 +21,7 @@ import androidx.appcompat.graphics.drawable.DrawerArrowDrawable
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.graphics.ColorUtils import androidx.core.graphics.ColorUtils
import androidx.core.view.GestureDetectorCompat import androidx.core.view.GestureDetectorCompat
import androidx.lifecycle.lifecycleScope
import com.afollestad.materialdialogs.MaterialDialog import com.afollestad.materialdialogs.MaterialDialog
import com.bluelinelabs.conductor.Conductor import com.bluelinelabs.conductor.Conductor
import com.bluelinelabs.conductor.Controller 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.library.LibraryUpdateService
import eu.kanade.tachiyomi.data.notification.NotificationReceiver import eu.kanade.tachiyomi.data.notification.NotificationReceiver
import eu.kanade.tachiyomi.data.preference.getOrDefault 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.extension.api.ExtensionGithubApi
import eu.kanade.tachiyomi.ui.base.activity.BaseActivity import eu.kanade.tachiyomi.ui.base.activity.BaseActivity
import eu.kanade.tachiyomi.ui.base.controller.BaseController 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.recently_read.RecentlyReadController
import eu.kanade.tachiyomi.ui.recents.RecentsController import eu.kanade.tachiyomi.ui.recents.RecentsController
import eu.kanade.tachiyomi.ui.security.SecureActivityDelegate 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.SettingsController
import eu.kanade.tachiyomi.ui.setting.SettingsMainController import eu.kanade.tachiyomi.ui.setting.SettingsMainController
import eu.kanade.tachiyomi.ui.source.SourceController import eu.kanade.tachiyomi.ui.source.SourceController
@ -71,6 +75,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import timber.log.Timber import timber.log.Timber
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
import java.util.Date import java.util.Date
@ -96,6 +101,9 @@ open class MainActivity : BaseActivity(), DownloadServiceListener {
private val hideBottomNav private val hideBottomNav
get() = router.backstackSize > 1 && router.backstack[1].controller() !is DialogController 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) { fun setUndoSnackBar(snackBar: Snackbar?, extraViewToCheck: View? = null) {
this.snackBar = snackBar this.snackBar = snackBar
canDismissSnackBar = false canDismissSnackBar = false
@ -348,7 +356,7 @@ open class MainActivity : BaseActivity(), DownloadServiceListener {
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
// setting in case someone comes from the search activity to main getAppUpdates()
getExtensionUpdates() getExtensionUpdates()
DownloadService.callListeners() DownloadService.callListeners()
showDLQueueTutorial() showDLQueueTutorial()
@ -384,6 +392,29 @@ open class MainActivity : BaseActivity(), DownloadServiceListener {
snackBar?.dismiss() 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() { private fun getExtensionUpdates() {
if (Date().time >= preferences.lastExtCheck().getOrDefault() + TimeUnit.HOURS.toMillis(6)) { if (Date().time >= preferences.lastExtCheck().getOrDefault() + TimeUnit.HOURS.toMillis(6)) {
GlobalScope.launch(Dispatchers.IO) { GlobalScope.launch(Dispatchers.IO) {