Skip to content

Commit

Permalink
[Snackbar] Add tvOS support to MDCSnackbar. (#9428)
Browse files Browse the repository at this point in the history
[Snackbar] Add tvOS support to MDCSnackbar.
  • Loading branch information
yarneo committed Jan 14, 2020
1 parent bcaf187 commit 7f5d3c7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
12 changes: 8 additions & 4 deletions components/Snackbar/src/MDCSnackbarMessageView.m
Expand Up @@ -475,13 +475,17 @@ + (BOOL)requiresConstraintBasedLayout {
}

- (CGFloat)minimumWidth {
return UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ? kMinimumViewWidth_iPad
: kMinimumViewWidth_iPhone;
return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ||
UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomTV)
? kMinimumViewWidth_iPad
: kMinimumViewWidth_iPhone;
}

- (CGFloat)maximumWidth {
return UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ? kMaximumViewWidth_iPad
: kMaximumViewWidth_iPhone;
return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ||
UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomTV)
? kMaximumViewWidth_iPad
: kMaximumViewWidth_iPhone;
}

#pragma mark - Styling the view
Expand Down
23 changes: 18 additions & 5 deletions components/Snackbar/src/private/MDCSnackbarOverlayView.m
Expand Up @@ -22,7 +22,11 @@
#import "MDCSnackbarMessageViewInternal.h"
#import "MaterialAnimationTiming.h"
#import "MaterialApplication.h"

#ifndef TARGET_OS_TV
#import "MaterialKeyboardWatcher.h"
#endif

#import "MaterialOverlay.h"

NSString *const MDCSnackbarOverlayIdentifier = @"MDCSnackbar";
Expand Down Expand Up @@ -74,10 +78,12 @@ @interface MDCSnackbarOverlayView ()
*/
@property(nonatomic) NSLayoutConstraint *snackbarViewCenterConstraint;

#ifndef TARGET_OS_TV
/**
The object which will notify us of changes in the keyboard position.
*/
@property(nonatomic) MDCKeyboardWatcher *watcher;
#endif

/**
The layout constraint which determines the bottom of the containing view. Setting the constant
Expand Down Expand Up @@ -125,17 +131,17 @@ @implementation MDCSnackbarOverlayView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];

MDCKeyboardWatcher *watcher = [MDCKeyboardWatcher sharedKeyboardWatcher];

if (self) {
_watcher = watcher;
_containingView = [[UIView alloc] initWithFrame:frame];
_containingView.translatesAutoresizingMaskIntoConstraints = NO;
if (MDCSnackbarMessage.usesLegacySnackbar) {
_containingView.clipsToBounds = YES;
}
[self addSubview:_containingView];

#ifndef TARGET_OS_TV
MDCKeyboardWatcher *watcher = [MDCKeyboardWatcher sharedKeyboardWatcher];
_watcher = watcher;
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

[nc addObserver:self
Expand All @@ -162,7 +168,7 @@ - (instancetype)initWithFrame:(CGRect)frame {
selector:@selector(didRotate:)
name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil];

#endif
[self setupContainerConstraints];
}

Expand Down Expand Up @@ -219,7 +225,10 @@ - (void)dealloc {
change at any time during runtime.
*/
- (CGFloat)dynamicBottomMargin {
CGFloat keyboardHeight = self.watcher.visibleKeyboardHeight;
CGFloat keyboardHeight = 0.0;
#ifndef TARGET_OS_TV
keyboardHeight = self.watcher.visibleKeyboardHeight;
#endif
CGFloat userHeight = self.bottomOffset;
if (!MDCSnackbarMessage.usesLegacySnackbar) {
if (@available(iOS 11.0, *)) {
Expand Down Expand Up @@ -589,6 +598,7 @@ - (void)slideOutMessageView:(MDCSnackbarMessageView *)snackbarView
completion:completion];
}

#ifndef TARGET_OS_TV
#pragma mark - Keyboard Notifications

- (void)updatesnackbarPositionWithKeyboardUserInfo:(NSDictionary *)userInfo {
Expand Down Expand Up @@ -631,6 +641,7 @@ - (void)keyboardWillBeHidden:(NSNotification *)notification {
- (void)keyboardWillChangeFrame:(NSNotification *)notification {
[self updatesnackbarPositionWithKeyboardUserInfo:[notification userInfo]];
}
#endif

#pragma mark - Bottom And Side Margins

Expand Down Expand Up @@ -715,6 +726,7 @@ - (void)layoutSubviews {
}
}

#ifndef TARGET_OS_TV
- (void)willRotate:(NSNotification *)notification {
UIApplication *application = [UIApplication mdc_safeSharedApplication];
UIInterfaceOrientation currentOrientation = application.statusBarOrientation;
Expand Down Expand Up @@ -742,6 +754,7 @@ - (void)didRotate:(__unused NSNotification *)notification {
self.rotationDuration = -1;
});
}
#endif

#pragma mark - Overlay Support

Expand Down

0 comments on commit 7f5d3c7

Please sign in to comment.