Skip to content
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

fix(health-sdk): Fix paymentComponentView not collecting again afte… #482

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -56,6 +57,7 @@ class PaymentComponentView(context: Context, attrs: AttributeSet?) : ConstraintL

@VisibleForTesting
internal var coroutineScope: CoroutineScope? = null
private var collectJob: Job? = null

/**
* Sets the payable state of the [PaymentComponentView]. If `true`, the view will be shown, otherwise it will be hidden.
Expand All @@ -80,6 +82,10 @@ class PaymentComponentView(context: Context, attrs: AttributeSet?) : ConstraintL
* The document id of the invoice item. This will be returned in the [PaymentComponent.Listener.onPayInvoiceClicked] method.
*/
var documentId: String? = null
set(value) {
field = value
launchJobs()
}

private val binding = GhsViewPaymentComponentBinding.inflate(getLayoutInflaterWithGiniHealthTheme(), this)

Expand All @@ -90,12 +96,20 @@ class PaymentComponentView(context: Context, attrs: AttributeSet?) : ConstraintL
override fun onAttachedToWindow() {
super.onAttachedToWindow()
LOG.debug("onAttachedToWindow")
launchJobs()
}

private fun launchJobs() {
if (coroutineScope == null) {
LOG.debug("Creating coroutine scope")
coroutineScope = CoroutineScope(coroutineContext)
}
collectJob?.let {
it.cancel()
collectJob = null
}
checkPaymentComponentHeight()
coroutineScope?.launch {
collectJob = coroutineScope?.launch {
if (paymentComponent == null) {
LOG.warn("Cannot show payment provider apps: PaymentComponent must be set before showing the PaymentComponentView")
return@launch
Expand Down Expand Up @@ -233,6 +247,8 @@ class PaymentComponentView(context: Context, attrs: AttributeSet?) : ConstraintL
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
LOG.debug("onDetachedFromWindow")
collectJob?.cancel()
collectJob = null
coroutineScope?.cancel()
coroutineScope = null
}
Expand All @@ -254,6 +270,7 @@ class PaymentComponentView(context: Context, attrs: AttributeSet?) : ConstraintL
binding.ghsSelectBankPicker.visibility = VISIBLE
binding.ghsPoweredByGini.visibility = VISIBLE
changeLabelsVisibilityIfNeeded()
launchJobs()
}

private fun hide() {
Expand Down
Loading