From 7713274d159f944ee3b2cbfb9b557bccce9aa158 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A7=92=E7=A0=82=E7=B3=96?= <90336521+lings03@users.noreply.github.com> Date: Fri, 20 Sep 2024 22:24:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=8C=E6=AC=A1=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E7=A1=AE=E8=AE=A4=20(#21)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 二次返回确认 * 其他页返回控制台 --- .../top/laoxin/modmanager/MainActivity.kt | 29 -------------- .../modmanager/ui/view/ModManagerApp.kt | 38 ++++++++++++++++++- 2 files changed, 37 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/top/laoxin/modmanager/MainActivity.kt b/app/src/main/java/top/laoxin/modmanager/MainActivity.kt index 270e756..1351acb 100644 --- a/app/src/main/java/top/laoxin/modmanager/MainActivity.kt +++ b/app/src/main/java/top/laoxin/modmanager/MainActivity.kt @@ -69,7 +69,6 @@ class MainActivity : ComponentActivity() { } } - override fun onDestroy() { super.onDestroy() if (PermissionTools.isShizukuAvailable) { @@ -77,34 +76,6 @@ class MainActivity : ComponentActivity() { Shizuku.removeRequestPermissionResultListener(PermissionTools.REQUEST_PERMISSION_RESULT_LISTENER) } } - - - // 读取文件路径 - /* private fun loadPath(path: String?, isUserClicked: Boolean) { - if (path == null) { - return - } - val isNavigate = !TextUtils.equals(mPathCache, path) - mPathCache = path - if (FileTools.shouldRequestUriPermission(path)) { - if (isUserClicked) { - - // 读取安卓目录权限 - showRequestUriPermissionDialog() - } - } else { - mDirectory = File(path) - binding.tvPath.setText(mDirectory!!.getPath()) - val list: List = FileTools.getSortedFileList(path) - val bundle = Bundle() - bundle.putParcelableArrayList(BundleKey.FILE_LIST, list as ArrayList) - if (!isNavigate) { - mNavController.popBackStack() - } - mNavController.navigate(R.id.fileListFragment, bundle) - } - }*/ - } diff --git a/app/src/main/java/top/laoxin/modmanager/ui/view/ModManagerApp.kt b/app/src/main/java/top/laoxin/modmanager/ui/view/ModManagerApp.kt index 63697e8..153275e 100644 --- a/app/src/main/java/top/laoxin/modmanager/ui/view/ModManagerApp.kt +++ b/app/src/main/java/top/laoxin/modmanager/ui/view/ModManagerApp.kt @@ -1,6 +1,9 @@ package top.laoxin.modmanager.ui.view +import android.app.Activity import android.content.res.Configuration +import android.widget.Toast +import androidx.activity.compose.BackHandler import androidx.annotation.StringRes import androidx.compose.animation.AnimatedVisibility import androidx.compose.animation.fadeIn @@ -26,6 +29,7 @@ import androidx.compose.runtime.* import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.platform.LocalConfiguration +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import androidx.lifecycle.viewmodel.compose.viewModel @@ -62,7 +66,11 @@ fun ModManagerApp() { val pagerState = rememberPagerState() val configuration = LocalConfiguration.current + val scope = rememberCoroutineScope() + var exitTime by remember { mutableLongStateOf(0L) } + Row { + // 根据屏幕方向选择布局 if (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) { NavigationRail( pagerState = pagerState, @@ -72,6 +80,7 @@ fun ModManagerApp() { Scaffold( topBar = { + // 根据当前页面显示不同的顶部工具栏 when (pagerState.currentPage) { NavigationIndex.CONSOLE.ordinal -> ConsoleTopBar(consoleViewModel) NavigationIndex.MOD.ordinal -> ModTopBar(modViewModel) @@ -86,6 +95,7 @@ fun ModManagerApp() { ) }, bottomBar = { + // 在纵向模式下显示底部导航栏 if (configuration.orientation == Configuration.ORIENTATION_PORTRAIT) { NavigationBar( pagerState = pagerState, @@ -94,11 +104,37 @@ fun ModManagerApp() { } } ) { innerPadding -> + val context = LocalContext.current // 在这里获取 Context + val exitToast: Toast = + remember { Toast.makeText(context, "再按一次退出应用", Toast.LENGTH_SHORT) } + val activity = context as? Activity // 获取当前 Activity + + BackHandler(enabled = pagerState.currentPage == NavigationIndex.CONSOLE.ordinal) { + // 在 ConsolePage 显示退出确认 + val currentTime = System.currentTimeMillis() + if (currentTime - exitTime > 2000) { + exitToast.show() + exitTime = currentTime + } else { + // 安全关闭应用 + exitToast.cancel() + activity?.finish() + } + } + + BackHandler(enabled = pagerState.currentPage != NavigationIndex.CONSOLE.ordinal) { + // 返回到 ConsolePage + scope.launch { + pagerState.scrollToPage(NavigationIndex.CONSOLE.ordinal) + } + } + HorizontalPager( state = pagerState, count = NavigationIndex.entries.size, modifier = Modifier.padding(innerPadding) ) { page -> + // 根据当前页显示不同的内容 when (page) { NavigationIndex.CONSOLE.ordinal -> ConsolePage(consoleViewModel) NavigationIndex.MOD.ordinal -> ModPage(modViewModel) @@ -166,6 +202,7 @@ fun NavigationRail( } } +// 底部导航 @Composable fun NavigationBar( pagerState: PagerState, @@ -230,7 +267,6 @@ private fun refreshCurrentPage(currentPage: Int, modViewModel: ModViewModel) { } } - //导航 @Composable fun NavigationHost(