diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/module/ModuleFragment.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/module/ModuleFragment.kt index c2e7b3ff5..4ee37fa2a 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/module/ModuleFragment.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/module/ModuleFragment.kt @@ -1,45 +1,71 @@ package com.topjohnwu.magisk.ui.module import android.os.Bundle +import android.view.LayoutInflater import android.view.View -import com.topjohnwu.magisk.R -import com.topjohnwu.magisk.arch.BaseFragment -import com.topjohnwu.magisk.arch.viewModel +import android.view.ViewGroup +import androidx.compose.ui.platform.ComposeView +import androidx.compose.ui.platform.ViewCompositionStrategy +import androidx.fragment.app.Fragment +import androidx.lifecycle.ViewModelProvider +import com.topjohnwu.magisk.arch.ActivityExecutor +import com.topjohnwu.magisk.arch.ContextExecutor +import com.topjohnwu.magisk.arch.NavigationActivity +import com.topjohnwu.magisk.arch.UIActivity +import com.topjohnwu.magisk.arch.VMFactory +import com.topjohnwu.magisk.arch.ViewEvent +import com.topjohnwu.magisk.arch.ViewModelHolder import com.topjohnwu.magisk.core.utils.MediaStoreUtils.displayName -import com.topjohnwu.magisk.databinding.FragmentModuleMd2Binding -import rikka.recyclerview.addEdgeSpacing -import rikka.recyclerview.addInvalidateItemDecorationsObserver -import rikka.recyclerview.addItemSpacing -import rikka.recyclerview.fixEdgeEffect +import com.topjohnwu.magisk.ui.theme.MagiskTheme import com.topjohnwu.magisk.core.R as CoreR -class ModuleFragment : BaseFragment() { +class ModuleFragment : Fragment(), ViewModelHolder { - override val layoutRes = R.layout.fragment_module_md2 - override val viewModel by viewModel() + override val viewModel by lazy { + ViewModelProvider(this, VMFactory)[ModuleViewModel::class.java] + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + startObserveLiveData() + } override fun onStart() { super.onStart() - activity?.title = resources.getString(CoreR.string.modules) - viewModel.data.observe(this) { + (activity as? NavigationActivity<*>)?.setTitle(CoreR.string.modules) + (viewModel as ModuleViewModel).data.observe(this) { it ?: return@observe + val vm = viewModel as ModuleViewModel val displayName = runCatching { it.displayName }.getOrNull() ?: return@observe - viewModel.requestInstallLocalModule(it, displayName) - viewModel.data.value = null + vm.requestInstallLocalModule(it, displayName) + vm.data.value = null } } - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) - - binding.moduleList.apply { - addEdgeSpacing(top = R.dimen.l_50, bottom = R.dimen.l1) - addItemSpacing(R.dimen.l1, R.dimen.l_50, R.dimen.l1) - fixEdgeEffect() - post { addInvalidateItemDecorationsObserver() } + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + return ComposeView(requireContext()).apply { + setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed) + setContent { + MagiskTheme { + ModuleScreen(viewModel = viewModel as ModuleViewModel) + } + } } } - override fun onPreBind(binding: FragmentModuleMd2Binding) = Unit + override fun onResume() { + super.onResume() + (viewModel as ModuleViewModel).startLoading() + } + override fun onEventDispatched(event: ViewEvent) { + when (event) { + is ContextExecutor -> event(requireContext()) + is ActivityExecutor -> (activity as? UIActivity<*>)?.let { event(it) } + } + } } diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/module/ModuleRvItem.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/module/ModuleRvItem.kt deleted file mode 100644 index 97c164db0..000000000 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/module/ModuleRvItem.kt +++ /dev/null @@ -1,78 +0,0 @@ -package com.topjohnwu.magisk.ui.module - -import androidx.databinding.Bindable -import com.topjohnwu.magisk.BR -import com.topjohnwu.magisk.R -import com.topjohnwu.magisk.core.Info -import com.topjohnwu.magisk.core.model.module.LocalModule -import com.topjohnwu.magisk.databinding.DiffItem -import com.topjohnwu.magisk.databinding.ItemWrapper -import com.topjohnwu.magisk.databinding.ObservableRvItem -import com.topjohnwu.magisk.databinding.RvItem -import com.topjohnwu.magisk.databinding.set -import com.topjohnwu.magisk.utils.TextHolder -import com.topjohnwu.magisk.utils.asText -import com.topjohnwu.magisk.core.R as CoreR - -object InstallModule : RvItem(), DiffItem { - override val layoutRes = R.layout.item_module_download -} - -class LocalModuleRvItem( - override val item: LocalModule -) : ObservableRvItem(), DiffItem, ItemWrapper { - - override val layoutRes = R.layout.item_module_md2 - - val showNotice: Boolean - val showAction: Boolean - val noticeText: TextHolder - - init { - val isZygisk = item.isZygisk - val isRiru = item.isRiru - val zygiskUnloaded = isZygisk && item.zygiskUnloaded - - showNotice = zygiskUnloaded || - (Info.isZygiskEnabled && isRiru) || - (!Info.isZygiskEnabled && isZygisk) - showAction = item.hasAction && !showNotice - noticeText = - when { - zygiskUnloaded -> CoreR.string.zygisk_module_unloaded.asText() - isRiru -> CoreR.string.suspend_text_riru.asText(CoreR.string.zygisk.asText()) - else -> CoreR.string.suspend_text_zygisk.asText(CoreR.string.zygisk.asText()) - } - } - - @get:Bindable - var isEnabled = item.enable - set(value) = set(value, field, { field = it }, BR.enabled, BR.updateReady) { - item.enable = value - } - - @get:Bindable - var isRemoved = item.remove - set(value) = set(value, field, { field = it }, BR.removed, BR.updateReady) { - item.remove = value - } - - @get:Bindable - val showUpdate get() = item.updateInfo != null - - @get:Bindable - val updateReady get() = item.outdated && !isRemoved && isEnabled - - val isUpdated = item.updated - - fun fetchedUpdateInfo() { - notifyPropertyChanged(BR.showUpdate) - notifyPropertyChanged(BR.updateReady) - } - - fun delete() { - isRemoved = !isRemoved - } - - override fun itemSameAs(other: LocalModuleRvItem): Boolean = item.id == other.item.id -} diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/module/ModuleScreen.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/module/ModuleScreen.kt new file mode 100644 index 000000000..4408553d6 --- /dev/null +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/module/ModuleScreen.kt @@ -0,0 +1,179 @@ +package com.topjohnwu.magisk.ui.module + +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.items +import androidx.compose.runtime.Composable +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.alpha +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.style.TextDecoration +import androidx.compose.ui.unit.dp +import top.yukonga.miuix.kmp.basic.Card +import top.yukonga.miuix.kmp.basic.CircularProgressIndicator +import top.yukonga.miuix.kmp.basic.Switch +import top.yukonga.miuix.kmp.basic.Text +import top.yukonga.miuix.kmp.basic.TextButton +import top.yukonga.miuix.kmp.theme.MiuixTheme +import com.topjohnwu.magisk.core.R as CoreR + +@Composable +fun ModuleScreen(viewModel: ModuleViewModel) { + val uiState by viewModel.uiState.collectAsState() + + if (uiState.loading) { + Box( + modifier = Modifier.fillMaxSize(), + contentAlignment = Alignment.Center + ) { + CircularProgressIndicator() + } + return + } + + LazyColumn( + modifier = Modifier + .fillMaxSize() + .padding(horizontal = 12.dp), + verticalArrangement = Arrangement.spacedBy(8.dp) + ) { + item { Spacer(Modifier.height(4.dp)) } + + item { + TextButton( + text = stringResource(CoreR.string.module_action_install_external), + onClick = { viewModel.installPressed() }, + modifier = Modifier.fillMaxWidth() + ) + } + + if (uiState.modules.isEmpty()) { + item { + Box( + modifier = Modifier + .fillMaxWidth() + .padding(vertical = 32.dp), + contentAlignment = Alignment.Center + ) { + Text( + text = stringResource(CoreR.string.module_empty), + style = MiuixTheme.textStyles.body1, + color = MiuixTheme.colorScheme.onSurfaceVariantSummary + ) + } + } + } else { + items(uiState.modules, key = { it.module.id }) { item -> + ModuleCard(item = item, viewModel = viewModel) + } + } + + item { Spacer(Modifier.height(4.dp)) } + } +} + +@Composable +private fun ModuleCard(item: ModuleItem, viewModel: ModuleViewModel) { + val cardAlpha = if (!item.isRemoved && item.isEnabled && !item.showNotice) 1f else 0.5f + val strikeThrough = if (item.isRemoved) TextDecoration.LineThrough else TextDecoration.None + + Card( + modifier = Modifier + .fillMaxWidth() + .alpha(cardAlpha) + ) { + Column(modifier = Modifier.padding(16.dp)) { + Row( + modifier = Modifier.fillMaxWidth(), + verticalAlignment = Alignment.CenterVertically + ) { + Column(modifier = Modifier.weight(1f)) { + Text( + text = item.module.name, + style = MiuixTheme.textStyles.body1, + textDecoration = strikeThrough, + ) + Text( + text = stringResource( + CoreR.string.module_version_author, + item.module.version, + item.module.author + ), + style = MiuixTheme.textStyles.body2, + color = MiuixTheme.colorScheme.onSurfaceVariantSummary, + textDecoration = strikeThrough, + ) + } + Switch( + checked = item.isEnabled, + onCheckedChange = { viewModel.toggleEnabled(item) } + ) + } + + if (item.module.description.isNotEmpty()) { + Spacer(Modifier.height(8.dp)) + Text( + text = item.module.description, + style = MiuixTheme.textStyles.body2, + color = MiuixTheme.colorScheme.onSurfaceVariantSummary, + textDecoration = strikeThrough, + maxLines = 5, + ) + } + + if (item.showNotice) { + Spacer(Modifier.height(8.dp)) + Text( + text = item.noticeText, + style = MiuixTheme.textStyles.body2, + color = MiuixTheme.colorScheme.primary, + ) + } + + Spacer(Modifier.height(8.dp)) + + Row( + modifier = Modifier.fillMaxWidth(), + horizontalArrangement = Arrangement.spacedBy(8.dp) + ) { + if (item.showAction) { + TextButton( + text = stringResource(CoreR.string.module_action), + onClick = { viewModel.runAction(item.module.id, item.module.name) }, + enabled = item.isEnabled + ) + } + + if (item.showUpdate) { + TextButton( + text = stringResource(CoreR.string.update), + onClick = { viewModel.downloadPressed(item.module.updateInfo) }, + enabled = item.updateReady + ) + } + + Spacer(Modifier.weight(1f)) + + TextButton( + text = stringResource( + if (item.isRemoved) CoreR.string.module_state_restore + else CoreR.string.module_state_remove + ), + onClick = { viewModel.toggleRemove(item) }, + enabled = !item.isUpdated + ) + } + } + } +} diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/module/ModuleViewModel.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/module/ModuleViewModel.kt index 863206338..ec8795862 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/module/ModuleViewModel.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/module/ModuleViewModel.kt @@ -1,77 +1,93 @@ package com.topjohnwu.magisk.ui.module import android.net.Uri -import androidx.databinding.Bindable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.setValue import androidx.lifecycle.MutableLiveData -import com.topjohnwu.magisk.BR import com.topjohnwu.magisk.MainDirections -import com.topjohnwu.magisk.R import com.topjohnwu.magisk.arch.AsyncLoadViewModel import com.topjohnwu.magisk.core.Const import com.topjohnwu.magisk.core.Info import com.topjohnwu.magisk.core.base.ContentResultCallback import com.topjohnwu.magisk.core.model.module.LocalModule import com.topjohnwu.magisk.core.model.module.OnlineModule -import com.topjohnwu.magisk.databinding.MergeObservableList -import com.topjohnwu.magisk.databinding.RvItem -import com.topjohnwu.magisk.databinding.bindExtra -import com.topjohnwu.magisk.databinding.diffList -import com.topjohnwu.magisk.databinding.set import com.topjohnwu.magisk.dialog.LocalModuleInstallDialog import com.topjohnwu.magisk.dialog.OnlineModuleInstallDialog import com.topjohnwu.magisk.events.GetContentEvent import com.topjohnwu.magisk.events.SnackbarEvent import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.update import kotlinx.coroutines.withContext import kotlinx.parcelize.Parcelize import com.topjohnwu.magisk.core.R as CoreR +class ModuleItem(val module: LocalModule) { + val showNotice: Boolean + val showAction: Boolean + val noticeText: String + + init { + val isZygisk = module.isZygisk + val isRiru = module.isRiru + val zygiskUnloaded = isZygisk && module.zygiskUnloaded + + showNotice = zygiskUnloaded || + (Info.isZygiskEnabled && isRiru) || + (!Info.isZygiskEnabled && isZygisk) + showAction = module.hasAction && !showNotice + noticeText = when { + zygiskUnloaded -> "Zygisk module not loaded due to incompatibility" + isRiru -> "Module suspended because Zygisk is enabled" + else -> "Module suspended because Zygisk isn't enabled" + } + } + + var isEnabled by mutableStateOf(module.enable) + var isRemoved by mutableStateOf(module.remove) + var showUpdate by mutableStateOf(module.updateInfo != null) + val isUpdated = module.updated + val updateReady get() = module.outdated && !isRemoved && isEnabled +} + class ModuleViewModel : AsyncLoadViewModel() { - val bottomBarBarrierIds = intArrayOf(R.id.module_update, R.id.module_remove) + data class UiState( + val loading: Boolean = true, + val modules: List = emptyList(), + ) - private val itemsInstalled = diffList() - - val items = MergeObservableList() - val extraBindings = bindExtra { - it.put(BR.viewModel, this) - } + private val _uiState = MutableStateFlow(UiState()) + val uiState: StateFlow = _uiState.asStateFlow() val data get() = uri - @get:Bindable - var loading = true - private set(value) = set(value, field, { field = it }, BR.loading) - override suspend fun doLoadWork() { - loading = true + _uiState.update { it.copy(loading = true) } val moduleLoaded = Info.env.isActive && - withContext(Dispatchers.IO) { LocalModule.loaded() } + withContext(Dispatchers.IO) { LocalModule.loaded() } if (moduleLoaded) { - loadInstalled() - if (items.isEmpty()) { - items.insertItem(InstallModule) - .insertList(itemsInstalled) + val modules = withContext(Dispatchers.Default) { + LocalModule.installed().map { ModuleItem(it) } } + _uiState.update { it.copy(loading = false, modules = modules) } + loadUpdateInfo() + } else { + _uiState.update { it.copy(loading = false) } } - loading = false - loadUpdateInfo() } override fun onNetworkChanged(network: Boolean) = startLoading() - private suspend fun loadInstalled() { - withContext(Dispatchers.Default) { - val installed = LocalModule.installed().map { LocalModuleRvItem(it) } - itemsInstalled.update(installed) - } - } - private suspend fun loadUpdateInfo() { withContext(Dispatchers.IO) { - itemsInstalled.forEach { - if (it.item.fetch()) - it.fetchedUpdateInfo() + _uiState.value.modules.forEach { item -> + if (item.module.fetch()) { + item.showUpdate = item.module.updateInfo != null + } } } } @@ -102,6 +118,16 @@ class ModuleViewModel : AsyncLoadViewModel() { MainDirections.actionActionFragment(id, name).navigate() } + fun toggleEnabled(item: ModuleItem) { + item.isEnabled = !item.isEnabled + item.module.enable = item.isEnabled + } + + fun toggleRemove(item: ModuleItem) { + item.isRemoved = !item.isRemoved + item.module.remove = item.isRemoved + } + companion object { private val uri = MutableLiveData() } diff --git a/app/apk/src/main/res/layout/fragment_module_md2.xml b/app/apk/src/main/res/layout/fragment_module_md2.xml deleted file mode 100644 index 11ceb23de..000000000 --- a/app/apk/src/main/res/layout/fragment_module_md2.xml +++ /dev/null @@ -1,65 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/app/apk/src/main/res/layout/item_module_download.xml b/app/apk/src/main/res/layout/item_module_download.xml deleted file mode 100644 index 6f444bcd2..000000000 --- a/app/apk/src/main/res/layout/item_module_download.xml +++ /dev/null @@ -1,29 +0,0 @@ - - - - - - - - - - - -