Skip to content

Commit

Permalink
#57
Browse files Browse the repository at this point in the history
  • Loading branch information
Soltus committed Jul 8, 2024
1 parent 932e685 commit a1ca45a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 40 deletions.
66 changes: 28 additions & 38 deletions app/src/main/java/org/b3log/ld246/Home.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* Sillot T☳Converbenk Matrix 汐洛彖夲肜矩阵:为智慧新彖务服务
* Copyright (c) 2024.
*
* lastModified: 2024/7/8 下午12:06
* updated: 2024/7/8 下午12:06
* lastModified: 2024/7/8 下午11:33
* updated: 2024/7/8 下午11:33
*/

package org.b3log.ld246
Expand Down Expand Up @@ -315,7 +315,7 @@ class HomeActivity : ComponentActivity() {
val isMenuVisible = rememberSaveable { mutableStateOf(false) }
val userPageData = rememberSerializableMMKV(mmkv, "${srcPath}_@userPageData", ld246_User())
val showFullScreenWebView = rememberSaveable { mutableStateOf(false) }
viewmodel = NotificationsViewModel(currentTab)
viewmodel = NotificationsViewModel(currentTab, pullToRefreshState)

DisposableEffect(viewmodel) {
onDispose {
Expand All @@ -326,8 +326,9 @@ class HomeActivity : ComponentActivity() {
val notificationsState = viewmodel.notificationsState.collectAsState(
// initial = listOf() // 由于使用了 savableStateFlowMMKV ,这里不提供初始值
)
if (pullToRefreshState.isRefreshing) {
LaunchedEffect(true) {

LaunchedEffect(pullToRefreshState.isRefreshing) {
if (pullToRefreshState.isRefreshing) {
apiService?.let {
if (currentTab.value == "用户") {
token.value?.let { it1 ->
Expand All @@ -338,7 +339,7 @@ class HomeActivity : ComponentActivity() {
)
} ?: { PopNotification.show("token 异常") }
} else {
viewmodel.fetchNotificationV2(pullToRefreshState, it, token)
viewmodel.fetchNotificationV2(it, token)
}
}
}
Expand All @@ -348,24 +349,7 @@ class HomeActivity : ComponentActivity() {
snapshotFlow { currentTab.value } // 创建一个Flow,它在每次currentTab.value变化时发出(启动时也会执行一次).snapshotFlow 与其他 Flow 的主要区别在于它是如何检测状态变化的。snapshotFlow 使用 Compose 的状态对象(如 State、MutableState 等)来检测变化,并且它是通过 Compose 的重组机制来实现的。这意味着 snapshotFlow 只在 Compose 的重组过程中检测状态变化,而不是在每次状态值发生变化时。
.conflate() // 当新值到来时,如果上一个值还没被处理,就忽略它
.collectLatest { // collectLatest会取消当前正在进行的操作,并开始新的操作
// pullToRefreshState.startRefresh() 之所以要在这里重复代码是因为:用户体验更好
apiService?.let {
if (currentTab.value == "用户") {
token.value?.let { it1 ->
updateUserPage(
it1,
userPageData,
pullToRefreshState
)
} ?: { PopNotification.show("token 异常") }
} else {
viewmodel.fetchNotificationV2(
pullToRefreshState,
it,
token
)
}
}
pullToRefreshState.startRefresh()
}

}
Expand Down Expand Up @@ -776,25 +760,33 @@ class HomeActivity : ComponentActivity() {
}
}

@OptIn(ExperimentalMaterial3Api::class)
@SuppressLint("StaticFieldLeak")
private inner class NotificationsViewModel(_currentTab: MutableState<String>) : ViewModel() {
private inner class NotificationsViewModel(
_currentTab: MutableState<String>,
_pullToRefreshState: PullToRefreshState
) : ViewModel() {
val TAG = "NotificationsViewModel"
val currentTab = _currentTab
val pullToRefreshState = _pullToRefreshState
private val _notificationsState = savableStateFlowMMKV(
mmkv,
"${srcPath}@NotificationsViewModel",
map
map.toMap()
) {
it?.let { map = it }
it?.let { map = it.toMutableMap() }
}
val notificationsState: StateFlow<MutableMap<String, List<ld246_Response_Data_Notification>?>?> =
val notificationsState: StateFlow<Map<String, List<ld246_Response_Data_Notification>?>?> =
_notificationsState

init {
BuglyLog.d(TAG, "currentTab ${currentTab.value}")
}


@OptIn(ExperimentalMaterial3Api::class)
fun fetchAllNotifications(
apiService: ApiServiceNotification,
pullToRefreshState: PullToRefreshState?,
token: MutableState<String?>
) {
viewModelScope.launch {
Expand Down Expand Up @@ -877,8 +869,8 @@ class HomeActivity : ComponentActivity() {
handle_ld246_Response(response)
}
viewModelScope.launch {
_notificationsState.emit(map)
pullToRefreshState?.endRefresh()
_notificationsState.emit(map.toMap())
pullToRefreshState.endRefresh()
}
}

Expand All @@ -887,7 +879,7 @@ class HomeActivity : ComponentActivity() {
PopNotification.show(call.toString(), t.toString())
.noAutoDismiss()
viewModelScope.launch {
pullToRefreshState?.endRefresh()
pullToRefreshState.endRefresh()
}
}
})
Expand All @@ -897,21 +889,19 @@ class HomeActivity : ComponentActivity() {

@OptIn(ExperimentalMaterial3Api::class)
fun fetchNotificationV2(
pullToRefreshState: PullToRefreshState?,
apiService: ApiServiceNotification,
token: MutableState<String?>
) {
job = viewModelScope.launch {
try {
if (map.values.all { it.isNullOrEmpty() }) {
if (map.values.any { it.isNullOrEmpty() }) {
apiService.let {
viewmodel.fetchAllNotifications(
it,
pullToRefreshState,
token
)
}
} else if (pullToRefreshState != null && pullToRefreshState.isRefreshing) {
} else if (pullToRefreshState.isRefreshing) {
// 执行当前tab的请求
val caller: Call<ld246_Response> = when (currentTab.value) {
"回帖" -> apiService.apiV2NotificationsCommentedGet(
Expand Down Expand Up @@ -981,7 +971,7 @@ class HomeActivity : ComponentActivity() {
}
}
viewModelScope.launch {
_notificationsState.emit(map)
_notificationsState.emit(map.toMap())
pullToRefreshState.endRefresh()
}
handle_ld246_Response(response)
Expand All @@ -999,7 +989,7 @@ class HomeActivity : ComponentActivity() {
} else {
// 不请求只更新显示TAB对于数据,一般是点击TAB的时候
viewModelScope.launch {
_notificationsState.emit(map)
_notificationsState.emit(map.toMap())
}
}
} catch (e: Exception) {
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/java/sc/windom/sofill/pioneer/Store.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
* Sillot T☳Converbenk Matrix 汐洛彖夲肜矩阵:为智慧新彖务服务
* Copyright (c) 2024.
*
* lastModified: 2024/7/7 下午10:31
* updated: 2024/7/7 下午10:31
* lastModified: 2024/7/8 下午8:13
* updated: 2024/7/8 下午8:13
*/

package sc.windom.sofill.pioneer

import android.content.Context
import android.util.Log
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
Expand Down Expand Up @@ -130,6 +131,7 @@ inline fun <reified T : Any> rememberSerializableMMKV(
* @param defaultValue 状态的默认值,当从 MMKV 中恢复状态时,如果找不到对应的键,则返回此默认值。
* @param onInit 初始化后需要做什么,提供一个可空的 savedValue ,从MMKV中恢复。应当只进行数据操作,不要直接操作UI
* @return 返回一个 MutableStateFlow<T?> 对象,其中 T 是状态的实际类型。
* @suppress T 不能为可变类型,例如 MutableMap 。在 Kotlin 中,MutableMap 是一个可变的映射,而 Map 是一个不可变的映射。如果源必须是 MutableMap ,可以转换为 Map 传递
*/
inline fun <reified T : Any> savableStateFlowMMKV(
mmkv: MMKV,
Expand All @@ -152,6 +154,7 @@ inline fun <reified T : Any> savableStateFlowMMKV(
stateFlow.collect { value ->
// Serialize and save only the value, not the StateFlow itself
value?.let {
Log.d("savableStateFlowMMKV", "value.hashCode ${value.hashCode()}")
mmkv.encode(key, json.encodeToString(serializer(), value))
}
}
Expand Down

0 comments on commit a1ca45a

Please sign in to comment.