App state banner tweaks (#8746)

* Move download indexing notification to this banner group
* Animate state changes
This commit is contained in:
Ivan Iskandar
2022-12-17 10:18:17 +07:00
committed by GitHub
parent 5f4825465e
commit e20c66b156
5 changed files with 212 additions and 132 deletions
@@ -20,11 +20,14 @@ import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.withTimeout
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
@@ -53,8 +56,6 @@ class DownloadCache(
.onStart { emit(Unit) }
.shareIn(scope, SharingStarted.Eagerly, 1)
private val notifier by lazy { DownloadNotifier(context) }
/**
* The interval after which this cache should be invalidated. 1 hour shouldn't cause major
* issues, as the cache is only used for UI feedback.
@@ -66,6 +67,10 @@ class DownloadCache(
*/
private var lastRenew = 0L
private var renewalJob: Job? = null
val isRenewing = changes
.map { renewalJob?.isActive ?: false }
.distinctUntilChanged()
.stateIn(scope, SharingStarted.WhileSubscribed(), false)
private var rootDownloadsDir = RootDirectory(getDirectoryFromPreference())
@@ -260,8 +265,6 @@ class DownloadCache(
}
renewalJob = scope.launchIO {
notifier.onCacheProgress()
var sources = getSources()
// Try to wait until extensions and sources have loaded
@@ -320,7 +323,6 @@ class DownloadCache(
lastRenew = System.currentTimeMillis()
notifyChanges()
}
renewalJob?.invokeOnCompletion { notifier.dismissCacheProgress() }
}
private fun getSources(): List<Source> {
@@ -39,17 +39,6 @@ internal class DownloadNotifier(private val context: Context) {
}
}
private val cacheNotificationBuilder by lazy {
context.notificationBuilder(Notifications.CHANNEL_DOWNLOADER_CACHE) {
setSmallIcon(R.drawable.ic_tachi)
setContentTitle(context.getString(R.string.download_notifier_cache_renewal))
setProgress(100, 100, true)
setOngoing(true)
setAutoCancel(false)
setOnlyAlertOnce(true)
}
}
/**
* Status of download. Used for correct notification icon.
*/
@@ -223,14 +212,4 @@ internal class DownloadNotifier(private val context: Context) {
errorThrown = true
isDownloading = false
}
fun onCacheProgress() {
with(cacheNotificationBuilder) {
show(Notifications.ID_DOWNLOAD_CACHE)
}
}
fun dismissCacheProgress() {
context.notificationManager.cancel(Notifications.ID_DOWNLOAD_CACHE)
}
}