forked from coding/Coding-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddUserViewController.m
More file actions
executable file
·330 lines (296 loc) · 13.1 KB
/
AddUserViewController.m
File metadata and controls
executable file
·330 lines (296 loc) · 13.1 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
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
//
// AddUserViewController.m
// Coding_iOS
//
// Created by 王 原闯 on 14-10-15.
// Copyright (c) 2014年 Coding. All rights reserved.
//
#import "AddUserViewController.h"
#import "UserCell.h"
#import "UserInfoViewController.h"
#import "Coding_NetAPIManager.h"
#import "ToMessageCell.h"
#import "ODRefreshControl.h"
@interface AddUserViewController ()
@property (strong, nonatomic) UISearchBar *mySearchBar;
@property (strong, nonatomic) UITableView *myTableView;
@property (strong, nonatomic) ODRefreshControl *myRefreshControl;
@property (strong, nonatomic) Users *curUsers;
@end
@implementation AddUserViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
if (self.type < AddUserTypeFollow) {
self.title = (self.type == AddUserTypeProjectRoot? @"添加成员":
self.type == AddUserTypeProjectFollows? @"我的关注":
@"我的粉丝");
_queryingArray = [NSMutableArray array];
_searchedArray = [NSMutableArray array];
}else if (self.type == AddUserTypeFollow){
self.title = @"添加好友";
}
_myTableView = ({
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
tableView.backgroundColor = [UIColor clearColor];
tableView.delegate = self;
tableView.dataSource = self;
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[tableView registerClass:[UserCell class] forCellReuseIdentifier:kCellIdentifier_UserCell];
[tableView registerClass:[ToMessageCell class] forCellReuseIdentifier:kCellIdentifier_ToMessage];
[self.view addSubview:tableView];
[tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
tableView;
});
_mySearchBar = ({
UISearchBar *searchBar = [[UISearchBar alloc] init];
searchBar.delegate = self;
[searchBar sizeToFit];
[searchBar setPlaceholder:@"昵称/个性后缀"];
searchBar;
});
_myTableView.tableHeaderView = _mySearchBar;
if (self.type == AddUserTypeProjectFollows || self.type == AddUserTypeProjectFans) {
_myRefreshControl = [[ODRefreshControl alloc] initInScrollView:self.myTableView];
[_myRefreshControl addTarget:self action:@selector(refresh) forControlEvents:UIControlEventValueChanged];
_curUsers = [Users usersWithOwner:[Login curLoginUser] Type:self.type == AddUserTypeProjectFollows? UsersTypeFriends_Attentive: UsersTypeFollowers];
[self refresh];
}
}
- (void)refresh{
if (_curUsers.isLoading) {
return;
}
_curUsers.willLoadMore = NO;
if (_curUsers.list.count <= 0) {
[self.view beginLoading];
}
__weak typeof(self) weakSelf = self;
[[Coding_NetAPIManager sharedManager] request_FollowersOrFriends_WithObj:self.curUsers andBlock:^(id data, NSError *error) {
[weakSelf.myRefreshControl endRefreshing];
[weakSelf.view endLoading];
if (data) {
[weakSelf.curUsers configWithObj:data];
[weakSelf searchUserWithStr:weakSelf.mySearchBar.text];
}
}];
}
- (void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
if (self.popSelfBlock) {
self.popSelfBlock();
}
}
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
if (_mySearchBar) {
[self searchUserWithStr:_mySearchBar.text];
}
}
- (void)configAddedArrayWithMembers:(NSArray *)memberArray{
if (!_addedArray) {
_addedArray = [NSMutableArray array];
}
for (ProjectMember *member in memberArray) {
[_addedArray addObject:member.user];
}
}
- (BOOL)userIsInProject:(User *)curUser{
for (User *item in _addedArray) {
if ([item.global_key isEqualToString:curUser.global_key]) {
return YES;
}
}
return NO;
}
- (BOOL)userIsQuering:(User *)curUser{
for (User *item in _queryingArray) {
if ([item.global_key isEqualToString:curUser.global_key]) {
return YES;
}
}
return NO;
}
#pragma mark Table M
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return _type == AddUserTypeFollow? 0: 44;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
if (_type == AddUserTypeFollow) {
return [UIView new];
}else{
NSInteger leftNum = _curProject.max_member.integerValue - _addedArray.count;
UILabel *label = [UILabel labelWithSystemFontSize:13 textColorHexString:leftNum > 0? @"0x999999": @"0xF34A4A"];
label.backgroundColor = self.view.backgroundColor;
label.textAlignment = NSTextAlignmentCenter;
label.text = leftNum > 0? [NSString stringWithFormat:@"你还可以添加 %lu 个项目成员", leftNum]: @"已到达到成员最大数,不能再继续选择成员!";
return label;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (self.type == AddUserTypeProjectRoot && _searchedArray.count == 0) {
return 2;
}else{
return _searchedArray.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (self.type == AddUserTypeProjectRoot && _searchedArray.count == 0) {
ToMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_ToMessage forIndexPath:indexPath];
cell.type = ToMessageTypeProjectFollows + indexPath.row;
[tableView addLineforPlainCell:cell forRowAtIndexPath:indexPath withLeftSpace:kPaddingLeftWidth];
return cell;
}else{
__weak typeof(self) weakSelf = self;
UserCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_UserCell forIndexPath:indexPath];
User *curUser = [_searchedArray objectAtIndex:indexPath.row];
cell.curUser = curUser;
if (self.type < AddUserTypeFollow) {
cell.usersType = UsersTypeAddToProject;
cell.isInProject = [self userIsInProject:curUser];
cell.isQuerying = [self userIsQuering:curUser];
cell.leftBtnClickedBlock = ^(User *clickedUser){
NSLog(@"add %@ to pro:%@", clickedUser.name, weakSelf.curProject.name);
if (![weakSelf userIsQuering:clickedUser]) {
// 添加改用户到项目
[weakSelf.queryingArray addObject:clickedUser];
[weakSelf.myTableView reloadData];
[[Coding_NetAPIManager sharedManager] request_AddUser:clickedUser ToProject:weakSelf.curProject andBlock:^(id data, NSError *error) {
if (data) {
[weakSelf.addedArray addObject:clickedUser];
}
[weakSelf.queryingArray removeObject:clickedUser];
[weakSelf.myTableView reloadData];
}];
}
};
}else{
cell.usersType = UsersTypeAddFriend;
cell.leftBtnClickedBlock = nil;
}
[tableView addLineforPlainCell:cell forRowAtIndexPath:indexPath withLeftSpace:60];
return cell;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (self.type == AddUserTypeProjectRoot && _searchedArray.count == 0) {
return [ToMessageCell cellHeight];
}else{
return [UserCell cellHeight];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (self.type == AddUserTypeProjectRoot && _searchedArray.count == 0) {
AddUserViewController *vc = [AddUserViewController new];
vc.curProject = _curProject;
vc.type = AddUserTypeProjectFollows + indexPath.row;
vc.addedArray = self.addedArray;
[self.navigationController pushViewController:vc animated:YES];
}else{
[self goToUserInfo:[_searchedArray objectAtIndex:indexPath.row]];
}
}
- (void)goToUserInfo:(User *)user{
UserInfoViewController *vc = [[UserInfoViewController alloc] init];
vc.curUser = user;
[self.navigationController pushViewController:vc animated:YES];
}
#pragma mark ScrollView Delegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
if (scrollView == _myTableView) {
[self.mySearchBar resignFirstResponder];
}
}
#pragma mark UISearchBarDelegate
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
NSLog(@"textDidChange: %@", searchText);
[self searchUserWithStr:searchText];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
NSLog(@"searchBarSearchButtonClicked: %@", searchBar.text);
[searchBar resignFirstResponder];
[self searchUserWithStr:searchBar.text];
}
- (void)searchUserWithStr:(NSString *)string{
NSString *strippedStr = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if (self.type == AddUserTypeProjectRoot || self.type == AddUserTypeFollow) {
if (strippedStr.length > 0) {
__weak typeof(self) weakSelf = self;
[[Coding_NetAPIManager sharedManager] request_Users_WithSearchString:string andBlock:^(id data, NSError *error) {
if (data) {
weakSelf.searchedArray = data;
[weakSelf.myTableView reloadData];
}
}];
}else{
_searchedArray = nil;
[_myTableView reloadData];
}
}else{
if (strippedStr.length > 0) {
[self updateFilteredContentForSearchString:strippedStr];
}else{
_searchedArray = _curUsers.list.copy;
[_myTableView reloadData];
}
}
}
- (void)updateFilteredContentForSearchString:(NSString *)searchString{
// start out with the entire list
NSMutableArray *searchResults = [self.curUsers.list mutableCopy];
// strip out all the leading and trailing spaces
NSString *strippedStr = [searchString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
// break up the search terms (separated by spaces)
NSArray *searchItems = nil;
if (strippedStr.length > 0)
{
searchItems = [strippedStr componentsSeparatedByString:@" "];
}
// build all the "AND" expressions for each value in the searchString
NSMutableArray *andMatchPredicates = [NSMutableArray array];
for (NSString *searchString in searchItems)
{
// each searchString creates an OR predicate for: name, global_key
NSMutableArray *searchItemsPredicate = [NSMutableArray array];
// name field matching
NSExpression *lhs = [NSExpression expressionForKeyPath:@"name"];
NSExpression *rhs = [NSExpression expressionForConstantValue:searchString];
NSPredicate *finalPredicate = [NSComparisonPredicate
predicateWithLeftExpression:lhs
rightExpression:rhs
modifier:NSDirectPredicateModifier
type:NSContainsPredicateOperatorType
options:NSCaseInsensitivePredicateOption];
[searchItemsPredicate addObject:finalPredicate];
// pinyinName field matching
lhs = [NSExpression expressionForKeyPath:@"pinyinName"];
rhs = [NSExpression expressionForConstantValue:searchString];
finalPredicate = [NSComparisonPredicate
predicateWithLeftExpression:lhs
rightExpression:rhs
modifier:NSDirectPredicateModifier
type:NSContainsPredicateOperatorType
options:NSCaseInsensitivePredicateOption];
[searchItemsPredicate addObject:finalPredicate];
// global_key field matching
lhs = [NSExpression expressionForKeyPath:@"global_key"];
rhs = [NSExpression expressionForConstantValue:searchString];
finalPredicate = [NSComparisonPredicate
predicateWithLeftExpression:lhs
rightExpression:rhs
modifier:NSDirectPredicateModifier
type:NSContainsPredicateOperatorType
options:NSCaseInsensitivePredicateOption];
[searchItemsPredicate addObject:finalPredicate];
// at this OR predicate to ourr master AND predicate
NSCompoundPredicate *orMatchPredicates = (NSCompoundPredicate *)[NSCompoundPredicate orPredicateWithSubpredicates:searchItemsPredicate];
[andMatchPredicates addObject:orMatchPredicates];
}
NSCompoundPredicate *finalCompoundPredicate = (NSCompoundPredicate *)[NSCompoundPredicate andPredicateWithSubpredicates:andMatchPredicates];
self.searchedArray = [searchResults filteredArrayUsingPredicate:finalCompoundPredicate].copy;
[self.myTableView reloadData];
}
@end