Skip to content

Commit

Permalink
添加二次返回确认 (#21)
Browse files Browse the repository at this point in the history
* 二次返回确认
* 其他页返回控制台
  • Loading branch information
lings03 authored Sep 20, 2024
1 parent 28c1be7 commit 7713274
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
29 changes: 0 additions & 29 deletions app/src/main/java/top/laoxin/modmanager/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,42 +69,13 @@ class MainActivity : ComponentActivity() {
}
}


override fun onDestroy() {
super.onDestroy()
if (PermissionTools.isShizukuAvailable) {
// 移除Shizuku权限请求回调
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<BeanFile?> = FileTools.getSortedFileList(path)
val bundle = Bundle()
bundle.putParcelableArrayList(BundleKey.FILE_LIST, list as ArrayList<out Parcelable?>)
if (!isNavigate) {
mNavController.popBackStack()
}
mNavController.navigate(R.id.fileListFragment, bundle)
}
}*/

}


Expand Down
38 changes: 37 additions & 1 deletion app/src/main/java/top/laoxin/modmanager/ui/view/ModManagerApp.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -72,6 +80,7 @@ fun ModManagerApp() {

Scaffold(
topBar = {
// 根据当前页面显示不同的顶部工具栏
when (pagerState.currentPage) {
NavigationIndex.CONSOLE.ordinal -> ConsoleTopBar(consoleViewModel)
NavigationIndex.MOD.ordinal -> ModTopBar(modViewModel)
Expand All @@ -86,6 +95,7 @@ fun ModManagerApp() {
)
},
bottomBar = {
// 在纵向模式下显示底部导航栏
if (configuration.orientation == Configuration.ORIENTATION_PORTRAIT) {
NavigationBar(
pagerState = pagerState,
Expand All @@ -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)
Expand Down Expand Up @@ -166,6 +202,7 @@ fun NavigationRail(
}
}

// 底部导航
@Composable
fun NavigationBar(
pagerState: PagerState,
Expand Down Expand Up @@ -230,7 +267,6 @@ private fun refreshCurrentPage(currentPage: Int, modViewModel: ModViewModel) {
}
}


//导航
@Composable
fun NavigationHost(
Expand Down

0 comments on commit 7713274

Please sign in to comment.