Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Amir mousavi/feature/sorting #146

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,32 @@ package ir.composenews.domain.use_case

import ir.composenews.domain.model.Market
import ir.composenews.domain.repository.MarketRepository
import ir.composenews.domain.util.MarketListOrder
import ir.composenews.domain.util.OrderType
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject

class GetMarketListUseCase @Inject constructor(
private val repository: MarketRepository,
) {
operator fun invoke(): Flow<List<Market>> {
return repository.getMarketList()
operator fun invoke(
marketListOrder: MarketListOrder = MarketListOrder.Price(OrderType.Ascending),
): Flow<List<Market>> {
return repository.getMarketList().map { marketList ->
when (marketListOrder.orderType) {
is OrderType.Ascending -> when (marketListOrder) {
is MarketListOrder.Name -> marketList.sortedBy { it.name }
is MarketListOrder.Price -> marketList.sortedBy { it.priceChangePercentage24h }
is MarketListOrder.Symbol -> marketList.sortedBy { it.symbol }
}

is OrderType.Descending -> when (marketListOrder) {
is MarketListOrder.Name -> marketList.sortedByDescending { it.name }
is MarketListOrder.Price -> marketList.sortedByDescending { it.priceChangePercentage24h }
is MarketListOrder.Symbol -> marketList.sortedByDescending { it.symbol }
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ir.composenews.domain.util

sealed class MarketListOrder(val orderType: OrderType) {
class Symbol(orderType: OrderType) : MarketListOrder(orderType)
class Name(orderType: OrderType) : MarketListOrder(orderType)
class Price(orderType: OrderType) : MarketListOrder(orderType)

fun copy(orderType: OrderType): MarketListOrder = when (this) {
is Name -> Name(orderType)
is Price -> Price(orderType)
is Symbol -> Symbol(orderType)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package ir.composenews.domain.util

sealed interface OrderType {
data object Ascending : OrderType
data object Descending : OrderType
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ir.composenews.marketlist

import ir.composenews.base.UnidirectionalViewModel
import ir.composenews.domain.util.MarketListOrder
import ir.composenews.domain.util.OrderType
import ir.composenews.uimarket.model.MarketModel
import kotlinx.collections.immutable.PersistentList
import kotlinx.collections.immutable.persistentListOf
Expand All @@ -13,12 +15,15 @@ interface MarketListContract :
val refreshing: Boolean = false,
val showFavoriteList: Boolean = false,
val showFavoriteEmptyState: Boolean = false,
val showMarketListOrderSection: Boolean = false,
val marketListOrder: MarketListOrder = MarketListOrder.Price(OrderType.Descending),
)

sealed class Event {
data class OnSetShowFavoriteList(val showFavoriteList: Boolean) : Event()
data class OnFavoriteClick(val market: MarketModel) : Event()
data object OnGetMarketList : Event()
data object OnRefresh : Event()
data class onOrder(val marketListOrder: MarketListOrder) : Event()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
Expand All @@ -30,7 +31,9 @@ import ir.composenews.designsystem.component.pull_refresh_indicator.pullRefresh
import ir.composenews.designsystem.component.pull_refresh_indicator.rememberPullRefreshState
import ir.composenews.designsystem.preview.ThemePreviews
import ir.composenews.designsystem.theme.ComposeNewsTheme
import ir.composenews.domain.util.MarketListOrder
import ir.composenews.marketlist.component.MarketListItem
import ir.composenews.marketlist.component.MarketListOrderSection
import ir.composenews.marketlist.preview_provider.MarketListStateProvider
import ir.composenews.uimarket.model.MarketModel
import ir.composenews.utils.ContentType
Expand Down Expand Up @@ -64,24 +67,28 @@ fun MarketListRoute(
if (contentType == ContentType.DUAL_PANE && !state.refreshing && state.marketList.isNotEmpty() && marketModel == null) {
onNavigateToDetailScreen(state.marketList[0])
}

BaseRoute(
baseViewModel = viewModel,
shimmerView = {
ShimmerMarketListItem()
},
) {
MarketListScreen(
marketListState = state,
onNavigateToDetailScreen = onNavigateToDetailScreen,
showFavoriteList = showFavoriteList,
onFavoriteClick = { market ->
event.invoke(MarketListContract.Event.OnFavoriteClick(market = market))
Column {
BaseRoute(
baseViewModel = viewModel,
shimmerView = {
ShimmerMarketListItem()
},
onRefresh = {
event.invoke(MarketListContract.Event.OnRefresh)
},
)
) {
MarketListScreen(
marketListState = state,
onNavigateToDetailScreen = onNavigateToDetailScreen,
showFavoriteList = showFavoriteList,
onFavoriteClick = { market ->
event.invoke(MarketListContract.Event.OnFavoriteClick(market = market))
},
onRefresh = {
event.invoke(MarketListContract.Event.OnRefresh)
},
onOrder = { marketListOrder ->
event.invoke(MarketListContract.Event.onOrder(marketListOrder))
}
)
}
}
}

Expand All @@ -93,10 +100,15 @@ private fun MarketListScreen(
onNavigateToDetailScreen: (market: MarketModel) -> Unit,
onFavoriteClick: (market: MarketModel) -> Unit,
onRefresh: () -> Unit,
onOrder: (MarketListOrder) -> Unit,
) {
val refreshState =
rememberPullRefreshState(refreshing = marketListState.refreshing, onRefresh = onRefresh)
val marketLazyState = rememberLazyListState()

LaunchedEffect(key1 = marketListState.marketList) {
marketLazyState.scrollToItem(0)
}
Box(
modifier = Modifier
.fillMaxWidth()
Expand All @@ -114,29 +126,35 @@ private fun MarketListScreen(
),
)
} else {
LazyColumn(modifier = Modifier.fillMaxWidth()) {
items(
items = marketListState.marketList,
key = { it.name },
) { market ->
Column(
modifier = Modifier
.fillMaxWidth()
.animateItemPlacement(
animationSpec = tween(durationMillis = 250),
),
) {
MarketListItem(
modifier = Modifier,
market = market,
showFavoriteList = showFavoriteList,
onItemClick = {
onNavigateToDetailScreen(market)
},
onFavoriteClick = {
onFavoriteClick(market)
},
)
Column {

MarketListOrderSection(marketListOrder = marketListState.marketListOrder) { marketListOrder ->
onOrder(marketListOrder)
}
LazyColumn(modifier = Modifier.fillMaxWidth(), state = marketLazyState) {
items(
items = marketListState.marketList,
key = { it.name },
) { market ->
Column(
modifier = Modifier
.fillMaxWidth()
.animateItemPlacement(
animationSpec = tween(durationMillis = 250),
),
) {
MarketListItem(
modifier = Modifier,
market = market,
showFavoriteList = showFavoriteList,
onItemClick = {
onNavigateToDetailScreen(market)
},
onFavoriteClick = {
onFavoriteClick(market)
},
)
}
}
}
}
Expand All @@ -148,6 +166,7 @@ private fun MarketListScreen(
Modifier.align(Alignment.TopCenter),
)
}

}

@ThemePreviews
Expand All @@ -164,7 +183,8 @@ private fun MarketListScreenPrev(
onNavigateToDetailScreen = {},
onFavoriteClick = {},
onRefresh = {},
{}
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import ir.composenews.domain.use_case.GetFavoriteMarketListUseCase
import ir.composenews.domain.use_case.GetMarketListUseCase
import ir.composenews.domain.use_case.SyncMarketListUseCase
import ir.composenews.domain.use_case.ToggleFavoriteMarketListUseCase
import ir.composenews.domain.util.MarketListOrder
import ir.composenews.domain.util.OrderType
import ir.composenews.uimarket.mapper.toMarket
import ir.composenews.uimarket.mapper.toMarketModel
import ir.composenews.uimarket.model.MarketModel
Expand Down Expand Up @@ -39,11 +41,19 @@ class MarketListViewModel @Inject constructor(

override fun event(event: MarketListContract.Event) = when (event) {
MarketListContract.Event.OnGetMarketList -> getData()
MarketListContract.Event.OnRefresh -> getData(isRefreshing = true)
MarketListContract.Event.OnRefresh -> getData(
isRefreshing = true,
marketListOrder = mutableState.value.marketListOrder
)

is MarketListContract.Event.OnFavoriteClick -> onFavoriteClick(news = event.market)
is MarketListContract.Event.OnSetShowFavoriteList -> onSetShowFavoriteList(
showFavoriteList = event.showFavoriteList,
)

is MarketListContract.Event.onOrder -> {
getData(marketListOrder = event.marketListOrder)
}
}

private fun onSetShowFavoriteList(showFavoriteList: Boolean) {
Expand All @@ -52,7 +62,12 @@ class MarketListViewModel @Inject constructor(
}
}

private fun getData(isRefreshing: Boolean = false) {
private fun getData(
isRefreshing: Boolean = false,
marketListOrder: MarketListOrder = MarketListOrder.Price(
OrderType.Descending
),
) {
if (isRefreshing) {
mutableState.update {
it.copy(refreshing = true)
Expand All @@ -62,12 +77,12 @@ class MarketListViewModel @Inject constructor(
if (mutableState.value.showFavoriteList) {
getFavoriteMarketList()
} else {
getMarketList()
getMarketList(marketListOrder)
}
}
}

private suspend fun getMarketList() {
private suspend fun getMarketList(marketListOrder: MarketListOrder) {
mutableBaseState.update { BaseContract.BaseState.OnLoading }
try {
syncMarketListUseCase()
Expand All @@ -79,13 +94,14 @@ class MarketListViewModel @Inject constructor(
)
}
}
getMarketListUseCase()
getMarketListUseCase(marketListOrder)
.onEach { result ->
mutableState.update { prevState ->
prevState.copy(
marketList = result.map { it.toMarketModel() }.toPersistentList(),
refreshing = false,
showFavoriteEmptyState = result.isEmpty(),
marketListOrder = marketListOrder
)
}
mutableBaseState.update { BaseContract.BaseState.OnSuccess }
Expand Down Expand Up @@ -120,4 +136,4 @@ class MarketListViewModel @Inject constructor(
}
}
}
}
}
Loading
Loading