1111#import " UserCell.h"
1212#import " UserInfoViewController.h"
1313#import " Coding_NetAPIManager.h"
14+ #import " ToMessageCell.h"
15+ #import " ODRefreshControl.h"
16+
1417
1518@interface AddUserViewController ()
1619@property (strong , nonatomic ) UISearchBar *mySearchBar;
1720@property (strong , nonatomic ) UITableView *myTableView;
21+ @property (strong , nonatomic ) ODRefreshControl *myRefreshControl;
22+ @property (strong , nonatomic ) Users *curUsers;
1823@end
1924
2025@implementation AddUserViewController
2126
2227- (void )viewDidLoad {
2328 [super viewDidLoad ];
2429 // Do any additional setup after loading the view.
25- if (self.type == AddUserTypeProject) {
26- self.title = @" 添加成员" ;
30+ if (self.type < AddUserTypeFollow) {
31+ self.title = (self.type == AddUserTypeProjectRoot? @" 添加成员" :
32+ self.type == AddUserTypeProjectFollows? @" 我的关注" :
33+ @" 我的粉丝" );
2734 _queryingArray = [NSMutableArray array ];
2835 _searchedArray = [NSMutableArray array ];
2936 }else if (self.type == AddUserTypeFollow){
@@ -37,6 +44,7 @@ - (void)viewDidLoad {
3744 tableView.dataSource = self;
3845 tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
3946 [tableView registerClass: [UserCell class ] forCellReuseIdentifier: kCellIdentifier_UserCell ];
47+ [tableView registerClass: [ToMessageCell class ] forCellReuseIdentifier: kCellIdentifier_ToMessage ];
4048 [self .view addSubview: tableView];
4149 [tableView mas_makeConstraints: ^(MASConstraintMaker *make) {
4250 make.edges .equalTo (self.view );
@@ -51,11 +59,31 @@ - (void)viewDidLoad {
5159 searchBar;
5260 });
5361 _myTableView.tableHeaderView = _mySearchBar;
62+ if (self.type == AddUserTypeProjectFollows || self.type == AddUserTypeProjectFans) {
63+ _myRefreshControl = [[ODRefreshControl alloc ] initInScrollView: self .myTableView];
64+ [_myRefreshControl addTarget: self action: @selector (refresh ) forControlEvents: UIControlEventValueChanged];
65+ _curUsers = [Users usersWithOwner: [Login curLoginUser ] Type: self .type == AddUserTypeProjectFollows? UsersTypeFriends_Attentive: UsersTypeFollowers];
66+ [self refresh ];
67+ }
5468}
5569
56- - (void )didReceiveMemoryWarning {
57- [super didReceiveMemoryWarning ];
58- // Dispose of any resources that can be recreated.
70+ - (void )refresh {
71+ if (_curUsers.isLoading ) {
72+ return ;
73+ }
74+ _curUsers.willLoadMore = NO ;
75+ if (_curUsers.list .count <= 0 ) {
76+ [self .view beginLoading ];
77+ }
78+ __weak typeof (self) weakSelf = self;
79+ [[Coding_NetAPIManager sharedManager ] request_FollowersOrFriends_WithObj: self .curUsers andBlock: ^(id data, NSError *error) {
80+ [weakSelf.myRefreshControl endRefreshing ];
81+ [weakSelf.view endLoading ];
82+ if (data) {
83+ [weakSelf.curUsers configWithObj: data];
84+ [weakSelf searchUserWithStr: weakSelf.mySearchBar.text];
85+ }
86+ }];
5987}
6088
6189- (void )viewDidDisappear : (BOOL )animated {
@@ -74,7 +102,9 @@ - (void)viewDidAppear:(BOOL)animated{
74102}
75103
76104- (void )configAddedArrayWithMembers : (NSArray *)memberArray {
77- _addedArray = [NSMutableArray array ];
105+ if (!_addedArray) {
106+ _addedArray = [NSMutableArray array ];
107+ }
78108 for (ProjectMember *member in memberArray) {
79109 [_addedArray addObject: member.user];
80110 }
@@ -97,51 +127,72 @@ - (BOOL)userIsQuering:(User *)curUser{
97127}
98128#pragma mark Table M
99129- (NSInteger )tableView : (UITableView *)tableView numberOfRowsInSection : (NSInteger )section {
100- return _searchedArray.count ;
130+ if (self.type == AddUserTypeProjectRoot && _searchedArray.count == 0 ) {
131+ return 2 ;
132+ }else {
133+ return _searchedArray.count ;
134+ }
101135}
102136
103137- (UITableViewCell *)tableView : (UITableView *)tableView cellForRowAtIndexPath : (NSIndexPath *)indexPath {
104- __weak typeof (self) weakSelf = self;
105-
106- UserCell *cell = [tableView dequeueReusableCellWithIdentifier: kCellIdentifier_UserCell forIndexPath: indexPath];
107- User *curUser = [_searchedArray objectAtIndex: indexPath.row];
108- cell.curUser = curUser;
109- if (self.type == AddUserTypeProject) {
110- cell.usersType = UsersTypeAddToProject;
111- cell.isInProject = [self userIsInProject: curUser];
112- cell.isQuerying = [self userIsQuering: curUser];
113- cell.leftBtnClickedBlock = ^(User *clickedUser){
114- NSLog (@" add %@ to pro:%@ " , clickedUser.name , weakSelf.curProject .name );
115- if (![weakSelf userIsQuering: clickedUser]) {
116- // 添加改用户到项目
117- [weakSelf.queryingArray addObject: clickedUser];
118- [weakSelf.myTableView reloadData ];
119-
120- [[Coding_NetAPIManager sharedManager ] request_AddUser: clickedUser ToProject: weakSelf.curProject andBlock: ^(id data, NSError *error) {
121- if (data) {
122- [weakSelf.addedArray addObject: clickedUser];
123- }
124- [weakSelf.queryingArray removeObject: clickedUser];
125- [weakSelf.myTableView reloadData ];
126- }];
127- }
128- };
138+ if (self.type == AddUserTypeProjectRoot && _searchedArray.count == 0 ) {
139+ ToMessageCell *cell = [tableView dequeueReusableCellWithIdentifier: kCellIdentifier_ToMessage forIndexPath: indexPath];
140+ cell.type = ToMessageTypeProjectFollows + indexPath.row ;
141+ [tableView addLineforPlainCell: cell forRowAtIndexPath: indexPath withLeftSpace: kPaddingLeftWidth ];
142+ return cell;
129143 }else {
130- cell.usersType = UsersTypeAddFriend;
131- cell.leftBtnClickedBlock = nil ;
144+ __weak typeof (self) weakSelf = self;
145+ UserCell *cell = [tableView dequeueReusableCellWithIdentifier: kCellIdentifier_UserCell forIndexPath: indexPath];
146+ User *curUser = [_searchedArray objectAtIndex: indexPath.row];
147+ cell.curUser = curUser;
148+ if (self.type < AddUserTypeFollow) {
149+ cell.usersType = UsersTypeAddToProject;
150+ cell.isInProject = [self userIsInProject: curUser];
151+ cell.isQuerying = [self userIsQuering: curUser];
152+ cell.leftBtnClickedBlock = ^(User *clickedUser){
153+ NSLog (@" add %@ to pro:%@ " , clickedUser.name , weakSelf.curProject .name );
154+ if (![weakSelf userIsQuering: clickedUser]) {
155+ // 添加改用户到项目
156+ [weakSelf.queryingArray addObject: clickedUser];
157+ [weakSelf.myTableView reloadData ];
158+
159+ [[Coding_NetAPIManager sharedManager ] request_AddUser: clickedUser ToProject: weakSelf.curProject andBlock: ^(id data, NSError *error) {
160+ if (data) {
161+ [weakSelf.addedArray addObject: clickedUser];
162+ }
163+ [weakSelf.queryingArray removeObject: clickedUser];
164+ [weakSelf.myTableView reloadData ];
165+ }];
166+ }
167+ };
168+ }else {
169+ cell.usersType = UsersTypeAddFriend;
170+ cell.leftBtnClickedBlock = nil ;
171+ }
172+ [tableView addLineforPlainCell: cell forRowAtIndexPath: indexPath withLeftSpace: 60 ];
173+ return cell;
132174 }
133-
134- [tableView addLineforPlainCell: cell forRowAtIndexPath: indexPath withLeftSpace: 60 ];
135- return cell;
136175}
137176
138177- (CGFloat)tableView : (UITableView *)tableView heightForRowAtIndexPath : (NSIndexPath *)indexPath {
139- return [UserCell cellHeight ];
178+ if (self.type == AddUserTypeProjectRoot && _searchedArray.count == 0 ) {
179+ return [ToMessageCell cellHeight ];
180+ }else {
181+ return [UserCell cellHeight ];
182+ }
140183}
141184
142185- (void )tableView : (UITableView *)tableView didSelectRowAtIndexPath : (NSIndexPath *)indexPath {
143186 [tableView deselectRowAtIndexPath: indexPath animated: YES ];
144- [self goToUserInfo: [_searchedArray objectAtIndex: indexPath.row]];
187+ if (self.type == AddUserTypeProjectRoot && _searchedArray.count == 0 ) {
188+ AddUserViewController *vc = [AddUserViewController new ];
189+ vc.curProject = _curProject;
190+ vc.type = AddUserTypeProjectFollows + indexPath.row ;
191+ vc.addedArray = self.addedArray ;
192+ [self .navigationController pushViewController: vc animated: YES ];
193+ }else {
194+ [self goToUserInfo: [_searchedArray objectAtIndex: indexPath.row]];
195+ }
145196}
146197
147198- (void )goToUserInfo : (User *)user {
@@ -172,28 +223,91 @@ - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
172223}
173224
174225- (void )searchUserWithStr : (NSString *)string {
175- NSString *strippedStr = [string stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet ]];
176- if (strippedStr.length > 0 ) {
177- __weak typeof (self) weakSelf = self;
178- [[Coding_NetAPIManager sharedManager ] request_Users_WithSearchString: string andBlock: ^(id data, NSError *error) {
179- if (data) {
180- weakSelf.searchedArray = data;
181- [weakSelf.myTableView reloadData ];
182- }
183- }];
226+ NSString *strippedStr = [string stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet ]];
227+ if (self.type == AddUserTypeProjectRoot || self.type == AddUserTypeFollow) {
228+ if (strippedStr.length > 0 ) {
229+ __weak typeof (self) weakSelf = self;
230+ [[Coding_NetAPIManager sharedManager ] request_Users_WithSearchString: string andBlock: ^(id data, NSError *error) {
231+ if (data) {
232+ weakSelf.searchedArray = data;
233+ [weakSelf.myTableView reloadData ];
234+ }
235+ }];
236+ }else {
237+ _searchedArray = nil ;
238+ [_myTableView reloadData ];
239+ }
184240 }else {
185- [_searchedArray removeAllObjects ];
186- [_myTableView reloadData ];
241+ if (strippedStr.length > 0 ) {
242+ [self updateFilteredContentForSearchString: strippedStr];
243+ }else {
244+ _searchedArray = _curUsers.list .copy ;
245+ [_myTableView reloadData ];
246+ }
187247 }
188-
189248}
190249
191-
192- - (void )dealloc
193- {
194- _myTableView.delegate = nil ;
195- _myTableView.dataSource = nil ;
250+ - (void )updateFilteredContentForSearchString : (NSString *)searchString {
251+ // start out with the entire list
252+ NSMutableArray *searchResults = [self .curUsers.list mutableCopy ];
253+
254+ // strip out all the leading and trailing spaces
255+ NSString *strippedStr = [searchString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet ]];
256+
257+ // break up the search terms (separated by spaces)
258+ NSArray *searchItems = nil ;
259+ if (strippedStr.length > 0 )
260+ {
261+ searchItems = [strippedStr componentsSeparatedByString: @" " ];
262+ }
263+
264+ // build all the "AND" expressions for each value in the searchString
265+ NSMutableArray *andMatchPredicates = [NSMutableArray array ];
266+
267+ for (NSString *searchString in searchItems)
268+ {
269+ // each searchString creates an OR predicate for: name, global_key
270+ NSMutableArray *searchItemsPredicate = [NSMutableArray array ];
271+
272+ // name field matching
273+ NSExpression *lhs = [NSExpression expressionForKeyPath: @" name" ];
274+ NSExpression *rhs = [NSExpression expressionForConstantValue: searchString];
275+ NSPredicate *finalPredicate = [NSComparisonPredicate
276+ predicateWithLeftExpression: lhs
277+ rightExpression: rhs
278+ modifier: NSDirectPredicateModifier
279+ type: NSContainsPredicateOperatorType
280+ options: NSCaseInsensitivePredicateOption];
281+ [searchItemsPredicate addObject: finalPredicate];
282+ // pinyinName field matching
283+ lhs = [NSExpression expressionForKeyPath: @" pinyinName" ];
284+ rhs = [NSExpression expressionForConstantValue: searchString];
285+ finalPredicate = [NSComparisonPredicate
286+ predicateWithLeftExpression: lhs
287+ rightExpression: rhs
288+ modifier: NSDirectPredicateModifier
289+ type: NSContainsPredicateOperatorType
290+ options: NSCaseInsensitivePredicateOption];
291+ [searchItemsPredicate addObject: finalPredicate];
292+ // global_key field matching
293+ lhs = [NSExpression expressionForKeyPath: @" global_key" ];
294+ rhs = [NSExpression expressionForConstantValue: searchString];
295+ finalPredicate = [NSComparisonPredicate
296+ predicateWithLeftExpression: lhs
297+ rightExpression: rhs
298+ modifier: NSDirectPredicateModifier
299+ type: NSContainsPredicateOperatorType
300+ options: NSCaseInsensitivePredicateOption];
301+ [searchItemsPredicate addObject: finalPredicate];
302+ // at this OR predicate to ourr master AND predicate
303+ NSCompoundPredicate *orMatchPredicates = (NSCompoundPredicate *)[NSCompoundPredicate orPredicateWithSubpredicates: searchItemsPredicate];
304+ [andMatchPredicates addObject: orMatchPredicates];
305+ }
306+
307+ NSCompoundPredicate *finalCompoundPredicate = (NSCompoundPredicate *)[NSCompoundPredicate andPredicateWithSubpredicates: andMatchPredicates];
308+
309+ self.searchedArray = [searchResults filteredArrayUsingPredicate: finalCompoundPredicate].copy ;
310+ [self .myTableView reloadData ];
196311}
197312
198-
199313@end
0 commit comments