1010#import " Coding_NetAPIManager.h"
1111#import " WebContentManager.h"
1212#import " CodeViewController.h"
13+ #import " AddMDCommentViewController.h"
1314
1415
1516@interface FileChangeDetailViewController ()<UIWebViewDelegate>
1617@property (strong , nonatomic ) UIWebView *webContentView;
1718@property (strong , nonatomic ) UIActivityIndicatorView *activityIndicator;
18- @property (strong , nonatomic ) NSDictionary *rawData;
19+ @property (strong , nonatomic ) NSDictionary *rawData, *commentsData ;
1920@property (strong , nonatomic ) NSString *linkRef;
2021@end
2122
@@ -50,15 +51,13 @@ - (void)viewDidLoad {
5051
5152- (void )refresh {
5253 [self .view beginLoading ];
53-
54- [[CodingNetAPIClient sharedJsonClient ] requestJsonDataWithPath: self .linkUrlStr withParams: nil withMethodType: Get andBlock: ^(id data, NSError *error) {
54+ [[Coding_NetAPIManager sharedManager ] request_FileDiffDetailWithPath: self .linkUrlStr andBlock: ^(id data, NSError *error) {
5555 [self .view endLoading ];
56-
57- data = [data valueForKey: @" data" ];
5856 if (data) {
59- self.rawData = @{@" data" : data};
60- if ([data isKindOfClass: [NSDictionary class ]]) {
61- self.linkRef = [data objectForKey: @" linkRef" ];
57+ self.rawData = data[@" rawData" ];
58+ self.commentsData = data[@" commentsData" ];
59+ if ([self .rawData isKindOfClass: [NSDictionary class ]]) {
60+ self.linkRef = self.rawData [@" data" ][@" linkRef" ];
6261 }
6362 [self refreshUI ];
6463 }
@@ -70,9 +69,13 @@ - (void)refresh{
7069
7170- (void )refreshUI {
7271 if (self.rawData ) {
73- NSData *JSONData = [NSJSONSerialization dataWithJSONObject: self .rawData options: NSJSONWritingPrettyPrinted error: nil ];
74- NSString *contentStr = [[NSString alloc ] initWithData: JSONData encoding: NSUTF8StringEncoding];
75- contentStr = [WebContentManager diffPatternedWithContent: contentStr];
72+ NSData *JSONDataRaw = [NSJSONSerialization dataWithJSONObject: self .rawData options: NSJSONWritingPrettyPrinted error: nil ];
73+ NSString *contentStr = [[NSString alloc ] initWithData: JSONDataRaw encoding: NSUTF8StringEncoding];
74+
75+ NSData *JSONDataComments = [NSJSONSerialization dataWithJSONObject: self .commentsData options: NSJSONWritingPrettyPrinted error: nil ];
76+ NSString *commentsStr = [[NSString alloc ] initWithData: JSONDataComments encoding: NSUTF8StringEncoding];
77+
78+ contentStr = [WebContentManager diffPatternedWithContent: contentStr andComments: commentsStr];
7679 [self .webContentView loadHTMLString: contentStr baseURL: nil ];
7780 }
7881 self.navigationItem .rightBarButtonItem = self.linkRef .length > 0 ? [UIBarButtonItem itemWithBtnTitle: @" 查看文件" target: self action: @selector (rightBarButtonClicked: )]: nil ;
@@ -96,7 +99,14 @@ - (UIInterfaceOrientationMask)supportedInterfaceOrientations{
9699#pragma mark UIWebViewDelegate
97100- (BOOL )webView : (UIWebView *)webView shouldStartLoadWithRequest : (NSURLRequest *)request navigationType : (UIWebViewNavigationType)navigationType {
98101 DebugLog (@" strLink=[%@ ]" ,request.URL .absoluteString );
99- return YES ;
102+ NSString *strLink = request.URL .absoluteString ;
103+ if ([strLink hasPrefix: @" coding://" ]) {
104+ [self handleURL: request.URL ];
105+ return NO ;
106+ }
107+ else {
108+ return YES ;
109+ }
100110}
101111- (void )webViewDidStartLoad : (UIWebView *)webView {
102112 [_activityIndicator startAnimating ];
@@ -111,4 +121,77 @@ - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
111121 else
112122 DebugLog (@" %@ " , error.description );
113123}
124+ #pragma mark Clicked One Line
125+ - (void )handleURL : (NSURL *)curURL {
126+ NSMutableDictionary *params = [self getParamsFromURLStr: curURL.absoluteString];
127+ if ([curURL.absoluteString hasPrefix: @" coding://line_note?" ]) {
128+ NSString *title = [NSString stringWithFormat: @" Line %@ " , params[@" line" ]];
129+ [[UIActionSheet bk_actionSheetCustomWithTitle: title buttonTitles: @[@" 添加评论" ] destructiveTitle: nil cancelTitle: @" 取消" andDidDismissBlock: ^(UIActionSheet *sheet, NSInteger index) {
130+ if (index == 0 ) {
131+ [self goToAddCommentWithParams: params];
132+ }
133+ }] showInView: self .view];
134+ }else if ([curURL.absoluteString hasPrefix: @" coding://line_note_comment?" ]){
135+ NSString *title = [NSString stringWithFormat: @" %@ 的评论" , params[@" clicked_user_name" ]];
136+ BOOL belongToSelf = [params[@" clicked_user_name" ] isEqualToString: [Login curLoginUser ].global_key];
137+ [[UIActionSheet bk_actionSheetCustomWithTitle: title buttonTitles: @[belongToSelf? @" 删除" : @" 回复" ] destructiveTitle: nil cancelTitle: @" 取消" andDidDismissBlock: ^(UIActionSheet *sheet, NSInteger index) {
138+ if (index == 0 ) {
139+ if (belongToSelf) {
140+ [self doDeleteCommentWithParams: params];
141+ }else {
142+ [self goToAddCommentWithParams: params];
143+ }
144+ }
145+ }] showInView: self .view];
146+ }
147+ }
148+
149+ - (NSMutableDictionary *)getParamsFromURLStr : (NSString *)urlStr {
150+ urlStr = [[urlStr componentsSeparatedByString: @" ?" ] lastObject ];
151+ NSMutableDictionary *params = [NSMutableDictionary new ];
152+ for (NSString *param in [urlStr componentsSeparatedByString: @" &" ]) {
153+ NSArray *elts = [param componentsSeparatedByString: @" =" ];
154+ if ([elts count ] < 2 ) continue ;
155+ [params setObject: [elts objectAtIndex: 1 ] forKey: [elts objectAtIndex: 0 ]];
156+ }
157+ return params;
158+ }
159+
160+ - (void )goToAddCommentWithParams : (NSMutableDictionary *)params {
161+ AddMDCommentViewController *vc = [AddMDCommentViewController new ];
162+ vc.curProject = _curProject;
163+
164+ NSString *requestPath = [[self .linkUrlStr componentsSeparatedByString: @" /git" ] firstObject ];
165+ requestPath = [requestPath stringByAppendingString: @" /git/line_notes" ];
166+ vc.requestPath = requestPath;
167+
168+ vc.contentStr = params[@" clicked_user_name" ]? [NSString stringWithFormat: @" @%@ " , params[@" clicked_user_name" ]] : @" " ;
169+
170+ [params removeObjectsForKeys: @[@" clicked_line_note_id" , @" clicked_user_name" , @" clicked_user_name" ]];
171+ if (self.noteable_id ) {
172+ params[@" noteable_id" ] = self.noteable_id ;
173+ }
174+ if ([params[@" noteable_type" ] hasSuffix: @" Request" ]) {
175+ params[@" noteable_type" ] = [params[@" noteable_type" ] stringByAppendingString: @" Bean" ];
176+ }
177+ vc.requestParams = params;
178+
179+ @weakify (self);
180+ vc.completeBlock = ^(id data){
181+ @strongify (self);
182+ if (data) {
183+ [self refresh ];
184+ }
185+ };
186+ [self .navigationController pushViewController: vc animated: YES ];
187+ }
188+
189+ - (void )doDeleteCommentWithParams : (NSMutableDictionary *)params {
190+ NSString *requestPath = [[self .linkUrlStr componentsSeparatedByString: @" /git" ] firstObject ];
191+ requestPath = [requestPath stringByAppendingFormat: @" /git/line_notes/%@ " , params[@" clicked_line_note_id" ]];
192+ [[Coding_NetAPIManager sharedManager ] request_DeleteLineNoteWithPath: requestPath andBlock: ^(id data, NSError *error) {
193+ [self refresh ];
194+ }];
195+ }
196+
114197@end
0 commit comments