Skip to content

Commit e688aaa

Browse files
authored
Merge pull request #14019 from woocommerce/issue/woomob-382-woo-poscoupons-show-cached-coupons-before-remote-fetch
[WOOMOB-382] Update Coupons Fetching/Loading Business Logic
2 parents 553b191 + ada3e0b commit e688aaa

File tree

3 files changed

+252
-46
lines changed

3 files changed

+252
-46
lines changed

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/items/coupons/WooPosCouponsListViewStateManager.kt

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ import com.woocommerce.android.ui.woopos.home.items.WooPosItemSelectionViewState
66
import com.woocommerce.android.ui.woopos.home.items.WooPosPaginationState.Error
77
import com.woocommerce.android.ui.woopos.home.items.WooPosPaginationState.Loading
88
import com.woocommerce.android.ui.woopos.home.items.WooPosPaginationState.None
9+
import com.woocommerce.android.ui.woopos.home.items.WooPosPullToRefreshState.Disabled
910
import com.woocommerce.android.ui.woopos.home.items.WooPosPullToRefreshState.Enabled
1011
import com.woocommerce.android.ui.woopos.home.items.WooPosPullToRefreshState.Refreshing
1112
import com.woocommerce.android.ui.woopos.home.items.coupons.WooPosCouponsListViewStateManager.FetchingCouponsState.ERROR_FETCHING_FIRST_PAGE
1213
import com.woocommerce.android.ui.woopos.home.items.coupons.WooPosCouponsListViewStateManager.FetchingCouponsState.ERROR_FETCHING_MORE
1314
import com.woocommerce.android.ui.woopos.home.items.coupons.WooPosCouponsListViewStateManager.FetchingCouponsState.FETCHING_FIRST_PAGE
1415
import com.woocommerce.android.ui.woopos.home.items.coupons.WooPosCouponsListViewStateManager.FetchingCouponsState.FETCHING_MORE
1516
import com.woocommerce.android.ui.woopos.home.items.coupons.WooPosCouponsListViewStateManager.FetchingCouponsState.IDLE
17+
import com.woocommerce.android.ui.woopos.home.items.coupons.WooPosCouponsListViewStateManager.FetchingCouponsState.PTR_FETCHING_FIRST_PAGE
18+
import com.woocommerce.android.ui.woopos.home.items.coupons.WooPosCouponsListViewStateManager.FetchingCouponsState.RETRY_FETCHING_FIRST_PAGE
1619
import com.woocommerce.android.ui.woopos.util.WooPosGetCachedStoreCurrency
1720
import com.woocommerce.android.ui.woopos.util.format.WooPosFormatCouponSummary
1821
import kotlinx.coroutines.CoroutineScope
@@ -37,50 +40,77 @@ class WooPosCouponsListViewStateManager @Inject constructor(
3740

3841
private var loadingMoreJob: Job? = null
3942
private var fetchingFirstPageJob: Job? = null
40-
private var canLoadMore: Boolean = false
43+
private var canLoadMore: Boolean = true
4144

4245
enum class FetchingCouponsState {
43-
IDLE, FETCHING_FIRST_PAGE, FETCHING_MORE, ERROR_FETCHING_FIRST_PAGE, ERROR_FETCHING_MORE
46+
IDLE, FETCHING_FIRST_PAGE, PTR_FETCHING_FIRST_PAGE, RETRY_FETCHING_FIRST_PAGE, FETCHING_MORE,
47+
ERROR_FETCHING_FIRST_PAGE, ERROR_FETCHING_MORE
4448
}
4549

4650
private val contentFlow = couponsDataSource.couponsFlow.combine(fetchingState) { coupons, fetchingState ->
4751
if (!cachedCouponEnabledChecker.isEnabled()) {
4852
return@combine WooPosCouponsViewState.Error.CouponsDisabledError()
4953
}
5054
when (fetchingState) {
51-
FETCHING_FIRST_PAGE -> {
52-
WooPosCouponsViewState.Loading()
55+
FETCHING_FIRST_PAGE, PTR_FETCHING_FIRST_PAGE -> {
56+
if (coupons.isEmpty()) {
57+
createLoadingState(fetchingState)
58+
} else {
59+
createContentViewState(coupons, fetchingState)
60+
}
61+
}
62+
RETRY_FETCHING_FIRST_PAGE -> {
63+
createLoadingState(fetchingState)
5364
}
5465

5566
ERROR_FETCHING_FIRST_PAGE -> {
5667
WooPosCouponsViewState.Error.GenericError()
5768
}
5869

5970
IDLE, FETCHING_MORE, ERROR_FETCHING_MORE -> {
60-
when {
61-
coupons.isEmpty() -> WooPosCouponsViewState.Empty()
62-
else -> {
63-
WooPosCouponsViewState.Content(
64-
items = mapCouponsToSelectionState(coupons),
65-
paginationState = getPaginationState(fetchingState),
66-
pullToRefreshState = getPullToRefreshState(fetchingState)
67-
)
68-
}
71+
if (coupons.isEmpty()) {
72+
WooPosCouponsViewState.Empty()
73+
} else {
74+
createContentViewState(coupons, fetchingState)
6975
}
7076
}
7177
}
7278
}
7379

80+
private fun createLoadingState(fetchingState: FetchingCouponsState) =
81+
WooPosCouponsViewState.Loading(
82+
pullToRefreshState = if (fetchingState == PTR_FETCHING_FIRST_PAGE) {
83+
Refreshing
84+
} else {
85+
Disabled
86+
}
87+
)
88+
89+
private suspend fun createContentViewState(
90+
coupons: List<Coupon>,
91+
fetchingState: FetchingCouponsState
92+
) = WooPosCouponsViewState.Content(
93+
items = mapCouponsToSelectionState(coupons),
94+
paginationState = getPaginationState(fetchingState),
95+
pullToRefreshState = getPullToRefreshState(fetchingState)
96+
)
97+
7498
val viewState: Flow<WooPosCouponsViewState> = contentFlow
7599

76-
fun fetchCoupons(viewModelScope: CoroutineScope) {
100+
fun fetchCoupons(viewModelScope: CoroutineScope, refreshType: WooPosCouponsListRefreshType) {
77101
if (fetchingFirstPageJob?.isActive == true) {
78102
return
79103
}
80104

81105
fetchingFirstPageJob = viewModelScope.launch {
82106
loadingMoreJob?.cancelAndJoin()
83-
fetchingState.emit(FETCHING_FIRST_PAGE)
107+
fetchingState.emit(
108+
when (refreshType) {
109+
WooPosCouponsListRefreshType.INITIAL -> FETCHING_FIRST_PAGE
110+
WooPosCouponsListRefreshType.PULL_TO_REFRESH -> PTR_FETCHING_FIRST_PAGE
111+
WooPosCouponsListRefreshType.RETRY -> RETRY_FETCHING_FIRST_PAGE
112+
}
113+
)
84114
val result = couponsDataSource.clearCacheAndFetchFirstPage()
85115
if (result.isSuccess) {
86116
canLoadMore = result.getOrNull() ?: false
@@ -130,15 +160,23 @@ class WooPosCouponsListViewStateManager @Inject constructor(
130160

131161
private fun getPaginationState(fetchingState: FetchingCouponsState) =
132162
when (fetchingState) {
133-
IDLE, FETCHING_FIRST_PAGE, FETCHING_MORE -> if (canLoadMore) Loading else None
163+
IDLE, FETCHING_FIRST_PAGE, PTR_FETCHING_FIRST_PAGE, RETRY_FETCHING_FIRST_PAGE, FETCHING_MORE -> {
164+
if (canLoadMore) Loading else None
165+
}
166+
134167
ERROR_FETCHING_MORE -> Error
135168
ERROR_FETCHING_FIRST_PAGE -> error("Full screen error should be displayed")
136169
}
137170

138171
private fun getPullToRefreshState(fetchingState: FetchingCouponsState) =
139172
when (fetchingState) {
140-
ERROR_FETCHING_MORE, IDLE, FETCHING_MORE -> Enabled
141-
FETCHING_FIRST_PAGE -> Refreshing
173+
IDLE, FETCHING_MORE -> Enabled
174+
FETCHING_FIRST_PAGE, RETRY_FETCHING_FIRST_PAGE, ERROR_FETCHING_MORE -> Disabled
175+
PTR_FETCHING_FIRST_PAGE -> Refreshing
142176
ERROR_FETCHING_FIRST_PAGE -> error("Full screen error should be displayed")
143177
}
178+
179+
enum class WooPosCouponsListRefreshType {
180+
INITIAL, PULL_TO_REFRESH, RETRY
181+
}
144182
}

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/items/coupons/WooPosCouponsViewModel.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.woocommerce.android.ui.woopos.home.ChildToParentEvent
66
import com.woocommerce.android.ui.woopos.home.WooPosChildrenToParentEventSender
77
import com.woocommerce.android.ui.woopos.home.items.WooPosCouponsViewState
88
import com.woocommerce.android.ui.woopos.home.items.WooPosItemsViewModel.ItemClickedData
9+
import com.woocommerce.android.ui.woopos.home.items.coupons.WooPosCouponsListViewStateManager.WooPosCouponsListRefreshType
910
import com.woocommerce.android.ui.woopos.home.items.coupons.WooPosCouponsUIEvent.BackButtonClicked
1011
import com.woocommerce.android.ui.woopos.home.items.coupons.WooPosCouponsUIEvent.CouponClicked
1112
import com.woocommerce.android.ui.woopos.home.items.coupons.WooPosCouponsUIEvent.EndOfListReached
@@ -46,7 +47,7 @@ class WooPosCouponsViewModel @Inject constructor(
4647
}
4748
}
4849

49-
listViewStateManager.fetchCoupons(viewModelScope)
50+
fetchCoupons(WooPosCouponsListRefreshType.INITIAL)
5051
}
5152

5253
fun onUIEvent(event: WooPosCouponsUIEvent) {
@@ -55,7 +56,7 @@ class WooPosCouponsViewModel @Inject constructor(
5556
handleCouponClicked(event)
5657
}
5758

58-
PullToRefreshTriggered -> fetchCoupons()
59+
PullToRefreshTriggered -> fetchCoupons(WooPosCouponsListRefreshType.PULL_TO_REFRESH)
5960

6061
is EndOfListReached -> {
6162
onEndOfListReached()
@@ -69,16 +70,16 @@ class WooPosCouponsViewModel @Inject constructor(
6970
navigateBackToItemListScreen()
7071
}
7172

72-
RetryTriggered -> fetchCoupons()
73+
RetryTriggered -> fetchCoupons(WooPosCouponsListRefreshType.RETRY)
7374

7475
is WooPosCouponsUIEvent.CreateCouponClicked -> {
7576
error("Create coupon clicked event not implemented yet")
7677
}
7778
}
7879
}
7980

80-
private fun fetchCoupons() {
81-
listViewStateManager.fetchCoupons(viewModelScope)
81+
private fun fetchCoupons(refreshType: WooPosCouponsListRefreshType) {
82+
listViewStateManager.fetchCoupons(viewModelScope, refreshType)
8283
}
8384

8485
private fun onEndOfListReached() {

0 commit comments

Comments
 (0)