manager: Implement module count refresh

- update count data periodically and optimize the bottom bar display
This commit is contained in:
ShirkNeko
2025-06-02 17:24:34 +08:00
parent a6ed7befdc
commit c0e839dd8e

View File

@@ -47,7 +47,30 @@ import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import com.sukisu.ultra.ui.viewmodel.HomeViewModel import com.sukisu.ultra.ui.viewmodel.HomeViewModel
import com.sukisu.ultra.ui.viewmodel.SuperUserViewModel import com.sukisu.ultra.ui.viewmodel.SuperUserViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.Dispatchers
// 数据刷新管理
object DataRefreshManager {
private val _superuserCount = MutableStateFlow(0)
private val _moduleCount = MutableStateFlow(0)
private val _kpmModuleCount = MutableStateFlow(0)
val superuserCount: StateFlow<Int> = _superuserCount.asStateFlow()
val moduleCount: StateFlow<Int> = _moduleCount.asStateFlow()
val kpmModuleCount: StateFlow<Int> = _kpmModuleCount.asStateFlow()
fun refreshData() {
_superuserCount.value = getSuperuserCount()
_moduleCount.value = getModuleCount()
_kpmModuleCount.value = getKpmModuleCount()
}
}
class MainActivity : ComponentActivity() { class MainActivity : ComponentActivity() {
private lateinit var superUserViewModel: SuperUserViewModel private lateinit var superUserViewModel: SuperUserViewModel
@@ -134,6 +157,9 @@ class MainActivity : ComponentActivity() {
homeViewModel.initializeData() homeViewModel.initializeData()
} }
// 数据刷新协程
startDataRefreshCoroutine()
val prefs = getSharedPreferences("settings", MODE_PRIVATE) val prefs = getSharedPreferences("settings", MODE_PRIVATE)
val isFirstRun = prefs.getBoolean("is_first_run", true) val isFirstRun = prefs.getBoolean("is_first_run", true)
@@ -193,21 +219,21 @@ class MainActivity : ComponentActivity() {
initPlatform() initPlatform()
} }
Scaffold( CompositionLocalProvider(
bottomBar = { LocalSnackbarHost provides snackBarHostState
AnimatedVisibility( ) {
visible = showBottomBar, Scaffold(
enter = slideInVertically(initialOffsetY = { it }) + fadeIn(), bottomBar = {
exit = slideOutVertically(targetOffsetY = { it }) + fadeOut() AnimatedVisibility(
) { visible = showBottomBar,
BottomBar(navController) enter = slideInVertically(initialOffsetY = { it }) + fadeIn(),
} exit = slideOutVertically(targetOffsetY = { it }) + fadeOut()
}, ) {
contentWindowInsets = WindowInsets(0, 0, 0, 0) BottomBar(navController)
) { innerPadding -> }
CompositionLocalProvider( },
LocalSnackbarHost provides snackBarHostState contentWindowInsets = WindowInsets(0, 0, 0, 0)
) { ) { innerPadding ->
DestinationsNavHost( DestinationsNavHost(
modifier = Modifier.padding(innerPadding), modifier = Modifier.padding(innerPadding),
navGraph = NavGraphs.root as NavHostGraphSpec, navGraph = NavGraphs.root as NavHostGraphSpec,
@@ -225,6 +251,16 @@ class MainActivity : ComponentActivity() {
} }
} }
// 数据刷新协程
private fun startDataRefreshCoroutine() {
lifecycleScope.launch(Dispatchers.IO) {
while (isActive) {
DataRefreshManager.refreshData()
delay(5000)
}
}
}
// 应用自定义DPI设置 // 应用自定义DPI设置
private fun applyCustomDpi() { private fun applyCustomDpi() {
val prefs = getSharedPreferences("settings", MODE_PRIVATE) val prefs = getSharedPreferences("settings", MODE_PRIVATE)
@@ -268,6 +304,10 @@ class MainActivity : ComponentActivity() {
lifecycleScope.launch { lifecycleScope.launch {
homeViewModel.initializeData() homeViewModel.initializeData()
} }
lifecycleScope.launch {
DataRefreshManager.refreshData()
}
} }
private val destroyListeners = mutableListOf<() -> Unit>() private val destroyListeners = mutableListOf<() -> Unit>()
@@ -293,10 +333,10 @@ private fun BottomBar(navController: NavHostController) {
val cardColor = MaterialTheme.colorScheme.surfaceContainer val cardColor = MaterialTheme.colorScheme.surfaceContainer
val context = LocalContext.current val context = LocalContext.current
// 获取计数数据 // 收集计数数据
val superuserCount = getSuperuserCount() val superuserCount by DataRefreshManager.superuserCount.collectAsState()
val moduleCount = getModuleCount() val moduleCount by DataRefreshManager.moduleCount.collectAsState()
val kpmModuleCount = getKpmModuleCount() val kpmModuleCount by DataRefreshManager.kpmModuleCount.collectAsState()
// 检查是否显示KPM // 检查是否显示KPM
val showKpmInfo = context.getSharedPreferences("settings", Context.MODE_PRIVATE) val showKpmInfo = context.getSharedPreferences("settings", Context.MODE_PRIVATE)
@@ -336,7 +376,7 @@ private fun BottomBar(navController: NavHostController) {
badge = { badge = {
if (kpmModuleCount > 0) { if (kpmModuleCount > 0) {
Badge( Badge(
containerColor = MaterialTheme.colorScheme.error containerColor = MaterialTheme.colorScheme.secondary
) { ) {
Text( Text(
text = kpmModuleCount.toString(), text = kpmModuleCount.toString(),
@@ -380,7 +420,7 @@ private fun BottomBar(navController: NavHostController) {
badge = { badge = {
if (superuserCount > 0) { if (superuserCount > 0) {
Badge( Badge(
containerColor = MaterialTheme.colorScheme.error containerColor = MaterialTheme.colorScheme.secondary
) { ) {
Text( Text(
text = superuserCount.toString(), text = superuserCount.toString(),
@@ -423,8 +463,7 @@ private fun BottomBar(navController: NavHostController) {
badge = { badge = {
if (moduleCount > 0) { if (moduleCount > 0) {
Badge( Badge(
containerColor = MaterialTheme.colorScheme.error containerColor = MaterialTheme.colorScheme.secondary ) {
) {
Text( Text(
text = moduleCount.toString(), text = moduleCount.toString(),
style = MaterialTheme.typography.labelSmall style = MaterialTheme.typography.labelSmall