-
Notifications
You must be signed in to change notification settings - Fork 50
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
Show dialog after completed or error purchasing #1246
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,20 +4,26 @@ import android.app.Application | |
import androidx.lifecycle.AndroidViewModel | ||
import androidx.lifecycle.SavedStateHandle | ||
import androidx.lifecycle.viewModelScope | ||
import com.revenuecat.purchases.CustomerInfo | ||
import com.revenuecat.purchases.Purchases | ||
import com.revenuecat.purchases.PurchasesError | ||
import com.revenuecat.purchases.PurchasesException | ||
import com.revenuecat.purchases.awaitOfferings | ||
import com.revenuecat.purchases.models.StoreTransaction | ||
import com.revenuecat.purchases.ui.revenuecatui.PaywallViewListener | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.flow.update | ||
import kotlinx.coroutines.launch | ||
|
||
interface PaywallScreenViewModel { | ||
interface PaywallScreenViewModel : PaywallViewListener { | ||
companion object { | ||
const val OFFERING_ID_KEY = "offering_id" | ||
} | ||
val state: StateFlow<PaywallScreenState> | ||
|
||
fun onDialogDismissed() | ||
} | ||
|
||
class PaywallScreenViewModelImpl( | ||
|
@@ -35,6 +41,72 @@ class PaywallScreenViewModelImpl( | |
updateOffering() | ||
} | ||
|
||
override fun onRestoreStarted() { | ||
val value = _state.value | ||
if (value is PaywallScreenState.Loaded) { | ||
_state.update { | ||
value.copy( | ||
dialogText = "Restoring purchases...", | ||
) | ||
} | ||
} | ||
} | ||
|
||
override fun onRestoreCompleted(customerInfo: CustomerInfo) { | ||
val value = _state.value | ||
if (value is PaywallScreenState.Loaded) { | ||
_state.update { | ||
value.copy( | ||
dialogText = "Restore completed", | ||
) | ||
} | ||
} | ||
} | ||
|
||
override fun onRestoreError(error: PurchasesError) { | ||
val value = _state.value | ||
if (value is PaywallScreenState.Loaded) { | ||
_state.update { | ||
value.copy( | ||
dialogText = "There was an error restoring purchases:\n${error.message}", | ||
) | ||
} | ||
} | ||
} | ||
|
||
override fun onPurchaseCompleted(customerInfo: CustomerInfo, storeTransaction: StoreTransaction) { | ||
val value = _state.value | ||
if (value is PaywallScreenState.Loaded) { | ||
_state.update { | ||
value.copy( | ||
dialogText = "Purchase was successful", | ||
) | ||
} | ||
} | ||
} | ||
|
||
override fun onPurchaseError(error: PurchasesError) { | ||
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. I guess we are not displaying the error message in the dialog right? I wonder if instead of a boolean, we should use a nullable string in the state so we can display the error message. 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. I see we already made the conversion to a nullable string in the followup PR... So then the question is whether we want to include the error in the dialog... IMO, it might be a good idea for a tester app since it would be easier to visualize errors in case they happen when downloaded from the play store. 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. Added error message |
||
val value = _state.value | ||
if (value is PaywallScreenState.Loaded) { | ||
_state.update { | ||
value.copy( | ||
dialogText = "There was an error purchasing:\n${error.message}", | ||
) | ||
} | ||
} | ||
} | ||
|
||
override fun onDialogDismissed() { | ||
val value = _state.value | ||
if (value is PaywallScreenState.Loaded) { | ||
_state.update { | ||
value.copy( | ||
dialogText = null, | ||
) | ||
} | ||
} | ||
} | ||
|
||
private fun updateOffering() { | ||
viewModelScope.launch { | ||
try { | ||
|
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.
We might want to extract this to a different file... But for now, I think it's fine to keep here.