Using typealias for extension session pair

also fix issue with progress flashing away after selecting install from package installer popup

Also setting the rate to 500ms for install status
This commit is contained in:
Jays2Kings 2021-06-23 21:25:53 -04:00
parent c8049b2307
commit 0b9754c5fa
4 changed files with 18 additions and 11 deletions

View File

@ -1,20 +1,19 @@
package eu.kanade.tachiyomi.extension package eu.kanade.tachiyomi.extension
import android.content.Context import android.content.Context
import android.content.pm.PackageInstaller
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import com.jakewharton.rxrelay.BehaviorRelay import com.jakewharton.rxrelay.BehaviorRelay
import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.data.preference.getOrDefault
import eu.kanade.tachiyomi.extension.api.ExtensionGithubApi import eu.kanade.tachiyomi.extension.api.ExtensionGithubApi
import eu.kanade.tachiyomi.extension.model.Extension import eu.kanade.tachiyomi.extension.model.Extension
import eu.kanade.tachiyomi.extension.model.InstallStep
import eu.kanade.tachiyomi.extension.model.LoadResult import eu.kanade.tachiyomi.extension.model.LoadResult
import eu.kanade.tachiyomi.extension.util.ExtensionInstallReceiver import eu.kanade.tachiyomi.extension.util.ExtensionInstallReceiver
import eu.kanade.tachiyomi.extension.util.ExtensionInstaller import eu.kanade.tachiyomi.extension.util.ExtensionInstaller
import eu.kanade.tachiyomi.extension.util.ExtensionLoader import eu.kanade.tachiyomi.extension.util.ExtensionLoader
import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.ui.extension.ExtensionIntallInfo
import eu.kanade.tachiyomi.util.system.launchNow import eu.kanade.tachiyomi.util.system.launchNow
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async import kotlinx.coroutines.async
@ -239,7 +238,7 @@ class ExtensionManager(
* *
* @param extension The extension to be installed. * @param extension The extension to be installed.
*/ */
fun installExtension(extension: Extension.Available): Observable<Pair<InstallStep, PackageInstaller.SessionInfo?>> { fun installExtension(extension: Extension.Available): Observable<ExtensionIntallInfo> {
return installer.downloadAndInstall(api.getApkUrl(extension), extension) return installer.downloadAndInstall(api.getApkUrl(extension), extension)
} }
@ -250,7 +249,7 @@ class ExtensionManager(
* *
* @param extension The extension to be updated. * @param extension The extension to be updated.
*/ */
fun updateExtension(extension: Extension.Installed): Observable<Pair<InstallStep, PackageInstaller.SessionInfo?>> { fun updateExtension(extension: Extension.Installed): Observable<ExtensionIntallInfo> {
val availableExt = availableExtensions.find { it.pkgName == extension.pkgName } val availableExt = availableExtensions.find { it.pkgName == extension.pkgName }
?: return Observable.empty() ?: return Observable.empty()
return installExtension(availableExt) return installExtension(availableExt)

View File

@ -12,6 +12,7 @@ import androidx.core.net.toUri
import com.jakewharton.rxrelay.PublishRelay import com.jakewharton.rxrelay.PublishRelay
import eu.kanade.tachiyomi.extension.model.Extension import eu.kanade.tachiyomi.extension.model.Extension
import eu.kanade.tachiyomi.extension.model.InstallStep import eu.kanade.tachiyomi.extension.model.InstallStep
import eu.kanade.tachiyomi.ui.extension.ExtensionIntallInfo
import eu.kanade.tachiyomi.util.storage.getUriCompat import eu.kanade.tachiyomi.util.storage.getUriCompat
import rx.Observable import rx.Observable
import rx.android.schedulers.AndroidSchedulers import rx.android.schedulers.AndroidSchedulers
@ -110,7 +111,7 @@ internal class ExtensionInstaller(private val context: Context) {
* *
* @param id The id of the download to poll. * @param id The id of the download to poll.
*/ */
private fun pollStatus(id: Long): Observable<Pair<InstallStep, PackageInstaller.SessionInfo?>> { private fun pollStatus(id: Long): Observable<ExtensionIntallInfo> {
val query = DownloadManager.Query().setFilterById(id) val query = DownloadManager.Query().setFilterById(id)
return Observable.interval(0, 1, TimeUnit.SECONDS) return Observable.interval(0, 1, TimeUnit.SECONDS)
@ -132,15 +133,15 @@ internal class ExtensionInstaller(private val context: Context) {
DownloadManager.STATUS_RUNNING -> InstallStep.Downloading DownloadManager.STATUS_RUNNING -> InstallStep.Downloading
else -> return@flatMap Observable.empty() else -> return@flatMap Observable.empty()
} }
Observable.just(step to null as PackageInstaller.SessionInfo?) Observable.just(ExtensionIntallInfo(step, null))
} }
.doOnError { .doOnError {
Timber.e(it) Timber.e(it)
} }
} }
private fun pollInstallStatus(id: Long): Observable<Pair<InstallStep, PackageInstaller.SessionInfo?>> { private fun pollInstallStatus(id: Long): Observable<ExtensionIntallInfo> {
return Observable.interval(0, 250, TimeUnit.MILLISECONDS) return Observable.interval(0, 500, TimeUnit.MILLISECONDS)
.flatMap { .flatMap {
val sessionId = downloadInstallerMap[id] ?: return@flatMap Observable.empty() val sessionId = downloadInstallerMap[id] ?: return@flatMap Observable.empty()
val session = context.packageManager.packageInstaller.getSessionInfo(sessionId) val session = context.packageManager.packageInstaller.getSessionInfo(sessionId)

View File

@ -30,6 +30,7 @@ import uy.kohesive.injekt.api.get
typealias ExtensionTuple = typealias ExtensionTuple =
Triple<List<Extension.Installed>, List<Extension.Untrusted>, List<Extension.Available>> Triple<List<Extension.Installed>, List<Extension.Untrusted>, List<Extension.Available>>
typealias ExtensionIntallInfo = Pair<InstallStep, PackageInstaller.SessionInfo?>
/** /**
* Presenter of [ExtensionBottomSheet]. * Presenter of [ExtensionBottomSheet].
@ -48,7 +49,7 @@ class ExtensionBottomPresenter(
var mangaItems = hashMapOf<Long, List<MangaItem>>() var mangaItems = hashMapOf<Long, List<MangaItem>>()
private set private set
private var currentDownloads = hashMapOf<String, InstallStep>() private var currentDownloads = hashMapOf<String, ExtensionIntallInfo>()
private val sourceManager: SourceManager = Injekt.get() private val sourceManager: SourceManager = Injekt.get()
@ -247,8 +248,8 @@ class ExtensionBottomPresenter(
extensionManager.updateExtension(extension).subscribeToInstallUpdate(extension) extensionManager.updateExtension(extension).subscribeToInstallUpdate(extension)
} }
private fun Observable<Pair<InstallStep, PackageInstaller.SessionInfo?>>.subscribeToInstallUpdate(extension: Extension) { private fun Observable<ExtensionIntallInfo>.subscribeToInstallUpdate(extension: Extension) {
this.doOnNext { currentDownloads[extension.pkgName] = it.first } this.doOnNext { currentDownloads[extension.pkgName] = it }
.doOnUnsubscribe { currentDownloads.remove(extension.pkgName) } .doOnUnsubscribe { currentDownloads.remove(extension.pkgName) }
.map { state -> updateInstallStep(extension, state.first, state.second) } .map { state -> updateInstallStep(extension, state.first, state.second) }
.subscribe { item -> .subscribe { item ->

View File

@ -25,6 +25,12 @@ data class ExtensionItem(
) : ) :
AbstractSectionableItem<ExtensionHolder, ExtensionGroupItem>(header) { AbstractSectionableItem<ExtensionHolder, ExtensionGroupItem>(header) {
constructor(
extension: Extension,
header: ExtensionGroupItem? = null,
installInfo: ExtensionIntallInfo?
) : this(extension, header, installInfo?.first, installInfo?.second)
val sessionProgress: Int? val sessionProgress: Int?
get() = (session?.progress?.times(100)?.toInt()) get() = (session?.progress?.times(100)?.toInt())