Move fetch of first emission to the observeInAppPaymentRedemption call.

This commit is contained in:
Alex Hart
2026-03-12 11:17:18 -03:00
committed by Michelle Tang
parent 874bc1a1c9
commit dba5252be6
2 changed files with 4 additions and 18 deletions

View File

@@ -588,12 +588,14 @@ object InAppPaymentsRepository {
val fromDatabase: Observable<DonationRedemptionJobStatus> = Observable.create { emitter ->
val observer = InAppPaymentObserver {
val latestInAppPayment = SignalDatabase.inAppPayments.getLatestInAppPaymentByType(type)
emitter.onNext(Optional.ofNullable(latestInAppPayment))
}
AppDependencies.databaseObserver.registerInAppPaymentObserver(observer)
emitter.setCancellable { AppDependencies.databaseObserver.unregisterObserver(observer) }
val latestInAppPayment = SignalDatabase.inAppPayments.getLatestInAppPaymentByType(type)
emitter.onNext(Optional.ofNullable(latestInAppPayment))
}.switchMap { inAppPaymentOptional ->
val inAppPayment = inAppPaymentOptional.getOrNull() ?: return@switchMap Observable.just(DonationRedemptionJobStatus.None)

View File

@@ -66,8 +66,6 @@ class ManageDonationsViewModel : ViewModel() {
}
viewModelScope.launch(Dispatchers.IO) {
updateRecurringDonationState()
InAppPaymentsRepository.observeInAppPaymentRedemption(InAppPaymentType.RECURRING_DONATION)
.asFlow()
.collect { redemptionStatus ->
@@ -79,6 +77,7 @@ class ManageDonationsViewModel : ViewModel() {
store.update { manageDonationsState ->
manageDonationsState.copy(
isLoaded = true,
nonVerifiedMonthlyDonation = if (redemptionStatus is DonationRedemptionJobStatus.PendingExternalVerification) redemptionStatus.nonVerifiedMonthlyDonation else null,
subscriptionRedemptionState = deriveRedemptionState(redemptionStatus, latestPayment),
activeSubscription = activeSubscription
@@ -160,21 +159,6 @@ class ManageDonationsViewModel : ViewModel() {
)
}
private fun updateRecurringDonationState() {
val latestPayment = SignalDatabase.inAppPayments.getLatestInAppPaymentByType(InAppPaymentType.RECURRING_DONATION)
val activeSubscription: InAppPaymentTable.InAppPayment? = latestPayment?.let {
if (it.data.cancellation == null) it else null
}
store.update { manageDonationsState ->
manageDonationsState.copy(
isLoaded = true,
activeSubscription = activeSubscription
)
}
}
private fun deriveRedemptionState(status: DonationRedemptionJobStatus, latestPayment: InAppPaymentTable.InAppPayment?): ManageDonationsState.RedemptionState {
return when (status) {
DonationRedemptionJobStatus.None -> ManageDonationsState.RedemptionState.NONE