diff --git a/feature-home/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/home/postlist/PostListViewModel.kt b/feature-home/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/home/postlist/PostListViewModel.kt index c5f6749fc..3629e6d32 100644 --- a/feature-home/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/home/postlist/PostListViewModel.kt +++ b/feature-home/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/home/postlist/PostListViewModel.kt @@ -115,6 +115,7 @@ class PostListViewModel( private fun loadNextPage() { val currentState = mvi.uiState.value if (!currentState.canFetchMore || currentState.loading) { + mvi.updateState { it.copy(refreshing = false) } return } diff --git a/feature-home/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/home/postlist/PostsTopBar.kt b/feature-home/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/home/postlist/PostsTopBar.kt index 790c3b940..9833a19f3 100644 --- a/feature-home/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/home/postlist/PostsTopBar.kt +++ b/feature-home/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/home/postlist/PostsTopBar.kt @@ -32,9 +32,7 @@ internal fun PostsTopBar( onSelectSortType: () -> Unit, ) { Row( - modifier = Modifier.height(50.dp).padding( - horizontal = Spacing.s, - ), + modifier = Modifier.height(50.dp).padding(horizontal = Spacing.s), verticalAlignment = Alignment.CenterVertically, ) { Row( diff --git a/feature-profile/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/profile/content/logged/comments/ProfileCommentsViewModel.kt b/feature-profile/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/profile/content/logged/comments/ProfileCommentsViewModel.kt index 70b90bad6..528ed2f9e 100644 --- a/feature-profile/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/profile/content/logged/comments/ProfileCommentsViewModel.kt +++ b/feature-profile/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/profile/content/logged/comments/ProfileCommentsViewModel.kt @@ -45,6 +45,7 @@ class ProfileCommentsViewModel( private fun loadNextPage() { val currentState = mvi.uiState.value if (!currentState.canFetchMore || currentState.loading) { + mvi.updateState { it.copy(refreshing = false) } return } diff --git a/feature-profile/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/profile/content/logged/posts/ProfilePostsViewModel.kt b/feature-profile/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/profile/content/logged/posts/ProfilePostsViewModel.kt index c6b91dce0..92eb999e5 100644 --- a/feature-profile/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/profile/content/logged/posts/ProfilePostsViewModel.kt +++ b/feature-profile/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/profile/content/logged/posts/ProfilePostsViewModel.kt @@ -46,6 +46,7 @@ class ProfilePostsViewModel( private fun loadNextPage() { val currentState = mvi.uiState.value if (!currentState.canFetchMore || currentState.loading) { + mvi.updateState { it.copy(refreshing = false) } return } diff --git a/feature-search/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/search/communitylist/CommunityListMviModel.kt b/feature-search/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/search/communitylist/CommunityListMviModel.kt index 9a7b23d42..8bdb77602 100644 --- a/feature-search/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/search/communitylist/CommunityListMviModel.kt +++ b/feature-search/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/search/communitylist/CommunityListMviModel.kt @@ -12,6 +12,7 @@ interface CommunityListMviModel : object LoadNextPage : Intent data class SetSearch(val value: String) : Intent data class SetListingType(val value: ListingType) : Intent + data class SetSortType(val value: SortType) : Intent } data class UiState( diff --git a/feature-search/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/search/communitylist/CommunityListScreen.kt b/feature-search/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/search/communitylist/CommunityListScreen.kt index 0675872d8..ee0bfa049 100644 --- a/feature-search/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/search/communitylist/CommunityListScreen.kt +++ b/feature-search/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/search/communitylist/CommunityListScreen.kt @@ -1,10 +1,8 @@ package com.github.diegoberaldin.raccoonforlemmy.feature.search.communitylist -import androidx.compose.foundation.Image import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth @@ -35,7 +33,6 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier -import androidx.compose.ui.graphics.ColorFilter import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.unit.Density @@ -52,8 +49,7 @@ import com.github.diegoberaldin.raccoonforlemmy.core.architecture.bindToLifecycl import com.github.diegoberaldin.raccoonforlemmy.core.commonui.communitydetail.CommunityDetailScreen import com.github.diegoberaldin.raccoonforlemmy.core.commonui.components.CommunityItem import com.github.diegoberaldin.raccoonforlemmy.core.commonui.modals.ListingTypeBottomSheet -import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.data.toIcon -import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.data.toReadableName +import com.github.diegoberaldin.raccoonforlemmy.core.commonui.modals.SortBottomSheet import com.github.diegoberaldin.raccoonforlemmy.feature.search.di.getSearchScreenModel import com.github.diegoberaldin.raccoonforlemmy.resources.MR import dev.icerock.moko.resources.compose.stringResource @@ -71,48 +67,35 @@ class CommunityListScreen : Screen { Scaffold( modifier = Modifier.padding(Spacing.xxs), topBar = { - Row( - modifier = Modifier.height(50.dp).padding( - horizontal = Spacing.s, - ), - verticalAlignment = Alignment.CenterVertically, - ) { - Row( - modifier = Modifier.onClick { - bottomNavigator.show( - ListingTypeBottomSheet( - isLogged = uiState.isLogged, - onHide = { - bottomNavigator.hide() - }, - onSelected = { - model.reduce(CommunityListMviModel.Intent.SetListingType(it)) - }, - ), - ) - }, - verticalAlignment = Alignment.CenterVertically, - horizontalArrangement = Arrangement.spacedBy(Spacing.m), - ) { - Image( - imageVector = uiState.listingType.toIcon(), - contentDescription = null, - colorFilter = ColorFilter.tint(MaterialTheme.colorScheme.onBackground), + CommunityTopBar( + listingType = uiState.listingType, + sortType = uiState.sortType, + onSelectListingType = { + bottomNavigator.show( + ListingTypeBottomSheet( + isLogged = uiState.isLogged, + onHide = { + bottomNavigator.hide() + }, + onSelected = { + model.reduce(CommunityListMviModel.Intent.SetListingType(it)) + }, + ), ) - Column( - verticalArrangement = Arrangement.spacedBy(Spacing.xxs), - ) { - Text( - text = stringResource(MR.strings.instance_detail_communities), - style = MaterialTheme.typography.titleLarge, - ) - Text( - text = uiState.listingType.toReadableName(), - style = MaterialTheme.typography.titleMedium, - ) - } - } - } + }, + onSelectSortType = { + bottomNavigator.show( + SortBottomSheet( + onHide = { + bottomNavigator.hide() + }, + onSelected = { + model.reduce(CommunityListMviModel.Intent.SetSortType(it)) + }, + ), + ) + }, + ) }, ) { padding -> Column( diff --git a/feature-search/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/search/communitylist/CommunityListViewModel.kt b/feature-search/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/search/communitylist/CommunityListViewModel.kt index a816a2711..9c361ff9e 100644 --- a/feature-search/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/search/communitylist/CommunityListViewModel.kt +++ b/feature-search/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/search/communitylist/CommunityListViewModel.kt @@ -8,6 +8,7 @@ import com.github.diegoberaldin.raccoonforlemmy.core.preferences.TemporaryKeySto import com.github.diegoberaldin.raccoonforlemmy.domain.identity.repository.ApiConfigurationRepository import com.github.diegoberaldin.raccoonforlemmy.domain.identity.repository.IdentityRepository import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.data.ListingType +import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.data.SortType import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.CommunityRepository import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.PostsRepository import kotlinx.coroutines.Dispatchers @@ -69,6 +70,7 @@ class CommunityListViewModel( is CommunityListMviModel.Intent.SetSearch -> setSearch(intent.value) is CommunityListMviModel.Intent.SetListingType -> changeListingType(intent.value) + is CommunityListMviModel.Intent.SetSortType -> changeSortType(intent.value) } } @@ -88,6 +90,13 @@ class CommunityListViewModel( } } + private fun changeSortType(value: SortType) { + mvi.updateState { it.copy(sortType = value) } + mvi.scope.launch(Dispatchers.IO) { + refresh() + } + } + private suspend fun refresh() { currentPage = 1 mvi.updateState { it.copy(canFetchMore = true, refreshing = true) } @@ -97,6 +106,7 @@ class CommunityListViewModel( private suspend fun loadNextPage() { val currentState = mvi.uiState.value if (!currentState.canFetchMore || currentState.loading) { + mvi.updateState { it.copy(refreshing = false) } return } mvi.updateState { it.copy(loading = true) } diff --git a/feature-search/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/search/communitylist/CommunityTopBar.kt b/feature-search/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/search/communitylist/CommunityTopBar.kt new file mode 100644 index 000000000..898118f11 --- /dev/null +++ b/feature-search/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/search/communitylist/CommunityTopBar.kt @@ -0,0 +1,74 @@ +package com.github.diegoberaldin.raccoonforlemmy.feature.search.communitylist + +import androidx.compose.foundation.Image +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.ColorFilter +import androidx.compose.ui.unit.dp +import com.github.diegoberaldin.racconforlemmy.core.utils.onClick +import com.github.diegoberaldin.raccoonforlemmy.core.appearance.theme.Spacing +import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.data.ListingType +import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.data.SortType +import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.data.toIcon +import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.data.toReadableName +import com.github.diegoberaldin.raccoonforlemmy.resources.MR +import dev.icerock.moko.resources.compose.stringResource + +@Composable +internal fun CommunityTopBar( + listingType: ListingType, + sortType: SortType, + onSelectListingType: () -> Unit, + onSelectSortType: () -> Unit, +) { + Row( + modifier = Modifier.height(50.dp).padding(horizontal = Spacing.s), + verticalAlignment = Alignment.CenterVertically, + ) { + Row( + modifier = Modifier.onClick { + onSelectListingType() + }, + verticalAlignment = Alignment.CenterVertically, + horizontalArrangement = Arrangement.spacedBy(Spacing.m), + ) { + Image( + imageVector = listingType.toIcon(), + contentDescription = null, + colorFilter = ColorFilter.tint(MaterialTheme.colorScheme.onBackground), + ) + Column( + verticalArrangement = Arrangement.spacedBy(Spacing.xxs), + ) { + Text( + text = stringResource(MR.strings.instance_detail_communities), + style = MaterialTheme.typography.titleLarge, + ) + Text( + text = listingType.toReadableName(), + style = MaterialTheme.typography.titleMedium, + ) + } + + Spacer(modifier = Modifier.weight(1f)) + + Image( + modifier = Modifier.onClick { + onSelectSortType() + }, + imageVector = sortType.toIcon(), + contentDescription = null, + colorFilter = ColorFilter.tint(MaterialTheme.colorScheme.onBackground), + ) + } + } +} \ No newline at end of file