Skip to content

Commit 57ac9dc

Browse files
.
1 parent 5f05397 commit 57ac9dc

File tree

4 files changed

+76
-49
lines changed

4 files changed

+76
-49
lines changed

app/src/main/java/com/rk/taskmanager/MainActivity.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ class MainActivity : ComponentActivity() {
6464
lifecycleScope.launch(Dispatchers.IO) {
6565
MobileAds.initialize(this@MainActivity)
6666
isinitialized = true
67-
loadAd(this@MainActivity)
67+
if (Settings.shouldPreLoadThemeAd){
68+
loadAd(this@MainActivity)
69+
}
6870
}
6971

7072

@@ -177,7 +179,6 @@ class MainActivity : ComponentActivity() {
177179
}
178180
}
179181
viewModel.refreshProcessesAuto()
180-
loadAd(this)
181182
}
182183
}
183184

app/src/main/java/com/rk/taskmanager/ads/Ads.kt

Lines changed: 64 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import com.rk.taskmanager.MainActivity
1616
import kotlinx.coroutines.Dispatchers
1717
import kotlinx.coroutines.GlobalScope
1818
import kotlinx.coroutines.launch
19+
import kotlinx.coroutines.sync.Mutex
20+
import kotlinx.coroutines.sync.withLock
1921

2022
const val TEST_UNIT_AD_ID = "ca-app-pub-3940256099942544/5224354917"
2123
const val REWARDED_AD_UNIT_ID = "ca-app-pub-4934006287453841/1573729325"
@@ -24,31 +26,42 @@ private var rewardedAd: RewardedAd? = null
2426
private const val TAG = "AD_MANAGER"
2527

2628
fun isAdAvailable(): Boolean = rewardedAd != null
29+
private val loadMutex = Mutex()
2730

2831
fun loadAd(activity: MainActivity){
32+
if (loadMutex.isLocked){
33+
return
34+
}
2935
if (rewardedAd != null){
3036
return
3137
}
3238
if (MainActivity.instance?.isinitialized == false){
3339
return
3440
}
3541
activity.lifecycleScope.launch(Dispatchers.Main.immediate) {
36-
RewardedAd.load(
37-
activity,
38-
REWARDED_AD_UNIT_ID,
39-
AdRequest.Builder().build(),
40-
object : RewardedAdLoadCallback() {
41-
override fun onAdLoaded(ad: RewardedAd) {
42-
Log.d(TAG, "Ad was loaded.")
43-
rewardedAd = ad
44-
}
42+
runCatching {
43+
loadMutex.withLock {
44+
RewardedAd.load(
45+
activity,
46+
REWARDED_AD_UNIT_ID,
47+
AdRequest.Builder().build(),
48+
object : RewardedAdLoadCallback() {
49+
override fun onAdLoaded(ad: RewardedAd) {
50+
Log.d(TAG, "Ad was loaded.")
51+
rewardedAd = ad
52+
}
53+
54+
override fun onAdFailedToLoad(adError: LoadAdError) {
55+
Log.d(TAG, adError.message)
56+
rewardedAd = null
57+
}
58+
},
59+
)
60+
}
61+
}.onFailure {
62+
it.printStackTrace()
63+
}
4564

46-
override fun onAdFailedToLoad(adError: LoadAdError) {
47-
Log.d(TAG, adError.message)
48-
rewardedAd = null
49-
}
50-
},
51-
)
5265
}
5366

5467
}
@@ -59,44 +72,49 @@ fun showAd(activity: MainActivity, callback: () -> Unit) {
5972
return
6073
}
6174
activity.lifecycleScope.launch(Dispatchers.Main.immediate) {
62-
if (rewardedAd != null) {
63-
var userEarnedReward = false
64-
65-
// Set up the full-screen callback BEFORE showing
66-
rewardedAd?.fullScreenContentCallback = object : FullScreenContentCallback() {
67-
override fun onAdDismissedFullScreenContent() {
68-
Log.d(TAG, "Ad was dismissed.")
69-
rewardedAd = null
70-
71-
// Only invoke callback if user actually earned the reward
72-
if (userEarnedReward) {
73-
callback.invoke()
75+
runCatching {
76+
if (rewardedAd != null) {
77+
var userEarnedReward = false
78+
79+
// Set up the full-screen callback BEFORE showing
80+
rewardedAd?.fullScreenContentCallback = object : FullScreenContentCallback() {
81+
override fun onAdDismissedFullScreenContent() {
82+
Log.d(TAG, "Ad was dismissed.")
83+
rewardedAd = null
84+
85+
// Only invoke callback if user actually earned the reward
86+
if (userEarnedReward) {
87+
callback.invoke()
88+
}
89+
90+
// Load next ad
91+
loadAd(activity)
7492
}
7593

76-
// Load next ad
77-
loadAd(activity)
78-
}
94+
override fun onAdFailedToShowFullScreenContent(adError: AdError) {
95+
Log.d(TAG, "Ad failed to show: ${adError.message}")
96+
rewardedAd = null
97+
loadAd(activity)
98+
}
7999

80-
override fun onAdFailedToShowFullScreenContent(adError: AdError) {
81-
Log.d(TAG, "Ad failed to show: ${adError.message}")
82-
rewardedAd = null
83-
loadAd(activity)
100+
override fun onAdShowedFullScreenContent() {
101+
Log.d(TAG, "Ad showed fullscreen content.")
102+
}
84103
}
85104

86-
override fun onAdShowedFullScreenContent() {
87-
Log.d(TAG, "Ad showed fullscreen content.")
105+
// Show the ad with reward listener
106+
rewardedAd?.show(activity) { rewardItem ->
107+
Log.d(TAG, "User earned the reward: ${rewardItem.amount} ${rewardItem.type}")
108+
userEarnedReward = true
109+
// Don't call callback here - wait for onAdDismissedFullScreenContent
88110
}
111+
} else {
112+
Log.d(TAG, "Ad not ready, loading a new one...")
113+
loadAd(activity)
89114
}
90-
91-
// Show the ad with reward listener
92-
rewardedAd?.show(activity) { rewardItem ->
93-
Log.d(TAG, "User earned the reward: ${rewardItem.amount} ${rewardItem.type}")
94-
userEarnedReward = true
95-
// Don't call callback here - wait for onAdDismissedFullScreenContent
96-
}
97-
} else {
98-
Log.d(TAG, "Ad not ready, loading a new one...")
99-
loadAd(activity)
115+
}.onFailure {
116+
it.printStackTrace()
100117
}
118+
101119
}
102120
}

app/src/main/java/com/rk/taskmanager/screens/SettingsScreen.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import com.rk.taskmanager.ads.loadAd
4343
import com.rk.taskmanager.ads.showAd
4444
import com.rk.taskmanager.getString
4545
import com.rk.taskmanager.settings.Settings
46+
import com.rk.taskmanager.settings.Settings.shouldPreLoadThemeAd
4647
import com.rk.taskmanager.strings
4748
import com.rk.taskmanager.ui.theme.currentTheme
4849
import com.rk.taskmanager.ui.theme.dynamicTheme
@@ -117,6 +118,7 @@ fun SettingsScreen(modifier: Modifier = Modifier, navController: NavController)
117118
dynamicTheme.value = true
118119
Settings.monet = true
119120
}
121+
Settings.shouldPreLoadThemeAd = false
120122

121123
}
122124
}
@@ -149,6 +151,7 @@ fun SettingsScreen(modifier: Modifier = Modifier, navController: NavController)
149151
Settings.monet = false
150152
}
151153
}
154+
Settings.shouldPreLoadThemeAd = false
152155

153156
}
154157
}

app/src/main/java/com/rk/taskmanager/settings/Settings.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@ import android.os.Build
77
import com.rk.taskmanager.TaskManager
88

99
object Settings {
10+
var shouldPreLoadThemeAd
11+
get() = Preference.getBoolean(key = "theme-ad",true)
12+
set(value) = Preference.setBoolean(key = "theme-ad",value)
13+
14+
1015
var theme
1116
//0 is the id of the theme in the themes hashmap
1217
get() = Preference.getInt(key = "theme",0)
13-
set(value) = Preference.setInt(key = "theme",value)
18+
set(value) { Preference.setInt(key = "theme",value) }
1419

1520
var updateFrequency
1621
//0 is the id of the theme in the themes hashmap

0 commit comments

Comments
 (0)