Skip to content

Commit

Permalink
导航栏修改 (#19)
Browse files Browse the repository at this point in the history
* 仅显示当前页label
* mod页双击刷新
* 边栏宽度缩减
  • Loading branch information
lings03 authored Sep 20, 2024
1 parent 6599fd7 commit 197e18d
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 28 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/top/laoxin/modmanager/ui/view/Console.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import top.laoxin.modmanager.ui.viewmodel.ConsoleViewModel

@SuppressLint("NewApi")
@Composable
fun ConsoleContent(innerPadding: PaddingValues = PaddingValues(0.dp), viewModel: ConsoleViewModel) {
fun ConsoleContent(viewModel: ConsoleViewModel) {
val context = LocalContext.current

val scrollState = rememberScrollState()
Expand Down
93 changes: 72 additions & 21 deletions app/src/main/java/top/laoxin/modmanager/ui/view/ModManagerApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package top.laoxin.modmanager.ui.view

import android.content.res.Configuration
import androidx.annotation.StringRes
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.defaultMinSize
Expand All @@ -19,10 +22,7 @@ import androidx.compose.material3.NavigationRail
import androidx.compose.material3.NavigationRailItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalConfiguration
Expand All @@ -35,7 +35,6 @@ import androidx.navigation.compose.composable
import com.google.accompanist.pager.HorizontalPager
import com.google.accompanist.pager.PagerState
import com.google.accompanist.pager.rememberPagerState
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import top.laoxin.modmanager.R
import top.laoxin.modmanager.ui.view.modview.ModPage
Expand Down Expand Up @@ -120,30 +119,48 @@ fun NavigationRail(

NavigationRail(
modifier = Modifier
.defaultMinSize(minWidth = 100.dp)
.defaultMinSize(minWidth = 80.dp)
.fillMaxHeight()
.padding(0.dp)
) {
Spacer(Modifier.weight(1f))
NavigationIndex.entries.forEachIndexed { index, navigationItem ->
Spacer(Modifier.height(16.dp))
val isSelected = pagerState.currentPage == index
var lastClickTime by remember { mutableLongStateOf(0L) }

Spacer(Modifier.height(12.dp))
NavigationRailItem(
selected = pagerState.currentPage == index,
onClick = {
modViewModel.exitSelect()
// 使用 coroutineScope 启动协程去更新页面状态
coroutineScope.launch {
pagerState.animateScrollToPage(index)
val currentTime = System.currentTimeMillis()
if ((currentTime - lastClickTime) < 300) { // 检测双击
// 刷新当前页面的逻辑
refreshCurrentPage(pagerState.currentPage, modViewModel)
} else {
modViewModel.exitSelect()
if (!isSelected) {
coroutineScope.launch {
pagerState.scrollToPage(index)
}
}
}
lastClickTime = currentTime
},
icon = {
Icon(imageVector = navigationItem.icon, contentDescription = null)
},
label = {
Text(text = stringResource(id = navigationItem.title))
}
AnimatedVisibility(
visible = isSelected,
enter = fadeIn(),
exit = fadeOut()
) {
Text(text = stringResource(id = navigationItem.title))
}
},
alwaysShowLabel = false // 确保标签只在 isSelected 为 true 时显示
)
Spacer(Modifier.height(16.dp))
Spacer(Modifier.height(12.dp))
}
Spacer(Modifier.weight(1f))
}
Expand All @@ -155,29 +172,63 @@ fun NavigationBar(
modViewModel: ModViewModel
) {
val coroutineScope = rememberCoroutineScope()
var lastClickTime by remember { mutableLongStateOf(0L) }

NavigationBar {
NavigationIndex.entries.forEachIndexed { index, navigationItem ->
val isSelected = pagerState.currentPage == index

NavigationBarItem(
selected = pagerState.currentPage == index,
selected = isSelected,
onClick = {
modViewModel.exitSelect()
// 使用 coroutineScope 启动协程去更新页面状态
coroutineScope.launch {
pagerState.animateScrollToPage(index)
val currentTime = System.currentTimeMillis()
if ((currentTime - lastClickTime) < 300) { // 检测双击
// 刷新当前页面的逻辑
refreshCurrentPage(pagerState.currentPage, modViewModel)
} else {
modViewModel.exitSelect()
if (!isSelected) {
coroutineScope.launch {
pagerState.scrollToPage(index)
}
}
}
lastClickTime = currentTime
},
icon = {
Icon(imageVector = navigationItem.icon, contentDescription = null)
},
label = {
Text(text = stringResource(id = navigationItem.title))
}
AnimatedVisibility(
visible = isSelected,
enter = fadeIn(),
exit = fadeOut()
) {
Text(text = stringResource(id = navigationItem.title))
}
},
alwaysShowLabel = false // 确保标签只在 isSelected 为 true 时显示
)
}
}
}

private fun refreshCurrentPage(currentPage: Int, modViewModel: ModViewModel) {
// 根据当前页面的类型,执行相应的刷新逻辑
when (currentPage) {
NavigationIndex.CONSOLE.ordinal -> {
// 刷新控制台页面的逻辑
}

NavigationIndex.MOD.ordinal -> {
modViewModel.flashMods(false, true)
}

NavigationIndex.SETTINGS.ordinal -> {
// 刷新设置页面逻辑
}
}
}


//导航
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ class ConsoleViewModel(
)
}
}
var gameInfoJob: Job? = null
var updateModCountJob: Job? = null
var updateAntiHarmonyJob: Job? = null
var updateEnableModCountJob: Job? = null
Expand Down Expand Up @@ -180,7 +179,7 @@ class ConsoleViewModel(
}.onFailure {
Log.e("ConsoleViewModel", "信息提示: $it")
}.onSuccess { info ->
Log.d("ConsoleViewModel", "信息提示: ${info}")
Log.d("ConsoleViewModel", "信息提示: $info")
if (info.version > ModTools.getInfoVersion()) {
_uiState.update {
it.copy(infoBean = info)
Expand Down Expand Up @@ -415,8 +414,8 @@ class ConsoleViewModel(
Bitmap.Config.ARGB_8888
).also { bitmap ->
val canvas = Canvas(bitmap)
(drawable as AdaptiveIconDrawable).setBounds(0, 0, canvas.width, canvas.height)
(drawable as AdaptiveIconDrawable).draw(canvas)
drawable.setBounds(0, 0, canvas.width, canvas.height)
drawable.draw(canvas)
}
}

Expand All @@ -427,7 +426,7 @@ class ConsoleViewModel(
}
}
return bitmap.asImageBitmap()
} catch (e: PackageManager.NameNotFoundException) {
} catch (_: PackageManager.NameNotFoundException) {
val context = App.get()
val drawable = context.resources.getDrawable(R.drawable.app_icon, context.theme)
val bitmap = drawable.toBitmap()
Expand Down Expand Up @@ -518,7 +517,7 @@ class ConsoleViewModel(
Log.e("ConsoleViewModel", "checkUpdate: $it")
}.onSuccess {
if (it.code > ModTools.getVersionCode()) {
Log.d("ConsoleViewModel", "checkUpdate: ${it}")
Log.d("ConsoleViewModel", "checkUpdate: $it")
_downloadUrl = it.url
_updateContent = it.des
setShowUpgradeDialog(true)
Expand Down

0 comments on commit 197e18d

Please sign in to comment.