单军华
2018-07-12 3e8437ae559487362fae3525beb79c534c213a51
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
//
//  JXTAlertController.m
//  JXTAlertManager
//
//  Created by JXT on 2016/12/22.
//  Copyright © 2016年 JXT. All rights reserved.
//
 
#import "JXTAlertController.h"
#import "UIWindow+CurrentViewController.h"
 
//toast默认展示时间
static NSTimeInterval const JXTAlertShowDurationDefault = 1.0f;
 
 
#pragma mark - I.AlertActionModel
@interface JXTAlertActionModel : NSObject
@property (nonatomic, copy) NSString * title;
@property (nonatomic, assign) UIAlertActionStyle style;
@end
@implementation JXTAlertActionModel
- (instancetype)init
{
    if (self = [super init]) {
        self.title = @"";
        self.style = UIAlertActionStyleDefault;
    }
    return self;
}
@end
 
 
 
#pragma mark - II.JXTAlertController
/**
 AlertActions配置
 
 @param actionBlock JXTAlertActionBlock
 */
typedef void (^JXTAlertActionsConfig)(JXTAlertActionBlock actionBlock);
 
 
@interface JXTAlertController ()
//JXTAlertActionModel数组
@property (nonatomic, strong) NSMutableArray <JXTAlertActionModel *>* jxt_alertActionArray;
//是否操作动画
@property (nonatomic, assign) BOOL jxt_setAlertAnimated;
//action配置
- (JXTAlertActionsConfig)alertActionsConfig;
@end
 
@implementation JXTAlertController
 
- (void)viewDidLoad {
    [super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    if (self.alertDidDismiss) {
        self.alertDidDismiss();
    }
}
- (void)dealloc
{
    NSLogFunc;
}
 
#pragma mark - Private
//action-title数组
- (NSMutableArray<JXTAlertActionModel *> *)jxt_alertActionArray
{
    if (_jxt_alertActionArray == nil) {
        _jxt_alertActionArray = [NSMutableArray array];
    }
    return _jxt_alertActionArray;
}
//action配置
- (JXTAlertActionsConfig)alertActionsConfig
{
    return ^(JXTAlertActionBlock actionBlock) {
        if (self.jxt_alertActionArray.count > 0)
        {
            //创建action
            __weak typeof(self)weakSelf = self;
            
            [self.jxt_alertActionArray enumerateObjectsUsingBlock:^(JXTAlertActionModel *actionModel, NSUInteger idx, BOOL * _Nonnull stop) {
                UIAlertAction *alertAction = [UIAlertAction actionWithTitle:actionModel.title style:actionModel.style handler:^(UIAlertAction * _Nonnull action) {
                    __strong typeof(weakSelf)strongSelf = weakSelf;
                    if (actionBlock) {
                        actionBlock(idx, action, strongSelf);
                    }
                }];
                //可利用这个改变字体颜色,但是不推荐!!!
//                [alertAction setValue:[UIColor grayColor] forKey:@"titleTextColor"];
                //action作为self元素,其block实现如果引用本类指针,会造成循环引用
                [weakSelf addAction:alertAction];
            }];
        }
        else
        {
            NSTimeInterval duration = self.toastStyleDuration > 0 ? self.toastStyleDuration : JXTAlertShowDurationDefault;
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self dismissViewControllerAnimated:!(self.jxt_setAlertAnimated) completion:NULL];
            });
        }
    };
}
 
#pragma mark - Public
 
- (instancetype)initAlertControllerWithTitle:(NSString *)title message:(NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle
{
    if (!(title.length > 0) && (message.length > 0) && (preferredStyle == UIAlertControllerStyleAlert)) {
        title = @"";
    }
    
    self = [[self class] alertControllerWithTitle:title message:message preferredStyle:preferredStyle];
    
    if (!self) return nil;
    
    self.jxt_setAlertAnimated = NO;
    self.toastStyleDuration = JXTAlertShowDurationDefault;
    
    return self;
}
 
 
- (void)alertAnimateDisabled
{
    self.jxt_setAlertAnimated = YES;
}
 
 
#pragma mark - addButton
 
- (JXTAlertActionTitle)addActionDefaultTitle
{
    //该block返回值不是本类属性,只是局部变量,不会造成循环引用
    return ^(NSString *title) {
        JXTAlertActionModel *actionModel = [[JXTAlertActionModel alloc] init];
        actionModel.title = title;
        actionModel.style = UIAlertActionStyleDefault;
        [self.jxt_alertActionArray addObject:actionModel];
        return self;
    };
}
 
- (JXTAlertActionTitle)addActionCancelTitle
{
    return ^(NSString *title) {
        JXTAlertActionModel *actionModel = [[JXTAlertActionModel alloc] init];
        actionModel.title = title;
        actionModel.style = UIAlertActionStyleCancel;
        [self.jxt_alertActionArray addObject:actionModel];
        return self;
    };
}
 
- (JXTAlertActionTitle)addActionDestructiveTitle
{
    return ^(NSString *title) {
        JXTAlertActionModel *actionModel = [[JXTAlertActionModel alloc] init];
        actionModel.title = title;
        actionModel.style = UIAlertActionStyleDestructive;
        [self.jxt_alertActionArray addObject:actionModel];
        return self;
    };
}
 
@end
 
 
 
#pragma mark - III.UIViewController扩展
@implementation UIViewController (JXTAlertController)
 
- (void)jxt_showAlertWithPreferredStyle:(UIAlertControllerStyle)preferredStyle title:(NSString *)title message:(NSString *)message appearanceProcess:(JXTAlertAppearanceProcess)appearanceProcess actionsBlock:(JXTAlertActionBlock)actionBlock
{
    if (appearanceProcess)
    {
        JXTAlertController *alertMaker = [[JXTAlertController alloc] initAlertControllerWithTitle:title message:message preferredStyle:preferredStyle];
        //防止nil
        if (!alertMaker) {
            return ;
        }
        //加工链
        !appearanceProcess ?: appearanceProcess(alertMaker);
        //配置响应
        alertMaker.alertActionsConfig(actionBlock);
 
        [self presentViewController:alertMaker animated:!(alertMaker.jxt_setAlertAnimated) completion:^{
            !alertMaker.alertDidShown ?: alertMaker.alertDidShown();
        }];
    }
}
 
- (void)jxt_showAlertWithTitle:(NSString *)title message:(NSString *)message appearanceProcess:(JXTAlertAppearanceProcess)appearanceProcess actionsBlock:(JXTAlertActionBlock)actionBlock
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self jxt_showAlertWithPreferredStyle:UIAlertControllerStyleAlert title:title message:message appearanceProcess:appearanceProcess actionsBlock:actionBlock];
    });
}
 
- (void)jxt_showActionSheetWithTitle:(NSString *)title message:(NSString *)message appearanceProcess:(JXTAlertAppearanceProcess)appearanceProcess actionsBlock:(JXTAlertActionBlock)actionBlock
{
    dispatch_async(dispatch_get_main_queue(), ^{
    [self jxt_showAlertWithPreferredStyle:UIAlertControllerStyleActionSheet title:title message:message appearanceProcess:appearanceProcess actionsBlock:actionBlock];
    });
}
 
 
@end
 
@implementation UIAlertController (LMJ)
 
/**
 JXTAlertController: show-alert(iOS8)
 
 @param title             title
 @param message           message
 @param appearanceProcess alert配置过程
 @param actionBlock       alert点击响应回调
 */
+ (void)mj_showAlertWithTitle:(nullable NSString *)title
                      message:(nullable NSString *)message
            appearanceProcess:(JXTAlertAppearanceProcess)appearanceProcess
                 actionsBlock:(nullable JXTAlertActionBlock)actionBlock NS_AVAILABLE_IOS(8_0)
{
    [[UIWindow zf_currentViewController] jxt_showAlertWithTitle:title message:message appearanceProcess:appearanceProcess actionsBlock:actionBlock];
}
 
/**
 JXTAlertController: show-actionSheet(iOS8)
 
 @param title             title
 @param message           message
 @param appearanceProcess actionSheet配置过程
 @param actionBlock       actionSheet点击响应回调
 */
+ (void)mj_showActionSheetWithTitle:(nullable NSString *)title
                            message:(nullable NSString *)message
                  appearanceProcess:(JXTAlertAppearanceProcess)appearanceProcess
                       actionsBlock:(nullable JXTAlertActionBlock)actionBlock NS_AVAILABLE_IOS(8_0)
{
    [[UIWindow zf_currentViewController] jxt_showActionSheetWithTitle:title message:message appearanceProcess:appearanceProcess actionsBlock:actionBlock];
}
@end