Skip to content

Commit

Permalink
refactor: Improved App Core Manager for reliable app startup
Browse files Browse the repository at this point in the history
Enhanced the App Core Manager for more robust and predictable application startup behavior.
  • Loading branch information
Mihai-Cristian Condrea committed Dec 24, 2024
1 parent 9ebf4c6 commit a33932a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ android {
applicationId = "com.d4rk.androidtutorials"
minSdk = 23
targetSdk = 35
versionCode = 94
versionCode = 95
versionName = "1.1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
resourceConfigurations += listOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class AppCoreManager : MultiDexApplication() , Application.ActivityLifecycleCall
private val adsCoreManager : AdsCoreManager = AdsCoreManager(context = this)

private enum class AppInitializationStage {
DATA_STORE , ADS
DATA_STORE , ADS , FINALIZATION
}

private var currentStage = AppInitializationStage.DATA_STORE
Expand Down Expand Up @@ -64,17 +64,23 @@ class AppCoreManager : MultiDexApplication() , Application.ActivityLifecycleCall
}

private fun proceedToNextStage() {
when (currentStage) {
currentStage = when (currentStage) {
AppInitializationStage.DATA_STORE -> {
currentStage = AppInitializationStage.ADS
adsCoreManager.setDataStore(dataStoreCoreManager.dataStore)
AppInitializationStage.ADS
}

AppInitializationStage.ADS -> {
adsCoreManager.initializeAds()
AppInitializationStage.FINALIZATION
}

else -> {
AppInitializationStage.FINALIZATION -> {
markAppAsLoaded()
return
}
}
proceedToNextStage()
}

fun isAppLoaded() : Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ open class AdsCoreManager(protected val context : Context) {

private inner class AppOpenAdManager {
private var appOpenAd : AppOpenAd? = null
private var isLoadingAd = false
var isShowingAd = false
private var isLoadingAd : Boolean = false
var isShowingAd : Boolean = false
private var loadTime : Long = 0

fun loadAd(context : Context) {
Expand Down Expand Up @@ -75,7 +75,7 @@ open class AdsCoreManager(protected val context : Context) {
}

fun showAdIfAvailable(activity : Activity) {
showAdIfAvailable(activity , object : OnShowAdCompleteListener {
showAdIfAvailable(activity = activity , onShowAdCompleteListener = object : OnShowAdCompleteListener {
override fun onShowAdComplete() {
}
})
Expand All @@ -90,22 +90,22 @@ open class AdsCoreManager(protected val context : Context) {
}
if (! isAdAvailable()) {
onShowAdCompleteListener.onShowAdComplete()
loadAd(activity)
loadAd(context = activity)
return
}
appOpenAd?.fullScreenContentCallback = object : FullScreenContentCallback() {
override fun onAdDismissedFullScreenContent() {
appOpenAd = null
isShowingAd = false
onShowAdCompleteListener.onShowAdComplete()
loadAd(activity)
loadAd(context = activity)
}

override fun onAdFailedToShowFullScreenContent(adError : AdError) {
appOpenAd = null
isShowingAd = false
onShowAdCompleteListener.onShowAdComplete()
loadAd(activity)
loadAd(context = activity)
}

override fun onAdShowedFullScreenContent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import kotlinx.coroutines.flow.firstOrNull

open class DataStoreCoreManager(protected val context : Context) {

private var isDataStoreLoaded = false
private var isDataStoreLoaded : Boolean = false
lateinit var dataStore : DataStore

suspend fun initializeDataStore() : Boolean = coroutineScope {
Expand Down

0 comments on commit a33932a

Please sign in to comment.