-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathSendRewardManager.m
357 lines (326 loc) · 13.4 KB
/
SendRewardManager.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
//
// SendRewardManager.m
// Coding_iOS
//
// Created by Ease on 15/12/2.
// Copyright © 2015年 Coding. All rights reserved.
//
#import "SendRewardManager.h"
#import "Coding_NetAPIManager.h"
#import "Login.h"
#import "SettingPhoneViewController.h"
@interface SendRewardManager ()
@property (strong, nonatomic) Tweet *curTweet;
@property (copy, nonatomic) void(^completion)(Tweet *curTweet, BOOL sendSucess);
@property (strong, nonatomic) NSString *tipStr;
@property (strong, nonatomic) UIView *bgView, *contentView;
@property (strong, nonatomic) UIImageView *userImgV;
@property (strong, nonatomic) UIButton *closeBtn, *submitBtn, *tipBgView;
@property (strong, nonatomic) UILabel *titleL, *tipL, *bottomL;
@property (strong, nonatomic) UITextField *passwordF;
@property (strong, nonatomic) UIActivityIndicatorView *activityIndicator;
@property (assign, nonatomic) BOOL isSubmitting, isNeedPassword;
@end
@implementation SendRewardManager
+ (instancetype)shareManager{
static SendRewardManager *shared_manager = nil;
static dispatch_once_t pred;
dispatch_once(&pred, ^{
shared_manager = [[self alloc] init];
});
return shared_manager;
}
+ (instancetype)handleTweet:(Tweet *)curTweet completion:(void(^)(Tweet *curTweet, BOOL sendSucess))block{
SendRewardManager *manager = [self shareManager];
if (manager.curTweet) {//还有未处理完的冒泡,此次调用无效
return nil;
}
NSString *tipStr = nil;
User *loginUser = [Login curLoginUser];
if (curTweet.rewarded.boolValue) {
tipStr = @"您已经打赏过了";
}else if ([curTweet.owner.global_key isEqualToString:loginUser.global_key]){
tipStr = @"不可以打赏自己哟";
}else if (loginUser.points_left.floatValue < 0.01){
tipStr = @"您的余额不足";
}
if (tipStr.length > 0) {
[NSObject showHudTipStr:tipStr];
return nil;
}else{
manager.curTweet = curTweet;
manager.completion = block;
[manager p_setupWithTipStr:tipStr isNeedPassword:NO animate:NO];
[manager p_show];
return manager;
}
}
- (instancetype)init
{
self = [super init];
if (self) {
CGFloat buttonHeight = 44;
CGFloat userIconWidth = kScaleFrom_iPhone5_Desgin(50.0);
//层级关系
_bgView = [UIView new];
_contentView = [UIView new];
_closeBtn = [UIButton new];
_userImgV = [YLImageView new];
_titleL = [UILabel new];
_passwordF = [UITextField new];
_submitBtn = [UIButton buttonWithStyle:StrapSuccessStyle andTitle:@"确认打赏" andFrame:CGRectMake(0, 0, buttonHeight, buttonHeight) target:self action:@selector(submitBtnClicked)];
_activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleGray];
_bottomL = [UILabel new];
_tipBgView = [UIButton new];
[_tipBgView addTarget:self action:@selector(errorTapped) forControlEvents:UIControlEventTouchUpInside];
_tipL = [UILabel new];
[_contentView addSubview:_closeBtn];
[_contentView addSubview:_userImgV];
[_contentView addSubview:_titleL];
[_contentView addSubview:_passwordF];
[_contentView addSubview:_submitBtn];
[_contentView addSubview:_activityIndicator];
[_contentView addSubview:_bottomL];
[_contentView addSubview:_tipBgView];
[_contentView addSubview:_tipL];
[_bgView addSubview:_contentView];
//属性设置
_contentView.backgroundColor = kColorTableSectionBg;
_contentView.layer.masksToBounds = YES;
_contentView.layer.cornerRadius = 6;
[_closeBtn setImage:[UIImage imageNamed:@"button_close"] forState:UIControlStateNormal];
[_closeBtn addTarget:self action:@selector(p_dismiss) forControlEvents:UIControlEventTouchUpInside];
_userImgV.layer.masksToBounds = YES;
_userImgV.layer.cornerRadius = userIconWidth/2;
_titleL.font = [UIFont systemFontOfSize:18];
_titleL.textColor = kColor222;
_titleL.textAlignment = NSTextAlignmentCenter;
_titleL.attributedText = [self p_titleStr];
_passwordF.font = [UIFont systemFontOfSize:15];
_passwordF.textColor = kColor222;
_passwordF.secureTextEntry = YES;
_passwordF.textAlignment = NSTextAlignmentCenter;
[_passwordF doBorderWidth:1.0 color:kColorCCC cornerRadius:2.0];
_passwordF.placeholder = @" 请输入密码";
_passwordF.alpha = 0;
_bottomL.font = [UIFont systemFontOfSize:12];
_bottomL.textColor = kColor999;
_bottomL.textAlignment = NSTextAlignmentCenter;
_tipBgView.backgroundColor = [UIColor colorWithHexString:@"0xF2DEDE"];
_tipBgView.layer.masksToBounds = YES;
_tipBgView.layer.cornerRadius = 3;
_tipL.font = [UIFont systemFontOfSize:15];
_tipL.textColor = [UIColor colorWithHexString:@"0xC55351"];
_tipL.textAlignment = NSTextAlignmentCenter;
_tipL.numberOfLines = 0;
//位置大小
//align top
[_closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.equalTo(_contentView);
make.width.height.mas_equalTo(buttonHeight);
}];
[_userImgV mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(_contentView).offset(25);
make.centerX.equalTo(_contentView);
make.height.width.mas_equalTo(userIconWidth);
}];
[_titleL mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(_contentView);
make.top.equalTo(_userImgV.mas_bottom).offset(30);
}];
//align bottom
[_bottomL mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(_contentView);
make.bottom.equalTo(_contentView).offset(-15);
make.height.mas_equalTo(15);
}];
[_tipBgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(_bottomL.mas_top).offset(-15);
make.left.equalTo(_contentView).offset(25);
make.right.equalTo(_contentView).offset(-25);
make.height.mas_equalTo(60);
}];
[_tipL mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(_tipBgView).insets(UIEdgeInsetsMake(10, 15, 10, 15));
}];
[_submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(_tipBgView);
make.left.equalTo(_contentView).offset(50);
make.right.equalTo(_contentView).offset(-50);
make.height.mas_equalTo(buttonHeight);
}];
[_activityIndicator mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(_submitBtn);
}];
[_passwordF mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(_submitBtn.mas_top).offset(-30);
make.left.equalTo(_contentView).offset(25);
make.right.equalTo(_contentView).offset(-25);
make.height.mas_equalTo(35);
}];
//关联事件
[_passwordF.rac_textSignal subscribeNext:^(NSString *password) {
if (_isNeedPassword) {
self.submitBtn.enabled = password.length > 0;
}
}];
[_bgView bk_whenTapped:^{//在不需要密码的时候,tap 就消失
if (!_isNeedPassword) {
[self p_dismiss];
}else{
[self.passwordF resignFirstResponder];
}
}];
}
return self;
}
- (NSAttributedString *)p_titleStr{
NSString *tempStr = @"打赏给该用户 0.01 码币";
NSMutableAttributedString *titleStr = [[NSMutableAttributedString alloc] initWithString:tempStr];
[titleStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"0xF5A623"] range:[tempStr rangeOfString:@"0.01"]];
return titleStr;
}
- (NSAttributedString *)p_bottomStr{
NSString *tempStr = [NSString stringWithFormat:@"我的码币余额:%.2f ", [Login curLoginUser].points_left.floatValue];
NSMutableAttributedString *bottomStr = [[NSMutableAttributedString alloc] initWithString:tempStr];
[bottomStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"0xF5A623"] range:[tempStr rangeOfString:[Login curLoginUser].points_left.stringValue]];
return bottomStr;
}
- (void)setCurTweet:(Tweet *)curTweet{
_curTweet = curTweet;
CGFloat userIconWidthDesgin = 50.0;
[_userImgV sd_setImageWithURL:[_curTweet.owner.avatar urlImageWithCodePathResize:userIconWidthDesgin * [UIScreen mainScreen].scale crop:NO] placeholderImage:kPlaceholderMonkeyRoundWidth(userIconWidthDesgin)];
}
- (void)setIsSubmitting:(BOOL)isSubmitting{
_isSubmitting = isSubmitting;
if (_isSubmitting) {
_passwordF.userInteractionEnabled = NO;
_submitBtn.enabled = NO;
[_activityIndicator startAnimating];
}else{
_passwordF.userInteractionEnabled = YES;
[_activityIndicator stopAnimating];
_submitBtn.enabled = YES;
}
}
- (void)submitBtnClicked{
self.isSubmitting = YES;
NSString *encodedPassword = [_passwordF.text sha1Str];
[[Coding_NetAPIManager sharedManager] request_RewardToTweet:_curTweet.id.stringValue encodedPassword:encodedPassword andBlock:^(id data, NSError *error) {
self.isSubmitting = NO;
if (data) {
[NSObject showHudTipStr:@"打赏成功"];
[self p_sucessDone];
}else{
[self p_handleError:error];
}
}];
}
- (void)errorTapped{
if ([_tipStr isEqualToString:@"验证了手机才能打赏哦"]) {
SettingPhoneViewController *vc = [SettingPhoneViewController new];
[[BaseViewController presentingVC].navigationController pushViewController:vc animated:YES];
}
[self p_dismiss];
}
- (void)p_setupWithTipStr:(NSString *)tipStr isNeedPassword:(BOOL)isNeedPassword animate:(BOOL)animate{
_tipStr = tipStr;
_isNeedPassword = isNeedPassword;
BOOL hasTip = _tipStr.length > 0;
_tipL.text = [_tipStr stringByRemoveHtmlTag];
_tipBgView.hidden = !hasTip;
_tipL.hidden = !hasTip;
_submitBtn.hidden = hasTip;
CGFloat contentHeight = 255 + (kScaleFrom_iPhone5_Desgin(50) - 50);
contentHeight += _isNeedPassword? 40: 0;
CGFloat contentY = (kScreen_Height - contentHeight)/2;
contentY += _isNeedPassword? -40: 0;
CGRect contentFrame = CGRectMake(25, contentY, kScreen_Width - 50, contentHeight);
if (animate) {
[UIView animateWithDuration:0.3 animations:^{
_contentView.frame = contentFrame;
_passwordF.alpha = _isNeedPassword? 1: 0;
} completion:^(BOOL finished) {
if (_isNeedPassword) {
[_passwordF becomeFirstResponder];
}else{
[_passwordF resignFirstResponder];
}
}];
}else{
_contentView.frame = contentFrame;
_passwordF.alpha = _isNeedPassword? 1: 0;
}
}
- (void)p_show{
//初始状态
_bgView.backgroundColor = [UIColor clearColor];
_contentView.alpha = 0;
_passwordF.text = @"";
_submitBtn.enabled = YES;
_bgView.frame = kScreen_Bounds;
_bottomL.attributedText = [self p_bottomStr];
@weakify(self);
[[CodingNetAPIClient sharedJsonClient] requestJsonDataWithPath:@"api/account/points" withParams:nil withMethodType:Get andBlock:^(id data, NSError *error) {
@strongify(self);
if (data) {
[Login curLoginUser].points_left = data[@"data"][@"points_left"];
self.bottomL.attributedText = [self p_bottomStr];
}
}];
[kKeyWindow addSubview:_bgView];
[UIView animateWithDuration:0.3 animations:^{
_bgView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
_contentView.alpha = 1;
} completion:^(BOOL finished) {
if (_isNeedPassword) {
[_passwordF becomeFirstResponder];
}
}];
}
- (void)p_dismiss{
_curTweet = nil;
_completion = nil;
[_passwordF resignFirstResponder];
[UIView animateWithDuration:0.3 animations:^{
_bgView.backgroundColor = [UIColor clearColor];
_contentView.alpha = 0;
} completion:^(BOOL finished) {
[_bgView removeFromSuperview];
}];
}
- (void)p_sucessDone{
User *loginUser = [Login curLoginUser];
loginUser.points_left = @(loginUser.points_left.floatValue - 0.01);
_curTweet.rewarded = @(YES);
_curTweet.rewards = @(_curTweet.rewards.integerValue +1);
if (_curTweet.reward_users.count > 0) {
[_curTweet.reward_users insertObject:[Login curLoginUser] atIndex:0];
}else{
_curTweet.reward_users = @[[Login curLoginUser]].mutableCopy;
}
if (self.completion) {
self.completion(_curTweet, YES);
}
[self p_dismiss];
}
- (void)p_handleError:(NSError *)error{
NSDictionary *userInfo = error.userInfo;
NSArray *errorKeyList = [userInfo[@"msg"] allKeys];
NSString *errorMsg = userInfo[@"msg"][errorKeyList.firstObject];
if ([errorKeyList containsObject:@"password_error"]) {
if (!_isNeedPassword) {
[self p_setupWithTipStr:nil isNeedPassword:YES animate:YES];
}else{
[self.passwordF becomeFirstResponder];
}
if (errorMsg.length > 0) {
[NSObject showHudTipStr:errorMsg];
}
}else{
if (errorMsg.length > 0) {
[self p_setupWithTipStr:errorMsg isNeedPassword:_isNeedPassword animate:YES];
}
}
}
@end