Cleanup DownloadService

This commit is contained in:
topjohnwu
2020-08-21 06:27:13 -07:00
parent 4b238a9cd0
commit abc5457136
12 changed files with 263 additions and 349 deletions
@@ -4,6 +4,7 @@ import android.os.Bundle
import android.view.*
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.arch.BaseUIFragment
import com.topjohnwu.magisk.core.download.BaseDownloadService
import com.topjohnwu.magisk.databinding.FragmentHomeMd2Binding
import com.topjohnwu.magisk.events.RebootEvent
import com.topjohnwu.superuser.Shell
@@ -18,6 +19,7 @@ class HomeFragment : BaseUIFragment<HomeViewModel, FragmentHomeMd2Binding>() {
super.onStart()
activity.title = resources.getString(R.string.section_home)
setHasOptionsMenu(true)
BaseDownloadService.observeProgress(this, viewModel::onProgressUpdate)
}
override fun onCreateView(
@@ -8,8 +8,8 @@ import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.arch.*
import com.topjohnwu.magisk.core.Config
import com.topjohnwu.magisk.core.Info
import com.topjohnwu.magisk.core.download.DownloadSubject
import com.topjohnwu.magisk.core.download.DownloadSubject.Manager
import com.topjohnwu.magisk.core.download.RemoteFileService
import com.topjohnwu.magisk.core.model.MagiskJson
import com.topjohnwu.magisk.core.model.ManagerJson
import com.topjohnwu.magisk.data.repository.MagiskRepository
@@ -77,14 +77,6 @@ class HomeViewModel(
private var shownDialog = false
init {
RemoteFileService.progressBroadcast.observeForever {
when (it?.second) {
is Manager -> stateManagerProgress = it.first.times(100f).roundToInt()
}
}
}
override fun refresh() = viewModelScope.launch {
notifyPropertyChanged(BR.showUninstall)
repoMagisk.fetchUpdate()?.apply {
@@ -119,6 +111,12 @@ class HomeViewModel(
}
}.publish()
fun onProgressUpdate(progress: Float, subject: DownloadSubject) {
when (subject) {
is Manager -> stateManagerProgress = progress.times(100f).roundToInt()
}
}
fun onLinkPressed(link: String) = OpenInappLinkEvent(link).publish()
fun onDeletePressed() = UninstallDialog().publish()
@@ -4,6 +4,7 @@ import android.content.Intent
import androidx.lifecycle.viewModelScope
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.arch.BaseUIFragment
import com.topjohnwu.magisk.core.download.BaseDownloadService
import com.topjohnwu.magisk.databinding.FragmentInstallMd2Binding
import com.topjohnwu.magisk.events.RequestFileEvent
import org.koin.androidx.viewmodel.ext.android.viewModel
@@ -24,6 +25,7 @@ class InstallFragment : BaseUIFragment<InstallViewModel, FragmentInstallMd2Bindi
// Allow markwon to run in viewmodel scope
binding.releaseNotes.tag = viewModel.viewModelScope
BaseDownloadService.observeProgress(this, viewModel::onProgressUpdate)
}
}
@@ -11,7 +11,6 @@ import com.topjohnwu.magisk.core.Info
import com.topjohnwu.magisk.core.download.Configuration
import com.topjohnwu.magisk.core.download.DownloadService
import com.topjohnwu.magisk.core.download.DownloadSubject
import com.topjohnwu.magisk.core.download.RemoteFileService
import com.topjohnwu.magisk.data.repository.StringRepository
import com.topjohnwu.magisk.events.RequestFileEvent
import com.topjohnwu.magisk.events.dialog.SecondSlotWarningDialog
@@ -60,22 +59,21 @@ class InstallViewModel(
set(value) = set(value, field, { field = it }, BR.notes)
init {
RemoteFileService.reset()
RemoteFileService.progressBroadcast.observeForever {
val (progress, subject) = it ?: return@observeForever
if (subject !is DownloadSubject.Magisk) {
return@observeForever
}
this.progress = progress.times(100).roundToInt()
if (this.progress >= 100) {
state = State.LOADED
}
}
viewModelScope.launch {
notes = stringRepo.getString(Info.remote.magisk.note)
}
}
fun onProgressUpdate(progress: Float, subject: DownloadSubject) {
if (subject !is DownloadSubject.Magisk) {
return
}
this.progress = progress.times(100).roundToInt()
if (this.progress >= 100) {
state = State.LOADED
}
}
fun step(nextStep: Int) {
step = nextStep
}
@@ -12,6 +12,7 @@ import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.arch.BaseUIFragment
import com.topjohnwu.magisk.arch.ReselectionTarget
import com.topjohnwu.magisk.arch.ViewEvent
import com.topjohnwu.magisk.core.download.BaseDownloadService
import com.topjohnwu.magisk.databinding.FragmentModuleMd2Binding
import com.topjohnwu.magisk.events.InstallExternalModuleEvent
import com.topjohnwu.magisk.ktx.hideKeyboard
@@ -50,6 +51,7 @@ class ModuleFragment : BaseUIFragment<ModuleViewModel, FragmentModuleMd2Binding>
super.onStart()
setHasOptionsMenu(true)
activity.title = resources.getString(R.string.modules)
BaseDownloadService.observeProgress(this, viewModel::onProgressUpdate)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@@ -2,14 +2,12 @@ package com.topjohnwu.magisk.ui.module
import androidx.databinding.Bindable
import androidx.databinding.ObservableArrayList
import androidx.lifecycle.Observer
import androidx.lifecycle.viewModelScope
import com.topjohnwu.magisk.BR
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.arch.*
import com.topjohnwu.magisk.core.Config
import com.topjohnwu.magisk.core.download.DownloadSubject
import com.topjohnwu.magisk.core.download.RemoteFileService
import com.topjohnwu.magisk.core.model.module.Module
import com.topjohnwu.magisk.core.tasks.RepoUpdater
import com.topjohnwu.magisk.data.database.RepoByNameDao
@@ -47,7 +45,7 @@ class ModuleViewModel(
private val repoName: RepoByNameDao,
private val repoUpdated: RepoByUpdatedDao,
private val repoUpdater: RepoUpdater
) : BaseViewModel(), Queryable, Observer<Pair<Float, DownloadSubject>> {
) : BaseViewModel(), Queryable {
override val queryDelay = 1000L
private var queryJob: Job? = null
@@ -125,9 +123,6 @@ class ModuleViewModel(
// ---
init {
RemoteFileService.reset()
RemoteFileService.progressBroadcast.observeForever(this)
itemsInstalled.addOnListChangedCallback(
onItemRangeInserted = { _, _, _ ->
if (installSectionList.isEmpty())
@@ -152,13 +147,7 @@ class ModuleViewModel(
// ---
override fun onCleared() {
super.onCleared()
RemoteFileService.progressBroadcast.removeObserver(this)
}
override fun onChanged(it: Pair<Float, DownloadSubject>?) {
val (progress, subject) = it ?: return
fun onProgressUpdate(progress: Float, subject: DownloadSubject) {
if (subject !is DownloadSubject.Module)
return