Checking for updates in settings

This commit is contained in:
Koitharu
2020-10-20 20:52:43 +03:00
parent 2135195f27
commit 6f3ae19345
10 changed files with 99 additions and 30 deletions

View File

@@ -76,7 +76,7 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList
return
}
TrackWorker.setup(applicationContext)
AppUpdateChecker(this).invoke()
AppUpdateChecker(this).launchIfNeeded()
}
override fun onDestroy() {

View File

@@ -96,7 +96,7 @@ class MangaSuggestionsProvider : SearchRecentSuggestionsProvider() {
uri,
projection,
" ?",
arrayOf(q.toString()),
arrayOf(q?.toString().orEmpty()),
null
)
}

View File

@@ -8,8 +8,8 @@ import android.net.Uri
import androidx.activity.ComponentActivity
import androidx.lifecycle.lifecycleScope
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import org.koin.android.ext.android.inject
import org.koitharu.kotatsu.BuildConfig
@@ -35,29 +35,38 @@ class AppUpdateChecker(private val activity: ComponentActivity) {
private val settings by activity.inject<AppSettings>()
private val repo by activity.inject<GithubRepository>()
operator fun invoke() {
if (isUpdateSupported(activity) && settings.appUpdateAuto && settings.appUpdate + PERIOD < System.currentTimeMillis()) {
fun launchIfNeeded(): Job? {
return if (settings.appUpdateAuto && settings.appUpdate + PERIOD < System.currentTimeMillis()) {
launch()
} else {
null
}
}
private fun launch() = activity.lifecycleScope.launch(Dispatchers.Main) {
try {
val version = repo.getLatestVersion()
val newVersionId = VersionId.parse(version.name)
val currentVersionId = VersionId.parse(BuildConfig.VERSION_NAME)
if (newVersionId > currentVersionId) {
showUpdateDialog(version)
}
settings.appUpdate = System.currentTimeMillis()
} catch (_: CancellationException) {
} catch (e: Throwable) {
if (BuildConfig.DEBUG) {
e.printStackTrace()
}
fun launch(): Job? {
return if (isUpdateSupported(activity)) {
launchInternal()
} else {
null
}
}
suspend fun checkNow() = runCatching {
val version = repo.getLatestVersion()
val newVersionId = VersionId.parse(version.name)
val currentVersionId = VersionId.parse(BuildConfig.VERSION_NAME)
val result = newVersionId > currentVersionId
if (result) {
showUpdateDialog(version)
}
settings.appUpdate = System.currentTimeMillis()
result
}.getOrNull()
private fun launchInternal() = activity.lifecycleScope.launch(Dispatchers.Main) {
checkNow()
}
private fun showUpdateDialog(version: AppVersion) {
MaterialAlertDialogBuilder(activity)
.setTitle(R.string.app_update_available)

View File

@@ -1,7 +1,6 @@
package org.koitharu.kotatsu.ui.settings
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import androidx.preference.Preference
import com.google.android.material.snackbar.Snackbar
import kotlinx.coroutines.Dispatchers
@@ -16,6 +15,7 @@ import org.koitharu.kotatsu.ui.search.MangaSuggestionsProvider
import org.koitharu.kotatsu.utils.CacheUtils
import org.koitharu.kotatsu.utils.FileSizeUtils
import org.koitharu.kotatsu.utils.ext.getDisplayMessage
import org.koitharu.kotatsu.utils.ext.viewLifecycleScope
class HistorySettingsFragment : BasePreferenceFragment(R.string.history_and_cache) {
@@ -24,7 +24,7 @@ class HistorySettingsFragment : BasePreferenceFragment(R.string.history_and_cach
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.pref_history)
findPreference<Preference>(R.string.key_pages_cache_clear)?.let { pref ->
lifecycleScope.launchWhenResumed {
viewLifecycleScope.launchWhenResumed {
val size = withContext(Dispatchers.IO) {
CacheUtils.computeCacheSize(pref.context, Cache.PAGES.dir)
}
@@ -32,7 +32,7 @@ class HistorySettingsFragment : BasePreferenceFragment(R.string.history_and_cach
}
}
findPreference<Preference>(R.string.key_thumbs_cache_clear)?.let { pref ->
lifecycleScope.launchWhenResumed {
viewLifecycleScope.launchWhenResumed {
val size = withContext(Dispatchers.IO) {
CacheUtils.computeCacheSize(pref.context, Cache.THUMBS.dir)
}
@@ -44,7 +44,7 @@ class HistorySettingsFragment : BasePreferenceFragment(R.string.history_and_cach
p.summary = p.context.resources.getQuantityString(R.plurals.items, items, items)
}
findPreference<Preference>(R.string.key_updates_feed_clear)?.let { p ->
lifecycleScope.launchWhenResumed {
viewLifecycleScope.launchWhenResumed {
val items = trackerRepo.count()
p.summary = p.context.resources.getQuantityString(R.plurals.items, items, items)
}
@@ -73,7 +73,7 @@ class HistorySettingsFragment : BasePreferenceFragment(R.string.history_and_cach
true
}
getString(R.string.key_updates_feed_clear) -> {
lifecycleScope.launch {
viewLifecycleScope.launch {
trackerRepo.clearLogs()
preference.summary = preference.context.resources
.getQuantityString(R.plurals.items, 0, 0)
@@ -91,7 +91,7 @@ class HistorySettingsFragment : BasePreferenceFragment(R.string.history_and_cach
private fun clearCache(preference: Preference, cache: Cache) {
val ctx = preference.context.applicationContext
lifecycleScope.launch {
viewLifecycleScope.launch {
try {
preference.isEnabled = false
val size = withContext(Dispatchers.IO) {

View File

@@ -12,6 +12,8 @@ import androidx.appcompat.app.AppCompatDelegate
import androidx.collection.arrayMapOf
import androidx.preference.*
import com.google.android.material.snackbar.Snackbar
import kotlinx.coroutines.launch
import org.koitharu.kotatsu.BuildConfig
import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.model.MangaSource
import org.koitharu.kotatsu.core.prefs.ListMode
@@ -23,6 +25,7 @@ import org.koitharu.kotatsu.ui.settings.utils.MultiSummaryProvider
import org.koitharu.kotatsu.ui.tracker.TrackWorker
import org.koitharu.kotatsu.utils.ext.getStorageName
import org.koitharu.kotatsu.utils.ext.md5
import org.koitharu.kotatsu.utils.ext.viewLifecycleScope
import java.io.File
@@ -54,6 +57,10 @@ class MainSettingsFragment : BasePreferenceFragment(R.string.settings),
}
findPreference<SwitchPreference>(R.string.key_protect_app)?.isChecked =
!settings.appPassword.isNullOrEmpty()
findPreference<Preference>(R.string.key_app_version)?.run {
title = getString(R.string.app_version, BuildConfig.VERSION_NAME)
isEnabled = AppUpdateChecker.isUpdateSupported(context)
}
}
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String?) {
@@ -126,6 +133,10 @@ class MainSettingsFragment : BasePreferenceFragment(R.string.settings),
}
true
}
getString(R.string.key_app_version) -> {
checkForUpdates()
true
}
else -> super.onPreferenceTreeClick(preference)
}
}
@@ -184,6 +195,26 @@ class MainSettingsFragment : BasePreferenceFragment(R.string.settings),
.show()
}
private fun checkForUpdates() {
viewLifecycleScope.launch {
findPreference<Preference>(R.string.key_app_version)?.run {
setSummary(R.string.checking_for_updates)
isSelectable = false
}
val result = AppUpdateChecker(activity ?: return@launch).checkNow()
findPreference<Preference>(R.string.key_app_version)?.run {
setSummary(
when (result) {
true -> R.string.check_for_updates
false -> R.string.no_update_available
null -> R.string.update_check_failed
}
)
isSelectable = true
}
}
}
private companion object {
val LIST_MODES = arrayMapOf(

View File

@@ -2,10 +2,14 @@ package org.koitharu.kotatsu.utils.ext
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.lifecycle.coroutineScope
inline fun <T : Fragment> T.withArgs(size: Int, block: Bundle.() -> Unit): T {
val b = Bundle(size)
b.block()
this.arguments = b
return this
}
}
val Fragment.viewLifecycleScope
get() = viewLifecycleOwner.lifecycle.coroutineScope