in ClassyTaxiAppKotlin/app/src/main/java/com/example/subscriptions/ui/MainActivity.kt [70:139]
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
sectionsPagerAdapter = SectionsPagerAdapter(supportFragmentManager)
// Set up the ViewPager with the sections adapter.
container.adapter = sectionsPagerAdapter
container.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tabs))
tabs.addOnTabSelectedListener(TabLayout.ViewPagerOnTabSelectedListener(container))
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)
}
})
}