Skip to content

Commit

Permalink
Merge pull request #4 from bparrishMines/horizontal
Browse files Browse the repository at this point in the history
Add horizontal commit from flutter/plugins repo
  • Loading branch information
bparrishMines authored Aug 22, 2019
2 parents b14e593 + 353da9b commit f1e60a5
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 12 deletions.
4 changes: 4 additions & 0 deletions packages/firebase_admob/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.9.0+4

* Add the ability to horizontally adjust the ads banner location by specifying a pixel offset from the centre.

## 0.9.0+3

* Update google-services Android gradle plugin to 4.3.0 in documentation and examples.
Expand Down
19 changes: 18 additions & 1 deletion packages/firebase_admob/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,23 @@ myBanner
..show(
// Positions the banner ad 60 pixels from the bottom of the screen
anchorOffset: 60.0,
// Positions the banner ad 10 pixels from the center of the screen to the right
horizontalCenterOffset: 10.0,
// Banner Position
anchorType: AnchorType.bottom,
);
```

Ads must be loaded before they're shown.
```dart
myBanner
// typically this happens well before the ad is shown
..load()
..show(
// Positions the banner ad 60 pixels from the bottom of the screen
anchorOffset: 60.0,
// Positions the banner ad 10 pixels from the center of the screen to the left
horizontalCenterOffset: -10.0,
// Banner Position
anchorType: AnchorType.bottom,
);
Expand All @@ -133,6 +150,7 @@ myInterstitial
..show(
anchorType: AnchorType.bottom,
anchorOffset: 0.0,
horizontalCenterOffset: 0.0,
);
```

Expand Down Expand Up @@ -186,7 +204,6 @@ method.
This is just an initial version of the plugin. There are still some
limitations:

- Banner ads have limited positioning functionality. They can be positioned at the top or the bottom of the screen and at a logical pixel offset from the edge.
- Banner ads cannot be animated into view.
- It's not possible to specify a banner ad's size.
- There's no support for native ads.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ private void callShowAd(int id, MethodCall call, Result result) {
if (call.argument("anchorOffset") != null) {
ad.anchorOffset = Double.parseDouble((String) call.argument("anchorOffset"));
}
if (call.argument("horizontalCenterOffset") != null) {
ad.horizontalCenterOffset =
Double.parseDouble((String) call.argument("horizontalCenterOffset"));
}
if (call.argument("anchorType") != null) {
ad.anchorType = call.argument("anchorType").equals("bottom") ? Gravity.BOTTOM : Gravity.TOP;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ abstract class MobileAd extends AdListener {
final int id;
Status status;
double anchorOffset;
double horizontalCenterOffset;
int anchorType;

enum Status {
Expand All @@ -44,6 +45,7 @@ private MobileAd(int id, Activity activity, MethodChannel channel) {
this.channel = channel;
this.status = Status.CREATED;
this.anchorOffset = 0.0;
this.horizontalCenterOffset = 0.0;
this.anchorType = Gravity.BOTTOM;
allAds.put(id, this);
}
Expand Down Expand Up @@ -160,10 +162,13 @@ void show() {
content.addView(adView);
final float scale = activity.getResources().getDisplayMetrics().density;

int left = horizontalCenterOffset > 0 ? (int) (horizontalCenterOffset * scale) : 0;
int right =
horizontalCenterOffset < 0 ? (int) (Math.abs(horizontalCenterOffset) * scale) : 0;
if (anchorType == Gravity.BOTTOM) {
content.setPadding(0, 0, 0, (int) (anchorOffset * scale));
content.setPadding(left, 0, right, (int) (anchorOffset * scale));
} else {
content.setPadding(0, (int) (anchorOffset * scale), 0, 0);
content.setPadding(left, (int) (anchorOffset * scale), right, 0);
}

activity.addContentView(
Expand Down
8 changes: 8 additions & 0 deletions packages/firebase_admob/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ class _MyAppState extends State<MyApp> {
..load()
..show();
}),
RaisedButton(
child: const Text('SHOW BANNER WITH OFFSET'),
onPressed: () {
_bannerAd ??= createBannerAd();
_bannerAd
..load()
..show(horizontalCenterOffset: -50, anchorOffset: 100);
}),
RaisedButton(
child: const Text('REMOVE BANNER'),
onPressed: () {
Expand Down
4 changes: 3 additions & 1 deletion packages/firebase_admob/ios/Classes/FLTMobileAd.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ typedef enum : NSUInteger {
- (FLTMobileAdStatus)status;
- (void)loadWithAdUnitId:(NSString *)adUnitId targetingInfo:(NSDictionary *)targetingInfo;
- (void)show;
- (void)showAtOffset:(double)anchorOffset fromAnchor:(int)anchorType;
- (void)showAtOffset:(double)anchorOffset
hCenterOffset:(double)horizontalCenterOffset
fromAnchor:(int)anchorType;
- (void)dispose;
@end

Expand Down
13 changes: 10 additions & 3 deletions packages/firebase_admob/ios/Classes/FLTMobileAd.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ @implementation FLTMobileAd
FlutterMethodChannel *_channel;
FLTMobileAdStatus _status;
double _anchorOffset;
double _horizontalCenterOffset;
int _anchorType;

+ (void)initialize {
Expand All @@ -22,6 +23,7 @@ + (void)initialize {
}
_anchorType = 0;
_anchorOffset = 0;
_horizontalCenterOffset = 0;

if (statusToString == nil) {
statusToString = @{
Expand Down Expand Up @@ -53,6 +55,7 @@ - (instancetype)initWithId:(NSNumber *)mobileAdId channel:(FlutterMethodChannel
_channel = channel;
_status = CREATED;
_anchorOffset = 0;
_horizontalCenterOffset = 0;
_anchorType = 0;
allAds[mobileAdId] = self;
}
Expand All @@ -67,12 +70,15 @@ - (void)loadWithAdUnitId:(NSString *)adUnitId targetingInfo:(NSDictionary *)targ
// Implemented by the Banner and Interstitial subclasses
}

- (void)showAtOffset:(double)anchorOffset fromAnchor:(int)anchorType {
- (void)showAtOffset:(double)anchorOffset
hCenterOffset:(double)horizontalCenterOffset
fromAnchor:(int)anchorType {
_anchorType = anchorType;
_anchorOffset = anchorOffset;
if (_anchorType == 0) {
_anchorOffset = -_anchorOffset;
}
_horizontalCenterOffset = horizontalCenterOffset;
[self show];
}

Expand Down Expand Up @@ -146,7 +152,8 @@ - (void)show {
if (@available(ios 11.0, *)) {
UILayoutGuide *guide = screen.safeAreaLayoutGuide;
[NSLayoutConstraint activateConstraints:@[
[_banner.centerXAnchor constraintEqualToAnchor:guide.centerXAnchor],
[_banner.centerXAnchor constraintEqualToAnchor:guide.centerXAnchor
constant:_horizontalCenterOffset],
[_banner.bottomAnchor
constraintEqualToAnchor:_anchorType == 0 ? guide.bottomAnchor : guide.topAnchor
constant:_anchorOffset]
Expand All @@ -161,7 +168,7 @@ - (void)show {

- (void)placeBannerPreIos11 {
UIView *screen = [FLTMobileAd rootViewController].view;
CGFloat x = screen.frame.size.width / 2 - _banner.frame.size.width / 2;
CGFloat x = screen.frame.size.width / 2 - _banner.frame.size.width / 2 + _horizontalCenterOffset;
CGFloat y;
if (_anchorType == 0) {
y = screen.frame.size.height - _banner.frame.size.height + _anchorOffset;
Expand Down
6 changes: 5 additions & 1 deletion packages/firebase_admob/ios/Classes/FirebaseAdMobPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,19 @@ - (void)callShowAd:(NSNumber *)mobileAdId
}

double offset = 0.0;
double horizontalCenterOffset = 0.0;
int type = 0;
if (call.arguments[@"anchorOffset"] != nil) {
offset = [call.arguments[@"anchorOffset"] doubleValue];
}
if (call.arguments[@"horizontalCenterOffset"] != nil) {
horizontalCenterOffset = [call.arguments[@"horizontalCenterOffset"] doubleValue];
}
if (call.arguments[@"anchorType"] != nil) {
type = [call.arguments[@"anchorType"] isEqualToString:@"bottom"] ? 0 : 1;
}

[ad showAtOffset:offset fromAnchor:type];
[ad showAtOffset:offset hCenterOffset:horizontalCenterOffset fromAnchor:type];
result([NSNumber numberWithBool:YES]);
}

Expand Down
8 changes: 6 additions & 2 deletions packages/firebase_admob/lib/firebase_admob.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import 'dart:async';
import 'dart:io' show Platform;

import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:meta/meta.dart';

Expand Down Expand Up @@ -221,11 +222,14 @@ abstract class MobileAd {
/// anchorOffset is the logical pixel offset from the edge of the screen (default 0.0)
/// anchorType place advert at top or bottom of screen (default bottom)
Future<bool> show(
{double anchorOffset = 0.0, AnchorType anchorType = AnchorType.bottom}) {
{double anchorOffset = 0.0,
double horizontalCenterOffset = 0.0,
AnchorType anchorType = AnchorType.bottom}) {
return _invokeBooleanMethod("showAd", <String, dynamic>{
'id': id,
'anchorOffset': anchorOffset.toString(),
'anchorType': anchorType == AnchorType.top ? "top" : "bottom"
'horizontalCenterOffset': horizontalCenterOffset.toString(),
'anchorType': describeEnum(anchorType)
});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/firebase_admob/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Flutter plugin for Firebase AdMob, supporting
banner, interstitial (full-screen), and rewarded video ads
author: Flutter Team <flutter-dev@googlegroups.com>
homepage: https://github.com/flutter/plugins/tree/master/packages/firebase_admob
version: 0.9.0+3
version: 0.9.0+4

flutter:
plugin:
Expand Down
6 changes: 5 additions & 1 deletion packages/firebase_admob/test/firebase_admob_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ void main() {
isMethodCall('showAd', arguments: <String, dynamic>{
'id': id,
'anchorOffset': '0.0',
'horizontalCenterOffset': '0.0',
'anchorType': 'bottom',
}),
isMethodCall('disposeAd', arguments: <String, dynamic>{
Expand All @@ -92,7 +93,9 @@ void main() {
expect(await interstitial.load(), true);
expect(
await interstitial.show(
anchorOffset: 60.0, anchorType: AnchorType.top),
anchorOffset: 60.0,
horizontalCenterOffset: 10.0,
anchorType: AnchorType.top),
true);
expect(await interstitial.dispose(), true);

Expand All @@ -105,6 +108,7 @@ void main() {
isMethodCall('showAd', arguments: <String, dynamic>{
'id': id,
'anchorOffset': '60.0',
'horizontalCenterOffset': '10.0',
'anchorType': 'top',
}),
isMethodCall('disposeAd', arguments: <String, dynamic>{
Expand Down

0 comments on commit f1e60a5

Please sign in to comment.