From d2e171eabc7859a0db42d1d045a774c4fd752231 Mon Sep 17 00:00:00 2001 From: Viktor De Pasquale Date: Tue, 22 Oct 2019 17:29:45 +0200 Subject: [PATCH] Added a way to listen to download service from homepage and behave accordingly --- .../magisk/redesign/home/HomeFragment.kt | 14 ------- .../magisk/redesign/home/HomeViewModel.kt | 41 ++++++++++++++++++- app/src/main/res/layout/fragment_home_md2.xml | 2 +- 3 files changed, 40 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/redesign/home/HomeFragment.kt b/app/src/main/java/com/topjohnwu/magisk/redesign/home/HomeFragment.kt index 674ee0fdf..b78a466fe 100644 --- a/app/src/main/java/com/topjohnwu/magisk/redesign/home/HomeFragment.kt +++ b/app/src/main/java/com/topjohnwu/magisk/redesign/home/HomeFragment.kt @@ -1,15 +1,10 @@ package com.topjohnwu.magisk.redesign.home import android.graphics.Insets -import androidx.lifecycle.Observer import com.topjohnwu.magisk.R import com.topjohnwu.magisk.databinding.FragmentHomeMd2Binding -import com.topjohnwu.magisk.model.download.RemoteFileService -import com.topjohnwu.magisk.model.entity.internal.DownloadSubject import com.topjohnwu.magisk.redesign.compat.CompatFragment import org.koin.androidx.viewmodel.ext.android.viewModel -import timber.log.Timber -import kotlin.math.roundToInt class HomeFragment : CompatFragment() { @@ -20,15 +15,6 @@ class HomeFragment : CompatFragment() { override fun onStart() { super.onStart() - RemoteFileService.progressBroadcast.observe(this, Observer { - when (it.second) { - is DownloadSubject.Magisk.Download, - is DownloadSubject.Magisk.Flash -> viewModel.stateMagiskProgress.value = - it.first.times(100f).roundToInt().also { Timber.i("Progress: $it") } - is DownloadSubject.Manager -> viewModel.stateManagerProgress.value = - it.first.times(100f).roundToInt().also { Timber.i("Progress: $it") } - } - }) activity.title = resources.getString(R.string.section_home) } } \ No newline at end of file diff --git a/app/src/main/java/com/topjohnwu/magisk/redesign/home/HomeViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/redesign/home/HomeViewModel.kt index 26e74b2a8..c244b3a3c 100644 --- a/app/src/main/java/com/topjohnwu/magisk/redesign/home/HomeViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/redesign/home/HomeViewModel.kt @@ -1,20 +1,26 @@ package com.topjohnwu.magisk.redesign.home +import android.Manifest import com.topjohnwu.magisk.BuildConfig import com.topjohnwu.magisk.Info import com.topjohnwu.magisk.R import com.topjohnwu.magisk.data.repository.MagiskRepository import com.topjohnwu.magisk.databinding.ComparableRvItem import com.topjohnwu.magisk.extensions.* +import com.topjohnwu.magisk.model.download.DownloadService +import com.topjohnwu.magisk.model.download.RemoteFileService import com.topjohnwu.magisk.model.entity.MagiskJson import com.topjohnwu.magisk.model.entity.ManagerJson import com.topjohnwu.magisk.model.entity.UpdateInfo +import com.topjohnwu.magisk.model.entity.internal.Configuration +import com.topjohnwu.magisk.model.entity.internal.DownloadSubject.Magisk +import com.topjohnwu.magisk.model.entity.internal.DownloadSubject.Manager import com.topjohnwu.magisk.model.entity.recycler.HomeItem import com.topjohnwu.magisk.model.events.OpenInappLinkEvent import com.topjohnwu.magisk.model.events.dialog.EnvFixDialog -import com.topjohnwu.magisk.model.events.dialog.MagiskInstallDialog import com.topjohnwu.magisk.model.events.dialog.ManagerInstallDialog import com.topjohnwu.magisk.model.events.dialog.UninstallDialog +import com.topjohnwu.magisk.model.navigation.Navigation import com.topjohnwu.magisk.model.observer.Observer import com.topjohnwu.magisk.redesign.compat.CompatViewModel import com.topjohnwu.magisk.ui.home.MagiskState @@ -23,6 +29,7 @@ import com.topjohnwu.superuser.Shell import me.tatarka.bindingcollectionadapter2.BR import me.tatarka.bindingcollectionadapter2.ItemBinding import me.tatarka.bindingcollectionadapter2.OnItemBind +import kotlin.math.roundToInt class HomeViewModel( private val repoMagisk: MagiskRepository @@ -74,6 +81,22 @@ class HomeViewModel( private var shownDialog = false + init { + RemoteFileService.progressBroadcast.observeForever { + when (it.second) { + is Magisk.Download, + is Magisk.Flash -> stateMagiskProgress.value = it.first.times(100f).roundToInt() + is Manager -> stateManagerProgress.value = it.first.times(100f).roundToInt() + } + } + + stateMagiskProgress.addOnPropertyChangedCallback { + if (it == 100) { + Navigation.install().publish() + } + } + } + override fun refresh() = repoMagisk.fetchUpdate() .onErrorReturn { Info.remote } .subscribeK { updateBy(it) } @@ -116,7 +139,21 @@ class HomeViewModel( fun onManagerPressed() = ManagerInstallDialog().publish() - fun onMagiskPressed() = MagiskInstallDialog().publish() + fun onMagiskPressed() { + //pre-fix so user doesn't click twice accidentally + stateMagiskProgress.value = 1 + + withPermissions( + Manifest.permission.READ_EXTERNAL_STORAGE, + Manifest.permission.WRITE_EXTERNAL_STORAGE + ).map { check(it);it }.subscribeK(onError = { + stateManagerProgress.value = 0 + }) { + DownloadService(get()) { + subject = Magisk(Configuration.Download) + } + }.add() + } private fun ensureEnv() { val invalidStates = listOf( diff --git a/app/src/main/res/layout/fragment_home_md2.xml b/app/src/main/res/layout/fragment_home_md2.xml index c9db30fb8..0eebe6f08 100644 --- a/app/src/main/res/layout/fragment_home_md2.xml +++ b/app/src/main/res/layout/fragment_home_md2.xml @@ -133,7 +133,7 @@