Skip to content

[WOOMOB-382] Update Coupons Fetching/Loading Business Logic #14019

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

Merged
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 @@ -6,13 +6,16 @@ import com.woocommerce.android.ui.woopos.home.items.WooPosItemSelectionViewState
import com.woocommerce.android.ui.woopos.home.items.WooPosPaginationState.Error
import com.woocommerce.android.ui.woopos.home.items.WooPosPaginationState.Loading
import com.woocommerce.android.ui.woopos.home.items.WooPosPaginationState.None
import com.woocommerce.android.ui.woopos.home.items.WooPosPullToRefreshState.Disabled
import com.woocommerce.android.ui.woopos.home.items.WooPosPullToRefreshState.Enabled
import com.woocommerce.android.ui.woopos.home.items.WooPosPullToRefreshState.Refreshing
import com.woocommerce.android.ui.woopos.home.items.coupons.WooPosCouponsListViewStateManager.FetchingCouponsState.ERROR_FETCHING_FIRST_PAGE
import com.woocommerce.android.ui.woopos.home.items.coupons.WooPosCouponsListViewStateManager.FetchingCouponsState.ERROR_FETCHING_MORE
import com.woocommerce.android.ui.woopos.home.items.coupons.WooPosCouponsListViewStateManager.FetchingCouponsState.FETCHING_FIRST_PAGE
import com.woocommerce.android.ui.woopos.home.items.coupons.WooPosCouponsListViewStateManager.FetchingCouponsState.FETCHING_MORE
import com.woocommerce.android.ui.woopos.home.items.coupons.WooPosCouponsListViewStateManager.FetchingCouponsState.IDLE
import com.woocommerce.android.ui.woopos.home.items.coupons.WooPosCouponsListViewStateManager.FetchingCouponsState.PTR_FETCHING_FIRST_PAGE
import com.woocommerce.android.ui.woopos.home.items.coupons.WooPosCouponsListViewStateManager.FetchingCouponsState.RETRY_FETCHING_FIRST_PAGE
import com.woocommerce.android.ui.woopos.util.WooPosGetCachedStoreCurrency
import com.woocommerce.android.ui.woopos.util.format.WooPosFormatCouponSummary
import kotlinx.coroutines.CoroutineScope
Expand All @@ -37,50 +40,77 @@ class WooPosCouponsListViewStateManager @Inject constructor(

private var loadingMoreJob: Job? = null
private var fetchingFirstPageJob: Job? = null
private var canLoadMore: Boolean = false
private var canLoadMore: Boolean = true

enum class FetchingCouponsState {
IDLE, FETCHING_FIRST_PAGE, FETCHING_MORE, ERROR_FETCHING_FIRST_PAGE, ERROR_FETCHING_MORE
IDLE, FETCHING_FIRST_PAGE, PTR_FETCHING_FIRST_PAGE, RETRY_FETCHING_FIRST_PAGE, FETCHING_MORE,
ERROR_FETCHING_FIRST_PAGE, ERROR_FETCHING_MORE
}

private val contentFlow = couponsDataSource.couponsFlow.combine(fetchingState) { coupons, fetchingState ->
if (!cachedCouponEnabledChecker.isEnabled()) {
return@combine WooPosCouponsViewState.Error.CouponsDisabledError()
}
when (fetchingState) {
FETCHING_FIRST_PAGE -> {
WooPosCouponsViewState.Loading()
FETCHING_FIRST_PAGE, PTR_FETCHING_FIRST_PAGE -> {
if (coupons.isEmpty()) {
createLoadingState(fetchingState)
} else {
createContentViewState(coupons, fetchingState)
}
}
RETRY_FETCHING_FIRST_PAGE -> {
createLoadingState(fetchingState)
}

ERROR_FETCHING_FIRST_PAGE -> {
WooPosCouponsViewState.Error.GenericError()
}

IDLE, FETCHING_MORE, ERROR_FETCHING_MORE -> {
when {
coupons.isEmpty() -> WooPosCouponsViewState.Empty()
else -> {
WooPosCouponsViewState.Content(
items = mapCouponsToSelectionState(coupons),
paginationState = getPaginationState(fetchingState),
pullToRefreshState = getPullToRefreshState(fetchingState)
)
}
if (coupons.isEmpty()) {
WooPosCouponsViewState.Empty()
} else {
createContentViewState(coupons, fetchingState)
}
}
}
}

private fun createLoadingState(fetchingState: FetchingCouponsState) =
WooPosCouponsViewState.Loading(
pullToRefreshState = if (fetchingState == PTR_FETCHING_FIRST_PAGE) {
Refreshing
} else {
Disabled
}
)

private suspend fun createContentViewState(
coupons: List<Coupon>,
fetchingState: FetchingCouponsState
) = WooPosCouponsViewState.Content(
items = mapCouponsToSelectionState(coupons),
paginationState = getPaginationState(fetchingState),
pullToRefreshState = getPullToRefreshState(fetchingState)
)

val viewState: Flow<WooPosCouponsViewState> = contentFlow

fun fetchCoupons(viewModelScope: CoroutineScope) {
fun fetchCoupons(viewModelScope: CoroutineScope, refreshType: WooPosCouponsListRefreshType) {
if (fetchingFirstPageJob?.isActive == true) {
return
}

fetchingFirstPageJob = viewModelScope.launch {
loadingMoreJob?.cancelAndJoin()
fetchingState.emit(FETCHING_FIRST_PAGE)
fetchingState.emit(
when (refreshType) {
WooPosCouponsListRefreshType.INITIAL -> FETCHING_FIRST_PAGE
WooPosCouponsListRefreshType.PULL_TO_REFRESH -> PTR_FETCHING_FIRST_PAGE
WooPosCouponsListRefreshType.RETRY -> RETRY_FETCHING_FIRST_PAGE
}
)
val result = couponsDataSource.clearCacheAndFetchFirstPage()
if (result.isSuccess) {
canLoadMore = result.getOrNull() ?: false
Expand Down Expand Up @@ -130,15 +160,23 @@ class WooPosCouponsListViewStateManager @Inject constructor(

private fun getPaginationState(fetchingState: FetchingCouponsState) =
when (fetchingState) {
IDLE, FETCHING_FIRST_PAGE, FETCHING_MORE -> if (canLoadMore) Loading else None
IDLE, FETCHING_FIRST_PAGE, PTR_FETCHING_FIRST_PAGE, RETRY_FETCHING_FIRST_PAGE, FETCHING_MORE -> {
if (canLoadMore) Loading else None
}

ERROR_FETCHING_MORE -> Error
ERROR_FETCHING_FIRST_PAGE -> error("Full screen error should be displayed")
}

private fun getPullToRefreshState(fetchingState: FetchingCouponsState) =
when (fetchingState) {
ERROR_FETCHING_MORE, IDLE, FETCHING_MORE -> Enabled
FETCHING_FIRST_PAGE -> Refreshing
IDLE, FETCHING_MORE -> Enabled
FETCHING_FIRST_PAGE, RETRY_FETCHING_FIRST_PAGE, ERROR_FETCHING_MORE -> Disabled
PTR_FETCHING_FIRST_PAGE -> Refreshing
ERROR_FETCHING_FIRST_PAGE -> error("Full screen error should be displayed")
}

enum class WooPosCouponsListRefreshType {
INITIAL, PULL_TO_REFRESH, RETRY
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.woocommerce.android.ui.woopos.home.ChildToParentEvent
import com.woocommerce.android.ui.woopos.home.WooPosChildrenToParentEventSender
import com.woocommerce.android.ui.woopos.home.items.WooPosCouponsViewState
import com.woocommerce.android.ui.woopos.home.items.WooPosItemsViewModel.ItemClickedData
import com.woocommerce.android.ui.woopos.home.items.coupons.WooPosCouponsListViewStateManager.WooPosCouponsListRefreshType
import com.woocommerce.android.ui.woopos.home.items.coupons.WooPosCouponsUIEvent.BackButtonClicked
import com.woocommerce.android.ui.woopos.home.items.coupons.WooPosCouponsUIEvent.CouponClicked
import com.woocommerce.android.ui.woopos.home.items.coupons.WooPosCouponsUIEvent.EndOfListReached
Expand Down Expand Up @@ -46,7 +47,7 @@ class WooPosCouponsViewModel @Inject constructor(
}
}

listViewStateManager.fetchCoupons(viewModelScope)
fetchCoupons(WooPosCouponsListRefreshType.INITIAL)
}

fun onUIEvent(event: WooPosCouponsUIEvent) {
Expand All @@ -55,7 +56,7 @@ class WooPosCouponsViewModel @Inject constructor(
handleCouponClicked(event)
}

PullToRefreshTriggered -> fetchCoupons()
PullToRefreshTriggered -> fetchCoupons(WooPosCouponsListRefreshType.PULL_TO_REFRESH)

is EndOfListReached -> {
onEndOfListReached()
Expand All @@ -69,16 +70,16 @@ class WooPosCouponsViewModel @Inject constructor(
navigateBackToItemListScreen()
}

RetryTriggered -> fetchCoupons()
RetryTriggered -> fetchCoupons(WooPosCouponsListRefreshType.RETRY)

is WooPosCouponsUIEvent.CreateCouponClicked -> {
error("Create coupon clicked event not implemented yet")
}
}
}

private fun fetchCoupons() {
listViewStateManager.fetchCoupons(viewModelScope)
private fun fetchCoupons(refreshType: WooPosCouponsListRefreshType) {
listViewStateManager.fetchCoupons(viewModelScope, refreshType)
}

private fun onEndOfListReached() {
Expand Down
Loading