in ClassyTaxiAppKotlin/app/src/main/java/com/example/subscriptions/ui/TvMainActivity.kt [52:113]
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_tv_main)
authenticationViewModel = ViewModelProviders.of(this).get(FirebaseUserViewModel::class.java)
billingViewModel = ViewModelProviders.of(this).get(BillingViewModel::class.java)
subscriptionViewModel =
ViewModelProviders.of(this).get(SubscriptionStatusViewModel::class.java)
// Billing APIs are all handled in the this lifecycle observer.
billingClientLifecycle = (application as SubApp).billingClientLifecycle
lifecycle.addObserver(billingClientLifecycle)
// Register purchases when they change.
billingClientLifecycle.purchaseUpdateEvent.observe(this, {
it?.let {
registerPurchases(it)
}
})
// Launch the billing flow when the user clicks a button to buy something.
billingViewModel.buyEvent.observe(this, {
it?.let {
billingClientLifecycle.launchBillingFlow(this, it)
}
})
// Open the Play Store when this event is triggered.
billingViewModel.openPlayStoreSubscriptionsEvent.observe(this, {
Log.i(TAG, "Viewing subscriptions on the Google Play Store")
val sku = it
val url = if (sku == null) {
// If the SKU is not specified, just open the Google Play subscriptions URL.
Constants.PLAY_STORE_SUBSCRIPTION_URL
} else {
// If the SKU is specified, open the deeplink for this SKU on Google Play.
String.format(Constants.PLAY_STORE_SUBSCRIPTION_DEEPLINK_URL, sku, packageName)
}
val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse(url)
startActivity(intent)
})
// Update authentication UI.
authenticationViewModel.firebaseUser.observe(this, {
invalidateOptionsMenu()
if (it == null) {
triggerSignIn()
} else {
Log.d(TAG, "CURRENT user: " + it.email + " " + it.displayName)
}
})
// Update subscription information when user changes.
authenticationViewModel.userChangeEvent.observe(this, {
subscriptionViewModel.userChanged()
billingClientLifecycle.purchaseUpdateEvent.value?.let {
registerPurchases(it)
}
})
}