|
| 1 | +// |
| 2 | +// FileViewController.m |
| 3 | +// Coding_iOS |
| 4 | +// |
| 5 | +// Created by Ease on 14/12/15. |
| 6 | +// Copyright (c) 2014年 Coding. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "FileViewController.h" |
| 10 | +#import "BasicPreviewItem.h" |
| 11 | +#import "FileDownloadView.h" |
| 12 | +#import "Coding_NetAPIManager.h" |
| 13 | +#import "WebContentManager.h" |
| 14 | +#import <MMMarkdown/MMMarkdown.h> |
| 15 | + |
| 16 | +@interface FileViewController () <QLPreviewControllerDataSource, QLPreviewControllerDelegate, UIDocumentInteractionControllerDelegate, UIWebViewDelegate> |
| 17 | +@property (strong, nonatomic) NSURL *fileUrl; |
| 18 | +@property (strong, nonatomic) QLPreviewController *previewController; |
| 19 | +@property (strong, nonatomic) FileDownloadView *downloadView; |
| 20 | +@property (nonatomic, strong) UIDocumentInteractionController *docInteractionController; |
| 21 | + |
| 22 | +@property (strong, nonatomic) UIWebView *contentWebView; |
| 23 | +@property (strong, nonatomic) UIActivityIndicatorView *activityIndicator; |
| 24 | + |
| 25 | +@end |
| 26 | + |
| 27 | +@implementation FileViewController |
| 28 | + |
| 29 | +- (void)viewDidLoad { |
| 30 | + [super viewDidLoad]; |
| 31 | + // Do any additional setup after loading the view. |
| 32 | +} |
| 33 | + |
| 34 | +- (void)didReceiveMemoryWarning { |
| 35 | + [super didReceiveMemoryWarning]; |
| 36 | + // Dispose of any resources that can be recreated. |
| 37 | +} |
| 38 | + |
| 39 | +- (void)loadView{ |
| 40 | + [super loadView]; |
| 41 | + self.title = self.curFile.name; |
| 42 | + self.view = [[UIView alloc] initWithFrame:[UIView frameWithOutNav]]; |
| 43 | + self.view.backgroundColor = [UIColor whiteColor]; |
| 44 | + |
| 45 | + if ([self.curFile isEmpty]) { |
| 46 | + [self requestFileData]; |
| 47 | + }else{ |
| 48 | + [self configContent]; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +- (void)viewWillDisappear:(BOOL)animated{ |
| 53 | + [super viewDidDisappear:animated]; |
| 54 | + |
| 55 | + [self.navigationController setNavigationBarHidden:NO]; |
| 56 | + [UIApplication sharedApplication].statusBarHidden = NO; |
| 57 | +} |
| 58 | + |
| 59 | +- (void)requestFileData{ |
| 60 | + [self.view beginLoading]; |
| 61 | + __weak typeof(self) weakSelf = self; |
| 62 | + [[Coding_NetAPIManager sharedManager] request_FileDetail:self.curFile andBlock:^(id data, NSError *error) { |
| 63 | + [weakSelf.view endLoading]; |
| 64 | + |
| 65 | + if (data) { |
| 66 | + weakSelf.curFile = data; |
| 67 | + [weakSelf configContent]; |
| 68 | + }else{ |
| 69 | + [weakSelf.view configBlankPage:EaseBlankPageTypeView hasData:NO hasError:(error != nil) reloadButtonBlock:^(id sender) { |
| 70 | + [weakSelf requestFileData]; |
| 71 | + }]; |
| 72 | + } |
| 73 | + }]; |
| 74 | +} |
| 75 | + |
| 76 | +- (void)configContent{ |
| 77 | + self.title = self.curFile.name; |
| 78 | + NSURL *fileUrl = [self.curFile hasBeenDownload]; |
| 79 | + |
| 80 | + if (!fileUrl ) { |
| 81 | + [self showDownloadView]; |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + self.fileUrl = fileUrl; |
| 86 | + [self setupDocumentControllerWithURL:fileUrl]; |
| 87 | + |
| 88 | + if ([self.curFile.fileType isEqualToString:@"md"] |
| 89 | + || [self.curFile.fileType isEqualToString:@"html"] |
| 90 | + || [self.curFile.fileType isEqualToString:@"txt"] |
| 91 | + || [self.curFile.fileType isEqualToString:@"plist"]){ |
| 92 | + [self loadWebView:fileUrl]; |
| 93 | + }else if ([QLPreviewController canPreviewItem:fileUrl]) { |
| 94 | + [self showDiskFile:fileUrl]; |
| 95 | + }else { |
| 96 | + [self showDownloadView]; |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | + |
| 101 | +- (void)showDiskFile:(NSURL *)fileUrl{ |
| 102 | + if (self.downloadView) { |
| 103 | + self.downloadView.hidden = YES; |
| 104 | + } |
| 105 | + |
| 106 | + CGRect frame = self.view.bounds; |
| 107 | + QLPreviewController* preview = [[QLPreviewController alloc] init]; |
| 108 | + preview.dataSource = self; |
| 109 | + preview.delegate = self; |
| 110 | + preview.view.frame = frame; |
| 111 | + |
| 112 | + [self.view addSubview:preview.view]; |
| 113 | + self.previewController = preview; |
| 114 | +} |
| 115 | + |
| 116 | +- (void)loadWebView:(NSURL *)fileUrl{ |
| 117 | + if (self.downloadView) { |
| 118 | + self.downloadView.hidden = YES; |
| 119 | + } |
| 120 | + |
| 121 | + if (!_contentWebView) { |
| 122 | + //用webView显示内容 |
| 123 | + _contentWebView = [[UIWebView alloc] initWithFrame:self.view.bounds]; |
| 124 | + _contentWebView.delegate = self; |
| 125 | + _contentWebView.backgroundColor = [UIColor clearColor]; |
| 126 | + _contentWebView.opaque = NO; |
| 127 | + _contentWebView.scalesPageToFit = YES; |
| 128 | + //webview加载指示 |
| 129 | + _activityIndicator = [[UIActivityIndicatorView alloc] |
| 130 | + initWithActivityIndicatorStyle: |
| 131 | + UIActivityIndicatorViewStyleGray]; |
| 132 | + _activityIndicator.hidesWhenStopped = YES; |
| 133 | + [_activityIndicator setCenter:CGPointMake(CGRectGetWidth(_contentWebView.frame)/2, CGRectGetHeight(_contentWebView.frame)/2)]; |
| 134 | + [_contentWebView addSubview:_activityIndicator]; |
| 135 | + [self.view addSubview:_contentWebView]; |
| 136 | + } |
| 137 | + if ([self.curFile.fileType isEqualToString:@"md"]){ |
| 138 | +// NSData *mdData = [NSData dataWithContentsOfURL:fileUrl]; |
| 139 | +// NSString *mdStr = [[NSString alloc] initWithData:mdData encoding:NSUTF8StringEncoding]; |
| 140 | + NSError *error = nil; |
| 141 | + NSString *htmlStr; |
| 142 | + @try { |
| 143 | + NSString *mdStr = [NSString stringWithContentsOfURL:fileUrl encoding:NSUTF8StringEncoding error:&error]; |
| 144 | + htmlStr = [MMMarkdown HTMLStringWithMarkdown:mdStr error:&error]; |
| 145 | + } |
| 146 | + @catch (NSException *exception) { |
| 147 | + htmlStr = @"加载失败!"; |
| 148 | + } |
| 149 | + |
| 150 | + if (error) { |
| 151 | + htmlStr = @"加载失败!"; |
| 152 | + } |
| 153 | + NSString *contentStr = [WebContentManager markdownPatternedWithContent:htmlStr]; |
| 154 | + [self.contentWebView loadHTMLString:contentStr baseURL:nil]; |
| 155 | + }else if ([self.curFile.fileType isEqualToString:@"html"]){ |
| 156 | + NSString* htmlString = [NSString stringWithContentsOfURL:fileUrl encoding:NSUTF8StringEncoding error:nil]; |
| 157 | + [self.contentWebView loadHTMLString:htmlString baseURL:nil]; |
| 158 | + }else if ([self.curFile.fileType isEqualToString:@"plist"] |
| 159 | + || [self.curFile.fileType isEqualToString:@"txt"]){ |
| 160 | + NSData *fileData = [NSData dataWithContentsOfURL:fileUrl]; |
| 161 | + [self.contentWebView loadData:fileData MIMEType:@"text/text" textEncodingName:@"UTF-8" baseURL:nil]; |
| 162 | + }else{ |
| 163 | + [self.contentWebView loadRequest:[NSURLRequest requestWithURL:fileUrl]]; |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +- (void)showDownloadView{ |
| 168 | + if (!self.downloadView) { |
| 169 | + self.downloadView = [[FileDownloadView alloc] initWithFrame:self.view.bounds]; |
| 170 | + self.downloadView.file = self.curFile; |
| 171 | + [self.view addSubview:self.downloadView]; |
| 172 | + } |
| 173 | + __weak typeof(self) weakSelf = self; |
| 174 | + self.downloadView.hidden = NO; |
| 175 | + self.downloadView.completionBlock = ^(){ |
| 176 | + [weakSelf configContent]; |
| 177 | + }; |
| 178 | + self.downloadView.goToFileBlock = ^(ProjectFile *file){ |
| 179 | + [weakSelf.docInteractionController presentOpenInMenuFromBarButtonItem:weakSelf.navigationItem.rightBarButtonItem animated:YES]; |
| 180 | + }; |
| 181 | +} |
| 182 | + |
| 183 | +- (void)setupDocumentControllerWithURL:(NSURL *)url{ |
| 184 | + if (self.docInteractionController == nil){ |
| 185 | + self.docInteractionController = [UIDocumentInteractionController interactionControllerWithURL:url]; |
| 186 | + }else{ |
| 187 | + self.docInteractionController.URL = url; |
| 188 | + } |
| 189 | + [self setupNavigationItem]; |
| 190 | +} |
| 191 | + |
| 192 | +- (void)setupNavigationItem{ |
| 193 | + UIButton *itemButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 19, 19)]; |
| 194 | + [itemButton setImage:[UIImage imageNamed:@"addBtn_Nav"] forState:UIControlStateNormal]; |
| 195 | + @weakify(self); |
| 196 | + [[itemButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) { |
| 197 | + @strongify(self); |
| 198 | + [self.docInteractionController presentOpenInMenuFromBarButtonItem:self.navigationItem.rightBarButtonItem animated:YES]; |
| 199 | + }]; |
| 200 | + |
| 201 | + if (self.fileUrl) { |
| 202 | + self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:itemButton]; |
| 203 | + }else{ |
| 204 | + self.navigationItem.rightBarButtonItem = nil; |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +#pragma mark - QLPreviewControllerDataSource |
| 209 | +- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller{ |
| 210 | + NSInteger num = 0; |
| 211 | + if (self.fileUrl) { |
| 212 | + num = 1; |
| 213 | + } |
| 214 | + return num; |
| 215 | +} |
| 216 | + |
| 217 | +- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index{ |
| 218 | + return [BasicPreviewItem itemWithUrl:self.fileUrl]; |
| 219 | +} |
| 220 | + |
| 221 | +#pragma mark UIWebViewDelegate |
| 222 | +- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{ |
| 223 | + NSLog(@"strLink=[%@]",request.URL.absoluteString); |
| 224 | + return YES; |
| 225 | +} |
| 226 | +- (void)webViewDidStartLoad:(UIWebView *)webView{ |
| 227 | + [_activityIndicator startAnimating]; |
| 228 | +} |
| 229 | +- (void)webViewDidFinishLoad:(UIWebView *)webView{ |
| 230 | + [_activityIndicator stopAnimating]; |
| 231 | + if ([self.curFile.fileType isEqualToString:@"plist"] |
| 232 | + || [self.curFile.fileType isEqualToString:@"txt"]){ |
| 233 | + [webView stringByEvaluatingJavaScriptFromString:@"document.body.style.zoom = 3.0;"]; |
| 234 | + }else if ([self.curFile.fileType isEqualToString:@"html"]){ |
| 235 | + [webView stringByEvaluatingJavaScriptFromString:@"document.body.style.zoom = 2.0;"]; |
| 236 | + } |
| 237 | +} |
| 238 | + |
| 239 | +- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{ |
| 240 | + if([error code] == NSURLErrorCancelled) |
| 241 | + return; |
| 242 | + else{ |
| 243 | + DebugLog(@"%@", error.description); |
| 244 | + [self showError:error]; |
| 245 | + } |
| 246 | +} |
| 247 | +@end |
| 248 | + |
| 249 | + |
| 250 | + |
| 251 | + |
0 commit comments