manager: Optimize Home data refresh logic

This commit is contained in:
ShirkNeko
2025-05-31 23:29:34 +08:00
parent 6ee9246650
commit 26d86aa2fe
3 changed files with 28 additions and 18 deletions

View File

@@ -102,6 +102,7 @@ class MainActivity : ComponentActivity() {
super.attachBaseContext(context)
}
@SuppressLint("RestrictedApi")
override fun onCreate(savedInstanceState: Bundle?) {
// 确保应用正确的语言设置
applyLanguageSetting()
@@ -192,8 +193,6 @@ class MainActivity : ComponentActivity() {
initPlatform()
}
homeViewModel.refreshAllData(this)
Scaffold(
bottomBar = {
AnimatedVisibility(

View File

@@ -15,11 +15,13 @@ import androidx.compose.animation.fadeOut
import androidx.compose.animation.shrinkVertically
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.WindowInsetsSides
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.only
@@ -31,6 +33,7 @@ import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Android
import androidx.compose.material.icons.filled.Archive
@@ -44,6 +47,8 @@ import androidx.compose.material.icons.filled.Storage
import androidx.compose.material.icons.outlined.Block
import androidx.compose.material.icons.outlined.TaskAlt
import androidx.compose.material.icons.outlined.Warning
import androidx.compose.material.pullrefresh.pullRefresh
import androidx.compose.material.pullrefresh.rememberPullRefreshState
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
@@ -112,7 +117,7 @@ import kotlin.random.Random
* @author ShirkNeko
* @date 2025/5/31.
*/
@OptIn(ExperimentalMaterial3Api::class)
@OptIn(ExperimentalMaterial3Api::class, ExperimentalMaterialApi::class)
@Destination<RootGraph>(start = true)
@Composable
fun HomeScreen(navigator: DestinationsNavigator) {
@@ -120,6 +125,12 @@ fun HomeScreen(navigator: DestinationsNavigator) {
val viewModel = viewModel<HomeViewModel>()
val coroutineScope = rememberCoroutineScope()
LaunchedEffect(key1 = navigator) {
coroutineScope.launch {
viewModel.refreshAllData(context)
}
}
LaunchedEffect(Unit) {
viewModel.loadUserSettings(context)
viewModel.initializeData()
@@ -141,22 +152,26 @@ fun HomeScreen(navigator: DestinationsNavigator) {
WindowInsetsSides.Top + WindowInsetsSides.Horizontal
)
) { innerPadding ->
PullToRefreshBox(
val pullRefreshState = rememberPullRefreshState(
refreshing = false,
onRefresh = {
coroutineScope.launch {
viewModel.refreshAllData(context)
}
},
isRefreshing = viewModel.isRefreshing
}
)
Box(
modifier = Modifier
.padding(innerPadding)
.fillMaxSize()
.pullRefresh(pullRefreshState)
) {
Column(
modifier = Modifier
.padding(innerPadding)
.disableOverscroll()
.nestedScroll(scrollBehavior.nestedScrollConnection)
.fillMaxSize()
.verticalScroll(scrollState)
.padding(top = 12.dp)
.padding(horizontal = 16.dp),
.padding(top = 12.dp, start = 16.dp, end = 16.dp),
verticalArrangement = Arrangement.spacedBy(12.dp)
) {
StatusCard(

View File

@@ -27,7 +27,7 @@ import androidx.core.content.edit
class HomeViewModel : ViewModel() {
companion object {
private const val TAG = "HomeViewModel"
private const val CACHE_DURATION = 12 * 60 * 60 * 1000L
private const val CACHE_DURATION = 5 * 60 * 1000L
private const val PREFS_NAME = "home_cache"
private const val KEY_SYSTEM_STATUS = "system_status"
private const val KEY_SYSTEM_INFO = "system_info"
@@ -67,9 +67,6 @@ class HomeViewModel : ViewModel() {
private val gson = Gson()
private val prefs by lazy { ksuApp.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE) }
var isRefreshing by mutableStateOf(false)
private set
var systemStatus by mutableStateOf(SystemStatus())
private set
@@ -169,13 +166,12 @@ class HomeViewModel : ViewModel() {
}
fun refreshAllData(context: Context) {
isRefreshing = true
viewModelScope.launch {
try {
fetchAndSaveData()
checkForUpdates(context)
} finally {
isRefreshing = false
} catch (e: Exception) {
Log.e(TAG, "Error refreshing data", e)
}
}
}