1313@interface SendRewardManager ()
1414@property (strong , nonatomic ) Tweet *curTweet;
1515@property (copy , nonatomic ) void (^completion)(Tweet *curTweet, BOOL sendSucess);
16- @property (strong , nonatomic ) NSArray *tipStrList ;
16+ @property (strong , nonatomic ) NSString *tipStr ;
1717
1818
1919@property (strong , nonatomic ) UIView *bgView, *contentView, *tipBgView;
20+ @property (strong , nonatomic ) UIImageView *userImgV;
2021@property (strong , nonatomic ) UIButton *closeBtn, *submitBtn;
2122@property (strong , nonatomic ) UILabel *titleL, *tipL, *bottomL;
2223@property (strong , nonatomic ) UITextField *passwordF;
2324@property (strong , nonatomic ) UIActivityIndicatorView *activityIndicator;
24- @property (assign , nonatomic ) BOOL isSubmitting;
25+ @property (assign , nonatomic ) BOOL isSubmitting, isNeedPassword ;
2526@end
2627
2728@implementation SendRewardManager
@@ -35,36 +36,43 @@ + (instancetype)shareManager{
3536}
3637
3738+ (instancetype )handleTweet : (Tweet *)curTweet completion : (void (^)(Tweet *curTweet, BOOL sendSucess))block {
38- if (curTweet.rewarded .boolValue ) {
39- [NSObject showHudTipStr: @" 您已经打赏过了" ];
40- return nil ;
41- }else if ([curTweet.owner.global_key isEqualToString: [Login curLoginUser ].global_key]){
42- [NSObject showHudTipStr: @" 不可以打赏自己哟" ];
43- return nil ;
44- }
4539 SendRewardManager *manager = [self shareManager ];
4640 if (manager.curTweet ) {// 还有未处理完的冒泡,此次调用无效
4741 return nil ;
4842 }
49- manager.curTweet = curTweet;
50- manager.completion = block;
51- [[Coding_NetAPIManager sharedManager ] request_Preparereward: curTweet.id .stringValue andBlock: ^(id data, NSError *error) {
52- manager.tipStrList = data;
43+
44+ NSString *tipStr = nil ;
45+ User *loginUser = [Login curLoginUser ];
46+ if (curTweet.rewarded .boolValue ) {
47+ tipStr = @" 您已经打赏过了" ;
48+ }else if ([curTweet.owner.global_key isEqualToString: loginUser.global_key]){
49+ tipStr = @" 不可以打赏自己哟" ;
50+ }else if (loginUser.points_left .floatValue < 0.01 ){
51+ tipStr = @" 您的余额不足" ;
52+ }
53+ if (tipStr.length > 0 ) {
54+ [NSObject showHudTipStr: tipStr];
55+ return nil ;
56+ }else {
57+ manager.curTweet = curTweet;
58+ manager.completion = block;
59+ [manager p_setupWithTipStr: tipStr isNeedPassword: NO animate: NO ];
5360 [manager p_show ];
54- }] ;
55- return manager;
61+ return manager ;
62+ }
5663}
5764
5865- (instancetype )init
5966{
6067 self = [super init ];
6168 if (self) {
6269 CGFloat buttonHeight = 44 ;
63-
70+ CGFloat userIconWidth = kScaleFrom_iPhone5_Desgin ( 50.0 );
6471 // 层级关系
6572 _bgView = [UIView new ];
6673 _contentView = [UIView new ];
6774 _closeBtn = [UIButton new ];
75+ _userImgV = [UIImageView new ];
6876 _titleL = [UILabel new ];
6977 _passwordF = [UITextField new ];
7078 _submitBtn = [UIButton buttonWithStyle: StrapSuccessStyle andTitle: @" 确认打赏" andFrame: CGRectMake (0 , 0 , buttonHeight, buttonHeight) target: self action: @selector (submitBtnClicked )];
@@ -73,6 +81,7 @@ - (instancetype)init
7381 _tipBgView = [UIView new ];
7482 _tipL = [UILabel new ];
7583 [_contentView addSubview: _closeBtn];
84+ [_contentView addSubview: _userImgV];
7685 [_contentView addSubview: _titleL];
7786 [_contentView addSubview: _passwordF];
7887 [_contentView addSubview: _submitBtn];
@@ -88,6 +97,8 @@ - (instancetype)init
8897 _contentView.layer .cornerRadius = 6 ;
8998 [_closeBtn setImage: [UIImage imageNamed: @" button_close" ] forState: UIControlStateNormal];
9099 [_closeBtn addTarget: self action: @selector (p_dismiss ) forControlEvents: UIControlEventTouchUpInside];
100+ _userImgV.layer .masksToBounds = YES ;
101+ _userImgV.layer .cornerRadius = userIconWidth/2 ;
91102 _titleL.font = [UIFont systemFontOfSize: 18 ];
92103 _titleL.textColor = [UIColor colorWithHexString: @" 0x222222" ];
93104 _titleL.textAlignment = NSTextAlignmentCenter;
@@ -98,10 +109,10 @@ - (instancetype)init
98109 _passwordF.textAlignment = NSTextAlignmentCenter;
99110 [_passwordF doBorderWidth: 1.0 color: [UIColor colorWithHexString: @" 0xCCCCCC" ] cornerRadius: 2.0 ];
100111 _passwordF.placeholder = @" 请输入密码" ;
112+ _passwordF.alpha = 0 ;
101113 _bottomL.font = [UIFont systemFontOfSize: 12 ];
102114 _bottomL.textColor = [UIColor colorWithHexString: @" 0x999999" ];
103115 _bottomL.textAlignment = NSTextAlignmentCenter;
104- _bottomL.attributedText = [self p_bottomStr ];
105116 _tipBgView.backgroundColor = [UIColor colorWithHexString: @" 0xF2DEDE" ];
106117 _tipBgView.layer .masksToBounds = YES ;
107118 _tipBgView.layer .cornerRadius = 3 ;
@@ -111,53 +122,64 @@ - (instancetype)init
111122 _tipL.numberOfLines = 0 ;
112123
113124 // 位置大小
125+ // align top
114126 [_closeBtn mas_makeConstraints: ^(MASConstraintMaker *make) {
115127 make.left .top .equalTo (_contentView);
116128 make.width .height .mas_equalTo (buttonHeight);
117129 }];
130+ [_userImgV mas_makeConstraints: ^(MASConstraintMaker *make) {
131+ make.top .equalTo (_contentView).offset (25 );
132+ make.centerX .equalTo (_contentView);
133+ make.height .width .mas_equalTo (userIconWidth);
134+ }];
118135 [_titleL mas_makeConstraints: ^(MASConstraintMaker *make) {
119136 make.left .right .equalTo (_contentView);
120- make.top .equalTo (_contentView ).offset (35 );
137+ make.top .equalTo (_userImgV. mas_bottom ).offset (30 );
121138 }];
122- [_passwordF mas_makeConstraints: ^(MASConstraintMaker *make) {
123- make.top .equalTo (_titleL.mas_bottom ).offset (20 );
139+ // align bottom
140+ [_bottomL mas_makeConstraints: ^(MASConstraintMaker *make) {
141+ make.left .right .equalTo (_contentView);
142+ make.bottom .equalTo (_contentView).offset (-15 );
143+ make.height .mas_equalTo (15 );
144+ }];
145+ [_tipBgView mas_makeConstraints: ^(MASConstraintMaker *make) {
146+ make.bottom .equalTo (_bottomL.mas_top ).offset (-15 );
124147 make.left .equalTo (_contentView).offset (25 );
125148 make.right .equalTo (_contentView).offset (-25 );
126- make.height .mas_equalTo (35 );
149+ make.height .mas_equalTo (60 );
150+ }];
151+ [_tipL mas_makeConstraints: ^(MASConstraintMaker *make) {
152+ make.edges .equalTo (_tipBgView).insets (UIEdgeInsetsMake (10 , 15 , 10 , 15 ));
127153 }];
128154 [_submitBtn mas_makeConstraints: ^(MASConstraintMaker *make) {
129- make.top .equalTo (_passwordF. mas_bottom ). offset ( 25 );
155+ make.centerY .equalTo (_tipBgView );
130156 make.left .equalTo (_contentView).offset (50 );
131157 make.right .equalTo (_contentView).offset (-50 );
132158 make.height .mas_equalTo (buttonHeight);
133159 }];
134160 [_activityIndicator mas_makeConstraints: ^(MASConstraintMaker *make) {
135161 make.center .equalTo (_submitBtn);
136162 }];
137- [_tipBgView mas_makeConstraints: ^(MASConstraintMaker *make) {
138- make.top .equalTo (_titleL. mas_bottom ).offset (20 );
163+ [_passwordF mas_makeConstraints: ^(MASConstraintMaker *make) {
164+ make.bottom .equalTo (_submitBtn. mas_top ).offset (- 30 );
139165 make.left .equalTo (_contentView).offset (25 );
140166 make.right .equalTo (_contentView).offset (-25 );
141- make.height .mas_equalTo (60 );
167+ make.height .mas_equalTo (35 );
142168 }];
143- [_tipL mas_makeConstraints: ^(MASConstraintMaker *make) {
144- make.edges .equalTo (_tipBgView).insets (UIEdgeInsetsMake (10 , 15 , 10 , 15 ));
169+
170+ // 关联事件
171+ [_passwordF.rac_textSignal subscribeNext: ^(NSString *password) {
172+ if (_isNeedPassword) {
173+ self.submitBtn .enabled = password.length > 0 ;
174+ }
145175 }];
146176 [_bgView bk_whenTapped: ^{// 在不能打赏的时候,tap 就消失
147- if (self.tipStrList . count > 0 ) {
177+ if (self.tipStr . length > 0 ) {
148178 [self p_dismiss ];
179+ }else {
180+ [self .passwordF resignFirstResponder ];
149181 }
150182 }];
151- [_bottomL mas_makeConstraints: ^(MASConstraintMaker *make) {
152- make.left .right .equalTo (_contentView);
153- make.bottom .equalTo (_contentView).offset (-15 );
154- make.height .mas_equalTo (15 );
155- }];
156-
157- // 关联事件
158- [_passwordF.rac_textSignal subscribeNext: ^(NSString *password) {
159- self.submitBtn .enabled = password.length > 0 ;
160- }];
161183 }
162184 return self;
163185}
@@ -177,27 +199,11 @@ - (NSAttributedString *)p_bottomStr{
177199 return bottomStr;
178200}
179201
180- - (void )setTipStrList : (NSArray *)tipStrList {
181- _tipStrList = tipStrList;
182-
183- BOOL hasTip = _tipStrList.count > 0 ;
184- if (hasTip) {
185- _tipL.text = tipStrList.firstObject ;
186- _tipL.text = [_tipL.text stringByRemoveHtmlTag ];
187- }
188- CGFloat contentHeight = hasTip? 185 : 229 ;
189- CGFloat centerYOffset = hasTip? 0 : -60 ;
190- _passwordF.hidden = hasTip;
191- _submitBtn.hidden = hasTip;
192- _tipBgView.hidden = !hasTip;
193- _tipL.hidden = !hasTip;
202+ - (void )setCurTweet : (Tweet *)curTweet {
203+ _curTweet = curTweet;
194204
195- [_contentView mas_remakeConstraints: ^(MASConstraintMaker *make) {
196- make.left .equalTo (_bgView).offset (kPaddingLeftWidth );
197- make.height .mas_equalTo (contentHeight);
198- make.centerX .equalTo (_bgView);
199- make.centerY .equalTo (_bgView).offset (centerYOffset);
200- }];
205+ CGFloat userIconWidthDesgin = 50.0 ;
206+ [_userImgV sd_setImageWithURL: [_curTweet.owner.avatar urlImageWithCodePathResize: userIconWidthDesgin * [UIScreen mainScreen ].scale crop: YES ] placeholderImage: kPlaceholderMonkeyRoundWidth (userIconWidthDesgin)];
201207}
202208
203209- (void )setIsSubmitting : (BOOL )isSubmitting {
@@ -222,25 +228,59 @@ - (void)submitBtnClicked{
222228 [NSObject showHudTipStr: @" 打赏成功" ];
223229 [self p_sucessDone ];
224230 }else {
225- [self .passwordF becomeFirstResponder ];
231+ [self p_handleError: error ];
226232 }
227233 }];
228234}
229235
236+ - (void )p_setupWithTipStr : (NSString *)tipStr isNeedPassword : (BOOL )isNeedPassword animate : (BOOL )animate {
237+ _tipStr = tipStr;
238+ _isNeedPassword = isNeedPassword;
239+
240+ BOOL hasTip = _tipStr.length > 0 ;
241+ _tipL.text = [_tipStr stringByRemoveHtmlTag ];
242+ _tipBgView.hidden = !hasTip;
243+ _tipL.hidden = !hasTip;
244+ _submitBtn.hidden = hasTip;
245+
246+ CGFloat contentHeight = 255 + (kScaleFrom_iPhone5_Desgin (50 ) - 50 );
247+ contentHeight += _isNeedPassword? 40 : 0 ;
248+ CGFloat contentY = (kScreen_Height - contentHeight)/2 ;
249+ contentY += _isNeedPassword? -40 : 0 ;
250+
251+ CGRect contentFrame = CGRectMake (kPaddingLeftWidth , contentY, kScreen_Width - 2 *kPaddingLeftWidth , contentHeight);
252+ if (animate) {
253+ [UIView animateWithDuration: 0.3 animations: ^{
254+ _contentView.frame = contentFrame;
255+ _passwordF.alpha = _isNeedPassword? 1 : 0 ;
256+ } completion: ^(BOOL finished) {
257+ if (_isNeedPassword) {
258+ [_passwordF becomeFirstResponder ];
259+ }else {
260+ [_passwordF resignFirstResponder ];
261+ }
262+ }];
263+ }else {
264+ _contentView.frame = contentFrame;
265+ _passwordF.alpha = _isNeedPassword? 1 : 0 ;
266+ }
267+ }
268+
230269- (void )p_show {
231270 // 初始状态
232271 _bgView.backgroundColor = [UIColor clearColor ];
233272 _contentView.alpha = 0 ;
234273 _passwordF.text = @" " ;
235- _submitBtn.enabled = NO ;
274+ _bottomL.attributedText = [self p_bottomStr ];
275+ _submitBtn.enabled = YES ;
236276 _bgView.frame = kScreen_Bounds ;
237277
238278 [kKeyWindow addSubview: _bgView];
239279 [UIView animateWithDuration: 0.3 animations: ^{
240280 _bgView.backgroundColor = [UIColor colorWithWhite: 0 alpha: 0.6 ];
241281 _contentView.alpha = 1 ;
242282 } completion: ^(BOOL finished) {
243- if (!_passwordF. hidden ) {
283+ if (_isNeedPassword ) {
244284 [_passwordF becomeFirstResponder ];
245285 }
246286 }];
@@ -272,4 +312,24 @@ - (void)p_sucessDone{
272312 [self p_dismiss ];
273313}
274314
315+ - (void )p_handleError : (NSError *)error {
316+ NSDictionary *userInfo = error.userInfo ;
317+ NSArray *errorKeyList = [userInfo[@" msg" ] allKeys ];
318+ NSString *errorMsg = userInfo[@" msg" ][errorKeyList.firstObject];
319+ if ([errorKeyList containsObject: @" password_error" ]) {
320+ if (!_isNeedPassword) {
321+ [self p_setupWithTipStr: nil isNeedPassword: YES animate: YES ];
322+ }else {
323+ [self .passwordF becomeFirstResponder ];
324+ }
325+ if (errorMsg.length > 0 ) {
326+ [NSObject showHudTipStr: errorMsg];
327+ }
328+ }else {
329+ if (errorMsg.length > 0 ) {
330+ [self p_setupWithTipStr: errorMsg isNeedPassword: _isNeedPassword animate: YES ];
331+ }
332+ }
333+ }
334+
275335@end
0 commit comments