manager: Simplify Home data initialization logic and remove unnecessary caching time
This commit is contained in:
@@ -3,7 +3,6 @@ package com.sukisu.ultra.ui.viewmodel
|
|||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.SystemClock
|
|
||||||
import android.system.Os
|
import android.system.Os
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
@@ -27,7 +26,6 @@ import androidx.core.content.edit
|
|||||||
class HomeViewModel : ViewModel() {
|
class HomeViewModel : ViewModel() {
|
||||||
companion object {
|
companion object {
|
||||||
private const val TAG = "HomeViewModel"
|
private const val TAG = "HomeViewModel"
|
||||||
private const val CACHE_DURATION = 5 * 60 * 1000L
|
|
||||||
private const val PREFS_NAME = "home_cache"
|
private const val PREFS_NAME = "home_cache"
|
||||||
private const val KEY_SYSTEM_STATUS = "system_status"
|
private const val KEY_SYSTEM_STATUS = "system_status"
|
||||||
private const val KEY_SYSTEM_INFO = "system_info"
|
private const val KEY_SYSTEM_INFO = "system_info"
|
||||||
@@ -103,15 +101,7 @@ class HomeViewModel : ViewModel() {
|
|||||||
|
|
||||||
fun initializeData() {
|
fun initializeData() {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
val currentTime = System.currentTimeMillis()
|
loadCachedData()
|
||||||
val lastUpdateTime = prefs.getLong(KEY_LAST_UPDATE, 0)
|
|
||||||
val shouldRefresh = currentTime - lastUpdateTime > CACHE_DURATION
|
|
||||||
|
|
||||||
if (!shouldRefresh) {
|
|
||||||
loadCachedData()
|
|
||||||
} else {
|
|
||||||
fetchAndSaveData()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -147,16 +137,11 @@ class HomeViewModel : ViewModel() {
|
|||||||
.getBoolean("check_update", true)
|
.getBoolean("check_update", true)
|
||||||
|
|
||||||
if (checkUpdate) {
|
if (checkUpdate) {
|
||||||
val currentTime = System.currentTimeMillis()
|
val newVersionInfo = checkNewVersion()
|
||||||
val lastUpdateTime = prefs.getLong(KEY_LAST_UPDATE, 0)
|
latestVersionInfo = newVersionInfo
|
||||||
val shouldRefresh = currentTime - lastUpdateTime > CACHE_DURATION
|
prefs.edit {
|
||||||
|
putString(KEY_VERSION_INFO, gson.toJson(newVersionInfo))
|
||||||
if (shouldRefresh) {
|
putLong(KEY_LAST_UPDATE, System.currentTimeMillis())
|
||||||
val start = SystemClock.elapsedRealtime()
|
|
||||||
val newVersionInfo = checkNewVersion()
|
|
||||||
latestVersionInfo = newVersionInfo
|
|
||||||
prefs.edit { putString(KEY_VERSION_INFO, gson.toJson(newVersionInfo)) }
|
|
||||||
Log.i(TAG, "Update check completed in ${SystemClock.elapsedRealtime() - start}ms")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
|
|||||||
Reference in New Issue
Block a user