manager: Add reboot shortcut for module screen

This commit is contained in:
tiann
2023-02-14 12:01:05 +08:00
parent 9a2a21ec5d
commit c691a1adb2
8 changed files with 84 additions and 76 deletions

View File

@@ -31,10 +31,7 @@ import kotlinx.coroutines.launch
import me.weishu.kernelsu.Natives import me.weishu.kernelsu.Natives
import me.weishu.kernelsu.R import me.weishu.kernelsu.R
import me.weishu.kernelsu.ui.screen.destinations.InstallScreenDestination import me.weishu.kernelsu.ui.screen.destinations.InstallScreenDestination
import me.weishu.kernelsu.ui.util.LocalSnackbarHost import me.weishu.kernelsu.ui.util.*
import me.weishu.kernelsu.ui.util.overlayFsAvailable
import me.weishu.kernelsu.ui.util.toggleModule
import me.weishu.kernelsu.ui.util.uninstallModule
import me.weishu.kernelsu.ui.viewmodel.ModuleViewModel import me.weishu.kernelsu.ui.viewmodel.ModuleViewModel
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@@ -54,42 +51,39 @@ fun ModuleScreen(navigator: DestinationsNavigator) {
val isSafeMode = Natives.isSafeMode() val isSafeMode = Natives.isSafeMode()
Scaffold( Scaffold(topBar = {
topBar = { TopBar()
TopBar() }, floatingActionButton = if (isSafeMode) {
}, { /* Empty */ }
floatingActionButton = if (isSafeMode) { } else {
{ /* Empty */ } {
} else { val moduleInstall = stringResource(id = R.string.module_install)
{ val selectZipLauncher = rememberLauncherForActivityResult(
val moduleInstall = stringResource(id = R.string.module_install) contract = ActivityResultContracts.StartActivityForResult()
val selectZipLauncher = rememberLauncherForActivityResult( ) {
contract = ActivityResultContracts.StartActivityForResult() if (it.resultCode != RESULT_OK) {
) { return@rememberLauncherForActivityResult
if (it.resultCode != RESULT_OK) {
return@rememberLauncherForActivityResult
}
val data = it.data ?: return@rememberLauncherForActivityResult
val uri = data.data ?: return@rememberLauncherForActivityResult
navigator.navigate(InstallScreenDestination(uri))
Log.i("ModuleScreen", "select zip result: ${it.data}")
} }
val data = it.data ?: return@rememberLauncherForActivityResult
val uri = data.data ?: return@rememberLauncherForActivityResult
ExtendedFloatingActionButton( navigator.navigate(InstallScreenDestination(uri))
onClick = {
// select the zip file to install Log.i("ModuleScreen", "select zip result: ${it.data}")
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.type = "application/zip"
selectZipLauncher.launch(intent)
},
icon = { Icon(Icons.Filled.Add, moduleInstall) },
text = { Text(text = moduleInstall) },
)
} }
ExtendedFloatingActionButton(
onClick = {
// select the zip file to install
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.type = "application/zip"
selectZipLauncher.launch(intent)
},
icon = { Icon(Icons.Filled.Add, moduleInstall) },
text = { Text(text = moduleInstall) },
)
} }
) { innerPadding -> }) { innerPadding ->
val failedEnable = stringResource(R.string.module_failed_to_enable) val failedEnable = stringResource(R.string.module_failed_to_enable)
val failedDisable = stringResource(R.string.module_failed_to_disable) val failedDisable = stringResource(R.string.module_failed_to_disable)
val failedUninstall = stringResource(R.string.module_uninstall_failed) val failedUninstall = stringResource(R.string.module_uninstall_failed)
@@ -103,11 +97,9 @@ fun ModuleScreen(navigator: DestinationsNavigator) {
return@Scaffold return@Scaffold
} }
SwipeRefresh( SwipeRefresh(
state = swipeState, state = swipeState, onRefresh = {
onRefresh = {
scope.launch { viewModel.fetchModuleList() } scope.launch { viewModel.fetchModuleList() }
}, }, modifier = Modifier
modifier = Modifier
.padding(innerPadding) .padding(innerPadding)
.padding(16.dp) .padding(16.dp)
.fillMaxSize() .fillMaxSize()
@@ -127,42 +119,54 @@ fun ModuleScreen(navigator: DestinationsNavigator) {
Text(stringResource(R.string.module_empty)) Text(stringResource(R.string.module_empty))
} }
} else { } else {
LazyColumn( LazyColumn(verticalArrangement = Arrangement.spacedBy(15.dp),
verticalArrangement = Arrangement.spacedBy(15.dp), contentPadding = remember { PaddingValues(bottom = 16.dp + 56.dp /* Scaffold Fab Spacing + Fab container height */) }) {
contentPadding = remember { PaddingValues(bottom = 16.dp + 56.dp /* Scaffold Fab Spacing + Fab container height */) }
) {
items(viewModel.moduleList) { module -> items(viewModel.moduleList) { module ->
var isChecked by rememberSaveable(module) { mutableStateOf(module.enabled) } var isChecked by rememberSaveable(module) { mutableStateOf(module.enabled) }
ModuleItem(module, val reboot = stringResource(id = R.string.reboot)
isChecked, val rebootToApply = stringResource(id = R.string.reboot_to_apply)
onUninstall = { ModuleItem(module, isChecked, onUninstall = {
scope.launch { scope.launch {
val result = uninstallModule(module.id) val success = uninstallModule(module.id)
if (result) {
viewModel.fetchModuleList()
}
snackBarHost.showSnackbar(
if (result) {
successUninstall.format(module.name)
} else {
failedUninstall.format(module.name)
}
)
}
},
onCheckChanged = {
val success = toggleModule(module.id, !isChecked)
if (success) { if (success) {
isChecked = it viewModel.fetchModuleList()
scope.launch { }
viewModel.fetchModuleList() val message = if (success) {
} successUninstall.format(module.name)
} else scope.launch { } else {
val message = if (isChecked) failedDisable else failedEnable failedUninstall.format(module.name)
snackBarHost.showSnackbar(message.format(module.name)) }
val actionLabel = if (success) {
reboot
} else {
null
}
val result = snackBarHost.showSnackbar(
message, actionLabel = actionLabel
)
if (result == SnackbarResult.ActionPerformed) {
reboot()
} }
} }
) }, onCheckChanged = {
val success = toggleModule(module.id, !isChecked)
if (success) {
isChecked = it
scope.launch {
viewModel.fetchModuleList()
val result = snackBarHost.showSnackbar(
rebootToApply, actionLabel = reboot
)
if (result == SnackbarResult.ActionPerformed) {
reboot()
}
}
} else scope.launch {
val message = if (isChecked) failedDisable else failedEnable
snackBarHost.showSnackbar(message.format(module.name))
}
})
// fix last item shadow incomplete in LazyColumn // fix last item shadow incomplete in LazyColumn
Spacer(Modifier.height(1.dp)) Spacer(Modifier.height(1.dp))
} }
@@ -175,9 +179,7 @@ fun ModuleScreen(navigator: DestinationsNavigator) {
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
private fun TopBar() { private fun TopBar() {
TopAppBar( TopAppBar(title = { Text(stringResource(R.string.module)) })
title = { Text(stringResource(R.string.module)) }
)
} }
@Composable @Composable

View File

@@ -1,8 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<string name="safe_mode">セーフモード</string> <string name="safe_mode">セーフモード</string>
<string name="app_name" translatable="false">KernelSU</string> <string name="home">ホーム</string>
<string name="home">ホーム</string>
<string name="home_not_installed">未インストール</string> <string name="home_not_installed">未インストール</string>
<string name="home_click_to_install">タップでインストール</string> <string name="home_click_to_install">タップでインストール</string>
<string name="home_working">動作中</string> <string name="home_working">動作中</string>
@@ -52,4 +51,5 @@
<string name="show_system_apps">システムアプリを表示</string> <string name="show_system_apps">システムアプリを表示</string>
<string name="hide_system_apps">システムアプリを非表示</string> <string name="hide_system_apps">システムアプリを非表示</string>
<string name="send_log">ログを送信</string> <string name="send_log">ログを送信</string>
<string name="reboot_to_apply">再起動して有効にする</string>
</resources> </resources>

View File

@@ -53,5 +53,6 @@
<string name="hide_system_apps">Скрыть системные приложения</string> <string name="hide_system_apps">Скрыть системные приложения</string>
<string name="send_log">Отправить лог</string> <string name="send_log">Отправить лог</string>
<string name="safe_mode">Безопасный режим</string> <string name="safe_mode">Безопасный режим</string>
<string name="reboot_to_apply">Перезагрузите, чтобы вступить в силу</string>
</resources> </resources>

View File

@@ -49,4 +49,5 @@
<string name="show_system_apps">Hiển thị ứng dụng hệ thống</string> <string name="show_system_apps">Hiển thị ứng dụng hệ thống</string>
<string name="hide_system_apps">Ẩn ứng dụng hệ thống</string> <string name="hide_system_apps">Ẩn ứng dụng hệ thống</string>
<string name="safe_mode">Chế độ an toàn</string> <string name="safe_mode">Chế độ an toàn</string>
<string name="reboot_to_apply">Khởi động lại để có hiệu lực</string>
</resources> </resources>

View File

@@ -51,4 +51,5 @@
<string name="hide_system_apps">隐藏系统应用</string> <string name="hide_system_apps">隐藏系统应用</string>
<string name="send_log">发送日志</string> <string name="send_log">发送日志</string>
<string name="safe_mode">安全模式</string> <string name="safe_mode">安全模式</string>
<string name="reboot_to_apply">重启生效</string>
</resources> </resources>

View File

@@ -47,4 +47,5 @@
<string name="module_author">作者</string> <string name="module_author">作者</string>
<string name="module_overlay_fs_not_available">內核不支持 overlayfs模塊功能無法運作</string> <string name="module_overlay_fs_not_available">內核不支持 overlayfs模塊功能無法運作</string>
<string name="safe_mode">安全模式</string> <string name="safe_mode">安全模式</string>
<string name="reboot_to_apply">重啟生效</string>
</resources> </resources>

View File

@@ -50,4 +50,5 @@
<string name="show_system_apps">顯示系統應用程式</string> <string name="show_system_apps">顯示系統應用程式</string>
<string name="hide_system_apps">隱藏系統應用程式</string> <string name="hide_system_apps">隱藏系統應用程式</string>
<string name="safe_mode">安全模式</string> <string name="safe_mode">安全模式</string>
<string name="reboot_to_apply">重啟生效</string>
</resources> </resources>

View File

@@ -55,5 +55,6 @@
<string name="hide_system_apps">Hide system apps</string> <string name="hide_system_apps">Hide system apps</string>
<string name="send_log">Send Log</string> <string name="send_log">Send Log</string>
<string name="safe_mode">Safe mode</string> <string name="safe_mode">Safe mode</string>
<string name="reboot_to_apply">Reboot to take effect</string>
</resources> </resources>