Skip to content

Commit 95891e1

Browse files
seanosheaianegordon
authored andcommitted
[Snackbar] Message View Styling. (#1120)
* Snackbar Message View Styling. * Small alteration to how colors are generated for the unit tests. * Not styling the snack bar in the init method. * Default styling for background views. * Remove overriding the styling in init. * Looks like swiftlint is already installed by default on travis-ci boxes. Trying this as an experiment. * Trying this? * Cant get unit tests to pass as there’s no window to add the message view to with logic tests.
1 parent 8bcb197 commit 95891e1

File tree

7 files changed

+92
-39
lines changed

7 files changed

+92
-39
lines changed

components/Snackbar/src/MDCSnackbarManager.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616

1717
#import "MDCSnackbarManager.h"
1818
#import "MDCSnackbarMessage.h"
19+
#import "MDCSnackbarMessageView.h"
1920
#import "MaterialOverlayWindow.h"
2021
#import "UIApplication+AppExtensions.h"
2122
#import "private/MDCSnackbarMessageInternal.h"
22-
#import "private/MDCSnackbarMessageView.h"
2323
#import "private/MDCSnackbarOverlayView.h"
24+
#import "private/MDCSnackbarMessageViewInternal.h"
2425

2526
@class MDCSnackbarManagerSuspensionToken;
2627

components/Snackbar/src/MDCSnackbarMessage.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
*/
1616

1717
#import "MDCSnackbarMessage.h"
18+
#import "MDCSnackbarMessageView.h"
1819
#import "private/MDCSnackbarMessageInternal.h"
19-
#import "private/MDCSnackbarMessageView.h"
20+
2021

2122
static const NSTimeInterval kDefaultDuration = 4.0f;
2223

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright 2016-present the Material Components for iOS authors. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
#import <UIKit/UIKit.h>
18+
19+
20+
/**
21+
Class which provides the default implementation of a snackbar.
22+
*/
23+
@interface MDCSnackbarMessageView : UIView
24+
25+
/**
26+
The color for the background of the snackbar message view.
27+
*/
28+
@property(nonatomic, strong, nullable) UIColor *snackbarMessageViewBackgroundColor UI_APPEARANCE_SELECTOR;
29+
30+
/**
31+
The color for the shadow color for the snackbar message view.
32+
*/
33+
@property(nonatomic, strong, nullable) UIColor *snackbarMessageViewShadowColor UI_APPEARANCE_SELECTOR;
34+
35+
/**
36+
The color for the message text in the snackbar message view.
37+
*/
38+
@property(nonatomic, strong, nullable) UIColor *snackbarMessageViewTextColor UI_APPEARANCE_SELECTOR;
39+
40+
@end

components/Snackbar/src/private/MDCSnackbarMessageView.m renamed to components/Snackbar/src/MDCSnackbarMessageView.m

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818

1919
#import "MDCSnackbarMessage.h"
2020
#import "MDCSnackbarMessageView.h"
21+
#import "MaterialButtons.h"
2122
#import "MDCSnackbarOverlayView.h"
2223
#import "MaterialAnimationTiming.h"
2324
#import "MaterialButtons.h"
2425
#import "MaterialTypography.h"
26+
#import "private/MDCSnackbarMessageViewInternal.h"
2527

2628
NSString *const MDCSnackbarMessageTitleAutomationIdentifier =
2729
@"MDCSnackbarMessageTitleAutomationIdentifier";
@@ -133,11 +135,6 @@ @interface MDCSnackbarMessageView ()
133135
*/
134136
@property(nonatomic, strong) UIView *contentView;
135137

136-
/**
137-
The message to display.
138-
*/
139-
@property(nonatomic, strong) MDCSnackbarMessage *message;
140-
141138
/**
142139
Holds onto the dismissal handler, called when the snackbar should dismiss due to user interaction.
143140
*/
@@ -164,6 +161,12 @@ - (instancetype)initWithFrame:(CGRect)frame {
164161

165162
@implementation MDCSnackbarMessageView
166163

164+
+ (void)initialize {
165+
[[self appearance] setSnackbarMessageViewShadowColor:MDCRGBAColor(0x00, 0x00, 0x00, 1.0f)];
166+
[[self appearance] setSnackbarMessageViewBackgroundColor:MDCRGBAColor(0x32, 0x32, 0x32, 1.0f)];
167+
[[self appearance] setSnackbarMessageViewTextColor:MDCRGBAColor(0xFF, 0xFF, 0xFF, 1.0f)];
168+
}
169+
167170
- (void)dismissWithAction:(MDCSnackbarMessageAction *)action userInitiated:(BOOL)userInitiated {
168171
if (self.dismissalHandler) {
169172
self.dismissalHandler(userInitiated, action);
@@ -189,9 +192,7 @@ - (CGFloat)maximumWidth {
189192
: kMaximumViewWidth_iPhone;
190193
}
191194

192-
- (UIColor *)snackbarBackgroundColor {
193-
return MDCRGBAColor(0x32, 0x32, 0x32, 1.0f);
194-
}
195+
#pragma mark - Styling the view
195196

196197
- (UIColor *)snackbarButtonTextColor {
197198
return MDCRGBAColor(0xFF, 0xFF, 0xFF, 0.6f);
@@ -205,25 +206,40 @@ - (UIColor *)snackbarSeparatorColor {
205206
return MDCRGBAColor(0xFF, 0xFF, 0xFF, 0.5f);
206207
}
207208

208-
- (UIColor *)snackbarShadowColor {
209-
return MDCRGBAColor(0x00, 0x00, 0x00, 1.0f);
209+
- (void)setSnackbarMessageViewBackgroundColor:(UIColor *)snackbarMessageViewBackgroundColor {
210+
_snackbarMessageViewBackgroundColor = snackbarMessageViewBackgroundColor;
211+
_containerView.backgroundColor = snackbarMessageViewBackgroundColor;
210212
}
211213

212-
- (UIColor *)snackbarTextColor {
213-
return MDCRGBAColor(0xFF, 0xFF, 0xFF, 1.0f);
214+
- (void)setSnackbarShadowColor:(UIColor *)snackbarMessageViewShadowColor {
215+
_snackbarMessageViewShadowColor = snackbarMessageViewShadowColor;
216+
self.layer.shadowColor = snackbarMessageViewShadowColor.CGColor;
217+
}
218+
219+
- (void)setSnackbarMessageViewTextColor:(UIColor *)snackbarMessageViewTextColor {
220+
_snackbarMessageViewTextColor = snackbarMessageViewTextColor;
221+
self.label.textColor = _snackbarMessageViewTextColor;
222+
}
223+
224+
- (void)addColorToMessageLabel:(UIColor *)color {
225+
NSMutableAttributedString *messageString = [_label.attributedText mutableCopy];
226+
[messageString addAttributes:@{
227+
NSForegroundColorAttributeName :color,
228+
} range:NSMakeRange(0, messageString.length)];
229+
_label.attributedText = messageString;
214230
}
215231

216232
- (instancetype)initWithMessage:(MDCSnackbarMessage *)message
217233
dismissHandler:(MDCSnackbarMessageDismissHandler)handler {
218234
self = [super init];
219235
if (self) {
236+
220237
_message = message;
221238
_dismissalHandler = [handler copy];
222-
223-
UIColor *shadowColor = [self snackbarShadowColor];
239+
224240
self.backgroundColor = [UIColor clearColor];
225241
self.layer.cornerRadius = kCornerRadius;
226-
self.layer.shadowColor = shadowColor.CGColor;
242+
self.layer.shadowColor = _snackbarMessageViewShadowColor.CGColor;
227243
self.layer.shadowOpacity = kShadowAlpha;
228244
self.layer.shadowOffset = kShadowOffset;
229245
self.layer.shadowRadius = kShadowSpread;
@@ -235,7 +251,7 @@ - (instancetype)initWithMessage:(MDCSnackbarMessage *)message
235251
[self addSubview:_containerView];
236252

237253
[_containerView setTranslatesAutoresizingMaskIntoConstraints:NO];
238-
_containerView.backgroundColor = [self snackbarBackgroundColor];
254+
_containerView.backgroundColor = _snackbarMessageViewBackgroundColor;
239255
_containerView.layer.cornerRadius = kCornerRadius;
240256
_containerView.layer.masksToBounds = YES;
241257

@@ -287,11 +303,6 @@ - (instancetype)initWithMessage:(MDCSnackbarMessage *)message
287303
}];
288304

289305
// Apply 'global' attributes along the whole string.
290-
NSDictionary *attributes = @{
291-
NSForegroundColorAttributeName : [self snackbarTextColor],
292-
};
293-
[messageString addAttributes:attributes range:NSMakeRange(0, messageString.length)];
294-
295306
_label.backgroundColor = [UIColor clearColor];
296307
_label.textAlignment = NSTextAlignmentNatural;
297308
_label.attributedText = messageString;
@@ -331,6 +342,8 @@ - (instancetype)initWithMessage:(MDCSnackbarMessage *)message
331342
UIColor *textColor = [self snackbarButtonTextColor];
332343
UIColor *textColorHighlighted = [self snackbarButtonTextColorHighlighted];
333344

345+
_label.textColor = textColor;
346+
334347
if (message.buttonTextColor) {
335348
textColor = message.buttonTextColor;
336349
}

components/Snackbar/src/MaterialSnackbar.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@
1616

1717
#import "MDCSnackbarManager.h"
1818
#import "MDCSnackbarMessage.h"
19+
#import "MDCSnackbarMessageView.h"

components/Snackbar/src/private/MDCSnackbarMessageView.h renamed to components/Snackbar/src/private/MDCSnackbarMessageViewInternal.h

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
/*
22
Copyright 2016-present the Material Components for iOS authors. All Rights Reserved.
3-
3+
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
66
You may obtain a copy of the License at
7-
7+
88
http://www.apache.org/licenses/LICENSE-2.0
9-
9+
1010
Unless required by applicable law or agreed to in writing, software
1111
distributed under the License is distributed on an "AS IS" BASIS,
1212
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
#import <UIKit/UIKit.h>
18-
1917
@class MDCSnackbarMessage;
2018
@class MDCSnackbarMessageAction;
2119

@@ -26,12 +24,10 @@
2624
interaction. @c action, if non-nil, indicates that the user chose to execute a specific action.
2725
*/
2826
typedef void (^MDCSnackbarMessageDismissHandler)(BOOL userInitiated,
29-
MDCSnackbarMessageAction *action);
27+
MDCSnackbarMessageAction * _Nullable action);
28+
29+
@interface MDCSnackbarMessageView ()
3030

31-
/**
32-
Class which provides the default implementation of a snackbar.
33-
*/
34-
@interface MDCSnackbarMessageView : UIView
3531

3632
/**
3733
If the user has tapped on the snackbar or if @c dismissWithAction:userInitiated: has been called.
@@ -51,16 +47,15 @@ typedef void (^MDCSnackbarMessageDismissHandler)(BOOL userInitiated,
5147
/**
5248
Convenience pointer to the message used to create the view.
5349
*/
54-
@property(nonatomic, readonly, strong) MDCSnackbarMessage *message;
55-
50+
@property(nonatomic, nullable, readonly, strong) MDCSnackbarMessage *message;
5651
/**
5752
Creates a snackbar view to display @c message.
5853
5954
The view will call @c handler when the user has interacted with the snackbar view in such a way
6055
that it needs to be dismissed prior to its timer-based dismissal time.
6156
*/
62-
- (instancetype)initWithMessage:(MDCSnackbarMessage *)message
63-
dismissHandler:(MDCSnackbarMessageDismissHandler)handler;
57+
- (_Nonnull instancetype)initWithMessage:(MDCSnackbarMessage * _Nullable)message
58+
dismissHandler:(MDCSnackbarMessageDismissHandler _Nullable)handler;
6459

6560
/**
6661
Dismisses the message view.
@@ -71,7 +66,7 @@ typedef void (^MDCSnackbarMessageDismissHandler)(BOOL userInitiated,
7166
@param action The action that prompted the dismissal.
7267
@param userInitiated Whether or not this is a user-initiated dismissal or a programmatic one.
7368
*/
74-
- (void)dismissWithAction:(MDCSnackbarMessageAction *)action userInitiated:(BOOL)userInitiated;
69+
- (void)dismissWithAction:(MDCSnackbarMessageAction * _Nullable)action userInitiated:(BOOL)userInitiated;
7570

7671
/**
7772
When VoiceOver is enabled the view should wait for user action before dismissing.
@@ -88,6 +83,7 @@ typedef void (^MDCSnackbarMessageDismissHandler)(BOOL userInitiated,
8883
- (void)animateContentOpacityFrom:(CGFloat)fromOpacity
8984
to:(CGFloat)toOpacity
9085
duration:(NSTimeInterval)duration
91-
timingFunction:(CAMediaTimingFunction *)timingFunction;
86+
timingFunction:(CAMediaTimingFunction * _Nullable)timingFunction;
87+
9288

9389
@end

components/Snackbar/src/private/MDCSnackbarOverlayView.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#import "MaterialKeyboardWatcher.h"
2424
#import "MaterialOverlays.h"
2525
#import "UIApplication+AppExtensions.h"
26+
#import "MDCSnackbarMessageViewInternal.h"
2627

2728
NSString *const MDCSnackbarOverlayIdentifier = @"MDCSnackbar";
2829

0 commit comments

Comments
 (0)