Skip to content

Commit

Permalink
Updated SettingsView.kt moving logic inside SettingsLoader.kt for avo…
Browse files Browse the repository at this point in the history
…iding uninitialized properties error
  • Loading branch information
Javinator9889 committed Jun 27, 2020
1 parent de1ca2b commit 128f997
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 160 deletions.
Expand Up @@ -20,41 +20,26 @@ package com.javinator9889.handwashingreminder.activities.views.fragments.setting

import android.os.Bundle
import android.view.View
import androidx.emoji.text.EmojiCompat
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreference
import com.afollestad.materialdialogs.MaterialDialog
import com.android.billingclient.api.BillingClient.BillingResponseCode
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.perf.FirebasePerformance
import com.javinator9889.handwashingreminder.R
import com.javinator9889.handwashingreminder.activities.base.LayoutVisibilityChange
import com.javinator9889.handwashingreminder.application.HandwashingApplication
import com.javinator9889.handwashingreminder.data.SettingsLoader
import com.javinator9889.handwashingreminder.gms.ads.AdsEnabler
import com.javinator9889.handwashingreminder.gms.splitservice.SplitInstallService
import com.javinator9889.handwashingreminder.gms.vendor.BillingService
import com.javinator9889.handwashingreminder.listeners.OnPurchaseFinishedListener
import com.javinator9889.handwashingreminder.utils.Ads
import com.javinator9889.handwashingreminder.utils.isConnected
import timber.log.Timber
import java.lang.ref.WeakReference

class SettingsView : PreferenceFragmentCompat(),
Preference.OnPreferenceChangeListener, OnPurchaseFinishedListener,
Preference.OnPreferenceChangeListener,
LayoutVisibilityChange {
lateinit var firebaseAnalyticsPreference:
WeakReference<Preference>
lateinit var firebasePerformancePreference:
WeakReference<Preference>
lateinit var adsPreference: WeakReference<Preference>
lateinit var donationsPreference: WeakReference<ListPreference>
private lateinit var emojiCompat: EmojiCompat
lateinit var billingService: BillingService
private val loader = SettingsLoader(view = this, lifecycleOwner = this)
private val app = HandwashingApplication.instance

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -76,148 +61,7 @@ class SettingsView : PreferenceFragmentCompat(),
override fun onPreferenceChange(
preference: Preference?,
newValue: Any?
): Boolean {
return when {
::firebaseAnalyticsPreference.isInitialized &&
preference == firebaseAnalyticsPreference.get() -> {
val enabled = newValue as Boolean
with(FirebaseAnalytics.getInstance(requireContext())) {
setAnalyticsCollectionEnabled(enabled)
if (!enabled)
setCurrentScreen(requireActivity(), null, null)
}
true
}
::firebasePerformancePreference.isInitialized &&
preference == firebasePerformancePreference.get() -> {
val enabled = newValue as Boolean
with(FirebasePerformance.getInstance()) {
isPerformanceCollectionEnabled = enabled
}
true
}
::adsPreference.isInitialized &&
preference == adsPreference.get() -> {
val enabled = newValue as Boolean
var ret = false
val adEnabler = AdsEnabler(app)
if (enabled) {
adEnabler.enableAds()
with(SplitInstallService.getInstance(app)) {
deferredInstall(Ads.MODULE_NAME)
}
ret = true
} else {
MaterialDialog(requireContext()).show {
title(R.string.ads_explanation_title)
try {
message(
text = emojiCompat.process(
context.getText(R.string.ads_explanation_desc)
)
)
} catch (_: IllegalStateException) {
message(R.string.ads_explanation_desc)
}
positiveButton(android.R.string.cancel) {
ret = false
}
negativeButton(R.string.disable) {
ret = true
adEnabler.disableAds()
app.adLoader = null
with(SplitInstallService.getInstance(app)) {
deferredUninstall(Ads.MODULE_NAME)
}
(preference as SwitchPreference).isChecked =
false
}
cancelOnTouchOutside(false)
cancelable(false)
}
}
ret
}
::donationsPreference.isInitialized &&
preference == donationsPreference.get() -> {
Timber.d("Purchase clicked - $newValue")
val purchaseId = newValue as String
if (isConnected())
billingService.doPurchase(purchaseId, requireActivity())
else {
if (context == null)
return false
MaterialDialog(requireContext()).show {
title(R.string.no_internet_connection)
message(R.string.no_internet_connection_long)
positiveButton(android.R.string.ok)
cancelable(true)
cancelOnTouchOutside(true)
}
}
false
}
else -> true
}
}

override fun onPurchaseFinished(token: String, resultCode: Int) {
if (context == null)
return
val context = requireContext()
when (resultCode) {
BillingResponseCode.OK -> {
try {
MaterialDialog(context)
.title(R.string.donation_thanks)
.message(
text = emojiCompat
.process(context.getText(R.string.donation_desc))
)
.positiveButton(android.R.string.ok)
} catch (_: IllegalStateException) {
MaterialDialog(context)
.title(R.string.donation_thanks)
.message(R.string.donation_desc)
.positiveButton(android.R.string.ok)
}
}
BillingResponseCode.USER_CANCELED -> {
try {
MaterialDialog(context)
.title(R.string.donation_cancelled)
.message(
text = emojiCompat.process(
context.getText(R.string.donation_cancelled_desc)
)
)
.positiveButton(android.R.string.ok)
} catch (_: IllegalStateException) {
MaterialDialog(context)
.title(R.string.donation_cancelled)
.message(R.string.donation_cancelled_desc)
.positiveButton(android.R.string.ok)
}
}
else -> {
try {
MaterialDialog(context)
.title(R.string.donation_error)
.message(
text = emojiCompat.process(
context.getText(R.string.donation_error_desc)
)
)
.positiveButton(android.R.string.ok)
} catch (_: IllegalStateException) {
MaterialDialog(context)
.title(R.string.donation_error)
.message(R.string.donation_error_desc)
.positiveButton(android.R.string.ok)
}
}
}.show()
}
): Boolean = loader.onPreferenceChange(preference, newValue)

override fun onVisibilityChanged(visibility: Int) {
if (visibility == View.VISIBLE)
Expand Down
Expand Up @@ -29,28 +29,36 @@ import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.SwitchPreference
import com.afollestad.materialdialogs.MaterialDialog
import com.android.billingclient.api.BillingClient
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.perf.FirebasePerformance
import com.javinator9889.handwashingreminder.R
import com.javinator9889.handwashingreminder.activities.PrivacyTermsActivity
import com.javinator9889.handwashingreminder.activities.views.fragments.settings.SettingsView
import com.javinator9889.handwashingreminder.activities.views.fragments.settings.TimePickerPreference
import com.javinator9889.handwashingreminder.application.HandwashingApplication
import com.javinator9889.handwashingreminder.emoji.EmojiLoader
import com.javinator9889.handwashingreminder.gms.ads.AdsEnabler
import com.javinator9889.handwashingreminder.gms.splitservice.SplitInstallService
import com.javinator9889.handwashingreminder.jobs.alarms.Alarms
import com.javinator9889.handwashingreminder.listeners.OnPurchaseFinishedListener
import com.javinator9889.handwashingreminder.utils.*
import com.mikepenz.aboutlibraries.LibsBuilder
import com.mikepenz.iconics.IconicsDrawable
import com.mikepenz.iconics.typeface.IIcon
import com.mikepenz.iconics.typeface.library.ionicons.Ionicons
import com.mikepenz.iconics.utils.sizeDp
import kotlinx.coroutines.*
import timber.log.Timber
import java.lang.ref.WeakReference
import java.util.concurrent.atomic.AtomicBoolean

class SettingsLoader(
private val view: SettingsView,
private val lifecycleOwner: LifecycleOwner
) {
) : OnPurchaseFinishedListener, Preference.OnPreferenceChangeListener {
private lateinit var emojiLoader: Deferred<EmojiCompat>
private lateinit var emojiCompat: EmojiCompat
private var arePreferencesInitialized = AtomicBoolean(false)
Expand Down Expand Up @@ -170,7 +178,7 @@ class SettingsLoader(
requireContext().resources.getTextArray(R.array.in_app_donations_debug)
else
requireContext().resources.getTextArray(R.array.in_app_donations)
billingService.addOnPurchaseFinishedListener(this@with)
billingService.addOnPurchaseFinishedListener(this@SettingsLoader)
donationsPreference = WeakReference(it)
}
).also { deferreds.add(it) }
Expand Down Expand Up @@ -291,6 +299,66 @@ class SettingsLoader(
}
}

override fun onPurchaseFinished(token: String, resultCode: Int) {
if (view.context == null)
return
val context = view.requireContext()
if (!::emojiCompat.isInitialized)
runBlocking { emojiCompat = emojiLoader.await() }
when (resultCode) {
BillingClient.BillingResponseCode.OK -> {
try {
MaterialDialog(context)
.title(R.string.donation_thanks)
.message(
text = emojiCompat
.process(context.getText(R.string.donation_desc))
)
.positiveButton(android.R.string.ok)
} catch (_: IllegalStateException) {
MaterialDialog(context)
.title(R.string.donation_thanks)
.message(R.string.donation_desc)
.positiveButton(android.R.string.ok)
}
}
BillingClient.BillingResponseCode.USER_CANCELED -> {
try {
MaterialDialog(context)
.title(R.string.donation_cancelled)
.message(
text = emojiCompat.process(
context.getText(R.string.donation_cancelled_desc)
)
)
.positiveButton(android.R.string.ok)
} catch (_: IllegalStateException) {
MaterialDialog(context)
.title(R.string.donation_cancelled)
.message(R.string.donation_cancelled_desc)
.positiveButton(android.R.string.ok)
}
}
else -> {
try {
MaterialDialog(context)
.title(R.string.donation_error)
.message(
text = emojiCompat.process(
context.getText(R.string.donation_error_desc)
)
)
.positiveButton(android.R.string.ok)
} catch (_: IllegalStateException) {
MaterialDialog(context)
.title(R.string.donation_error)
.message(R.string.donation_error_desc)
.positiveButton(android.R.string.ok)
}
}
}.show()
}

private fun setupPreferenceAsync(
name: String,
icon: IIcon? = null,
Expand Down Expand Up @@ -381,4 +449,88 @@ class SettingsLoader(
}
}
}

override fun onPreferenceChange(
preference: Preference?,
newValue: Any?
): Boolean =
when (preference) {
view.firebaseAnalyticsPreference.get() -> {
val enabled = newValue as Boolean
with(FirebaseAnalytics.getInstance(view.requireContext())) {
setAnalyticsCollectionEnabled(enabled)
if (!enabled)
setCurrentScreen(view.requireActivity(), null, null)
}
true
}
view.firebasePerformancePreference.get() -> {
val enabled = newValue as Boolean
with(FirebasePerformance.getInstance()) {
isPerformanceCollectionEnabled = enabled
}
true
}
view.adsPreference.get() -> {
val enabled = newValue as Boolean
var ret = false
val adEnabler = AdsEnabler(HandwashingApplication.instance)
if (enabled) {
adEnabler.enableAds()
with(SplitInstallService.getInstance(view.requireContext())) {
deferredInstall(Ads.MODULE_NAME)
}
ret = true
} else {
MaterialDialog(view.requireContext()).show {
title(R.string.ads_explanation_title)
try {
message(
text = emojiCompat.process(
context.getText(R.string.ads_explanation_desc)
)
)
} catch (_: IllegalStateException) {
message(R.string.ads_explanation_desc)
}
positiveButton(android.R.string.cancel) {
ret = false
}
negativeButton(R.string.disable) {
ret = true
adEnabler.disableAds()
HandwashingApplication.instance.adLoader = null
with(SplitInstallService.getInstance(view.context)) {
deferredUninstall(Ads.MODULE_NAME)
}
(preference as SwitchPreference).isChecked =
false
}
cancelOnTouchOutside(false)
cancelable(false)
}
}
ret
}
view.donationsPreference.get() -> {
Timber.d("Purchase clicked - $newValue")
val purchaseId = newValue as String
if (isConnected())
view.billingService.doPurchase(
purchaseId,
view.requireActivity()
)
else {
MaterialDialog(view.requireContext()).show {
title(R.string.no_internet_connection)
message(R.string.no_internet_connection_long)
positiveButton(android.R.string.ok)
cancelable(true)
cancelOnTouchOutside(true)
}
}
false
}
else -> true
}
}

0 comments on commit 128f997

Please sign in to comment.