Skip to content

Commit

Permalink
feat(health-sdk): Fixed error not showing in 'share with' scenario
Browse files Browse the repository at this point in the history
IPC-279
  • Loading branch information
danicretu committed Jun 17, 2024
1 parent 478b40e commit b0d3021
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.lifecycle.LifecycleEventObserver
import androidx.savedstate.SavedStateRegistry
import androidx.savedstate.SavedStateRegistryOwner
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -143,9 +144,12 @@ class GiniHealth(
}
}

internal fun setOpenBankState(state: PaymentState) {
internal fun setOpenBankState(state: PaymentState, scope: CoroutineScope) {
_openBankState.value = state
_openBankState.value = PaymentState.NoAction
scope.launch {
delay(50)
_openBankState.value = PaymentState.NoAction
}
}

internal suspend fun retryDocumentReview() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class ReviewFragment private constructor(
private var binding: GhsFragmentReviewBinding by autoCleared()
private var documentPageAdapter: DocumentPageAdapter by autoCleared()
private var isKeyboardShown = false
private var errorSnackbar: Snackbar? = null

override fun onGetLayoutInflater(savedInstanceState: Bundle?): LayoutInflater {
val inflater = super.onGetLayoutInflater(savedInstanceState)
Expand Down Expand Up @@ -374,12 +375,15 @@ class ReviewFragment private constructor(

private fun GhsFragmentReviewBinding.showSnackbar(text: String, onRetry: () -> Unit) {
val context = requireContext().wrappedWithGiniHealthTheme()
Snackbar.make(context, root, text, Snackbar.LENGTH_INDEFINITE).apply {
errorSnackbar?.dismiss()
errorSnackbar = Snackbar.make(context, root, text, Snackbar.LENGTH_INDEFINITE).apply {
if (context.getFontScale() < 1.5) {
anchorView = paymentDetailsScrollview
}
setTextMaxLines(2)
setAction(getString(R.string.ghs_snackbar_retry)) { onRetry() }
setAction(getString(R.string.ghs_snackbar_retry)) {
onRetry()
}
show()
}
}
Expand Down Expand Up @@ -538,6 +542,7 @@ class ReviewFragment private constructor(
}

private fun showInstallAppDialog(paymentProviderApp: PaymentProviderApp) {
errorSnackbar?.dismiss()
val dialog = InstallAppBottomSheet.newInstance(viewModel.paymentComponent, object : InstallAppForwardListener {
override fun onForwardToBankSelected() {
redirectToBankApp(paymentProviderApp)
Expand All @@ -552,6 +557,7 @@ class ReviewFragment private constructor(
}

private fun showOpenWithDialog(paymentProviderApp: PaymentProviderApp) {
errorSnackbar?.dismiss()
OpenWithBottomSheet.newInstance(paymentProviderApp, object: OpenWithForwardListener {
override fun onForwardSelected() {
viewModel.onForwardToSharePdfTapped(requireContext().externalCacheDir)
Expand All @@ -563,6 +569,7 @@ class ReviewFragment private constructor(
}

private fun startSharePdfIntent(paymentRequestFile: File) {
errorSnackbar?.dismiss()
val uriForFile = FileProvider.getUriForFile(
requireContext(),
requireContext().packageName+".health.sdk.fileprovider",
Expand All @@ -579,7 +586,7 @@ class ReviewFragment private constructor(
private fun handlePaymentNextStep(paymentNextStep: ReviewViewModel.PaymentNextStep) {
when (paymentNextStep) {
is ReviewViewModel.PaymentNextStep.SetLoadingVisibility -> {
binding.loading.isVisible = paymentNextStep.isVisible
errorSnackbar?.dismiss()
}
ReviewViewModel.PaymentNextStep.RedirectToBank -> {
viewModel.paymentProviderApp.value?.name?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,13 @@ internal class ReviewViewModel(
val paymentProviderApp = paymentProviderApp.value
if (paymentProviderApp == null) {
LOG.error("No selected payment provider app")
giniHealth.setOpenBankState(GiniHealth.PaymentState.Error(Exception("No selected payment provider app")))
giniHealth.setOpenBankState(GiniHealth.PaymentState.Error(Exception("No selected payment provider app")), viewModelScope)
return
}

if (paymentProviderApp.installedPaymentProviderApp == null) {
LOG.error("Payment provider app not installed")
giniHealth.setOpenBankState(GiniHealth.PaymentState.Error(Exception("Payment provider app not installed")))
giniHealth.setOpenBankState(GiniHealth.PaymentState.Error(Exception("Payment provider app not installed")), viewModelScope)
return
}

Expand All @@ -227,7 +227,8 @@ internal class ReviewViewModel(
GiniHealth.PaymentState.Success(getPaymentRequest())
} catch (throwable: Throwable) {
GiniHealth.PaymentState.Error(throwable)
}
},
viewModelScope
)
}
}
Expand All @@ -237,7 +238,7 @@ internal class ReviewViewModel(
// Schedule on the main dispatcher to allow all collectors to receive the current state before
// the state is overridden
viewModelScope.launch(Dispatchers.Main) {
giniHealth.setOpenBankState(GiniHealth.PaymentState.NoAction)
giniHealth.setOpenBankState(GiniHealth.PaymentState.NoAction, viewModelScope)
}
}

Expand Down Expand Up @@ -301,19 +302,19 @@ internal class ReviewViewModel(
val paymentRequest = try {
getPaymentRequest()
} catch (throwable: Throwable) {
giniHealth.setOpenBankState(GiniHealth.PaymentState.Error(throwable))
giniHealth.setOpenBankState(GiniHealth.PaymentState.Error(throwable), viewModelScope)
return@withContext
}
val byteArrayResource = async { giniHealth.giniHealthAPI.documentManager.getPaymentRequestDocument(paymentRequest.id) }.await()
when (byteArrayResource) {
is Resource.Cancelled -> {
giniHealth.setOpenBankState(GiniHealth.PaymentState.Error(Exception("Cancelled")))
giniHealth.setOpenBankState(GiniHealth.PaymentState.Error(Exception("Cancelled")), viewModelScope)
}
is Resource.Error -> {
giniHealth.setOpenBankState(GiniHealth.PaymentState.Error(byteArrayResource.exception ?: Exception("Error")))
giniHealth.setOpenBankState(GiniHealth.PaymentState.Error(byteArrayResource.exception ?: Exception("Error")), viewModelScope)
}
is Resource.Success -> {
giniHealth.setOpenBankState(GiniHealth.PaymentState.Success(paymentRequest))
giniHealth.setOpenBankState(GiniHealth.PaymentState.Success(paymentRequest), viewModelScope)
val newFile = externalCacheDir?.createTempPdfFile(byteArrayResource.data, "payment-request")
newFile?.let {
_paymentNextStep.tryEmit(PaymentNextStep.OpenSharePdf(it))
Expand All @@ -325,7 +326,7 @@ internal class ReviewViewModel(
}

private fun sendFeedbackAndStartLoading() {
giniHealth.setOpenBankState(GiniHealth.PaymentState.Loading)
giniHealth.setOpenBankState(GiniHealth.PaymentState.Loading, viewModelScope)
// TODO: first get the payment request and handle error before proceeding
sendFeedback()
}
Expand Down

0 comments on commit b0d3021

Please sign in to comment.