-
Notifications
You must be signed in to change notification settings - Fork 1.6k
add firebase in-app messaging display pod #1798
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2dd6a84
add inapp messaging display
mylyuic b3d4446
style enhancements
mylyuic 665f685
updated clang-format version to 7.0.0 and run style.sh again
mylyuic 14871d1
add README.md for fiam display
mylyuic 23de3c9
update more README.md files
mylyuic ee3ade4
address review comments
mylyuic 24b2ded
whitespace check fix
mylyuic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
Firebase/InAppMessagingDisplay/Banner/FIDBannerViewController.h
This file contains hidden or 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,35 @@ | ||
/* | ||
* Copyright 2018 Google | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
#import "FIDBaseRenderingViewController.h" | ||
|
||
@class FIRInAppMessagingBannerDisplay; | ||
@class FIDBaseRenderingViewController; | ||
@protocol FIDTimeFetcher; | ||
@protocol FIRInAppMessagingDisplayDelegate; | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
@interface FIDBannerViewController : FIDBaseRenderingViewController | ||
+ (FIDBannerViewController *) | ||
instantiateViewControllerWithResourceBundle:(NSBundle *)resourceBundle | ||
displayMessage:(FIRInAppMessagingBannerDisplay *)bannerMessage | ||
displayDelegate: | ||
(id<FIRInAppMessagingDisplayDelegate>)displayDelegate | ||
timeFetcher:(id<FIDTimeFetcher>)timeFetcher; | ||
@end | ||
NS_ASSUME_NONNULL_END |
292 changes: 292 additions & 0 deletions
292
Firebase/InAppMessagingDisplay/Banner/FIDBannerViewController.m
This file contains hidden or 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,292 @@ | ||
/* | ||
* Copyright 2018 Google | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#import <FirebaseInAppMessaging/FIRInAppMessagingRendering.h> | ||
|
||
#import "FIDBannerViewController.h" | ||
#import "FIRCore+InAppMessagingDisplay.h" | ||
|
||
@interface FIDBannerViewController () | ||
|
||
@property(nonatomic, readwrite) FIRInAppMessagingBannerDisplay *bannerDisplayMessage; | ||
|
||
@property(weak, nonatomic) IBOutlet NSLayoutConstraint *imageViewWidthConstraint; | ||
@property(weak, nonatomic) IBOutlet NSLayoutConstraint *imageViewHeightConstraint; | ||
|
||
@property(weak, nonatomic) | ||
IBOutlet NSLayoutConstraint *imageBottomAlignWithBodyLabelBottomConstraint; | ||
@property(weak, nonatomic) IBOutlet UIImageView *imageView; | ||
@property(weak, nonatomic) IBOutlet UILabel *titleLabel; | ||
@property(weak, nonatomic) IBOutlet UILabel *bodyLabel; | ||
|
||
// Banner view will be rendered and dismissed with animation. Within viewDidLayoutSubviews function, | ||
// we would position the view so that it's out of UIWindow range on the top so that later on it can | ||
// be slided in with animation. However, viewDidLayoutSubviews is also triggred in other scenarios | ||
// like split view on iPad or device orientation changes where we don't want to hide the banner for | ||
// animations. So to have different logic, we use this property to tell the two different | ||
// cases apart and apply different positioning logic accordingly in viewDidLayoutSubviews. | ||
@property(nonatomic) BOOL hidingForAnimation; | ||
|
||
@property(nonatomic, nullable) NSTimer *autoDismissTimer; | ||
@end | ||
|
||
// The image display area dimension in points | ||
static const CGFloat kBannerViewImageWidth = 60; | ||
static const CGFloat kBannerViewImageHeight = 60; | ||
|
||
static const NSTimeInterval kBannerViewAnimationDuration = 0.3; // in seconds | ||
|
||
// Banner view will auto dismiss after this amount of time of showing if user does not take | ||
// any other actions. It's in seconds. | ||
static const NSTimeInterval kBannerAutoDimissTime = 12; | ||
|
||
// If the window width is larger than this threshold, we cap banner view width | ||
// by it: showing a non full-width banner when it happens. | ||
static const CGFloat kBannerViewMaxWidth = 736; | ||
|
||
@implementation FIDBannerViewController | ||
|
||
+ (FIDBannerViewController *) | ||
instantiateViewControllerWithResourceBundle:(NSBundle *)resourceBundle | ||
displayMessage:(FIRInAppMessagingBannerDisplay *)bannerMessage | ||
displayDelegate: | ||
(id<FIRInAppMessagingDisplayDelegate>)displayDelegate | ||
timeFetcher:(id<FIDTimeFetcher>)timeFetcher { | ||
UIStoryboard *storyboard = | ||
[UIStoryboard storyboardWithName:@"FIRInAppMessageDisplayStoryboard" bundle:resourceBundle]; | ||
|
||
if (storyboard == nil) { | ||
FIRLogError(kFIRLoggerInAppMessagingDisplay, @"I-FID300002", | ||
@"Storyboard '" | ||
"FIRInAppMessageDisplayStoryboard' not found in bundle %@", | ||
resourceBundle); | ||
return nil; | ||
} | ||
FIDBannerViewController *bannerVC = (FIDBannerViewController *)[storyboard | ||
instantiateViewControllerWithIdentifier:@"banner-view-vc"]; | ||
bannerVC.displayDelegate = displayDelegate; | ||
bannerVC.bannerDisplayMessage = bannerMessage; | ||
bannerVC.timeFetcher = timeFetcher; | ||
|
||
return bannerVC; | ||
} | ||
|
||
- (void)setupRecognizers { | ||
UIPanGestureRecognizer *panSwipeRecognizer = | ||
[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanSwipe:)]; | ||
[self.view addGestureRecognizer:panSwipeRecognizer]; | ||
|
||
UITapGestureRecognizer *tapGestureRecognizer = | ||
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(messageTapped:)]; | ||
tapGestureRecognizer.delaysTouchesBegan = YES; | ||
tapGestureRecognizer.numberOfTapsRequired = 1; | ||
|
||
[self.view addGestureRecognizer:tapGestureRecognizer]; | ||
} | ||
|
||
static const CGFloat kSwipeUpThreshold = -10.0f; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should likely be at the top, not inline with method implementations. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved |
||
- (void)handlePanSwipe:(UIPanGestureRecognizer *)recognizer { | ||
// Detect the swipe gesture | ||
if (recognizer.state == UIGestureRecognizerStateEnded) { | ||
CGPoint vel = [recognizer velocityInView:recognizer.view]; | ||
if (vel.y < kSwipeUpThreshold) { | ||
[self closeViewFromManualDismiss]; | ||
} | ||
} | ||
} | ||
|
||
- (void)viewDidLoad { | ||
[super viewDidLoad]; | ||
// Do any additional setup after loading the view from its nib. | ||
|
||
[self setupRecognizers]; | ||
|
||
self.titleLabel.text = self.bannerDisplayMessage.title; | ||
self.bodyLabel.text = self.bannerDisplayMessage.bodyText; | ||
|
||
if (self.bannerDisplayMessage.imageData) { | ||
self.imageView.contentMode = UIViewContentModeScaleAspectFit; | ||
|
||
UIImage *image = [UIImage imageWithData:self.bannerDisplayMessage.imageData.imageRawData]; | ||
|
||
if (fabs(image.size.width / image.size.height - 1) > 0.02) { | ||
// width and height differ by at least 2%, need to adjust image view | ||
// size to respect the ratio | ||
|
||
// reduce height or width of the image view to retain the ratio of the image | ||
if (image.size.width > image.size.height) { | ||
CGFloat newImageHeight = kBannerViewImageWidth * image.size.height / image.size.width; | ||
self.imageViewHeightConstraint.constant = newImageHeight; | ||
} else { | ||
CGFloat newImageWidth = kBannerViewImageHeight * image.size.width / image.size.height; | ||
self.imageViewWidthConstraint.constant = newImageWidth; | ||
} | ||
} | ||
self.imageView.image = image; | ||
} else { | ||
// Hide image and remove the bottom constraint between body label and image view. | ||
self.imageViewWidthConstraint.constant = 0; | ||
self.imageBottomAlignWithBodyLabelBottomConstraint.active = NO; | ||
} | ||
|
||
// Set some rendering effects based on settings. | ||
self.view.backgroundColor = self.bannerDisplayMessage.displayBackgroundColor; | ||
self.titleLabel.textColor = self.bannerDisplayMessage.textColor; | ||
self.bodyLabel.textColor = self.bannerDisplayMessage.textColor; | ||
|
||
self.view.layer.masksToBounds = NO; | ||
self.view.layer.shadowOffset = CGSizeMake(2, 1); | ||
self.view.layer.shadowRadius = 2; | ||
self.view.layer.shadowOpacity = 0.4; | ||
|
||
// When created, we are hiding it for later animation | ||
self.hidingForAnimation = YES; | ||
[self setupAutoDismissTimer]; | ||
} | ||
|
||
- (void)dismissViewWithAnimation:(void (^)(void))completion { | ||
CGRect rectInNormalState = self.view.frame; | ||
CGAffineTransform hidingTransform = | ||
CGAffineTransformMakeTranslation(0, -rectInNormalState.size.height); | ||
|
||
[UIView animateWithDuration:kBannerViewAnimationDuration | ||
delay:0 | ||
options:UIViewAnimationOptionCurveEaseInOut | ||
animations:^{ | ||
self.view.transform = hidingTransform; | ||
} | ||
completion:^(BOOL finished) { | ||
completion(); | ||
}]; | ||
} | ||
|
||
- (void)closeViewFromAutoDismiss { | ||
FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID300001", @"Auto dismiss the banner view"); | ||
[self dismissViewWithAnimation:^(void) { | ||
[self dismissView:FIRInAppMessagingDismissTypeAuto]; | ||
}]; | ||
} | ||
|
||
- (void)closeViewFromManualDismiss { | ||
FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID300003", @"Manually dismiss the banner view"); | ||
[self.autoDismissTimer invalidate]; | ||
[self dismissViewWithAnimation:^(void) { | ||
[self dismissView:FIRInAppMessagingDismissTypeUserSwipe]; | ||
}]; | ||
} | ||
|
||
- (void)messageTapped:(UITapGestureRecognizer *)recognizer { | ||
[self.autoDismissTimer invalidate]; | ||
[self dismissViewWithAnimation:^(void) { | ||
[self followActionURL]; | ||
}]; | ||
} | ||
|
||
- (void)adjustBodyLabelViewHeight { | ||
// These lines make sure that we only change the height of the label view | ||
// to fit the content. Doing [self.bodyLabel sizeToFit] only could potentially | ||
// change the width as well. | ||
CGRect theFrame = self.bodyLabel.frame; | ||
[self.bodyLabel sizeToFit]; | ||
theFrame.size.height = self.bodyLabel.frame.size.height; | ||
self.bodyLabel.frame = theFrame; | ||
} | ||
|
||
- (void)viewDidLayoutSubviews { | ||
[super viewDidLayoutSubviews]; | ||
|
||
CGFloat bannerViewHeight = 0; | ||
|
||
[self adjustBodyLabelViewHeight]; | ||
|
||
if (self.bannerDisplayMessage.imageData) { | ||
CGFloat imageBottom = CGRectGetMaxY(self.imageView.frame); | ||
CGFloat bodyBottom = CGRectGetMaxY(self.bodyLabel.frame); | ||
bannerViewHeight = MAX(imageBottom, bodyBottom); | ||
} else { | ||
bannerViewHeight = CGRectGetMaxY(self.bodyLabel.frame); | ||
} | ||
|
||
bannerViewHeight += 5; // Add some padding margin on the bottom of the view | ||
|
||
CGFloat appWindowWidth = [self.view.window bounds].size.width; | ||
CGFloat bannerViewWidth = appWindowWidth; | ||
|
||
if (bannerViewWidth > kBannerViewMaxWidth) { | ||
bannerViewWidth = kBannerViewMaxWidth; | ||
self.view.layer.cornerRadius = 4; | ||
} | ||
|
||
CGRect viewRect = | ||
CGRectMake((appWindowWidth - bannerViewWidth) / 2, 0, bannerViewWidth, bannerViewHeight); | ||
self.view.frame = viewRect; | ||
|
||
if (self.hidingForAnimation) { | ||
// Move the banner to be just above the top of the window to hide it. | ||
self.view.center = CGPointMake(appWindowWidth / 2, -viewRect.size.height / 2); | ||
} | ||
} | ||
|
||
- (void)viewDidAppear:(BOOL)animated { | ||
[super viewDidAppear:animated]; | ||
CGRect rectInNormalState = self.view.frame; | ||
CGPoint normalCenterPoint = | ||
CGPointMake(rectInNormalState.origin.x + rectInNormalState.size.width / 2, | ||
rectInNormalState.size.height / 2); | ||
|
||
self.hidingForAnimation = NO; | ||
[UIView animateWithDuration:kBannerViewAnimationDuration | ||
delay:0 | ||
options:UIViewAnimationOptionCurveEaseInOut | ||
animations:^{ | ||
self.view.center = normalCenterPoint; | ||
} | ||
completion:nil]; | ||
} | ||
|
||
- (void)setupAutoDismissTimer { | ||
NSTimeInterval remaining = kBannerAutoDimissTime - super.aggregateImpressionTimeInSeconds; | ||
|
||
FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID300004", | ||
@"Remaining banner auto dismiss time is %lf", remaining); | ||
|
||
// Set up the auto dismiss behavior. | ||
__weak id weakSelf = self; | ||
self.autoDismissTimer = | ||
[NSTimer scheduledTimerWithTimeInterval:remaining | ||
target:weakSelf | ||
selector:@selector(closeViewFromAutoDismiss) | ||
userInfo:nil | ||
repeats:NO]; | ||
} | ||
|
||
// Handlers for app become active inactive so that we can better adjust our auto dismiss feature | ||
- (void)appDidBecomeInactive:(UIApplication *)application { | ||
[super appDidBecomeInactive:application]; | ||
[self.autoDismissTimer invalidate]; | ||
} | ||
|
||
- (void)appDidBecomeActive:(UIApplication *)application { | ||
[super appDidBecomeActive:application]; | ||
[self setupAutoDismissTimer]; | ||
} | ||
|
||
- (void)dealloc { | ||
FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID300005", | ||
@"-[FIRIAMBannerViewController dealloc] triggered for %p", self); | ||
[self.autoDismissTimer invalidate]; | ||
} | ||
@end |
23 changes: 23 additions & 0 deletions
23
Firebase/InAppMessagingDisplay/Banner/FIDBannerViewUIWindow.h
This file contains hidden or 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,23 @@ | ||
/* | ||
* Copyright 2018 Google | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#import <UIKit/UIKit.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
@interface FIDBannerViewUIWindow : UIWindow | ||
|
||
@end | ||
NS_ASSUME_NONNULL_END |
30 changes: 30 additions & 0 deletions
30
Firebase/InAppMessagingDisplay/Banner/FIDBannerViewUIWindow.m
This file contains hidden or 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,30 @@ | ||
/* | ||
* Copyright 2018 Google | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#import "FIDBannerViewUIWindow.h" | ||
|
||
@implementation FIDBannerViewUIWindow | ||
|
||
// For banner view message, we still allow the user to interact with the app's underlying view | ||
// outside banner view's visible area. | ||
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event { | ||
if (self.rootViewController && self.rootViewController.view) { | ||
return CGRectContainsPoint(self.rootViewController.view.frame, point); | ||
} else { | ||
return NO; | ||
} | ||
} | ||
@end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so that later on it can slide in
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done