diff --git a/app/src/main/java/com/huanchengfly/tieba/post/ui/page/history/HistoryPage.kt b/app/src/main/java/com/huanchengfly/tieba/post/ui/page/history/HistoryPage.kt index 123d7ab5..2580f895 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/ui/page/history/HistoryPage.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/ui/page/history/HistoryPage.kt @@ -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() } 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), @@ -115,18 +138,18 @@ fun HistoryPage( } ) { ProvideNavigator(navigator = navigator) { - HorizontalPager( - count = 2, - state = pagerState, - modifier = Modifier.fillMaxSize(), - verticalAlignment = Alignment.Top, - userScrollEnabled = true, - ) { - if (it == 0) { - HistoryListPage(type = HistoryUtil.TYPE_THREAD) - } else { - HistoryListPage(type = HistoryUtil.TYPE_FORUM) - } + HorizontalPager( + count = 2, + state = pagerState, + modifier = Modifier.fillMaxSize(), + verticalAlignment = Alignment.Top, + userScrollEnabled = true, + ) { + if (it == 0) { + HistoryListPage(type = HistoryUtil.TYPE_THREAD, eventFlow = eventFlow) + } else { + HistoryListPage(type = HistoryUtil.TYPE_FORUM, eventFlow = eventFlow) + } } } } diff --git a/app/src/main/java/com/huanchengfly/tieba/post/ui/page/history/list/HistoryListPage.kt b/app/src/main/java/com/huanchengfly/tieba/post/ui/page/history/list/HistoryListPage.kt index 33f0f927..d390dc50 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/ui/page/history/list/HistoryListPage.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/ui/page/history/list/HistoryListPage.kt @@ -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, viewModel: HistoryListViewModel = if (type == HistoryUtil.TYPE_THREAD) pageViewModel() else pageViewModel() ) { LazyLoad(loaded = viewModel.initialized) { viewModel.send(HistoryListUiIntent.Refresh) viewModel.initialized = true } + LaunchedEffect(null) { + launch { + eventFlow + .filterIsInstance() + .collect { + viewModel.send(HistoryListUiIntent.DeleteAll) + } + } + } val isLoadingMore by viewModel.uiState.collectPartialAsState( prop1 = HistoryListUiState::isLoadingMore, initial = false diff --git a/app/src/main/java/com/huanchengfly/tieba/post/ui/page/history/list/HistoryListViewModel.kt b/app/src/main/java/com/huanchengfly/tieba/post/ui/page/history/list/HistoryListViewModel.kt index 9874fcf4..57760102 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/ui/page/history/list/HistoryListViewModel.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/ui/page/history/list/HistoryListViewModel.kt @@ -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() .flatMapConcat { it.producePartialChange() }, + intentFlow.filterIsInstance() + .flatMapConcat { produceDeleteAllPartialChange() }, ) + private fun produceDeleteAllPartialChange() = flowOf(HistoryListPartialChange.DeleteAll) + private fun produceRefreshPartialChange() = HistoryUtil.getFlow(type, 0) .map, 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 { + 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 } \ No newline at end of file