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.Text
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Delete import androidx.compose.material.icons.outlined.Delete
import androidx.compose.material.rememberScaffoldState
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp 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.common.theme.compose.ExtendedTheme
import com.huanchengfly.tieba.post.ui.page.ProvideNavigator 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.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.BackNavigationIcon
import com.huanchengfly.tieba.post.ui.widgets.compose.MyScaffold import com.huanchengfly.tieba.post.ui.widgets.compose.MyScaffold
import com.huanchengfly.tieba.post.ui.widgets.compose.PagerTabIndicator 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.DeepLink
import com.ramcosta.composedestinations.annotation.Destination import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.navigation.DestinationsNavigator import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@OptIn(ExperimentalPagerApi::class) @OptIn(ExperimentalPagerApi::class)
@ -45,8 +50,14 @@ fun HistoryPage(
) { ) {
val pagerState = rememberPagerState() val pagerState = rememberPagerState()
val coroutineScope = rememberCoroutineScope() val coroutineScope = rememberCoroutineScope()
val scaffoldState = rememberScaffoldState()
val context = LocalContext.current
val eventFlow = remember { MutableSharedFlow<HistoryListUiEvent>() }
MyScaffold( MyScaffold(
scaffoldState = scaffoldState,
topBar = { topBar = {
TitleCentredToolbar( TitleCentredToolbar(
title = stringResource(id = R.string.title_history), title = stringResource(id = R.string.title_history),
@ -54,7 +65,19 @@ fun HistoryPage(
BackNavigationIcon(onBackPressed = { navigator.navigateUp() }) BackNavigationIcon(onBackPressed = { navigator.navigateUp() })
}, },
actions = { 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( Icon(
imageVector = Icons.Outlined.Delete, imageVector = Icons.Outlined.Delete,
contentDescription = stringResource(id = R.string.title_history_delete), contentDescription = stringResource(id = R.string.title_history_delete),
@ -115,18 +138,18 @@ fun HistoryPage(
} }
) { ) {
ProvideNavigator(navigator = navigator) { ProvideNavigator(navigator = navigator) {
HorizontalPager( HorizontalPager(
count = 2, count = 2,
state = pagerState, state = pagerState,
modifier = Modifier.fillMaxSize(), modifier = Modifier.fillMaxSize(),
verticalAlignment = Alignment.Top, verticalAlignment = Alignment.Top,
userScrollEnabled = true, userScrollEnabled = true,
) { ) {
if (it == 0) { if (it == 0) {
HistoryListPage(type = HistoryUtil.TYPE_THREAD) HistoryListPage(type = HistoryUtil.TYPE_THREAD, eventFlow = eventFlow)
} else { } 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.ui.widgets.compose.rememberMenuState
import com.huanchengfly.tieba.post.utils.DateTimeUtils import com.huanchengfly.tieba.post.utils.DateTimeUtils
import com.huanchengfly.tieba.post.utils.HistoryUtil import com.huanchengfly.tieba.post.utils.HistoryUtil
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.launch
@OptIn(ExperimentalFoundationApi::class) @OptIn(ExperimentalFoundationApi::class)
@Composable @Composable
fun HistoryListPage( fun HistoryListPage(
type: Int, type: Int,
eventFlow: Flow<HistoryListUiEvent>,
viewModel: HistoryListViewModel = if (type == HistoryUtil.TYPE_THREAD) pageViewModel<ThreadHistoryListViewModel>() else pageViewModel<ForumHistoryListViewModel>() viewModel: HistoryListViewModel = if (type == HistoryUtil.TYPE_THREAD) pageViewModel<ThreadHistoryListViewModel>() else pageViewModel<ForumHistoryListViewModel>()
) { ) {
LazyLoad(loaded = viewModel.initialized) { LazyLoad(loaded = viewModel.initialized) {
viewModel.send(HistoryListUiIntent.Refresh) viewModel.send(HistoryListUiIntent.Refresh)
viewModel.initialized = true viewModel.initialized = true
} }
LaunchedEffect(null) {
launch {
eventFlow
.filterIsInstance<HistoryListUiEvent.DeleteAll>()
.collect {
viewModel.send(HistoryListUiIntent.DeleteAll)
}
}
}
val isLoadingMore by viewModel.uiState.collectPartialAsState( val isLoadingMore by viewModel.uiState.collectPartialAsState(
prop1 = HistoryListUiState::isLoadingMore, prop1 = HistoryListUiState::isLoadingMore,
initial = false initial = false

View File

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