Skip to content

Commit

Permalink
feat(health-sdk): Included sendFeedback and setOpenBankState in …
Browse files Browse the repository at this point in the history
…PDF flow

IPC-251
  • Loading branch information
danicretu committed May 17, 2024
1 parent 2113dbf commit c38588f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import android.content.ActivityNotFoundException
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet
import androidx.core.content.FileProvider
Expand Down Expand Up @@ -344,6 +346,7 @@ class ReviewFragment private constructor(
}
when (paymentState) {
is GiniHealth.PaymentState.Success -> {
if (viewModel.paymentProviderApp.value?.paymentProvider?.gpcSupported() == false) return
try {
val intent =
paymentState.paymentRequest.bankApp.getIntent(paymentState.paymentRequest.id)
Expand Down Expand Up @@ -593,6 +596,7 @@ class ReviewFragment private constructor(
binding.loading.isVisible = false
startSharePdfIntent(paymentNextStep.file)
}
is ReviewViewModel.PaymentNextStep.OpenSharePdfError -> Toast.makeText(requireContext(), "Share PDF failed with error: ${paymentNextStep.error}", Toast.LENGTH_LONG).show()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.gini.android.health.sdk.review

import android.content.Context
import android.util.Log
import androidx.annotation.VisibleForTesting
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
Expand Down Expand Up @@ -217,9 +218,7 @@ internal class ReviewViewModel(val giniHealth: GiniHealth, val configuration: Re
viewModelScope.launch {
val valid = validatePaymentDetails(paymentDetails.value)
if (valid) {
giniHealth.setOpenBankState(GiniHealth.PaymentState.Loading)
// TODO: first get the payment request and handle error before proceeding
sendFeedback()
sendFeedbackAndStartLoading()
giniHealth.setOpenBankState(
try {
GiniHealth.PaymentState.Success(getPaymentRequest())
Expand Down Expand Up @@ -295,12 +294,26 @@ internal class ReviewViewModel(val giniHealth: GiniHealth, val configuration: Re
internal fun getFileAsByteArray(externalCacheDir: File?) {
viewModelScope.launch {
withContext(Dispatchers.IO) {
val paymentRequest = async { getPaymentRequest() }.await()
sendFeedbackAndStartLoading()
val paymentRequest = try {
getPaymentRequest()
} catch (throwable: Throwable) {
GiniHealth.PaymentState.Error(throwable)
_paymentNextStep.tryEmit(PaymentNextStep.OpenSharePdfError(throwable.message ?: "Error getting payment request"))
return@withContext
}
val byteArrayResource = async { giniHealth.giniHealthAPI.documentManager.getPaymentRequestDocument(paymentRequest.id) }.await()
when (byteArrayResource) {
is Resource.Cancelled -> throw Exception("Cancelled")
is Resource.Error -> throw Exception(byteArrayResource.exception)
is Resource.Cancelled -> {
GiniHealth.PaymentState.Error(Exception("Cancelled"))
_paymentNextStep.tryEmit(PaymentNextStep.OpenSharePdfError("Cancelled"))
}
is Resource.Error -> {
GiniHealth.PaymentState.Error(Exception(byteArrayResource.exception))
_paymentNextStep.tryEmit(PaymentNextStep.OpenSharePdfError(byteArrayResource.exception?.message ?: "Error getting payment request file"))
}
is Resource.Success -> {
giniHealth.setOpenBankState(GiniHealth.PaymentState.Success(paymentRequest))
val newFile = externalCacheDir?.createTempPdfFile(byteArrayResource.data, "payment-request")
newFile?.let {
_paymentNextStep.tryEmit(PaymentNextStep.OpenSharePdf(it))
Expand All @@ -310,12 +323,19 @@ internal class ReviewViewModel(val giniHealth: GiniHealth, val configuration: Re
}
}
}

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

sealed class PaymentNextStep {
object RedirectToBank: PaymentNextStep()
object ShowOpenWithSheet: PaymentNextStep()
data class OpenSharePdf(val file: File): PaymentNextStep()
object ShowInstallApp: PaymentNextStep()

data class OpenSharePdfError(val error: String): PaymentNextStep()
data class OpenSharePdf(val file: File): PaymentNextStep()
data class SetLoadingVisibility(val isVisible: Boolean): PaymentNextStep()
}

Expand Down

0 comments on commit c38588f

Please sign in to comment.