-
Notifications
You must be signed in to change notification settings - Fork 24
/
WTRSocialShareView.m
261 lines (225 loc) · 12.2 KB
/
WTRSocialShareView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
//
// WTRSocialShareView.m
// WootricSDK
//
// Copyright (c) 2015 Wootric (https://wootric.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import "WTRSocialShareView.h"
#import "WTRThankYouButton.h"
#import "WTRColor.h"
#import "NSString+FontAwesome.h"
#import "SimpleConstraints.h"
#import "UIItems.h"
@interface WTRSocialShareView ()
@property (nonatomic, strong) WTRSettings *settings;
@property (nonatomic, strong) WTRThankYouButton *thankYouButton;
@property (nonatomic, strong) UIButton *noThanksButton;
@property (nonatomic, strong) UIButton *facebookButton;
@property (nonatomic, strong) UIButton *twitterButton;
@property (nonatomic, strong) UIButton *facebookLikeButton;
@property (nonatomic, strong) UILabel *finalThankYouLabel;
@property (nonatomic, strong) UILabel *socialShareQuestionLabel;
@property (nonatomic, strong) UILabel *customThankYouLabel;
@property (nonatomic, strong) NSLayoutConstraint *facebookXConstraint;
@property (nonatomic, strong) NSLayoutConstraint *twitterXConstraint;
@property (nonatomic, strong) NSLayoutConstraint *facebookLikeXConstraint;
@end
@implementation WTRSocialShareView
- (instancetype)initWithSettings:(WTRSettings *)settings {
if (self = [super init]) {
_settings = settings;
self.backgroundColor = [UIColor whiteColor];
self.layer.borderWidth = 1;
self.layer.borderColor = [WTRColor sliderModalBorderColor].CGColor;
self.hidden = YES;
[self setTranslatesAutoresizingMaskIntoConstraints:NO];
}
return self;
}
- (void)setThankYouButtonTextAndURLDependingOnScore:(int)score andText:(NSString *)feedbackText{
NSString *text = [_settings thankYouLinkTextDependingOnScore:score];
NSURL *url = [_settings thankYouLinkURLDependingOnScore:score andText:feedbackText];
[_thankYouButton setText:text andURL:url];
}
- (void)displayShareButtonsWithTwitterAvailable:(BOOL)twitterAvailable andFacebookAvailable:(BOOL)facebookAvailable {
if (facebookAvailable) {
_facebookButton.hidden = NO;
_facebookLikeButton.hidden = NO;
}
if (twitterAvailable) {
_twitterButton.hidden = NO;
}
if (twitterAvailable && facebookAvailable) {
_facebookLikeXConstraint.constant = 64;
_twitterXConstraint.constant = 0;
_facebookXConstraint.constant = -64;
} else if (!twitterAvailable && facebookAvailable) {
_facebookLikeXConstraint.constant = 32;
_facebookXConstraint.constant = -32;
} else if (!twitterAvailable && !facebookAvailable) {
_socialShareQuestionLabel.hidden = YES;
}
}
- (void)setThankYouMessageDependingOnScore:(int)score {
_customThankYouLabel.text = [_settings thankYouMessageDependingOnScore:score];
}
- (void)initializeSubviewsWithTargetViewController:(UIViewController *)viewController {
[self setupThankYouButtonWithTargetViewController:viewController];
[self setupNoThanksButtonWithTargetViewController:viewController];
[self setupSocialShareQuestionLabel];
[self setupFinalThankYouLabel];
[self setupCustomThankYouLabel];
[self setupFacebookButtonWithTargetViewController:viewController];
[self setupTwitterButtonWithTargetViewController:viewController];
[self setupFacebookLikeButtonWithTargetViewController:viewController];
[self addSubviews];
}
- (void)setupSubviewsConstraints {
[self setupFinalThankYouLabelConstraints];
[self setupCustomThankYouLabelConstraints];
[self setupThankYouButtonConstraints];
[self setupNoThanksButtonConstraints];
[self setupSocialShareQuestionLabelConstraints];
[self setupFacebookButtonConstraints];
[self setupTwitterButtonConstraints];
[self setupFacebookLikeButtonConstraints];
}
- (void)addSubviews {
[self addSubview:_thankYouButton];
[self addSubview:_noThanksButton];
[self addSubview:_socialShareQuestionLabel];
[self addSubview:_finalThankYouLabel];
[self addSubview:_customThankYouLabel];
[self addSubview:_facebookButton];
[self addSubview:_twitterButton];
[self addSubview:_facebookLikeButton];
}
- (void)setupCustomThankYouLabel {
_customThankYouLabel = [UIItems customThankYouLabelWithFont:[UIFont boldSystemFontOfSize:12]];
}
- (void)setupThankYouButtonWithTargetViewController:(UIViewController *)viewController {
_thankYouButton = [[WTRThankYouButton alloc] initWithViewController:viewController backgroundColor:[_settings thankYouButtonBackgroundColor]];
}
- (void)setupFacebookButtonWithTargetViewController:(UIViewController *)viewController {
_facebookButton = [UIItems socialButtonWithTargetViewController:viewController title:[NSString fontAwesomeIconStringForEnum:FAFacebook] textColor:[_settings socialSharingColor]];
}
- (void)setupTwitterButtonWithTargetViewController:(UIViewController *)viewController {
_twitterButton = [UIItems socialButtonWithTargetViewController:viewController title:[NSString fontAwesomeIconStringForEnum:FATwitter] textColor:[_settings socialSharingColor]];
}
- (void)setupFacebookLikeButtonWithTargetViewController:(UIViewController *)viewController {
_facebookLikeButton = [UIItems socialButtonWithTargetViewController:viewController title:[NSString fontAwesomeIconStringForEnum:FAThumbsUp] textColor:[_settings socialSharingColor]];
}
- (void)setupNoThanksButtonWithTargetViewController:(UIViewController *)viewController {
_noThanksButton = [[UIButton alloc] init];
_noThanksButton.titleLabel.font = [UIFont boldSystemFontOfSize:12];
[_noThanksButton setTranslatesAutoresizingMaskIntoConstraints:NO];
[_noThanksButton setTitle:[[_settings socialShareDeclineText] uppercaseString] forState:UIControlStateNormal];
[_noThanksButton setTitleColor:[_settings sendButtonBackgroundColor] forState:UIControlStateNormal];
[_noThanksButton addTarget:viewController
action:NSSelectorFromString(@"noThanksButtonPressed")
forControlEvents:UIControlEventTouchUpInside];
}
- (void)setupFinalThankYouLabel {
if ([UIFont respondsToSelector:@selector(systemFontOfSize:weight:)]) {
_finalThankYouLabel = [UIItems finalThankYouLabelWithSettings:_settings
textColor:[UIColor blackColor]
andFont:[UIFont systemFontOfSize:18 weight:UIFontWeightMedium]];
} else {
_finalThankYouLabel = [UIItems finalThankYouLabelWithSettings:_settings
textColor:[UIColor blackColor]
andFont:[UIFont systemFontOfSize:18]];
}
}
- (void)setupSocialShareQuestionLabel {
_socialShareQuestionLabel = [[UILabel alloc] init];
_socialShareQuestionLabel.textAlignment = NSTextAlignmentCenter;
_socialShareQuestionLabel.textColor = [_settings socialSharingColor];
_socialShareQuestionLabel.numberOfLines = 0;
_socialShareQuestionLabel.lineBreakMode = NSLineBreakByWordWrapping;
_socialShareQuestionLabel.font = [UIFont boldSystemFontOfSize:12];
_socialShareQuestionLabel.text = [_settings socialShareQuestionText];
[_socialShareQuestionLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
}
- (void)setupCustomThankYouLabelConstraints {
[[[[_customThankYouLabel wtr_leftConstraint] toSecondViewLeft:self] withConstant:24] addToView:self];
[[[[_customThankYouLabel wtr_rightConstraint] toSecondViewRight:self] withConstant:-24] addToView:self];
[[[[_customThankYouLabel wtr_topConstraint] toSecondViewBottom:_finalThankYouLabel] withConstant:8] addToView:self];
}
- (void)setupFacebookButtonConstraints {
[_facebookButton wtr_constraintWidth:32];
[_facebookButton wtr_constraintHeight:32];
[[[[_facebookButton wtr_topConstraint] toSecondViewBottom:_socialShareQuestionLabel] withConstant:18] addToView:self];
_facebookXConstraint = [NSLayoutConstraint constraintWithItem:_facebookButton
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0];
[self addConstraint:_facebookXConstraint];
}
- (void)setupTwitterButtonConstraints {
[_twitterButton wtr_constraintWidth:32];
[_twitterButton wtr_constraintHeight:32];
[[[[_twitterButton wtr_topConstraint] toSecondViewBottom:_socialShareQuestionLabel] withConstant:18] addToView:self];
_twitterXConstraint = [NSLayoutConstraint constraintWithItem:_twitterButton
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0];
[self addConstraint:_twitterXConstraint];
}
- (void)setupFacebookLikeButtonConstraints {
[_facebookLikeButton wtr_constraintWidth:32];
[_facebookLikeButton wtr_constraintHeight:32];
[[[[_facebookLikeButton wtr_topConstraint] toSecondViewBottom:_socialShareQuestionLabel] withConstant:18] addToView:self];
_facebookLikeXConstraint = [NSLayoutConstraint constraintWithItem:_facebookLikeButton
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0];
[self addConstraint:_facebookLikeXConstraint];
}
- (void)setupSocialShareQuestionLabelConstraints {
[[[[_socialShareQuestionLabel wtr_leftConstraint] toSecondViewLeft:self] withConstant:24] addToView:self];
[[[[_socialShareQuestionLabel wtr_rightConstraint] toSecondViewRight:self] withConstant:-24] addToView:self];
[[[[_socialShareQuestionLabel wtr_topConstraint] toSecondViewBottom:_thankYouButton] withConstant:16] addToView:self];
}
- (void)setupFinalThankYouLabelConstraints {
[[[[_finalThankYouLabel wtr_topConstraint] toSecondViewTop:self] withConstant:16] addToView:self];
[[[[_finalThankYouLabel wtr_leftConstraint] toSecondViewLeft:self] withConstant:24] addToView:self];
[[[[self wtr_rightConstraint] toSecondViewRight:_finalThankYouLabel] withConstant:24] addToView:self];
}
- (void)setupThankYouButtonConstraints {
[_thankYouButton wtr_constraintHeight:50];
[[[_thankYouButton wtr_centerXConstraint] toSecondViewCenterX:self] addToView:self];
[[[[_thankYouButton wtr_leftConstraint] toSecondViewLeft:self] withConstant:24] addToView:self];
[[[[_thankYouButton wtr_rightConstraint] toSecondViewRight:self] withConstant:-24] addToView:self];
[[[[_thankYouButton wtr_topConstraint] toSecondViewBottom:_finalThankYouLabel] withConstant:38] addToView:self];
}
- (void)setupNoThanksButtonConstraints {
[[[_noThanksButton wtr_centerXConstraint] toSecondViewCenterX:self] addToView:self];
[[[[_noThanksButton wtr_bottomConstraint] toSecondViewBottom:self] withConstant:-8] addToView:self];
}
@end