feat(explore): possibility to sort community list

This commit is contained in:
Diego Beraldin 2023-09-07 19:16:30 +02:00
parent b27045f43c
commit a69c6b4029
8 changed files with 118 additions and 49 deletions

View File

@ -115,6 +115,7 @@ class PostListViewModel(
private fun loadNextPage() { private fun loadNextPage() {
val currentState = mvi.uiState.value val currentState = mvi.uiState.value
if (!currentState.canFetchMore || currentState.loading) { if (!currentState.canFetchMore || currentState.loading) {
mvi.updateState { it.copy(refreshing = false) }
return return
} }

View File

@ -32,9 +32,7 @@ internal fun PostsTopBar(
onSelectSortType: () -> Unit, onSelectSortType: () -> Unit,
) { ) {
Row( Row(
modifier = Modifier.height(50.dp).padding( modifier = Modifier.height(50.dp).padding(horizontal = Spacing.s),
horizontal = Spacing.s,
),
verticalAlignment = Alignment.CenterVertically, verticalAlignment = Alignment.CenterVertically,
) { ) {
Row( Row(

View File

@ -45,6 +45,7 @@ class ProfileCommentsViewModel(
private fun loadNextPage() { private fun loadNextPage() {
val currentState = mvi.uiState.value val currentState = mvi.uiState.value
if (!currentState.canFetchMore || currentState.loading) { if (!currentState.canFetchMore || currentState.loading) {
mvi.updateState { it.copy(refreshing = false) }
return return
} }

View File

@ -46,6 +46,7 @@ class ProfilePostsViewModel(
private fun loadNextPage() { private fun loadNextPage() {
val currentState = mvi.uiState.value val currentState = mvi.uiState.value
if (!currentState.canFetchMore || currentState.loading) { if (!currentState.canFetchMore || currentState.loading) {
mvi.updateState { it.copy(refreshing = false) }
return return
} }

View File

@ -12,6 +12,7 @@ interface CommunityListMviModel :
object LoadNextPage : Intent object LoadNextPage : Intent
data class SetSearch(val value: String) : Intent data class SetSearch(val value: String) : Intent
data class SetListingType(val value: ListingType) : Intent data class SetListingType(val value: ListingType) : Intent
data class SetSortType(val value: SortType) : Intent
} }
data class UiState( data class UiState(

View File

@ -1,10 +1,8 @@
package com.github.diegoberaldin.raccoonforlemmy.feature.search.communitylist package com.github.diegoberaldin.raccoonforlemmy.feature.search.communitylist
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
@ -35,7 +33,6 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.Density 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.communitydetail.CommunityDetailScreen
import com.github.diegoberaldin.raccoonforlemmy.core.commonui.components.CommunityItem import com.github.diegoberaldin.raccoonforlemmy.core.commonui.components.CommunityItem
import com.github.diegoberaldin.raccoonforlemmy.core.commonui.modals.ListingTypeBottomSheet import com.github.diegoberaldin.raccoonforlemmy.core.commonui.modals.ListingTypeBottomSheet
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.data.toIcon import com.github.diegoberaldin.raccoonforlemmy.core.commonui.modals.SortBottomSheet
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.data.toReadableName
import com.github.diegoberaldin.raccoonforlemmy.feature.search.di.getSearchScreenModel import com.github.diegoberaldin.raccoonforlemmy.feature.search.di.getSearchScreenModel
import com.github.diegoberaldin.raccoonforlemmy.resources.MR import com.github.diegoberaldin.raccoonforlemmy.resources.MR
import dev.icerock.moko.resources.compose.stringResource import dev.icerock.moko.resources.compose.stringResource
@ -71,14 +67,10 @@ class CommunityListScreen : Screen {
Scaffold( Scaffold(
modifier = Modifier.padding(Spacing.xxs), modifier = Modifier.padding(Spacing.xxs),
topBar = { topBar = {
Row( CommunityTopBar(
modifier = Modifier.height(50.dp).padding( listingType = uiState.listingType,
horizontal = Spacing.s, sortType = uiState.sortType,
), onSelectListingType = {
verticalAlignment = Alignment.CenterVertically,
) {
Row(
modifier = Modifier.onClick {
bottomNavigator.show( bottomNavigator.show(
ListingTypeBottomSheet( ListingTypeBottomSheet(
isLogged = uiState.isLogged, isLogged = uiState.isLogged,
@ -91,28 +83,19 @@ class CommunityListScreen : Screen {
), ),
) )
}, },
verticalAlignment = Alignment.CenterVertically, onSelectSortType = {
horizontalArrangement = Arrangement.spacedBy(Spacing.m), bottomNavigator.show(
) { SortBottomSheet(
Image( onHide = {
imageVector = uiState.listingType.toIcon(), bottomNavigator.hide()
contentDescription = null, },
colorFilter = ColorFilter.tint(MaterialTheme.colorScheme.onBackground), onSelected = {
model.reduce(CommunityListMviModel.Intent.SetSortType(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,
)
}
}
}
}, },
) { padding -> ) { padding ->
Column( Column(

View File

@ -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.ApiConfigurationRepository
import com.github.diegoberaldin.raccoonforlemmy.domain.identity.repository.IdentityRepository 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.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.CommunityRepository
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.PostsRepository import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.PostsRepository
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -69,6 +70,7 @@ class CommunityListViewModel(
is CommunityListMviModel.Intent.SetSearch -> setSearch(intent.value) is CommunityListMviModel.Intent.SetSearch -> setSearch(intent.value)
is CommunityListMviModel.Intent.SetListingType -> changeListingType(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() { private suspend fun refresh() {
currentPage = 1 currentPage = 1
mvi.updateState { it.copy(canFetchMore = true, refreshing = true) } mvi.updateState { it.copy(canFetchMore = true, refreshing = true) }
@ -97,6 +106,7 @@ class CommunityListViewModel(
private suspend fun loadNextPage() { private suspend fun loadNextPage() {
val currentState = mvi.uiState.value val currentState = mvi.uiState.value
if (!currentState.canFetchMore || currentState.loading) { if (!currentState.canFetchMore || currentState.loading) {
mvi.updateState { it.copy(refreshing = false) }
return return
} }
mvi.updateState { it.copy(loading = true) } mvi.updateState { it.copy(loading = true) }

View File

@ -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),
)
}
}
}