forked from coding/Coding-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditTopicViewController.m
More file actions
executable file
·386 lines (334 loc) · 14.6 KB
/
EditTopicViewController.m
File metadata and controls
executable file
·386 lines (334 loc) · 14.6 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
//
// EditTopicViewController.m
// Coding_iOS
//
// Created by 王 原闯 on 14-8-27.
// Copyright (c) 2014年 Coding. All rights reserved.
//
#import "EditTopicViewController.h"
#import "ProjectTopic.h"
#import "Coding_NetAPIManager.h"
#import "EaseMarkdownTextView.h"
#import "WebContentManager.h"
#import "EditLabelViewController.h"
#import "TopicPreviewCell.h"
#import "ProjectTag.h"
#import "ProjectTagsView.h"
#import "WebViewController.h"
@interface EditTopicViewController ()<UIWebViewDelegate, UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) UISegmentedControl *segmentedControl;
@property (assign, nonatomic) NSInteger curIndex;
@property (strong, nonatomic) UITableView *preView;
@property (strong, nonatomic) UIView *editView;
@property (strong, nonatomic) UITextField *inputTitleView;
@property (strong, nonatomic) EaseMarkdownTextView *inputContentView;
@property (strong, nonatomic) UIView *lineView;
@property (strong, nonatomic) ProjectTagsView *tagsView;
@end
@implementation EditTopicViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = kColorTableBG;
if (!_segmentedControl) {
_segmentedControl = ({
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"编辑", @"预览"]];
[segmentedControl setWidth:80 forSegmentAtIndex:0];
[segmentedControl setWidth:80 forSegmentAtIndex:1];
[segmentedControl setTitleTextAttributes:@{
NSFontAttributeName: [UIFont systemFontOfSize:16],
NSForegroundColorAttributeName: [UIColor whiteColor]
}
forState:UIControlStateSelected];
[segmentedControl setTitleTextAttributes:@{
NSFontAttributeName: [UIFont systemFontOfSize:16],
NSForegroundColorAttributeName: kColorNavTitle
} forState:UIControlStateNormal];
[segmentedControl addTarget:self action:@selector(segmentedControlSelected:) forControlEvents:UIControlEventValueChanged];
segmentedControl;
});
self.navigationItem.titleView = _segmentedControl;
}
[self.navigationItem setRightBarButtonItem:[UIBarButtonItem itemWithBtnTitle:
self.type == TopicEditTypeFeedBack ? @"发送" :
self.type == TopicEditTypeModify? @"保存": @"完成"
target:self action:@selector(saveBtnClicked)] animated:YES];
self.navigationItem.rightBarButtonItem.enabled = NO;
[[[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardWillChangeFrameNotification object:nil] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(NSNotification *aNotification) {
if (self.inputContentView) {
NSDictionary* userInfo = [aNotification userInfo];
CGRect keyboardEndFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
self.inputContentView.contentInset = UIEdgeInsetsMake(0, 0, CGRectGetHeight(keyboardEndFrame), 0);
self.inputContentView.scrollIndicatorInsets = self.inputContentView.contentInset;
}
}];
self.curIndex = 0;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if (_curIndex == 0) {
[self loadEditView];
} else {
[self loadPreview];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark UISegmentedControl
- (void)segmentedControlSelected:(id)sender
{
UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
self.curIndex = segmentedControl.selectedSegmentIndex;
}
#pragma mark index_view
- (void)setCurIndex:(NSInteger)curIndex
{
_curIndex = curIndex;
if (_segmentedControl.selectedSegmentIndex != curIndex) {
[_segmentedControl setSelectedSegmentIndex:_curIndex];
}
if (_curIndex == 0) {
[self loadEditView];
} else {
[self loadPreview];
}
}
- (void)loadLabelView
{
if (!_tagsView) {
_tagsView = [ProjectTagsView viewWithTags:_curProTopic.mdLabels];
[self.editView addSubview:_tagsView];
}else{
_tagsView.tags = _curProTopic.mdLabels;
}
__weak typeof(self) weakSelf = self;
_tagsView.deleteTagBlock = ^(ProjectTag *curTag){
curTag = [ProjectTag tags:weakSelf.curProTopic.mdLabels hasTag:curTag];
if (curTag) {
[weakSelf.curProTopic.mdLabels removeObject:curTag];
}
[weakSelf loadEditView];
weakSelf.navigationItem.rightBarButtonItem.enabled = YES;
};
_tagsView.addTagBlock = ^(){
[weakSelf addtitleBtnClick];
};
}
- (void)loadEditView
{
if (!_editView) {
//控件
_editView = [[UIView alloc] initWithFrame:self.view.bounds];
_inputTitleView = [[UITextField alloc] initWithFrame:CGRectZero];
_inputTitleView.textColor = kColor222;
_inputTitleView.font = [UIFont systemFontOfSize:18];
_inputTitleView.attributedPlaceholder = [[NSAttributedString alloc] initWithString:(self.type == TopicEditTypeFeedBack ? @"反馈标题" : @"讨论标题") attributes:@{NSForegroundColorAttributeName : [UIColor lightGrayColor]}];
[_editView addSubview:_inputTitleView];
_lineView = [[UIView alloc] initWithFrame:CGRectZero];
_lineView.backgroundColor = kColorTableSectionBg;
[_editView addSubview:_lineView];
_inputContentView = [[EaseMarkdownTextView alloc] initWithFrame:CGRectZero];
_inputContentView.curProject = self.curProTopic.project;
_inputContentView.textColor = kColor222;
_inputContentView.placeholder = self.type == TopicEditTypeFeedBack ? @"反馈内容" : @"讨论内容";
_inputContentView.backgroundColor = [UIColor clearColor];
_inputContentView.font = [UIFont systemFontOfSize:15];
[_editView addSubview:_inputContentView];
[self.view addSubview:_editView];
// 内容
@weakify(self);
RAC(self.navigationItem.rightBarButtonItem, enabled) = [RACSignal combineLatest:@[self.inputTitleView.rac_textSignal, self.inputContentView.rac_textSignal, RACObserve(self.curProTopic, mdLabels)] reduce:^id (NSString *title, NSString *content, NSArray *mdLabels) {
// 刚开始编辑content的时候,title传过来的总是nil
@strongify(self);
title = self.inputTitleView.text;
content = self.inputContentView.text;
BOOL enabled = ([title stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].length > 0
&& [content stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].length > 0
&& (![title isEqualToString:self.curProTopic.mdTitle]
|| ![content isEqualToString:self.curProTopic.mdContent]
|| ![ProjectTag tags:mdLabels isEqualTo:self.curProTopic.labels]));
return @(enabled);
}];
_inputTitleView.text = _curProTopic.mdTitle;
_inputContentView.text = _curProTopic.mdContent;
}
if (self.type != TopicEditTypeFeedBack) {
[self loadLabelView];
}
// 布局
_inputContentView.textContainerInset = UIEdgeInsetsMake(10, kPaddingLeftWidth - 5, 8, kPaddingLeftWidth - 5);
[_editView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
[_inputTitleView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(_editView.mas_top).offset(10.0);
make.height.mas_equalTo(30);
make.left.equalTo(_editView).offset(kPaddingLeftWidth);
make.right.equalTo(_editView).offset(-kPaddingLeftWidth);
}];
if (self.type != TopicEditTypeFeedBack) {
// 标签
CGFloat tagsViewHeight = [ProjectTagsView getHeightForTags:_curProTopic.mdLabels];
[_tagsView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(_inputTitleView.mas_bottom).offset(16.0);
make.height.mas_equalTo(tagsViewHeight);
make.left.right.equalTo(_editView);
}];
[_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(_tagsView.mas_bottom).offset(9.0);
make.left.equalTo(_editView).offset(kPaddingLeftWidth);
make.height.mas_equalTo(1.0);
make.right.equalTo(_editView);
}];
} else {
[_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(_inputTitleView.mas_bottom).offset(12.0);
make.left.equalTo(_editView).offset(kPaddingLeftWidth);
make.height.mas_equalTo(1.0);
make.right.equalTo(_editView);
}];
}
[_inputContentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(_lineView.mas_bottom).offset(5.0);
make.left.right.bottom.equalTo(_editView);
}];
_editView.hidden = NO;
_preView.hidden = YES;
}
- (void)loadPreview
{
if (!_preView) {
_preView = ({
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
tableView.backgroundColor = [UIColor clearColor];
tableView.delegate = self;
tableView.dataSource = self;
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[tableView registerClass:[TopicPreviewCell class] forCellReuseIdentifier:kCellIdentifier_TopicPreviewCell];
tableView;
});
[self.view addSubview:_preView];
}
// 布局
[_preView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
_preView.hidden = NO;
[_preView reloadData];
_editView.hidden = YES;
[_editView endEditing:YES];
}
#pragma mark - click
- (void)addtitleBtnClick
{
EditLabelViewController *vc = [[EditLabelViewController alloc] init];
vc.curProject = _curProTopic.project;
vc.orignalTags = _curProTopic.mdLabels;
@weakify(self);
vc.tagsSelectedBlock = ^(EditLabelViewController *vc, NSMutableArray *selectedTags){
@strongify(self);
self.curProTopic.mdLabels = selectedTags;
};
[self.navigationController pushViewController:vc animated:YES];
}
- (void)saveBtnClicked
{
self.curProTopic.mdTitle = _inputTitleView.text;
self.curProTopic.mdContent = _inputContentView.text;
if (self.type == TopicEditTypeModify) {
self.navigationItem.rightBarButtonItem.enabled = NO;
@weakify(self);
[[Coding_NetAPIManager sharedManager] request_ModifyProjectTpoic:self.curProTopic andBlock:^(id data, NSError *error) {
@strongify(self);
self.navigationItem.rightBarButtonItem.enabled = YES;
if (data) {
if (self.topicChangedBlock) {
self.topicChangedBlock(data, self.type);
}
[self.navigationController popViewControllerAnimated:YES];
}
}];
} else {
self.navigationItem.rightBarButtonItem.enabled = NO;
if (self.type == TopicEditTypeFeedBack) {
self.curProTopic.mdContent = [NSString stringWithFormat:@"%@\n%@", self.curProTopic.mdContent, [NSString userAgentStr]];
}
@weakify(self);
[[Coding_NetAPIManager sharedManager] request_AddProjectTpoic:self.curProTopic andBlock:^(id data, NSError *error) {
@strongify(self);
self.navigationItem.rightBarButtonItem.enabled = YES;
if (data) {
if (self.topicChangedBlock) {
self.topicChangedBlock(data, self.type);
}
[self.navigationController popViewControllerAnimated:YES];
}
}];
}
}
#pragma mark Table M
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TopicPreviewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_TopicPreviewCell forIndexPath:indexPath];
_curProTopic.created_at = [NSDate date];
cell.isLabel = (self.type == TopicEditTypeFeedBack ? FALSE : TRUE);
self.curProTopic.mdTitle = _inputTitleView.text;
self.curProTopic.mdContent = _inputContentView.text;
cell.curTopic = self.curProTopic;
__weak typeof(self) weakSelf = self;
cell.cellHeightChangedBlock = ^(){
[weakSelf.preView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
};
cell.addLabelBlock = ^(){
[weakSelf addtitleBtnClick];
};
cell.delLabelBlock = ^(){
weakSelf.navigationItem.rightBarButtonItem.enabled = YES;
};
cell.clickedLinkStrBlock = ^(NSString *linkStr){
[weakSelf analyseLinkStr:linkStr];
};
//[tableView addLineforPlainCell:cell forRowAtIndexPath:indexPath withLeftSpace:0];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return (self.type == TopicEditTypeFeedBack ? [TopicPreviewCell cellHeightWithObj:self.curProTopic] : [TopicPreviewCell cellHeightWithObjWithLabel:self.curProTopic]);
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark analyseLinkStr
- (void)analyseLinkStr:(NSString *)linkStr
{
if (linkStr.length <= 0) {
return;
}
UIViewController *vc = [BaseViewController analyseVCFromLinkStr:linkStr];
if (vc) {
[self.navigationController pushViewController:vc animated:YES];
}else{
// 跳转去网页
WebViewController *webVc = [WebViewController webVCWithUrlStr:linkStr];
[self.navigationController pushViewController:webVc animated:YES];
}
}
@end