-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathFLEXAlert.m
More file actions
265 lines (221 loc) · 7.76 KB
/
FLEXAlert.m
File metadata and controls
265 lines (221 loc) · 7.76 KB
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
//
// FLEXAlert.m
// FLEX
//
// Created by Tanner Bennett on 8/20/19.
// Copyright © 2020 FLEX Team. All rights reserved.
//
#import "FLEXAlert.h"
#import "FLEXMacros.h"
@interface FLEXAlert ()
@property (nonatomic, readonly) UIAlertController *_controller;
@property (nonatomic, readonly) NSMutableArray<FLEXAlertAction *> *_actions;
@end
#define FLEXAlertActionMutationAssertion() \
NSAssert(!self._action, @"在获取底层 UIAlertAction 后无法更改操作");
@interface FLEXAlertAction ()
@property (nonatomic) UIAlertController *_controller;
@property (nonatomic) NSString *_title;
@property (nonatomic) UIAlertActionStyle _style;
@property (nonatomic) BOOL _disable;
@property (nonatomic) BOOL _isPreferred;
@property (nonatomic) void(^_handler)(UIAlertAction *action);
@property (nonatomic) UIAlertAction *_action;
@end
@implementation FLEXAlert
+ (void)showAlert:(NSString *)title message:(NSString *)message from:(UIViewController *)viewController {
[self makeAlert:^(FLEXAlert *make) {
make.title(title).message(message).button(@"关闭").cancelStyle();
} showFrom:viewController];
}
+ (void)showQuickAlert:(NSString *)title from:(UIViewController *)viewController {
UIAlertController *alert = [self makeAlert:^(FLEXAlert *make) {
make.title(title);
}];
[viewController presentViewController:alert animated:YES completion:^{
flex_dispatch_after(0.5, dispatch_get_main_queue(), ^{
[alert dismissViewControllerAnimated:YES completion:nil];
});
}];
}
#pragma mark Initialization
- (instancetype)initWithController:(UIAlertController *)controller {
self = [super init];
if (self) {
__controller = controller;
__actions = [NSMutableArray new];
}
return self;
}
+ (UIAlertController *)make:(FLEXAlertBuilder)block withStyle:(UIAlertControllerStyle)style {
// 创建警告构建器
FLEXAlert *alert = [[self alloc] initWithController:
[UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:style]
];
// 配置警告
block(alert);
// 添加操作
for (FLEXAlertAction *builder in alert._actions) {
[alert._controller addAction:builder.action];
}
UIAlertController *controller = alert._controller;
// 在警告控制器上设置首选操作
for (FLEXAlertAction *builder in alert._actions) {
UIAlertAction *action = builder.action;
if (builder._isPreferred) {
controller.preferredAction = action;
break;
}
}
return controller;
}
+ (void)make:(FLEXAlertBuilder)block
withStyle:(UIAlertControllerStyle)style
showFrom:(UIViewController *)viewController
source:(id)viewOrBarItem {
UIAlertController *alert = [self make:block withStyle:style];
if ([viewOrBarItem isKindOfClass:[UIBarButtonItem class]]) {
alert.popoverPresentationController.barButtonItem = viewOrBarItem;
} else if ([viewOrBarItem isKindOfClass:[UIView class]]) {
alert.popoverPresentationController.sourceView = viewOrBarItem;
alert.popoverPresentationController.sourceRect = [viewOrBarItem bounds];
} else if (viewOrBarItem) {
NSParameterAssert(
[viewOrBarItem isKindOfClass:[UIBarButtonItem class]] ||
[viewOrBarItem isKindOfClass:[UIView class]] ||
!viewOrBarItem
);
}
[viewController presentViewController:alert animated:YES completion:nil];
}
+ (void)makeAlert:(FLEXAlertBuilder)block showFrom:(UIViewController *)controller {
[self make:block withStyle:UIAlertControllerStyleAlert showFrom:controller source:nil];
}
+ (void)makeSheet:(FLEXAlertBuilder)block showFrom:(UIViewController *)controller {
[self make:block withStyle:UIAlertControllerStyleActionSheet showFrom:controller source:nil];
}
/// 构建并显示一个操作表样式的警告
+ (void)makeSheet:(FLEXAlertBuilder)block
showFrom:(UIViewController *)controller
source:(id)viewOrBarItem {
[self make:block
withStyle:UIAlertControllerStyleActionSheet
showFrom:controller
source:viewOrBarItem];
}
+ (UIAlertController *)makeAlert:(FLEXAlertBuilder)block {
return [self make:block withStyle:UIAlertControllerStyleAlert];
}
+ (UIAlertController *)makeSheet:(FLEXAlertBuilder)block {
return [self make:block withStyle:UIAlertControllerStyleActionSheet];
}
#pragma mark Configuration
- (FLEXAlertStringProperty)title {
return ^FLEXAlert *(NSString *title) {
if (self._controller.title) {
self._controller.title = [self._controller.title stringByAppendingString:title ?: @""];
} else {
self._controller.title = title;
}
return self;
};
}
- (FLEXAlertStringProperty)message {
return ^FLEXAlert *(NSString *message) {
if (self._controller.message) {
self._controller.message = [self._controller.message stringByAppendingString:message ?: @""];
} else {
self._controller.message = message;
}
return self;
};
}
- (FLEXAlertAddAction)button {
return ^FLEXAlertAction *(NSString *title) {
FLEXAlertAction *action = FLEXAlertAction.new.title(title);
action._controller = self._controller;
[self._actions addObject:action];
return action;
};
}
- (FLEXAlertStringArg)textField {
return ^FLEXAlert *(NSString *placeholder) {
[self._controller addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = placeholder;
}];
return self;
};
}
- (FLEXAlertTextField)configuredTextField {
return ^FLEXAlert *(void(^configurationHandler)(UITextField *)) {
[self._controller addTextFieldWithConfigurationHandler:configurationHandler];
return self;
};
}
@end
@implementation FLEXAlertAction
- (FLEXAlertActionStringProperty)title {
return ^FLEXAlertAction *(NSString *title) {
FLEXAlertActionMutationAssertion();
if (self._title) {
self._title = [self._title stringByAppendingString:title ?: @""];
} else {
self._title = title;
}
return self;
};
}
- (FLEXAlertActionProperty)destructiveStyle {
return ^FLEXAlertAction *() {
FLEXAlertActionMutationAssertion();
self._style = UIAlertActionStyleDestructive;
return self;
};
}
- (FLEXAlertActionProperty)cancelStyle {
return ^FLEXAlertAction *() {
FLEXAlertActionMutationAssertion();
self._style = UIAlertActionStyleCancel;
return self;
};
}
- (FLEXAlertActionProperty)preferred {
return ^FLEXAlertAction *() {
FLEXAlertActionMutationAssertion();
self._isPreferred = YES;
return self;
};
}
- (FLEXAlertActionBOOLProperty)enabled {
return ^FLEXAlertAction *(BOOL enabled) {
FLEXAlertActionMutationAssertion();
self._disable = !enabled;
return self;
};
}
- (FLEXAlertActionHandler)handler {
return ^FLEXAlertAction *(void(^handler)(NSArray<NSString *> *)) {
FLEXAlertActionMutationAssertion();
// 获取警告的弱引用以避免 block <--> alert 循环引用
UIAlertController *controller = self._controller; weakify(controller)
self._handler = ^(UIAlertAction *action) { strongify(controller)
// 强化该引用并将文本字段字符串传递给处理程序
NSArray *strings = [controller.textFields valueForKeyPath:@"text"];
handler(strings);
};
return self;
};
}
- (UIAlertAction *)action {
if (self._action) {
return self._action;
}
self._action = [UIAlertAction
actionWithTitle:self._title
style:self._style
handler:self._handler
];
self._action.enabled = !self._disable;
return self._action;
}
@end