@@ -6,13 +6,16 @@ import com.woocommerce.android.ui.woopos.home.items.WooPosItemSelectionViewState
6
6
import com.woocommerce.android.ui.woopos.home.items.WooPosPaginationState.Error
7
7
import com.woocommerce.android.ui.woopos.home.items.WooPosPaginationState.Loading
8
8
import com.woocommerce.android.ui.woopos.home.items.WooPosPaginationState.None
9
+ import com.woocommerce.android.ui.woopos.home.items.WooPosPullToRefreshState.Disabled
9
10
import com.woocommerce.android.ui.woopos.home.items.WooPosPullToRefreshState.Enabled
10
11
import com.woocommerce.android.ui.woopos.home.items.WooPosPullToRefreshState.Refreshing
11
12
import com.woocommerce.android.ui.woopos.home.items.coupons.WooPosCouponsListViewStateManager.FetchingCouponsState.ERROR_FETCHING_FIRST_PAGE
12
13
import com.woocommerce.android.ui.woopos.home.items.coupons.WooPosCouponsListViewStateManager.FetchingCouponsState.ERROR_FETCHING_MORE
13
14
import com.woocommerce.android.ui.woopos.home.items.coupons.WooPosCouponsListViewStateManager.FetchingCouponsState.FETCHING_FIRST_PAGE
14
15
import com.woocommerce.android.ui.woopos.home.items.coupons.WooPosCouponsListViewStateManager.FetchingCouponsState.FETCHING_MORE
15
16
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
16
19
import com.woocommerce.android.ui.woopos.util.WooPosGetCachedStoreCurrency
17
20
import com.woocommerce.android.ui.woopos.util.format.WooPosFormatCouponSummary
18
21
import kotlinx.coroutines.CoroutineScope
@@ -37,50 +40,77 @@ class WooPosCouponsListViewStateManager @Inject constructor(
37
40
38
41
private var loadingMoreJob: Job ? = null
39
42
private var fetchingFirstPageJob: Job ? = null
40
- private var canLoadMore: Boolean = false
43
+ private var canLoadMore: Boolean = true
41
44
42
45
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
44
48
}
45
49
46
50
private val contentFlow = couponsDataSource.couponsFlow.combine(fetchingState) { coupons, fetchingState ->
47
51
if (! cachedCouponEnabledChecker.isEnabled()) {
48
52
return @combine WooPosCouponsViewState .Error .CouponsDisabledError ()
49
53
}
50
54
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)
53
64
}
54
65
55
66
ERROR_FETCHING_FIRST_PAGE -> {
56
67
WooPosCouponsViewState .Error .GenericError ()
57
68
}
58
69
59
70
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)
69
75
}
70
76
}
71
77
}
72
78
}
73
79
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
+
74
98
val viewState: Flow <WooPosCouponsViewState > = contentFlow
75
99
76
- fun fetchCoupons (viewModelScope : CoroutineScope ) {
100
+ fun fetchCoupons (viewModelScope : CoroutineScope , refreshType : WooPosCouponsListRefreshType ) {
77
101
if (fetchingFirstPageJob?.isActive == true ) {
78
102
return
79
103
}
80
104
81
105
fetchingFirstPageJob = viewModelScope.launch {
82
106
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
+ )
84
114
val result = couponsDataSource.clearCacheAndFetchFirstPage()
85
115
if (result.isSuccess) {
86
116
canLoadMore = result.getOrNull() ? : false
@@ -130,15 +160,23 @@ class WooPosCouponsListViewStateManager @Inject constructor(
130
160
131
161
private fun getPaginationState (fetchingState : FetchingCouponsState ) =
132
162
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
+
134
167
ERROR_FETCHING_MORE -> Error
135
168
ERROR_FETCHING_FIRST_PAGE -> error(" Full screen error should be displayed" )
136
169
}
137
170
138
171
private fun getPullToRefreshState (fetchingState : FetchingCouponsState ) =
139
172
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
142
176
ERROR_FETCHING_FIRST_PAGE -> error(" Full screen error should be displayed" )
143
177
}
178
+
179
+ enum class WooPosCouponsListRefreshType {
180
+ INITIAL , PULL_TO_REFRESH , RETRY
181
+ }
144
182
}
0 commit comments