mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-02-20 07:38:29 +00:00
Cleanup fragment navigations
This commit is contained in:
parent
a9b3b7a359
commit
bd935b0553
@ -10,7 +10,6 @@ import androidx.appcompat.app.AppCompatDelegate
|
|||||||
import androidx.core.content.res.use
|
import androidx.core.content.res.use
|
||||||
import androidx.databinding.DataBindingUtil
|
import androidx.databinding.DataBindingUtil
|
||||||
import androidx.databinding.ViewDataBinding
|
import androidx.databinding.ViewDataBinding
|
||||||
import androidx.lifecycle.MutableLiveData
|
|
||||||
import androidx.navigation.NavController
|
import androidx.navigation.NavController
|
||||||
import androidx.navigation.NavDirections
|
import androidx.navigation.NavDirections
|
||||||
import androidx.navigation.fragment.NavHostFragment
|
import androidx.navigation.fragment.NavHostFragment
|
||||||
@ -28,7 +27,7 @@ abstract class BaseUIActivity<VM : BaseViewModel, Binding : ViewDataBinding> :
|
|||||||
protected open val themeRes: Int = Theme.selected.themeRes
|
protected open val themeRes: Int = Theme.selected.themeRes
|
||||||
|
|
||||||
private val navHostFragment by lazy {
|
private val navHostFragment by lazy {
|
||||||
supportFragmentManager.findFragmentById(navHost) as? NavHostFragment
|
supportFragmentManager.findFragmentById(navHostId) as? NavHostFragment
|
||||||
}
|
}
|
||||||
private val topFragment get() = navHostFragment?.childFragmentManager?.fragments?.getOrNull(0)
|
private val topFragment get() = navHostFragment?.childFragmentManager?.fragments?.getOrNull(0)
|
||||||
protected val currentFragment get() = topFragment as? BaseUIFragment<*, *>
|
protected val currentFragment get() = topFragment as? BaseUIFragment<*, *>
|
||||||
@ -36,7 +35,7 @@ abstract class BaseUIActivity<VM : BaseViewModel, Binding : ViewDataBinding> :
|
|||||||
override val viewRoot: View get() = binding.root
|
override val viewRoot: View get() = binding.root
|
||||||
open val navigation: NavController? get() = navHostFragment?.navController
|
open val navigation: NavController? get() = navHostFragment?.navController
|
||||||
|
|
||||||
open val navHost: Int = 0
|
open val navHostId: Int = 0
|
||||||
open val snackbarView get() = binding.root
|
open val snackbarView get() = binding.root
|
||||||
|
|
||||||
init {
|
init {
|
||||||
@ -58,14 +57,6 @@ abstract class BaseUIActivity<VM : BaseViewModel, Binding : ViewDataBinding> :
|
|||||||
.use { it.getDrawable(0) }
|
.use { it.getDrawable(0) }
|
||||||
.also { window.setBackgroundDrawable(it) }
|
.also { window.setBackgroundDrawable(it) }
|
||||||
|
|
||||||
directionsDispatcher.observe(this) {
|
|
||||||
it?.navigate()
|
|
||||||
// we don't want the directions to be re-dispatched, so we preemptively set them to null
|
|
||||||
if (it != null) {
|
|
||||||
directionsDispatcher.value = null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||||
window?.decorView?.let {
|
window?.decorView?.let {
|
||||||
it.systemUiVisibility = (it.systemUiVisibility
|
it.systemUiVisibility = (it.systemUiVisibility
|
||||||
@ -127,14 +118,4 @@ abstract class BaseUIActivity<VM : BaseViewModel, Binding : ViewDataBinding> :
|
|||||||
fun NavDirections.navigate() {
|
fun NavDirections.navigate() {
|
||||||
navigation?.navigate(this)
|
navigation?.navigate(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
|
||||||
|
|
||||||
private val directionsDispatcher = MutableLiveData<NavDirections?>()
|
|
||||||
|
|
||||||
fun postDirections(navDirections: NavDirections) =
|
|
||||||
directionsDispatcher.postValue(navDirections)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package com.topjohnwu.magisk.arch
|
package com.topjohnwu.magisk.arch
|
||||||
|
|
||||||
import android.Manifest
|
import android.Manifest
|
||||||
import android.os.Build
|
|
||||||
import androidx.annotation.CallSuper
|
import androidx.annotation.CallSuper
|
||||||
import androidx.core.graphics.Insets
|
import androidx.core.graphics.Insets
|
||||||
import androidx.databinding.Bindable
|
import androidx.databinding.Bindable
|
||||||
@ -107,7 +106,7 @@ abstract class BaseViewModel(
|
|||||||
_viewEvents.postValue(this)
|
_viewEvents.postValue(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun NavDirections.publish() {
|
fun NavDirections.navigate() {
|
||||||
_viewEvents.postValue(NavigationEvent(this))
|
_viewEvents.postValue(NavigationEvent(this))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,6 +7,8 @@ import android.content.Context
|
|||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import androidx.core.net.toFile
|
import androidx.core.net.toFile
|
||||||
|
import com.topjohnwu.magisk.arch.BaseUIActivity
|
||||||
|
import com.topjohnwu.magisk.core.ForegroundTracker
|
||||||
import com.topjohnwu.magisk.core.download.Action.Flash
|
import com.topjohnwu.magisk.core.download.Action.Flash
|
||||||
import com.topjohnwu.magisk.core.download.Subject.Manager
|
import com.topjohnwu.magisk.core.download.Subject.Manager
|
||||||
import com.topjohnwu.magisk.core.download.Subject.Module
|
import com.topjohnwu.magisk.core.download.Subject.Module
|
||||||
@ -26,7 +28,11 @@ open class DownloadService : BaseDownloader() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun Module.onFinish(id: Int) = when (action) {
|
private fun Module.onFinish(id: Int) = when (action) {
|
||||||
Flash -> FlashFragment.install(file, id)
|
Flash -> {
|
||||||
|
(ForegroundTracker.foreground as? BaseUIActivity<*, *>)
|
||||||
|
?.navigation?.navigate(FlashFragment.install(file, id))
|
||||||
|
Unit
|
||||||
|
}
|
||||||
else -> Unit
|
else -> Unit
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ open class MainActivity : BaseUIActivity<MainViewModel, ActivityMainMd2Binding>(
|
|||||||
|
|
||||||
override val layoutRes = R.layout.activity_main_md2
|
override val layoutRes = R.layout.activity_main_md2
|
||||||
override val viewModel by viewModel<MainViewModel>()
|
override val viewModel by viewModel<MainViewModel>()
|
||||||
override val navHost: Int = R.id.main_nav_host
|
override val navHostId: Int = R.id.main_nav_host
|
||||||
|
|
||||||
private var isRootFragment = true
|
private var isRootFragment = true
|
||||||
|
|
||||||
@ -166,9 +166,9 @@ open class MainActivity : BaseUIActivity<MainViewModel, ActivityMainMd2Binding>(
|
|||||||
|
|
||||||
private fun getScreen(name: String?): NavDirections? {
|
private fun getScreen(name: String?): NavDirections? {
|
||||||
return when (name) {
|
return when (name) {
|
||||||
Const.Nav.SUPERUSER -> HomeFragmentDirections.actionSuperuserFragment()
|
Const.Nav.SUPERUSER -> MainDirections.actionSuperuserFragment()
|
||||||
Const.Nav.HIDE -> HomeFragmentDirections.actionHideFragment()
|
Const.Nav.HIDE -> MainDirections.actionHideFragment()
|
||||||
Const.Nav.MODULES -> HomeFragmentDirections.actionModuleFragment()
|
Const.Nav.MODULES -> MainDirections.actionModuleFragment()
|
||||||
Const.Nav.SETTINGS -> HomeFragmentDirections.actionHomeFragmentToSettingsFragment()
|
Const.Nav.SETTINGS -> HomeFragmentDirections.actionHomeFragmentToSettingsFragment()
|
||||||
else -> null
|
else -> null
|
||||||
}
|
}
|
||||||
|
@ -7,8 +7,8 @@ import android.net.Uri
|
|||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.view.*
|
import android.view.*
|
||||||
import androidx.navigation.NavDeepLinkBuilder
|
import androidx.navigation.NavDeepLinkBuilder
|
||||||
|
import com.topjohnwu.magisk.MainDirections
|
||||||
import com.topjohnwu.magisk.R
|
import com.topjohnwu.magisk.R
|
||||||
import com.topjohnwu.magisk.arch.BaseUIActivity
|
|
||||||
import com.topjohnwu.magisk.arch.BaseUIFragment
|
import com.topjohnwu.magisk.arch.BaseUIFragment
|
||||||
import com.topjohnwu.magisk.core.Const
|
import com.topjohnwu.magisk.core.Const
|
||||||
import com.topjohnwu.magisk.core.cmp
|
import com.topjohnwu.magisk.core.cmp
|
||||||
@ -16,7 +16,6 @@ import com.topjohnwu.magisk.databinding.FragmentFlashMd2Binding
|
|||||||
import com.topjohnwu.magisk.ui.MainActivity
|
import com.topjohnwu.magisk.ui.MainActivity
|
||||||
import org.koin.androidx.viewmodel.ext.android.viewModel
|
import org.koin.androidx.viewmodel.ext.android.viewModel
|
||||||
import org.koin.core.parameter.parametersOf
|
import org.koin.core.parameter.parametersOf
|
||||||
import com.topjohnwu.magisk.MainDirections.Companion.actionFlashFragment as toFlash
|
|
||||||
|
|
||||||
class FlashFragment : BaseUIFragment<FlashViewModel, FragmentFlashMd2Binding>() {
|
class FlashFragment : BaseUIFragment<FlashViewModel, FragmentFlashMd2Binding>() {
|
||||||
|
|
||||||
@ -91,22 +90,22 @@ class FlashFragment : BaseUIFragment<FlashViewModel, FragmentFlashMd2Binding>()
|
|||||||
|
|
||||||
/* Flashing is understood as installing / flashing magisk itself */
|
/* Flashing is understood as installing / flashing magisk itself */
|
||||||
|
|
||||||
fun flash(isSecondSlot: Boolean) = toFlash(
|
fun flash(isSecondSlot: Boolean) = MainDirections.actionFlashFragment(
|
||||||
action = flashType(isSecondSlot)
|
action = flashType(isSecondSlot)
|
||||||
).let { BaseUIActivity.postDirections(it) }
|
)
|
||||||
|
|
||||||
/* Patching is understood as injecting img files with magisk */
|
/* Patching is understood as injecting img files with magisk */
|
||||||
|
|
||||||
fun patch(uri: Uri) = toFlash(
|
fun patch(uri: Uri) = MainDirections.actionFlashFragment(
|
||||||
action = Const.Value.PATCH_FILE,
|
action = Const.Value.PATCH_FILE,
|
||||||
additionalData = uri
|
additionalData = uri
|
||||||
).let { BaseUIActivity.postDirections(it) }
|
)
|
||||||
|
|
||||||
/* Uninstalling is understood as removing magisk entirely */
|
/* Uninstalling is understood as removing magisk entirely */
|
||||||
|
|
||||||
fun uninstall() = toFlash(
|
fun uninstall() = MainDirections.actionFlashFragment(
|
||||||
action = Const.Value.UNINSTALL
|
action = Const.Value.UNINSTALL
|
||||||
).let { BaseUIActivity.postDirections(it) }
|
)
|
||||||
|
|
||||||
/* Installing is understood as flashing modules / zips */
|
/* Installing is understood as flashing modules / zips */
|
||||||
|
|
||||||
@ -116,11 +115,11 @@ class FlashFragment : BaseUIFragment<FlashViewModel, FragmentFlashMd2Binding>()
|
|||||||
dismissId = id
|
dismissId = id
|
||||||
).let { createIntent(context, it) }
|
).let { createIntent(context, it) }
|
||||||
|
|
||||||
fun install(file: Uri, id: Int) = toFlash(
|
fun install(file: Uri, id: Int) = MainDirections.actionFlashFragment(
|
||||||
action = Const.Value.FLASH_ZIP,
|
action = Const.Value.FLASH_ZIP,
|
||||||
additionalData = file,
|
additionalData = file,
|
||||||
dismissId = id
|
dismissId = id
|
||||||
).let { BaseUIActivity.postDirections(it) }
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -127,11 +127,11 @@ class HomeViewModel(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun onMagiskPressed() = withExternalRW {
|
fun onMagiskPressed() = withExternalRW {
|
||||||
HomeFragmentDirections.actionHomeFragmentToInstallFragment().publish()
|
HomeFragmentDirections.actionHomeFragmentToInstallFragment().navigate()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onSafetyNetPressed() =
|
fun onSafetyNetPressed() =
|
||||||
HomeFragmentDirections.actionHomeFragmentToSafetynetFragment().publish()
|
HomeFragmentDirections.actionHomeFragmentToSafetynetFragment().navigate()
|
||||||
|
|
||||||
fun hideNotice() {
|
fun hideNotice() {
|
||||||
Config.safetyNotice = false
|
Config.safetyNotice = false
|
||||||
|
@ -89,9 +89,9 @@ class InstallViewModel(
|
|||||||
|
|
||||||
fun install() {
|
fun install() {
|
||||||
when (method) {
|
when (method) {
|
||||||
R.id.method_patch -> FlashFragment.patch(data!!)
|
R.id.method_patch -> FlashFragment.patch(data!!).navigate()
|
||||||
R.id.method_direct -> FlashFragment.flash(false)
|
R.id.method_direct -> FlashFragment.flash(false).navigate()
|
||||||
R.id.method_inactive_slot -> FlashFragment.flash(true)
|
R.id.method_inactive_slot -> FlashFragment.flash(true).navigate()
|
||||||
else -> error("Unknown value")
|
else -> error("Unknown value")
|
||||||
}
|
}
|
||||||
state = State.LOADING
|
state = State.LOADING
|
||||||
|
@ -103,7 +103,7 @@ class SettingsViewModel(
|
|||||||
override fun onItemPressed(view: View, item: BaseSettingsItem, callback: () -> Unit) = when (item) {
|
override fun onItemPressed(view: View, item: BaseSettingsItem, callback: () -> Unit) = when (item) {
|
||||||
is DownloadPath -> withExternalRW(callback)
|
is DownloadPath -> withExternalRW(callback)
|
||||||
is Biometrics -> authenticate(callback)
|
is Biometrics -> authenticate(callback)
|
||||||
is Theme -> SettingsFragmentDirections.actionSettingsFragmentToThemeFragment().publish()
|
is Theme -> SettingsFragmentDirections.actionSettingsFragmentToThemeFragment().navigate()
|
||||||
is ClearRepoCache -> clearRepoCache()
|
is ClearRepoCache -> clearRepoCache()
|
||||||
is SystemlessHosts -> createHosts()
|
is SystemlessHosts -> createHosts()
|
||||||
is Restore -> HideAPK.restore(view.activity)
|
is Restore -> HideAPK.restore(view.activity)
|
||||||
|
@ -80,7 +80,7 @@ class SuperuserViewModel(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun hidePressed() =
|
private fun hidePressed() =
|
||||||
SuperuserFragmentDirections.actionSuperuserFragmentToHideFragment().publish()
|
SuperuserFragmentDirections.actionSuperuserFragmentToHideFragment().navigate()
|
||||||
|
|
||||||
fun deletePressed(item: PolicyRvItem) {
|
fun deletePressed(item: PolicyRvItem) {
|
||||||
fun updateState() = viewModelScope.launch {
|
fun updateState() = viewModelScope.launch {
|
||||||
|
@ -145,8 +145,7 @@
|
|||||||
app:exitAnim="@anim/fragment_exit"
|
app:exitAnim="@anim/fragment_exit"
|
||||||
app:popEnterAnim="@anim/fragment_enter_pop"
|
app:popEnterAnim="@anim/fragment_enter_pop"
|
||||||
app:popExitAnim="@anim/fragment_exit_pop"
|
app:popExitAnim="@anim/fragment_exit_pop"
|
||||||
app:popUpTo="@id/superuserFragment"
|
app:popUpTo="@id/homeFragment" />
|
||||||
app:popUpToInclusive="true" />
|
|
||||||
|
|
||||||
<action
|
<action
|
||||||
android:id="@+id/action_logFragment"
|
android:id="@+id/action_logFragment"
|
||||||
@ -155,8 +154,7 @@
|
|||||||
app:exitAnim="@anim/fragment_exit"
|
app:exitAnim="@anim/fragment_exit"
|
||||||
app:popEnterAnim="@anim/fragment_enter_pop"
|
app:popEnterAnim="@anim/fragment_enter_pop"
|
||||||
app:popExitAnim="@anim/fragment_exit_pop"
|
app:popExitAnim="@anim/fragment_exit_pop"
|
||||||
app:popUpTo="@id/logFragment"
|
app:popUpTo="@id/homeFragment" />
|
||||||
app:popUpToInclusive="true" />
|
|
||||||
|
|
||||||
<action
|
<action
|
||||||
android:id="@+id/action_moduleFragment"
|
android:id="@+id/action_moduleFragment"
|
||||||
@ -165,8 +163,7 @@
|
|||||||
app:exitAnim="@anim/fragment_exit"
|
app:exitAnim="@anim/fragment_exit"
|
||||||
app:popEnterAnim="@anim/fragment_enter_pop"
|
app:popEnterAnim="@anim/fragment_enter_pop"
|
||||||
app:popExitAnim="@anim/fragment_exit_pop"
|
app:popExitAnim="@anim/fragment_exit_pop"
|
||||||
app:popUpTo="@id/modulesFragment"
|
app:popUpTo="@id/homeFragment" />
|
||||||
app:popUpToInclusive="true" />
|
|
||||||
|
|
||||||
<action
|
<action
|
||||||
android:id="@+id/action_flashFragment"
|
android:id="@+id/action_flashFragment"
|
||||||
@ -175,8 +172,7 @@
|
|||||||
app:exitAnim="@anim/fragment_exit"
|
app:exitAnim="@anim/fragment_exit"
|
||||||
app:popEnterAnim="@anim/fragment_enter_pop"
|
app:popEnterAnim="@anim/fragment_enter_pop"
|
||||||
app:popExitAnim="@anim/fragment_exit_pop"
|
app:popExitAnim="@anim/fragment_exit_pop"
|
||||||
app:popUpTo="@id/installFragment"
|
app:popUpTo="@id/homeFragment" />
|
||||||
app:popUpToInclusive="true" />
|
|
||||||
|
|
||||||
<action
|
<action
|
||||||
android:id="@+id/action_hideFragment"
|
android:id="@+id/action_hideFragment"
|
||||||
@ -184,8 +180,6 @@
|
|||||||
app:enterAnim="@anim/fragment_enter"
|
app:enterAnim="@anim/fragment_enter"
|
||||||
app:exitAnim="@anim/fragment_exit"
|
app:exitAnim="@anim/fragment_exit"
|
||||||
app:popEnterAnim="@anim/fragment_enter_pop"
|
app:popEnterAnim="@anim/fragment_enter_pop"
|
||||||
app:popExitAnim="@anim/fragment_exit_pop"
|
app:popExitAnim="@anim/fragment_exit_pop" />
|
||||||
app:popUpTo="@id/superuserFragment"
|
|
||||||
app:popUpToInclusive="false" />
|
|
||||||
|
|
||||||
</navigation>
|
</navigation>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user