Skip to content

Commit

Permalink
#27 Cleaning the code
Browse files Browse the repository at this point in the history
Former-commit-id: 1ee04e1
  • Loading branch information
gumaciel committed Jan 24, 2021
1 parent 35441d6 commit 847d2d3
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 77 deletions.
Binary file not shown.
9 changes: 8 additions & 1 deletion ios/admob_plugin/Banner.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
//
// Banner.h
// Banner
//
// Created by Gustavo Maciel on 24/01/21.
//


#include "core/object.h"

#import <GoogleMobileAds/GADBannerView.h>
Expand All @@ -15,7 +23,6 @@
NSString *adUnitId;
ViewController *rootController;
}
@property (nonatomic, strong) Banner * banner;

- (instancetype)init: (int) instance_id;
- (void)load_banner: (NSString*) ad_unit_id : (int) position : (NSString*) size;
Expand Down
14 changes: 11 additions & 3 deletions ios/admob_plugin/Banner.mm
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
//
// Banner.mm
// Banner
//
// Created by Gustavo Maciel on 24/01/21.
//


#import "Banner.h"

@implementation Banner
Expand Down Expand Up @@ -31,7 +39,7 @@ - (void) load_banner:(NSString*)ad_unit_id :(int)position :(NSString*)size {


UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

if (bannerView == nil) {
if ([size isEqualToString:@"BANNER"]) {
bannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
Expand Down Expand Up @@ -91,7 +99,7 @@ - (void)addBannerViewToView {
[NSLayoutConstraint constraintWithItem:bannerView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:rootController.bottomLayoutGuide
toItem:rootController.view.safeAreaLayoutGuide.bottomAnchor
attribute:NSLayoutAttributeTop
multiplier:1
constant:0]];
Expand All @@ -102,7 +110,7 @@ - (void)addBannerViewToView {
[NSLayoutConstraint constraintWithItem:bannerView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:rootController.topLayoutGuide
toItem:rootController.view.safeAreaLayoutGuide.topAnchor
attribute:NSLayoutAttributeBottom
multiplier:1
constant:0]];
Expand Down
10 changes: 8 additions & 2 deletions ios/admob_plugin/Interstitial.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
//
// Interstitial.h
// Interstitial
//
// Created by Gustavo Maciel on 24/01/21.
//


#import <GoogleMobileAds/GADInterstitial.h>
#import <GoogleMobileAds/GADExtras.h>
#import "app_delegate.h"
Expand All @@ -14,8 +22,6 @@
ViewController *rootController;
}

@property (nonatomic, strong) Interstitial *interstitial;


- (instancetype)init: (int) instance_id;
- (void)load_interstitial: (NSString*)ad_unit_id;
Expand Down
7 changes: 7 additions & 0 deletions ios/admob_plugin/Interstitial.mm
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
//
// Interstitial.mm
// Interstitial
//
// Created by Gustavo Maciel on 24/01/21.
//

#import "Interstitial.h"

@implementation Interstitial
Expand Down
9 changes: 7 additions & 2 deletions ios/admob_plugin/Rewarded.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
//
// Rewarded.h
// Rewarded
//
// Created by Gustavo Maciel on 24/01/21.
//

#import <GoogleMobileAds/GADRewardedAd.h>
#import <GoogleMobileAds/GADExtras.h>
#import "app_delegate.h"
Expand All @@ -14,8 +21,6 @@
NSString *adUnitId;
ViewController *rootController;
}
@property (nonatomic, strong) Rewarded * rewarded;


- (instancetype)init: (int) instance_id;
- (void)load_rewarded: (NSString*) ad_unit_id;
Expand Down
11 changes: 9 additions & 2 deletions ios/admob_plugin/Rewarded.mm
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
//
// Rewarded.mm
// Rewarded
//
// Created by Gustavo Maciel on 24/01/21.
//

#import "Rewarded.h"

@implementation Rewarded
Expand Down Expand Up @@ -32,11 +39,11 @@ - (void) load_rewarded:(NSString*) ad_unit_id {
[rewarded loadRequest:request completionHandler:^(GADRequestError * _Nullable error) {
if (error) {
NSLog(@"error while creating reward");
Object *obj = ObjectDB::get_instance(instanceId);
Object *obj = ObjectDB::get_instance(self->instanceId);
obj->call_deferred("_on_AdMob_rewarded_ad_failed_to_show", (int) error.code);
} else {
NSLog(@"reward successfully loaded");
Object *obj = ObjectDB::get_instance(instanceId);
Object *obj = ObjectDB::get_instance(self->instanceId);
obj->call_deferred("_on_AdMob_rewarded_ad_loaded");
}
}];
Expand Down
2 changes: 1 addition & 1 deletion ios/admob_plugin/admob_plugin.mm
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// admob_plugin.m
// admob_plugin.mm
// admob_plugin
//
// Created by Gustavo Maciel on 16/01/21.
Expand Down
22 changes: 18 additions & 4 deletions ios/admob_plugin/admob_plugin_implementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,16 @@
static void _bind_methods();

public:
void initialize(bool is_for_child_directed_treatment, const String &max_ad_content_rating, bool is_real, bool is_test_europe_user_consent, int instance_id);
void load_banner(const String &ad_unit_id, int gravity, const String &size);
void initialize(bool is_for_child_directed_treatment,
const String &max_ad_content_rating,
bool is_real,
bool is_test_europe_user_consent,
int instance_id);

void load_banner(const String &ad_unit_id,
int gravity,
const String &size);

void destroy_banner();
void load_interstitial(const String &ad_unit_id);
void show_interstitial();
Expand All @@ -55,8 +63,14 @@
~AdMob();
private:
const char* getDeviceId();
void initializeAfterUMP(bool is_for_child_directed_treatment, bool is_real, int instance_id);
void loadConsentForm(bool is_for_child_directed_treatment, bool is_real, int instance_id);
void GADInitialize();
void initializeAfterUMP(bool is_for_child_directed_treatment,
bool is_real,
int instance_id);

void loadConsentForm(bool is_for_child_directed_treatment,
bool is_real,
int instance_id);

};

Expand Down
127 changes: 65 additions & 62 deletions ios/admob_plugin/admob_plugin_implementation.mm
Original file line number Diff line number Diff line change
Expand Up @@ -43,48 +43,61 @@
NSLog(@"deinitialize admob");
}

void AdMob::loadConsentForm(bool is_for_child_directed_treatment, bool is_real, int instance_id) {
[UMPConsentForm
loadWithCompletionHandler:^(UMPConsentForm *form, NSError *loadError) {
if (loadError) {
objectDB->call_deferred("_on_AdMob_consent_form_load_failure", (int) loadError.code, loadError.domain);
initializeAfterUMP(is_for_child_directed_treatment, is_real, instance_id);
} else {
String consentStatusMsg = "";
void AdMob::loadConsentForm(bool is_for_child_directed_treatment, bool is_real, int instance_id)
{
[UMPConsentForm
loadWithCompletionHandler:^(UMPConsentForm *form, NSError *loadError)
{
if (loadError)
{
objectDB->call_deferred("_on_AdMob_consent_form_load_failure", (int) loadError.code, loadError.domain);
initializeAfterUMP(is_for_child_directed_treatment, is_real, instance_id);
}
else
{
String consentStatusMsg = "";

if (UMPConsentInformation.sharedInstance.consentStatus == UMPConsentStatusRequired) {
if (UMPConsentInformation.sharedInstance.consentStatus == UMPConsentStatusRequired)
{
[form
presentFromViewController:(ViewController *)((AppDelegate *)[[UIApplication sharedApplication] delegate]).window.rootViewController
completionHandler:^(NSError *_Nullable dismissError) {
objectDB->call_deferred("_on_AdMob_consent_form_dismissed");
if (UMPConsentInformation.sharedInstance.consentStatus == UMPConsentStatusObtained) {
objectDB->call_deferred("_on_AdMob_consent_status_changed", "User consent obtained. Personalization not defined.");
}
initializeAfterUMP(is_for_child_directed_treatment, is_real, instance_id);
}];
consentStatusMsg = "User consent required but not yet obtained.";
}
completionHandler:^(NSError *_Nullable dismissError)
{
objectDB->call_deferred("_on_AdMob_consent_form_dismissed");
if (UMPConsentInformation.sharedInstance.consentStatus == UMPConsentStatusObtained)
{
objectDB->call_deferred("_on_AdMob_consent_status_changed", "User consent obtained. Personalization not defined.");
}
initializeAfterUMP(is_for_child_directed_treatment, is_real, instance_id);
}
];
consentStatusMsg = "User consent required but not yet obtained.";
}

if (UMPConsentInformation.sharedInstance.consentStatus == UMPConsentStatusUnknown){
consentStatusMsg = "Unknown consent status.";
initializeAfterUMP(is_for_child_directed_treatment, is_real, instance_id);
}
else if (UMPConsentInformation.sharedInstance.consentStatus == UMPConsentStatusNotRequired){
consentStatusMsg = "User consent not required. For example, the user is not in the EEA or the UK.";
initializeAfterUMP(is_for_child_directed_treatment, is_real, instance_id);
}
else if (UMPConsentInformation.sharedInstance.consentStatus == UMPConsentStatusObtained) {
consentStatusMsg = "User consent obtained. Personalization not defined.";
initializeAfterUMP(is_for_child_directed_treatment, is_real, instance_id);
if (UMPConsentInformation.sharedInstance.consentStatus == UMPConsentStatusUnknown)
{
consentStatusMsg = "Unknown consent status.";
initializeAfterUMP(is_for_child_directed_treatment, is_real, instance_id);
}
else if (UMPConsentInformation.sharedInstance.consentStatus == UMPConsentStatusNotRequired)
{
consentStatusMsg = "User consent not required. For example, the user is not in the EEA or the UK.";
initializeAfterUMP(is_for_child_directed_treatment, is_real, instance_id);
}
else if (UMPConsentInformation.sharedInstance.consentStatus == UMPConsentStatusObtained)
{
consentStatusMsg = "User consent obtained. Personalization not defined.";
initializeAfterUMP(is_for_child_directed_treatment, is_real, instance_id);
}
objectDB->call_deferred("_on_AdMob_consent_status_changed", consentStatusMsg);
}


objectDB->call_deferred("_on_AdMob_consent_status_changed", consentStatusMsg);
}
}];
}
];
}
void AdMob::initialize(bool is_for_child_directed_treatment, const String &max_ad_content_rating, bool is_real, bool is_test_europe_user_consent, int instance_id) {
if (instance != this || initialized) {
void AdMob::initialize(bool is_for_child_directed_treatment, const String &max_ad_content_rating, bool is_real, bool is_test_europe_user_consent, int instance_id)
{
if (instance != this || initialized)
{
return;
}
objectDB = ObjectDB::get_instance(instance_id);
Expand All @@ -96,7 +109,7 @@
{
NSLog(@"Testing the UMP");
NSLog(@"UUID: %@", [[[UIDevice currentDevice] identifierForVendor] UUIDString] );
NSLog(@"device ID: %@", [NSString stringWithCString: getDeviceId()] );
NSLog(@"device ID: %@", [NSString stringWithCString: getDeviceId() encoding: NSUTF8StringEncoding] );
[UMPConsentInformation.sharedInstance reset];
UMPDebugSettings *debugSettings = [[UMPDebugSettings alloc] init];
debugSettings.testDeviceIdentifiers = @[ [[[UIDevice currentDevice] identifierForVendor] UUIDString] ];
Expand Down Expand Up @@ -139,8 +152,8 @@
GADMobileAds.sharedInstance.requestConfiguration.testDeviceIdentifiers = @[ kGADSimulatorID ];
NSLog(@"on Testing Simulator: %@", kGADSimulatorID);
#else
GADMobileAds.sharedInstance.requestConfiguration.testDeviceIdentifiers = @ [ [NSString stringWithCString: getDeviceId()] ];
NSLog(@"on Testing Real Device: testDeviceIdentifiers: %@", [NSString stringWithCString: getDeviceId()]);
GADMobileAds.sharedInstance.requestConfiguration.testDeviceIdentifiers = @ [ [NSString stringWithCString: getDeviceId() encoding: NSUTF8StringEncoding] ];
NSLog(@"on Testing Real Device: testDeviceIdentifiers: %@", [NSString stringWithCString: getDeviceId() encoding: NSUTF8StringEncoding]);
#endif
}

Expand All @@ -165,32 +178,11 @@

if (@available(iOS 14, *)) {
[ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
[[GADMobileAds sharedInstance] startWithCompletionHandler:^(GADInitializationStatus *_Nonnull status)
{
NSLog(@"%s", "BEFORE adapterStatusesByClassName");
NSDictionary<NSString *, GADAdapterStatus *>* states = [status adapterStatusesByClassName];
NSLog(@"%s", "AFTER adapterStatusesByClassName");
NSLog(@"%s", "BEFORE states");
GADAdapterStatus * adapterStatus = states[@"GADMobileAds"];
NSLog(@"%s", "AFTER states");
NSLog(@"%s : %ld", "GADMobileAds", adapterStatus.state);

objectDB->call_deferred("_on_AdMob_initialization_complete", (int) adapterStatus.state, "GADMobileAds");
}];
GADInitialize();
}];
}
else{
[[GADMobileAds sharedInstance] startWithCompletionHandler:^(GADInitializationStatus *_Nonnull status) {
NSLog(@"%s", "BEFORE adapterStatusesByClassName");
NSDictionary<NSString *, GADAdapterStatus *>* states = [status adapterStatusesByClassName];
NSLog(@"%s", "AFTER adapterStatusesByClassName");
NSLog(@"%s", "BEFORE states");
GADAdapterStatus * adapterStatus = states[@"GADMobileAds"];
NSLog(@"%s", "AFTER states");
NSLog(@"%s : %ld", "GADMobileAds", adapterStatus.state);

objectDB->call_deferred("_on_AdMob_initialization_complete", (int) adapterStatus.state, "GADMobileAds");
}];
GADInitialize();
}

initialized = true;
Expand All @@ -200,6 +192,17 @@
}
}

void AdMob::GADInitialize(){
[[GADMobileAds sharedInstance] startWithCompletionHandler:^(GADInitializationStatus *_Nonnull status)
{
NSDictionary<NSString *, GADAdapterStatus *>* states = [status adapterStatusesByClassName];
GADAdapterStatus * adapterStatus = states[@"GADMobileAds"];
NSLog(@"%s : %ld", "GADMobileAds", adapterStatus.state);

objectDB->call_deferred("_on_AdMob_initialization_complete", (int) adapterStatus.state, "GADMobileAds");
}];
}

void AdMob::load_banner(const String &ad_unit_id, int position, const String &size) {
if (!initialized) {
NSLog(@"AdMob Module not initialized");
Expand Down

0 comments on commit 847d2d3

Please sign in to comment.