-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for more Android devices + better structure at the app …
…startup thanks to app core manager
- Loading branch information
1 parent
39d71a4
commit ecf725b
Showing
16 changed files
with
720 additions
and
545 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
app/src/main/kotlin/com/d4rk/cleaner/data/core/ads/AdsCoreManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package com.d4rk.cleaner.data.core.ads | ||
|
||
import android.app.Activity | ||
import android.content.Context | ||
import com.d4rk.cleaner.constants.ads.AdsConstants | ||
import com.d4rk.cleaner.data.datastore.DataStore | ||
import com.d4rk.cleaner.utils.interfaces.OnShowAdCompleteListener | ||
import com.google.android.gms.ads.AdError | ||
import com.google.android.gms.ads.AdRequest | ||
import com.google.android.gms.ads.FullScreenContentCallback | ||
import com.google.android.gms.ads.LoadAdError | ||
import com.google.android.gms.ads.MobileAds | ||
import com.google.android.gms.ads.appopen.AppOpenAd | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.runBlocking | ||
import java.util.Date | ||
|
||
open class AdsCoreManager(protected val context: Context) { | ||
|
||
private lateinit var dataStore: DataStore | ||
private var appOpenAdManager: AppOpenAdManager? = null | ||
val isShowingAd: Boolean | ||
get() = appOpenAdManager?.isShowingAd == true | ||
|
||
fun initializeAds() { | ||
MobileAds.initialize(context) | ||
appOpenAdManager = AppOpenAdManager() | ||
} | ||
|
||
fun showAdIfAvailable(activity: Activity) { | ||
appOpenAdManager?.showAdIfAvailable(activity) | ||
} | ||
|
||
fun setDataStore(dataStore: DataStore) { | ||
this.dataStore = dataStore | ||
} | ||
|
||
private inner class AppOpenAdManager { | ||
private var appOpenAd: AppOpenAd? = null | ||
private var isLoadingAd = false | ||
var isShowingAd = false | ||
private var loadTime: Long = 0 | ||
|
||
fun loadAd(context: Context) { | ||
if (isLoadingAd || isAdAvailable()) { | ||
return | ||
} | ||
isLoadingAd = true | ||
val request: AdRequest = AdRequest.Builder().build() | ||
@Suppress("DEPRECATION") | ||
AppOpenAd.load(context, | ||
AdsConstants.APP_OPEN_UNIT_ID, | ||
request, | ||
AppOpenAd.APP_OPEN_AD_ORIENTATION_PORTRAIT, | ||
object : AppOpenAd.AppOpenAdLoadCallback() { | ||
override fun onAdLoaded(ad: AppOpenAd) { | ||
appOpenAd = ad | ||
isLoadingAd = false | ||
loadTime = Date().time | ||
} | ||
|
||
override fun onAdFailedToLoad(loadAdError: LoadAdError) { | ||
isLoadingAd = false | ||
} | ||
}) | ||
} | ||
|
||
private fun wasLoadTimeLessThanNHoursAgo(): Boolean { | ||
val dateDifference: Long = Date().time - loadTime | ||
val numMilliSecondsPerHour: Long = 3600000 | ||
return dateDifference < numMilliSecondsPerHour * 4 | ||
} | ||
|
||
private fun isAdAvailable(): Boolean { | ||
return appOpenAd != null && wasLoadTimeLessThanNHoursAgo() | ||
} | ||
|
||
fun showAdIfAvailable(activity: Activity) { | ||
showAdIfAvailable(activity, object : OnShowAdCompleteListener { | ||
override fun onShowAdComplete() { | ||
} | ||
}) | ||
} | ||
|
||
fun showAdIfAvailable( | ||
activity: Activity, | ||
onShowAdCompleteListener: OnShowAdCompleteListener | ||
) { | ||
val isAdsChecked: Boolean = | ||
runBlocking { dataStore.ads.first() } | ||
if (isShowingAd || !isAdsChecked) { | ||
return | ||
} | ||
if (!isAdAvailable()) { | ||
onShowAdCompleteListener.onShowAdComplete() | ||
loadAd(activity) | ||
return | ||
} | ||
appOpenAd?.fullScreenContentCallback = object : FullScreenContentCallback() { | ||
override fun onAdDismissedFullScreenContent() { | ||
appOpenAd = null | ||
isShowingAd = false | ||
onShowAdCompleteListener.onShowAdComplete() | ||
loadAd(activity) | ||
} | ||
|
||
override fun onAdFailedToShowFullScreenContent(adError: AdError) { | ||
appOpenAd = null | ||
isShowingAd = false | ||
onShowAdCompleteListener.onShowAdComplete() | ||
loadAd(activity) | ||
} | ||
|
||
override fun onAdShowedFullScreenContent() { | ||
} | ||
} | ||
isShowingAd = true | ||
appOpenAd?.show(activity) | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
app/src/main/kotlin/com/d4rk/cleaner/data/core/datastore/DataStoreCoreManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.d4rk.cleaner.data.core.datastore | ||
|
||
import android.content.Context | ||
import com.d4rk.cleaner.constants.ui.bottombar.BottomBarRoutes | ||
import com.d4rk.cleaner.data.datastore.DataStore | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.awaitAll | ||
import kotlinx.coroutines.coroutineScope | ||
import kotlinx.coroutines.flow.firstOrNull | ||
|
||
open class DataStoreCoreManager(protected val context: Context) { | ||
|
||
private var isDataStoreLoaded = false | ||
lateinit var dataStore: DataStore | ||
|
||
suspend fun initializeDataStore(): Boolean = coroutineScope { | ||
dataStore = DataStore.getInstance(context.applicationContext) | ||
|
||
listOf( | ||
async { dataStore.getStartupPage().firstOrNull() ?: BottomBarRoutes.HOME }, | ||
async { dataStore.getShowBottomBarLabels().firstOrNull() ?: true }, | ||
async { dataStore.getLanguage().firstOrNull() ?: "en" }, | ||
).awaitAll() | ||
|
||
isDataStoreLoaded = true | ||
isDataStoreLoaded | ||
} | ||
} |
Oops, something went wrong.