forked from coding/Coding-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditLabelViewController.m
More file actions
323 lines (283 loc) · 11.3 KB
/
EditLabelViewController.m
File metadata and controls
323 lines (283 loc) · 11.3 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
//
// EditLabelViewController.m
// Coding_iOS
//
// Created by zwm on 15/4/16.
// Copyright (c) 2015年 Coding. All rights reserved.
//
#import "EditLabelViewController.h"
#import "EditLabelHeadCell.h"
#import "EditLabelCell.h"
#import "ResetLabelViewController.h"
#import "TPKeyboardAvoidingTableView.h"
#import "Coding_NetAPIManager.h"
#import "ProjectTag.h"
#import "MBProgressHUD+Add.h"
#import "EditColorViewController.h"
#define kCellIdentifier_EditLabelHeadCell @"EditLabelHeadCell"
#define kCellIdentifier_EditLabelCell @"EditLabelCell"
@interface EditLabelViewController () <UITableViewDataSource, UITableViewDelegate, SWTableViewCellDelegate, UITextFieldDelegate>
@property (strong, nonatomic) NSMutableArray *tagList, *selectedTags;
@property (strong, nonatomic) TPKeyboardAvoidingTableView *myTableView;
@property (strong, nonatomic) ProjectTag *tagToAdd;
@end
@implementation EditLabelViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)setOrignalTags:(NSArray *)orignalTags{
_orignalTags = orignalTags;
if (_orignalTags.count > 0) {
_selectedTags = [_orignalTags mutableCopy];
}else{
_selectedTags = [NSMutableArray new];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"标签管理";
self.tagToAdd = [ProjectTag new];
self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithBtnTitle:@"保存" target:self action:@selector(okBtnClick)];
self.navigationItem.rightBarButtonItem.enabled = FALSE;
self.view.backgroundColor = kColorTableSectionBg;
_myTableView = ({
TPKeyboardAvoidingTableView *tableView = [[TPKeyboardAvoidingTableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
tableView.backgroundColor = kColorTableSectionBg;
tableView.delegate = self;
tableView.dataSource = self;
[tableView registerClass:[EditLabelHeadCell class] forCellReuseIdentifier:kCellIdentifier_EditLabelHeadCell];
[tableView registerClass:[EditLabelCell class] forCellReuseIdentifier:kCellIdentifier_EditLabelCell];
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:tableView];
[tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
tableView;
});
[self sendRequest];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.myTableView reloadData];
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:YES];
if (_tagList.count > 0 && _selectedTags.count > 0) {
for (ProjectTag *tag in _tagList) {
ProjectTag *orignalTag = [ProjectTag tags:_selectedTags hasTag:tag];
if (orignalTag) {
[_selectedTags replaceObjectAtIndex:[_selectedTags indexOfObject:orignalTag] withObject:tag];
}
}
}
if (_tagsSelectedBlock) {
_tagsSelectedBlock(self, _selectedTags);
}
}
- (void)sendRequest
{
[self.view beginLoading];
__weak typeof(self) weakSelf = self;
[[Coding_NetAPIManager sharedManager] request_TagListInProject:_curProject type:ProjectTagTypeTopic andBlock:^(id data, NSError *error) {
[weakSelf.view endLoading];
if (data) {
weakSelf.tagList = data;
[weakSelf.myTableView reloadData];
}
}];}
#pragma mark - click
- (void)okBtnClick{
[self.navigationController popViewControllerAnimated:YES];
}
- (void)addBtnClick:(UIButton *)sender
{
[self.view endEditing:YES];
if (_tagToAdd.name.length > 0) {
__weak typeof(self) weakSelf = self;
[[Coding_NetAPIManager sharedManager] request_AddTag:_tagToAdd toProject:_curProject andBlock:^(id data, NSError *error) {
if (data) {
weakSelf.tagToAdd.id = data;
[weakSelf.tagList addObject:weakSelf.tagToAdd];
[weakSelf.selectedTags addObject:weakSelf.tagToAdd];
weakSelf.navigationItem.rightBarButtonItem.enabled = YES;
weakSelf.tagToAdd = [ProjectTag new];
[weakSelf.myTableView reloadData];
sender.enabled = FALSE;
[NSObject showHudTipStr:@"添加标签成功^^"];
}
}];
}
}
- (void)colorBtnClick:(UIButton *)sender{
EditColorViewController *vc = [EditColorViewController new];
vc.curTag = _tagToAdd;
[self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return _tagList.count > 0 ? 2 : 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger row = 1;
if (section == 1) {
row = _tagList.count;
}
return row;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
EditLabelHeadCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_EditLabelHeadCell forIndexPath:indexPath];
[cell.addBtn addTarget:self action:@selector(addBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[cell.colorBtn addTarget:self action:@selector(colorBtnClick:) forControlEvents:UIControlEventTouchUpInside];
cell.colorBtn.backgroundColor = [UIColor colorWithHexString:[_tagToAdd.color stringByReplacingOccurrencesOfString:@"#" withString:@"0x"]];
cell.labelField.text = _tagToAdd.name;
cell.labelField.delegate = self;
[cell.labelField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
[tableView addLineforPlainCell:cell forRowAtIndexPath:indexPath withLeftSpace:kPaddingLeftWidth];
return cell;
}else{
ProjectTag *ptLabel = _tagList[indexPath.row];
EditLabelCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_EditLabelCell forIndexPath:indexPath];
[cell setRightUtilityButtons:[self rightButtons] WithButtonWidth:[EditLabelCell cellHeight]];
cell.delegate = self;
BOOL selected = FALSE;
for (ProjectTag *lbl in _selectedTags) {
if ([lbl.id integerValue] == [ptLabel.id integerValue]) {
selected = TRUE;
break;
}
}
[cell setTag:ptLabel andSelected:selected];
[tableView addLineforPlainCell:cell forRowAtIndexPath:indexPath withLeftSpace:kPaddingLeftWidth];
return cell;
}
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [EditLabelCell cellHeight];
}
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section > 0) {
return TRUE;
}
return FALSE;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.view endEditing:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.section > 0) {
EditLabelCell *cell = (EditLabelCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.selectBtn.selected = !cell.selectBtn.selected;
ProjectTag *tagInSelected = [ProjectTag tags:_selectedTags hasTag:_tagList[indexPath.row]];
if (cell.selectBtn.selected && !tagInSelected) {
[_selectedTags addObject:_tagList[indexPath.row]];
}else if (!cell.selectBtn.selected && tagInSelected){
[_selectedTags removeObject:tagInSelected];
}
self.navigationItem.rightBarButtonItem.enabled = ![ProjectTag tags:_selectedTags isEqualTo:_orignalTags];
}
}
- (NSArray *)rightButtons
{
NSMutableArray *rightUtilityButtons = [NSMutableArray new];
[rightUtilityButtons sw_addUtilityButtonWithColor:[UIColor colorWithHexString:@"0xe6e6e6"] icon:[UIImage imageNamed:@"icon_file_cell_rename"]];
[rightUtilityButtons sw_addUtilityButtonWithColor:kColorBrandRed icon:[UIImage imageNamed:@"icon_file_cell_delete"]];
return rightUtilityButtons;
}
#pragma mark - SWTableViewCellDelegate
- (BOOL)swipeableTableViewCellShouldHideUtilityButtonsOnSwipe:(SWTableViewCell *)cell
{
return YES;
}
- (BOOL)swipeableTableViewCell:(SWTableViewCell *)cell canSwipeToState:(SWCellState)state
{
return YES;
}
- (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index
{
[cell hideUtilityButtonsAnimated:YES];
NSIndexPath *indexPath = [self.myTableView indexPathForCell:cell];
if (index == 0) {
[self renameBtnClick:indexPath.row];
} else {
__weak typeof(self) weakSelf = self;
ProjectTag *ptLabel = [_tagList objectAtIndex:indexPath.row];
NSString *tip = [NSString stringWithFormat:@"确定要删除标签:%@?", ptLabel.name];
UIActionSheet *actionSheet = [UIActionSheet bk_actionSheetCustomWithTitle:tip buttonTitles:nil destructiveTitle:@"确认删除" cancelTitle:@"取消" andDidDismissBlock:^(UIActionSheet *sheet, NSInteger index) {
if (index == 0) {
[weakSelf deleteBtnClick:indexPath.row];
}
}];
[actionSheet showInView:self.view];
}
}
- (void)renameBtnClick:(NSInteger)index
{
ResetLabelViewController *vc = [[ResetLabelViewController alloc] init];
vc.ptLabel = [_tagList objectAtIndex:index];
vc.curProject = _curProject;
[self.navigationController pushViewController:vc animated:YES];
}
- (void)deleteBtnClick:(NSInteger)index
{
__weak typeof(self) weakSelf = self;
[[Coding_NetAPIManager sharedManager] request_DeleteTag:_tagList[index] inProject:_curProject andBlock:^(id data, NSError *error) {
if (data) {
[weakSelf deleteLabel:index];
}
}];
}
- (void)deleteLabel:(NSInteger)index
{
ProjectTag *lbl = _tagList[index];
for (ProjectTag *tempLbl in _selectedTags) {
if ([tempLbl.id integerValue] == [lbl.id integerValue]) {
[_selectedTags removeObject:tempLbl];
self.navigationItem.rightBarButtonItem.enabled = YES;
break;
}
}
[self.tagList removeObjectAtIndex:index];
[self.myTableView reloadData];
}
#pragma mark - UITextFieldDelegate
- (void)textFieldDidChange:(UITextField *)textField
{
_tagToAdd.name = [textField.text trimWhitespace];
BOOL enabled = _tagToAdd.name.length > 0 ? TRUE : FALSE;
if (enabled) {
for (ProjectTag *lbl in _tagList) {
if ([lbl.name isEqualToString:_tagToAdd.name]) {
enabled = FALSE;
break;
}
}
}
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
EditLabelHeadCell *cell = (EditLabelHeadCell *)[_myTableView cellForRowAtIndexPath:indexPath];
cell.addBtn.enabled = enabled;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
[self.view endEditing:YES];
}
@end