Skip to content
This repository was archived by the owner on Feb 26, 2022. It is now read-only.

Commit 545d0a4

Browse files
committed
Added support to the functions to cache ads manually
1 parent a92d2d6 commit 545d0a4

File tree

8 files changed

+112
-34
lines changed

8 files changed

+112
-34
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.3.0
2+
3+
* Added support to the functions to cache ads manually.
4+
* Renamed the function `isLoaded()` to `isReadyForShow()`.
5+
16
## 0.2.3
27

38
* Small improvements and fixed some typos in the documentation.

android/src/main/kotlin/io/vinicius/appodeal_flutter/AppodealFlutterPlugin.kt

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ class AppodealFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAware
3737
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
3838
when (call.method) {
3939
"initialize" -> initialize(call, result)
40-
"isLoaded" -> isLoaded(call, result)
40+
"setAutoCache" -> setAutoCache(call, result)
41+
"cache" -> cache(call, result)
42+
"isReadyForShow" -> isReadyForShow(call, result)
4143
"show" -> show(activity, call, result)
4244

4345
"fetchConsentInfo" -> fetchConsentInfo(call, result)
@@ -83,7 +85,24 @@ class AppodealFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAware
8385
result.success(null)
8486
}
8587

86-
private fun isLoaded(call: MethodCall, result: Result) {
88+
private fun setAutoCache(call: MethodCall, result: Result) {
89+
val args = call.arguments as Map<*, *>
90+
val adType = getAdType(args["adType"] as Int)
91+
val autoCache = args["autoCache"] as Boolean
92+
93+
Appodeal.setAutoCache(adType, autoCache)
94+
result.success(null)
95+
}
96+
97+
private fun cache(call: MethodCall, result: Result) {
98+
val args = call.arguments as Map<*, *>
99+
val adType = getAdType(args["adType"] as Int)
100+
101+
Appodeal.cache(activity, adType)
102+
result.success(null)
103+
}
104+
105+
private fun isReadyForShow(call: MethodCall, result: Result) {
87106
val args = call.arguments as Map<*, *>
88107
val adType = getAdType(args["adType"] as Int)
89108

example/lib/main.dart

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class _MyAppState extends State<MyApp> {
3232

3333
// Request authorization to track the user
3434
Appodeal.requestIOSTrackingAuthorization().then((_) async {
35+
// Set interstitial ads to be cached manually
36+
await Appodeal.setAutoCache(AdType.INTERSTITIAL, false);
37+
3538
// Initialize Appodeal after the authorization was granted or not
3639
await Appodeal.initialize(
3740
hasConsent: true,
@@ -96,14 +99,21 @@ class _Body extends StatelessWidget {
9699
),
97100

98101
RaisedButton(
99-
child: Text('Check: Is Interstitial Ad loaded?'),
102+
child: Text('Is Interstitial Ad ready for show?'),
100103
onPressed: () async {
101-
var isLoaded = await Appodeal.isLoaded(AdType.INTERSTITIAL);
102-
Toast.show(isLoaded ? 'Interstitial ad is loaded' : 'Interstitial ad is NOT loaded', context,
104+
var isReady = await Appodeal.isReadyForShow(AdType.INTERSTITIAL);
105+
Toast.show(isReady ? 'Interstitial ad is ready' : 'Interstitial ad is NOT ready', context,
103106
duration: Toast.LENGTH_LONG, gravity: Toast.BOTTOM);
104107
},
105108
),
106109

110+
RaisedButton(
111+
child: Text('Cache Interstitial Ad'),
112+
onPressed: () async {
113+
await Appodeal.cache(AdType.INTERSTITIAL);
114+
},
115+
),
116+
107117
RaisedButton(
108118
child: Text('Show Interstitial Ad'),
109119
onPressed: () async {
@@ -112,10 +122,10 @@ class _Body extends StatelessWidget {
112122
),
113123

114124
RaisedButton(
115-
child: Text('Check: Is Reward Ad loaded?'),
125+
child: Text('Is Reward Ad ready for show?'),
116126
onPressed: () async {
117-
var isLoaded = await Appodeal.isLoaded(AdType.REWARD);
118-
Toast.show(isLoaded ? 'Reward ad is loaded' : 'Reward ad is NOT loaded', context,
127+
var isReady = await Appodeal.isReadyForShow(AdType.REWARD);
128+
Toast.show(isReady ? 'Reward ad is ready' : 'Reward ad is NOT ready', context,
119129
duration: Toast.LENGTH_LONG, gravity: Toast.BOTTOM);
120130
},
121131
),
@@ -129,10 +139,10 @@ class _Body extends StatelessWidget {
129139
),
130140

131141
RaisedButton(
132-
child: Text('Check: Is Non-Skippable Ad loaded?'),
142+
child: Text('Is Non-Skippable Ad ready?'),
133143
onPressed: () async {
134-
var isLoaded = await Appodeal.isLoaded(AdType.NON_SKIPPABLE);
135-
Toast.show(isLoaded ? 'Non-Skippable ad is loaded' : 'No-Skippable ad is NOT loaded', context,
144+
var isReady = await Appodeal.isReadyForShow(AdType.NON_SKIPPABLE);
145+
Toast.show(isReady ? 'Non-Skippable ad is ready' : 'No-Skippable ad is NOT ready', context,
136146
duration: Toast.LENGTH_LONG, gravity: Toast.BOTTOM);
137147
},
138148
),

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ packages:
77
path: ".."
88
relative: true
99
source: path
10-
version: "0.2.3"
10+
version: "0.3.0"
1111
async:
1212
dependency: transitive
1313
description:

ios/Classes/SwiftAppodealFlutterPlugin.swift

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ public class SwiftAppodealFlutterPlugin: NSObject, FlutterPlugin
1919
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
2020
switch call.method {
2121
case "initialize": initialize(call, result)
22-
case "isLoaded": isLoaded(call, result)
22+
case "setAutoCache": setAutoCache(call, result)
23+
case "cache": cache(call, result)
24+
case "isReadyForShow": isReadyForShow(call, result)
2325
case "show": show(call, result)
2426

2527
case "requestIOSTrackingAuthorization": requestIOSTrackingAuthorization(result)
@@ -31,7 +33,7 @@ public class SwiftAppodealFlutterPlugin: NSObject, FlutterPlugin
3133
default: result(FlutterMethodNotImplemented)
3234
}
3335
}
34-
36+
3537
private func requestIOSTrackingAuthorization(_ result: @escaping FlutterResult) {
3638
if #available(iOS 14, *) {
3739
ATTrackingManager.requestTrackingAuthorization { status in
@@ -41,7 +43,7 @@ public class SwiftAppodealFlutterPlugin: NSObject, FlutterPlugin
4143
result(true)
4244
}
4345
}
44-
46+
4547
// MARK: - Appodeal
4648
private func initialize(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
4749
let args = call.arguments as! Dictionary<String, Any>
@@ -59,29 +61,48 @@ public class SwiftAppodealFlutterPlugin: NSObject, FlutterPlugin
5961

6062
result(nil)
6163
}
62-
63-
private func isLoaded(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
64+
65+
private func setAutoCache(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
66+
let args = call.arguments as! Dictionary<String, Any>
67+
let adType = getAdType(adId: args["adType"] as! Int)
68+
let autoCache = args["autoCache"] as! Bool
69+
70+
Appodeal.setAutocache(autoCache, types: adType)
71+
72+
result(nil)
73+
}
74+
75+
private func cache(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
76+
let args = call.arguments as! Dictionary<String, Any>
77+
let adType = getAdType(adId: args["adType"] as! Int)
78+
79+
Appodeal.cacheAd(adType)
80+
81+
result(nil)
82+
}
83+
84+
private func isReadyForShow(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
6485
let args = call.arguments as! Dictionary<String, Any>
6586
let adType = getShowStyle(adType: getAdType(adId: args["adType"] as! Int))
6687

6788
result(Appodeal.isReadyForShow(with: adType))
6889
}
69-
90+
7091
private func show(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
7192
let args = call.arguments as! Dictionary<String, Any>
7293
let adType = getShowStyle(adType: getAdType(adId: args["adType"] as! Int))
7394

7495
let rootViewController = UIApplication.shared.keyWindow?.rootViewController
7596
result(Appodeal.showAd(adType, rootViewController: rootViewController))
7697
}
77-
98+
7899
private func setCallbacks() {
79100
Appodeal.setBannerDelegate(self)
80101
Appodeal.setInterstitialDelegate(self)
81102
Appodeal.setRewardedVideoDelegate(self)
82103
Appodeal.setNonSkippableVideoDelegate(self)
83104
}
84-
105+
85106
// MARK: - Consent Manager
86107
private func fetchConsentInfo(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
87108
let args = call.arguments as! Dictionary<String, Any>

lib/src/appodeal.dart

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,36 @@ class Appodeal {
5757
});
5858
}
5959

60-
/// Check if an ad of certain type [adType] is loaded and ready to be presented.
60+
/// Enable or disable the auto caching of an ad of certain type [adType].
61+
///
62+
/// When you initialize Appodeal all ads types are set to be downloaded automatically. If you want to prevent this
63+
/// behaviour you can call this function before the initialization passing `false` to the parameter [autoCache].
64+
///
65+
/// Use the constants in the class `AdType` to specify what ad should be loaded.
66+
static Future<void> setAutoCache(int adType, bool autoCache) async {
67+
return _channel.invokeMethod('setAutoCache', {
68+
'adType': adType,
69+
'autoCache': autoCache
70+
});
71+
}
72+
73+
/// Cache an ad of certain type [adType].
74+
///
75+
/// If you disabled the auto caching (function `setAutoCache()`) then you must call this function before you can
76+
/// display the ad type where auto cache has been disabled.
77+
static Future<void> cache(int adType) async {
78+
return _channel.invokeMethod('cache', {
79+
'adType': adType
80+
});
81+
}
82+
83+
/// Check if an ad of certain type [adType] is ready to be presented.
6184
///
6285
/// Use the constants in the class `AdType` to specify what ad should be loaded.
6386
///
6487
/// Returns `true` if the ad is loaded.
65-
static Future<bool> isLoaded(int adType) async {
66-
return _channel.invokeMethod('isLoaded', {
88+
static Future<bool> isReadyForShow(int adType) async {
89+
return _channel.invokeMethod('isReadyForShow', {
6790
'adType': adType
6891
});
6992
}

lib/src/banner.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ class AppodealBanner extends StatelessWidget {
1010
width: 320,
1111
height: 50,
1212
child: Platform.isIOS
13-
? UiKitView(
14-
viewType: 'plugins.io.vinicius.appodeal/banner',
15-
creationParams: {},
16-
creationParamsCodec: const StandardMessageCodec(),
17-
)
18-
: AndroidView(
19-
viewType: 'plugins.io.vinicius.appodeal/banner',
20-
creationParams: {},
21-
creationParamsCodec: const StandardMessageCodec(),
22-
),
13+
? UiKitView(
14+
viewType: 'plugins.io.vinicius.appodeal/banner',
15+
creationParams: {},
16+
creationParamsCodec: const StandardMessageCodec(),
17+
)
18+
: AndroidView(
19+
viewType: 'plugins.io.vinicius.appodeal/banner',
20+
creationParams: {},
21+
creationParamsCodec: const StandardMessageCodec(),
22+
),
2323
);
2424
}
2525
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: appodeal_flutter
22
description: A Flutter plugin to display ads from Appodeal; it supports the new reqs for iOS 14+ and GDPR/CCPA consent.
3-
version: 0.2.3
3+
version: 0.3.0
44
homepage: https://github.com/vegidio-flutter/appodeal
55

66
environment:

0 commit comments

Comments
 (0)