Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Using multiple deferreds and coroutines for loading data initially
  • Loading branch information
Javinator9889 committed Jun 28, 2020
1 parent 6488933 commit 66c93f7
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 72 deletions.
Expand Up @@ -97,10 +97,12 @@ class DynamicFeatureProgress : SplitCompatBaseActivity(),

private fun setResultWithIntent(resultCode: Int) {
val intent = if (launchOnInstall)
Intent().setClassName(
BuildConfig.APPLICATION_ID,
launchActivityName
)
Intent().apply {
setClassName(
BuildConfig.APPLICATION_ID,
launchActivityName
)
}
else
null
setResult(resultCode, intent)
Expand All @@ -117,7 +119,8 @@ class DynamicFeatureProgress : SplitCompatBaseActivity(),
SplitInstallSessionStatus.FAILED -> {
Toast.makeText(
this, getString(
R.string.dynamic_module_loading_error, state.errorCode),
R.string.dynamic_module_loading_error, state.errorCode
),
Toast.LENGTH_LONG
).show()
Timber.e(
Expand Down
Expand Up @@ -37,6 +37,7 @@ import com.google.firebase.ktx.Firebase
import com.google.firebase.perf.FirebasePerformance
import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings
import com.google.firebase.remoteconfig.ktx.remoteConfig
import com.javinator9889.handwashingreminder.BuildConfig
import com.javinator9889.handwashingreminder.R
import com.javinator9889.handwashingreminder.application.HandwashingApplication
import com.javinator9889.handwashingreminder.data.UserProperties
Expand Down Expand Up @@ -67,6 +68,7 @@ class LauncherActivity : AppCompatActivity() {
private val sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(app)
private val dynamicFeatureDeferred = CompletableDeferred<Boolean>()
private val activityIntentDeferred = CompletableDeferred<Intent>()
private val splitInstallManager = SplitInstallManagerFactory.create(app)

init {
Expand All @@ -78,18 +80,17 @@ class LauncherActivity : AppCompatActivity() {
}
whenStarted {
progressBar.show()
deferreds.add(showWelcomeScreenAsync())
val welcomeScreenDeferred = showWelcomeScreenAsync()
deferreds.add(installRequiredModulesAsync())
deferreds.awaitAll()
if (!launchOnInstall) {
with(Intent(this@LauncherActivity, MainActivity::class.java)) {
if (launchFromNotification)
flags = Intent.FLAG_ACTIVITY_NEW_TASK or
Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(this)
overridePendingTransition(0, android.R.anim.fade_out)
finish()
}
activityIntentDeferred.await().run {
Timber.d("Activity Init is now completed!")
if (launchFromNotification)
flags = Intent.FLAG_ACTIVITY_NEW_TASK or
Intent.FLAG_ACTIVITY_CLEAR_TASK
welcomeScreenDeferred.join()
startActivity(this)
overridePendingTransition(0, android.R.anim.fade_out)
finish()
}
}
}
Expand All @@ -100,7 +101,7 @@ class LauncherActivity : AppCompatActivity() {
setContentView(R.layout.splash_screen)
}

private fun showWelcomeScreenAsync() = lifecycleScope.async {
private fun showWelcomeScreenAsync() = lifecycleScope.launch(Dispatchers.Main) {
app.firebaseInitDeferred.await()
val isThereAnySpecialEvent = with(Firebase.remoteConfig) {
getBoolean(SPECIAL_EVENT) && !launchFromNotification
Expand Down Expand Up @@ -145,61 +146,30 @@ class LauncherActivity : AppCompatActivity() {
Timber.i("Unknown request code $requestCode")
return
}
if (Ads.MODULE_NAME in splitInstallManager.installedModules) {
if (Ads.MODULE_NAME in splitInstallManager.installedModules &&
sharedPreferences.getBoolean(ADS_ENABLED, true)) {
when (resultCode) {
Activity.RESULT_OK -> {
initAds()
data?.let {
val launchIntent = Intent(data)
createPackageContext(packageName, 0).also {
SplitCompat.install(it)
}
if (launchFromNotification) {
launchIntent.flags =
Intent.FLAG_ACTIVITY_NEW_TASK or
Intent.FLAG_ACTIVITY_CLEAR_TASK
}
startActivity(launchIntent)
}
}
Activity.RESULT_OK -> initAds()
Activity.RESULT_CANCELED -> app.adLoader = null
}
}
dynamicFeatureDeferred.complete(true)
/*if (requestCode == DYNAMIC_FEATURE_INSTALL_RESULT_CODE) {
EmojiLoader.loadAsync(this)
if (sharedPreferences.getBoolean(ADS_ENABLED, true)) {
when (resultCode) {
Activity.RESULT_OK -> {
initAds()
data.notNull {
createPackageContext(packageName, 0).also {
SplitCompat.install(it)
}
if (launchFromNotification)
data!!.flags = Intent.FLAG_ACTIVITY_NEW_TASK or
Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(data)
finish()
}
}
Activity.RESULT_CANCELED -> app.adLoader = null
if (sharedPreferences.getBoolean(APP_INIT_KEY, false) &&
AppIntro.MODULE_NAME in splitInstallManager.installedModules) {
data?.let {
val launchIntent = Intent(data)
createPackageContext(packageName, 0).also {
SplitCompat.install(it)
}
Timber.d("Created launch intent $launchIntent")
activityIntentDeferred.complete(launchIntent)
}
if (!launchOnInstall) {
Intent(this, MainActivity::class.java).also {
if (launchFromNotification)
it.flags = Intent.FLAG_ACTIVITY_NEW_TASK or
Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(it)
overridePendingTransition(0, android.R.anim.fade_out)
}
}
if (canFinishActivity)
finish()
else
canFinishActivity = true
}*/
} else {
Timber.d("Created launch intent at MainActivity")
activityIntentDeferred.complete(
Intent(this, MainActivity::class.java)
)
}
dynamicFeatureDeferred.complete(true)
}

override fun finish() {
Expand All @@ -210,11 +180,24 @@ class LauncherActivity : AppCompatActivity() {
}

private fun installRequiredModulesAsync() = lifecycleScope.async {
val modules = loadRequiredModules()
val (modules, installedModules) = loadRequiredModules()
Timber.d("Required to install modules: $modules")
if (modules.isEmpty())
if (modules.isEmpty()) {
val intent = if (AppIntro.MODULE_NAME in installedModules &&
!sharedPreferences.getBoolean(APP_INIT_KEY, false)) {
with(Intent()) {
setClassName(
BuildConfig.APPLICATION_ID,
"${AppIntro.PACKAGE_NAME}.${AppIntro.MAIN_ACTIVITY_NAME}"
)
this
}
} else Intent(this@LauncherActivity, MainActivity::class.java)
Timber.d("Created launch intent $intent")
activityIntentDeferred.complete(intent)
return@async
val intent = if (launchOnInstall) {
}
val intent = if (AppIntro.MODULE_NAME in modules) {
createDynamicFeatureActivityIntent(
modules.toTypedArray(),
launchOnInstall,
Expand All @@ -228,7 +211,7 @@ class LauncherActivity : AppCompatActivity() {
dynamicFeatureDeferred.await()
}

private fun loadRequiredModules(): Set<String> {
private fun loadRequiredModules(): Pair<Set<String>, Set<String>> {
val modules = mutableSetOf<String>()
val googleApi = GoogleApiAvailability.getInstance()
if (sharedPreferences.getBoolean(ADS_ENABLED, true))
Expand All @@ -243,7 +226,8 @@ class LauncherActivity : AppCompatActivity() {
) != ConnectionResult.SUCCESS
)
modules += BundledEmoji.MODULE_NAME
return modules - splitInstallManager.installedModules
return (modules - splitInstallManager.installedModules) to
splitInstallManager.installedModules
}

private fun initAds() {
Expand Down Expand Up @@ -298,7 +282,8 @@ class LauncherActivity : AppCompatActivity() {
}

private fun setupFirebasePropertiesAsync() = lifecycleScope.launch {
val firebaseAnalytics = FirebaseAnalytics.getInstance(this@LauncherActivity)
val firebaseAnalytics =
FirebaseAnalytics.getInstance(this@LauncherActivity)
val firebaseRemoteConfig = Firebase.remoteConfig
val firebasePerformance = FirebasePerformance.getInstance()
val config = with(FirebaseRemoteConfigSettings.Builder()) {
Expand Down

0 comments on commit 66c93f7

Please sign in to comment.