99#import " AddTopicViewController.h"
1010#import " ProjectTopic.h"
1111#import " Coding_NetAPIManager.h"
12- #import " UIPlaceHolderTextView.h"
12+ #import " EaseMarkdownTextView.h"
13+ #import " WebContentManager.h"
1314
14- @interface AddTopicViewController ()
15- @property (strong , nonatomic ) UITextField *inputTitleView;
16- @property (strong , nonatomic ) UIPlaceHolderTextView *inputContentView;
15+ @interface AddTopicViewController ()<UIWebViewDelegate>
1716@property (strong , nonatomic ) ProjectTopic *myProTopic;
17+
18+
19+ @property (strong , nonatomic ) UISegmentedControl *segmentedControl;
20+ @property (assign , nonatomic ) NSInteger curIndex;
21+
22+ @property (strong , nonatomic ) UIWebView *preview;
23+ @property (strong , nonatomic ) UIActivityIndicatorView *activityIndicator;
24+
25+ @property (strong , nonatomic ) UIView *editView;
26+ @property (strong , nonatomic ) UITextField *inputTitleView;
27+ @property (strong , nonatomic ) EaseMarkdownTextView *inputContentView;
28+
29+
1830@end
1931
2032@implementation AddTopicViewController
@@ -32,36 +44,45 @@ - (void)viewDidLoad
3244{
3345 [super viewDidLoad ];
3446 // Do any additional setup after loading the view.
35- self.view .backgroundColor = [UIColor colorWithHexString: @" 0xe5e5e5" ];
36- self.title = @" 创建讨论" ;
37-
38- CGRect frame = CGRectMake (0 , 30 , kScreen_Width , 44 );
39- UIView *bgWhiteView = [[UIView alloc ] initWithFrame: frame];
40- bgWhiteView.backgroundColor = [UIColor whiteColor ];
41- [bgWhiteView addLineUp: YES andDown: YES ];
42- [self .view addSubview: bgWhiteView];
4347
44- CGRect frameTitle = CGRectInset (frame, 5 , kPaddingLeftWidth );
48+ self.myProTopic = [ProjectTopic topicWithPro: self .curProject];
49+
50+ if (!_segmentedControl) {
51+ _segmentedControl = ({
52+ UISegmentedControl *segmentedControl = [[UISegmentedControl alloc ] initWithItems: @[@" 编辑" , @" 预览" ]];
53+ [segmentedControl setWidth: 80 forSegmentAtIndex: 0 ];
54+ [segmentedControl setWidth: 80 forSegmentAtIndex: 1 ];
55+ [segmentedControl setTitleTextAttributes: @{
56+ NSFontAttributeName : [UIFont boldSystemFontOfSize: 16 ],
57+ NSForegroundColorAttributeName : [UIColor colorWithHexString: @" 0x28303b" ]
58+ }
59+ forState: UIControlStateSelected];
60+ [segmentedControl setTitleTextAttributes: @{
61+ NSFontAttributeName : [UIFont boldSystemFontOfSize: 16 ],
62+ NSForegroundColorAttributeName : [UIColor whiteColor ]
63+ } forState: UIControlStateNormal];
64+ [segmentedControl addTarget: self action: @selector (segmentedControlSelected: ) forControlEvents: UIControlEventValueChanged];
65+ segmentedControl;
66+ });
67+
68+ self.navigationItem .titleView = _segmentedControl;
69+ }
4570
46- _inputTitleView = [[UITextField alloc ] initWithFrame: frameTitle];
47- _inputTitleView.font = [UIFont boldSystemFontOfSize: 15 ];
48- _inputTitleView.placeholder = @" 输入讨论标题" ;
49- [_inputTitleView setValue: [UIColor lightGrayColor ] forKeyPath: @" _placeholderLabel.textColor" ];
50- [_inputTitleView setValue: [UIFont boldSystemFontOfSize: 15 ] forKeyPath: @" _placeholderLabel.font" ];
51- // [_inputTitleView becomeFirstResponder];
52- [self .view addSubview: _inputTitleView];
71+ [self .navigationItem setRightBarButtonItem: [UIBarButtonItem itemWithBtnTitle: @" 完成" target: self action: @selector (saveBtnClicked )] animated: YES ];
72+ self.navigationItem .rightBarButtonItem .enabled = NO ;
5373
54- frame.origin .y += 44 +20 ;
55- frame.size .height = kScreen_Height -64 - 55 -280 -kHigher_iOS_6_1_DIS (20 );
56- _inputContentView = [[UIPlaceHolderTextView alloc ] initWithFrame: frame];
57- _inputContentView.font = [UIFont systemFontOfSize: 15 ];
58- _inputContentView.placeholder = @" 讨论内容" ;
59- [_inputContentView addLineUp: YES andDown: YES ];
60- [self .view addSubview: _inputContentView];
74+
6175
62- self.navigationItem .rightBarButtonItem = [UIBarButtonItem itemWithBtnTitle: @" 完成" target: self action: @selector (addTopicBtnClicked: )];
76+ [[[[NSNotificationCenter defaultCenter ] rac_addObserverForName: UIKeyboardWillChangeFrameNotification object: nil ] takeUntil: self .rac_willDeallocSignal] subscribeNext: ^(NSNotification *aNotification) {
77+ if (self.inputContentView ) {
78+ NSDictionary * userInfo = [aNotification userInfo ];
79+ CGRect keyboardEndFrame = [[userInfo objectForKey: UIKeyboardFrameEndUserInfoKey] CGRectValue ];
80+ self.inputContentView .contentInset = UIEdgeInsetsMake (0 , 0 , CGRectGetHeight (keyboardEndFrame), 0 );
81+ }
82+ }];
83+
84+ self.curIndex = 0 ;
6385
64- self.myProTopic = [ProjectTopic topicWithPro: self .curProject];
6586}
6687
6788- (void )didReceiveMemoryWarning
@@ -70,7 +91,136 @@ - (void)didReceiveMemoryWarning
7091 // Dispose of any resources that can be recreated.
7192}
7293
73- - (void )addTopicBtnClicked : (id )sender {
94+ #pragma mark UISegmentedControl
95+
96+ - (void )segmentedControlSelected : (id )sender {
97+ UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
98+ self.curIndex = segmentedControl.selectedSegmentIndex ;
99+ }
100+
101+ #pragma mark index_view
102+
103+ - (void )setCurIndex : (NSInteger )curIndex {
104+ _curIndex = curIndex;
105+ if (_segmentedControl.selectedSegmentIndex != curIndex) {
106+ [_segmentedControl setSelectedSegmentIndex: _curIndex];
107+ }
108+
109+ if (_curIndex == 0 ) {
110+ [self loadEditView ];
111+ }else {
112+ [self loadPreview ];
113+ }
114+ }
115+
116+ - (void )loadEditView {
117+ if (!_editView) {
118+ // 控件
119+ _editView = [[UIView alloc ] initWithFrame: self .view.bounds];
120+
121+ _inputTitleView = [[UITextField alloc ] initWithFrame: CGRectZero];
122+ _inputTitleView.textColor = [UIColor colorWithHexString: @" 0x222222" ];
123+
124+
125+ _inputTitleView.font = [UIFont boldSystemFontOfSize: 18 ];
126+ [_editView addSubview: _inputTitleView];
127+
128+ _inputContentView = [[EaseMarkdownTextView alloc ] initWithFrame: CGRectZero];
129+ _inputContentView.textColor = [UIColor colorWithHexString: @" 0x666666" ];
130+
131+ _inputContentView.backgroundColor = [UIColor clearColor ];
132+ _inputContentView.font = [UIFont systemFontOfSize: 15 ];
133+ [_editView addSubview: _inputContentView];
134+
135+ UIView *lineView = [[UIView alloc ] initWithFrame: CGRectZero];
136+ lineView.backgroundColor = kColorTableSectionBg ;
137+ [_editView addSubview: lineView];
138+
139+ [self .view addSubview: _editView];
140+
141+ // 布局
142+ _inputContentView.textContainerInset = UIEdgeInsetsMake (10 , kPaddingLeftWidth - 5 , 8 , kPaddingLeftWidth - 5 );
143+
144+ [_editView mas_makeConstraints: ^(MASConstraintMaker *make) {
145+ make.edges .equalTo (self.view );
146+ }];
147+ [_inputTitleView mas_makeConstraints: ^(MASConstraintMaker *make) {
148+ make.top .equalTo (_editView.mas_top ).offset (20.0 );
149+ make.height .mas_equalTo (20 );
150+
151+ make.left .equalTo (_editView).offset (kPaddingLeftWidth );
152+ make.right .equalTo (_editView).offset (-kPaddingLeftWidth );
153+ }];
154+ [lineView mas_makeConstraints: ^(MASConstraintMaker *make) {
155+ make.top .equalTo (_inputTitleView.mas_bottom ).offset (5.0 );
156+ make.left .equalTo (_editView).offset (kPaddingLeftWidth );
157+ make.height .mas_equalTo (1.0 );
158+ make.right .equalTo (_editView);
159+ }];
160+ [_inputContentView mas_makeConstraints: ^(MASConstraintMaker *make) {
161+ make.top .equalTo (lineView.mas_bottom );
162+ make.left .right .bottom .equalTo (_editView);
163+ }];
164+
165+ // 内容
166+ RAC (self.navigationItem .rightBarButtonItem , enabled) = [RACSignal combineLatest: @[self .inputTitleView.rac_textSignal, self .inputContentView.rac_textSignal] reduce: ^id (NSString *title, NSString *content){
167+ BOOL enabled = ([title stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet ]].length > 0
168+ && [content stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet ]].length > 0 );
169+ return @(enabled);
170+ }];
171+ _inputTitleView.placeholder = @" 讨论标题" ;
172+ [_inputTitleView setValue: [UIColor lightGrayColor ] forKeyPath: @" _placeholderLabel.textColor" ];
173+ _inputContentView.placeholder = @" 讨论内容" ;
174+ }
175+ _editView.hidden = NO ;
176+ _preview.hidden = YES ;
177+ }
178+
179+ - (void )loadPreview {
180+ if (!_preview) {
181+ _preview = [[UIWebView alloc ] initWithFrame: self .view.bounds];
182+ _preview.delegate = self;
183+ _preview.backgroundColor = [UIColor clearColor ];
184+ _preview.opaque = NO ;
185+ _preview.scalesPageToFit = YES ;
186+
187+ // webview加载指示
188+ _activityIndicator = [[UIActivityIndicatorView alloc ]
189+ initWithActivityIndicatorStyle:
190+ UIActivityIndicatorViewStyleGray];
191+ _activityIndicator.hidesWhenStopped = YES ;
192+ [_preview addSubview: _activityIndicator];
193+ [self .view addSubview: _preview];
194+
195+ [_preview mas_makeConstraints: ^(MASConstraintMaker *make) {
196+ make.edges .equalTo (self.view );
197+ }];
198+ [_activityIndicator mas_makeConstraints: ^(MASConstraintMaker *make) {
199+ make.center .equalTo (_preview);
200+ }];
201+ }
202+ _preview.hidden = NO ;
203+ _editView.hidden = YES ;
204+ [_editView endEditing: YES ];
205+ [self previewLoadMDData ];
206+ }
207+
208+ - (void )previewLoadMDData {
209+ NSString *mdStr = [NSString stringWithFormat: @" # %@ \n\n %@ " , _inputTitleView.text, _inputContentView.text];
210+ [_activityIndicator startAnimating ];
211+
212+ @weakify (self);
213+ [[Coding_NetAPIManager sharedManager ] request_MDHtmlStr_WithMDStr: mdStr andBlock: ^(id data, NSError *error) {
214+ @strongify (self);
215+ NSString *htmlStr = data? data : error.description ;
216+ NSString *contentStr = [WebContentManager markdownPatternedWithContent: htmlStr];
217+ [self .preview loadHTMLString: contentStr baseURL: nil ];
218+ }];
219+ }
220+
221+ #pragma mark nav_btn
222+
223+ - (void )saveBtnClicked {
74224 self.myProTopic .title = _inputTitleView.text ;
75225 self.myProTopic .content = _inputContentView.text ;
76226
@@ -88,4 +238,26 @@ - (void)addTopicBtnClicked:(id)sender{
88238 }];
89239}
90240
241+
242+ #pragma mark UIWebViewDelegate
243+ - (BOOL )webView : (UIWebView *)webView shouldStartLoadWithRequest : (NSURLRequest *)request navigationType : (UIWebViewNavigationType)navigationType {
244+ NSLog (@" strLink=[%@ ]" ,request.URL .absoluteString );
245+ return YES ;
246+ }
247+ - (void )webViewDidStartLoad : (UIWebView *)webView {
248+ [_activityIndicator startAnimating ];
249+ }
250+ - (void )webViewDidFinishLoad : (UIWebView *)webView {
251+ [_activityIndicator stopAnimating ];
252+ }
253+
254+ - (void )webView : (UIWebView *)webView didFailLoadWithError : (NSError *)error {
255+ if ([error code ] == NSURLErrorCancelled)
256+ return ;
257+ else {
258+ DebugLog (@" %@ " , error.description );
259+ [self showError: error];
260+ }
261+ }
262+
91263@end
0 commit comments