-
Notifications
You must be signed in to change notification settings - Fork 137
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
[Woo POS][Cash & Receipts] Order completion call. Navigation to the payment success #13086
Open
kidinov
wants to merge
18
commits into
trunk
Choose a base branch
from
13084-woo-poscash-receipts-implement-order-completion-call-during-cash-payment-flow-with-navigation-to-the-payment-success
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
7af85f4
completeOrder to repository
kidinov fcc008e
Handle order completion
kidinov 4ec0eed
Simplification of the code that handles request
kidinov 23d4975
Navigate back and show success screen
kidinov 013a53e
Working navigation, animatio is broken
kidinov defe37b
Fixed formatting
kidinov 3eb3020
Merge branch '13063-woo-poscash-receipts-implement-order-complition-c…
kidinov e17790c
Fixed wrong code merge
kidinov a06da29
Better naming for route to keep navigation clear
kidinov 95b2023
Fixed arg setting
kidinov 3c8fa06
Fixed back and forward animations
kidinov 4e0d796
Navigate to the success screen via forward navigation
kidinov f49d93f
Fixed a bug when it was navigating to success after success payment w…
kidinov 8d4a1cb
Merge branch '13063-woo-poscash-receipts-implement-order-complition-c…
kidinov b3de009
Use const as order id key
kidinov a0c133a
Repository unit test
kidinov 1d482ff
Fixed unit test
kidinov c4d2887
Formatting
kidinov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
19 changes: 6 additions & 13 deletions
19
.../main/kotlin/com/woocommerce/android/ui/woopos/cashpayment/WooPosCashPaymentNavigation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
.../main/kotlin/com/woocommerce/android/ui/woopos/cashpayment/WooPosCashPaymentRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,61 @@ | ||
package com.woocommerce.android.ui.woopos.cashpayment | ||
|
||
import com.woocommerce.android.model.Order | ||
import com.woocommerce.android.model.OrderMapper | ||
import com.woocommerce.android.tools.SelectedSite | ||
import com.woocommerce.android.util.WooLog | ||
import com.woocommerce.android.util.WooLog.T | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.filterIsInstance | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.withContext | ||
import org.wordpress.android.fluxc.model.WCOrderStatusModel | ||
import org.wordpress.android.fluxc.store.WCGatewayStore | ||
import org.wordpress.android.fluxc.store.WCOrderStore | ||
import javax.inject.Inject | ||
|
||
class WooPosCashPaymentRepository @Inject constructor( | ||
private val selectedSite: SelectedSite, | ||
private val orderStore: WCOrderStore, | ||
private val orderMapper: OrderMapper, | ||
private val gatewayStore: WCGatewayStore, | ||
) { | ||
suspend fun getOrderById(orderId: Long) = withContext(Dispatchers.IO) { | ||
orderStore.getOrderByIdAndSite(orderId, selectedSite.get())?.let { | ||
orderMapper.toAppModel(it) | ||
} | ||
} | ||
|
||
suspend fun completeOrder(orderId: Long): Result<Unit> = withContext(Dispatchers.IO) { | ||
val codGateway = gatewayStore.getGateway(selectedSite.get(), CASH_ON_DELIVERY_PAYMENT_TYPE) | ||
|
||
val statusModel = orderStore.getOrderStatusForSiteAndKey( | ||
selectedSite.get(), | ||
Order.Status.Completed.value | ||
) ?: WCOrderStatusModel(statusKey = Order.Status.Completed.value).apply { | ||
label = statusKey | ||
} | ||
|
||
orderStore.updateOrderStatusAndPaymentMethod( | ||
orderId = orderId, | ||
site = selectedSite.get(), | ||
newStatus = statusModel, | ||
newPaymentMethodId = CASH_ON_DELIVERY_PAYMENT_TYPE, | ||
codGateway?.title ?: "Pay in Person", | ||
) | ||
.filterIsInstance<WCOrderStore.UpdateOrderResult.RemoteUpdateResult>() | ||
.map { result -> | ||
if (result.event.isError) { | ||
WooLog.e(T.POS, "Order completion failed - ${result.event.error.message}") | ||
Result.failure(Exception(result.event.error.message)) | ||
} else { | ||
Result.success(Unit) | ||
} | ||
}.first() | ||
} | ||
|
||
private companion object { | ||
const val CASH_ON_DELIVERY_PAYMENT_TYPE = "cod" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ fun WooPosCashPaymentScreen(onNavigationEvent: (WooPosNavigationEvent) -> Unit) | |
onAmountChanged = { viewModel.onUIEvent(WooPosCashPaymentUIEvent.AmountChanged(it)) }, | ||
onCompleteOrderClicked = { viewModel.onUIEvent(WooPosCashPaymentUIEvent.CompleteOrderClicked) }, | ||
onBackClicked = { onNavigationEvent(WooPosNavigationEvent.BackFromCashPayment) }, | ||
onOrderComplete = { onNavigationEvent(WooPosNavigationEvent.OpenHomeFromCashPaymentAfterSuccessfulPayment) }, | ||
) | ||
} | ||
|
||
|
@@ -50,6 +51,7 @@ fun WooPosCashPaymentScreen( | |
onAmountChanged: (String) -> Unit, | ||
onCompleteOrderClicked: () -> Unit, | ||
onBackClicked: () -> Unit, | ||
onOrderComplete: () -> Unit, | ||
) { | ||
Column( | ||
modifier = Modifier | ||
|
@@ -66,7 +68,7 @@ fun WooPosCashPaymentScreen( | |
) | ||
} | ||
|
||
WooPosCashPaymentState.Finishing -> TODO() | ||
WooPosCashPaymentState.Complete -> onOrderComplete() | ||
WooPosCashPaymentState.Initiating -> { | ||
// show nothing | ||
} | ||
|
@@ -86,6 +88,7 @@ private fun Collecting( | |
Column( | ||
modifier = Modifier | ||
.width(540.dp) | ||
.padding(32.dp) | ||
) { | ||
Spacer(modifier = Modifier.weight(1f)) | ||
|
||
|
@@ -133,9 +136,9 @@ private fun Collecting( | |
Spacer(modifier = Modifier.height(16.dp)) | ||
|
||
WooPosButton( | ||
text = "Mark completed", | ||
text = state.button.text, | ||
onClick = onCompleteOrderClicked, | ||
enabled = state.canBeOrderBeCompleted, | ||
enabled = state.button.status == WooPosCashPaymentState.Collecting.Button.Status.ENABLED, | ||
) | ||
|
||
Spacer(modifier = Modifier.weight(1f)) | ||
|
@@ -196,11 +199,15 @@ fun WooPosTotalsPaymentCashScreenScreen() { | |
enteredAmount = "5$", | ||
changeDue = "5$", | ||
total = "10$", | ||
canBeOrderBeCompleted = true, | ||
button = WooPosCashPaymentState.Collecting.Button( | ||
text = "Complete order", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 To Res |
||
status = WooPosCashPaymentState.Collecting.Button.Status.ENABLED | ||
) | ||
), | ||
onAmountChanged = {}, | ||
onCompleteOrderClicked = {}, | ||
onBackClicked = {}, | ||
onOrderComplete = {}, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,9 @@ package com.woocommerce.android.ui.woopos.cashpayment | |
import androidx.lifecycle.SavedStateHandle | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.woocommerce.android.R | ||
import com.woocommerce.android.ui.woopos.util.format.WooPosFormatPrice | ||
import com.woocommerce.android.viewmodel.ResourceProvider | ||
import com.woocommerce.android.viewmodel.getStateFlow | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.flow.StateFlow | ||
|
@@ -15,9 +17,10 @@ import javax.inject.Inject | |
class WooPosCashPaymentViewModel @Inject constructor( | ||
private val repository: WooPosCashPaymentRepository, | ||
private val priceFormat: WooPosFormatPrice, | ||
private val resourceProvider: ResourceProvider, | ||
savedState: SavedStateHandle, | ||
) : ViewModel() { | ||
private val orderId = savedState.get<Long>("orderId")!! | ||
private val orderId = savedState.get<Long>(CASH_ROUTE_ORDER_ID_KEY)!! | ||
|
||
private val _state = savedState.getStateFlow<WooPosCashPaymentState>( | ||
scope = viewModelScope, | ||
|
@@ -34,15 +37,42 @@ class WooPosCashPaymentViewModel @Inject constructor( | |
enteredAmount = "", | ||
changeDue = priceFormat(BigDecimal.ZERO), | ||
total = priceFormat(order.total), | ||
canBeOrderBeCompleted = false | ||
button = WooPosCashPaymentState.Collecting.Button( | ||
text = resourceProvider.getString(R.string.woopos_complete_cash_order_button), | ||
status = WooPosCashPaymentState.Collecting.Button.Status.ENABLED | ||
) | ||
) | ||
} | ||
} | ||
|
||
fun onUIEvent(event: WooPosCashPaymentUIEvent) { | ||
when (event) { | ||
is WooPosCashPaymentUIEvent.AmountChanged -> TODO() | ||
WooPosCashPaymentUIEvent.CompleteOrderClicked -> TODO() | ||
WooPosCashPaymentUIEvent.CompleteOrderClicked -> handleOrderCompletion() | ||
} | ||
} | ||
|
||
private fun handleOrderCompletion() { | ||
viewModelScope.launch { | ||
val stateBeforeCompleting = _state.value as? WooPosCashPaymentState.Collecting ?: return@launch | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❓ Do we expect other states – shouldn't we crash? What will happen if we return here? |
||
_state.value = stateBeforeCompleting.copy( | ||
button = stateBeforeCompleting.button.copy( | ||
status = WooPosCashPaymentState.Collecting.Button.Status.LOADING | ||
) | ||
) | ||
|
||
val result = repository.completeOrder(orderId) | ||
if (result.isSuccess) { | ||
_state.value = WooPosCashPaymentState.Complete | ||
} else { | ||
val currentState = _state.value as? WooPosCashPaymentState.Collecting ?: return@launch | ||
currentState | ||
_state.value = currentState.copy( | ||
button = currentState.button.copy( | ||
status = WooPosCashPaymentState.Collecting.Button.Status.ENABLED | ||
) | ||
) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 np