feat: 清空历史记录

This commit is contained in:
HuanCheng65 2023-01-24 15:50:43 +08:00
parent bb4bfdabd8
commit d07feb9ecb
No known key found for this signature in database
GPG Key ID: E9031EF91A805148
3 changed files with 69 additions and 13 deletions

View File

@ -9,10 +9,13 @@ import androidx.compose.material.TabRow
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Delete
import androidx.compose.material.rememberScaffoldState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
@ -23,6 +26,7 @@ import com.huanchengfly.tieba.post.R
import com.huanchengfly.tieba.post.ui.common.theme.compose.ExtendedTheme
import com.huanchengfly.tieba.post.ui.page.ProvideNavigator
import com.huanchengfly.tieba.post.ui.page.history.list.HistoryListPage
import com.huanchengfly.tieba.post.ui.page.history.list.HistoryListUiEvent
import com.huanchengfly.tieba.post.ui.widgets.compose.BackNavigationIcon
import com.huanchengfly.tieba.post.ui.widgets.compose.MyScaffold
import com.huanchengfly.tieba.post.ui.widgets.compose.PagerTabIndicator
@ -31,6 +35,7 @@ import com.huanchengfly.tieba.post.utils.HistoryUtil
import com.ramcosta.composedestinations.annotation.DeepLink
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.launch
@OptIn(ExperimentalPagerApi::class)
@ -45,8 +50,14 @@ fun HistoryPage(
) {
val pagerState = rememberPagerState()
val coroutineScope = rememberCoroutineScope()
val scaffoldState = rememberScaffoldState()
val context = LocalContext.current
val eventFlow = remember { MutableSharedFlow<HistoryListUiEvent>() }
MyScaffold(
scaffoldState = scaffoldState,
topBar = {
TitleCentredToolbar(
title = stringResource(id = R.string.title_history),
@ -54,7 +65,19 @@ fun HistoryPage(
BackNavigationIcon(onBackPressed = { navigator.navigateUp() })
},
actions = {
IconButton(onClick = { /*TODO*/ }) {
IconButton(onClick = {
coroutineScope.launch {
HistoryUtil.deleteAll()
eventFlow.emit(HistoryListUiEvent.DeleteAll)
launch {
scaffoldState.snackbarHostState.showSnackbar(
context.getString(
R.string.toast_clear_success
)
)
}
}
}) {
Icon(
imageVector = Icons.Outlined.Delete,
contentDescription = stringResource(id = R.string.title_history_delete),
@ -123,9 +146,9 @@ fun HistoryPage(
userScrollEnabled = true,
) {
if (it == 0) {
HistoryListPage(type = HistoryUtil.TYPE_THREAD)
HistoryListPage(type = HistoryUtil.TYPE_THREAD, eventFlow = eventFlow)
} else {
HistoryListPage(type = HistoryUtil.TYPE_FORUM)
HistoryListPage(type = HistoryUtil.TYPE_FORUM, eventFlow = eventFlow)
}
}
}

View File

@ -43,17 +43,30 @@ import com.huanchengfly.tieba.post.ui.widgets.compose.UserHeader
import com.huanchengfly.tieba.post.ui.widgets.compose.rememberMenuState
import com.huanchengfly.tieba.post.utils.DateTimeUtils
import com.huanchengfly.tieba.post.utils.HistoryUtil
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.launch
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun HistoryListPage(
type: Int,
eventFlow: Flow<HistoryListUiEvent>,
viewModel: HistoryListViewModel = if (type == HistoryUtil.TYPE_THREAD) pageViewModel<ThreadHistoryListViewModel>() else pageViewModel<ForumHistoryListViewModel>()
) {
LazyLoad(loaded = viewModel.initialized) {
viewModel.send(HistoryListUiIntent.Refresh)
viewModel.initialized = true
}
LaunchedEffect(null) {
launch {
eventFlow
.filterIsInstance<HistoryListUiEvent.DeleteAll>()
.collect {
viewModel.send(HistoryListUiIntent.DeleteAll)
}
}
}
val isLoadingMore by viewModel.uiState.collectPartialAsState(
prop1 = HistoryListUiState::isLoadingMore,
initial = false

View File

@ -18,6 +18,7 @@ import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.flatMapConcat
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.merge
@ -65,8 +66,12 @@ private class HistoryListPartialChangeProducer(val type: Int) :
.flatMapConcat { it.producePartialChange() },
intentFlow.filterIsInstance<HistoryListUiIntent.Delete>()
.flatMapConcat { it.producePartialChange() },
intentFlow.filterIsInstance<HistoryListUiIntent.DeleteAll>()
.flatMapConcat { produceDeleteAllPartialChange() },
)
private fun produceDeleteAllPartialChange() = flowOf(HistoryListPartialChange.DeleteAll)
private fun produceRefreshPartialChange() =
HistoryUtil.getFlow(type, 0)
.map<List<History>, HistoryListPartialChange.Refresh> { histories ->
@ -107,9 +112,22 @@ sealed interface HistoryListUiIntent : UiIntent {
data class LoadMore(val page: Int) : HistoryListUiIntent
data class Delete(val id: Int) : HistoryListUiIntent
object DeleteAll : HistoryListUiIntent
}
sealed interface HistoryListPartialChange : PartialChange<HistoryListUiState> {
object DeleteAll : HistoryListPartialChange {
override fun reduce(oldState: HistoryListUiState): HistoryListUiState = oldState.copy(
todayHistoryData = emptyList(),
beforeHistoryData = emptyList(),
currentPage = 0,
hasMore = false,
isLoadingMore = false,
isRefreshing = false
)
}
sealed class Refresh : HistoryListPartialChange {
override fun reduce(oldState: HistoryListUiState): HistoryListUiState = when (this) {
is Failure -> oldState
@ -194,4 +212,6 @@ sealed interface HistoryListUiEvent : UiEvent {
val errorMsg: String
) : Delete
}
object DeleteAll : HistoryListUiEvent
}