Skip to content

Commit 9c39e80

Browse files
committed
文件下载详情
1 parent 545f3b3 commit 9c39e80

21 files changed

+775
-146
lines changed

Coding_iOS.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
/* Begin PBXBuildFile section */
1010
4E15C7D01A26D2F000FB8DAD /* FolderToMoveViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4E15C7CF1A26D2F000FB8DAD /* FolderToMoveViewController.m */; };
1111
4E15C7D61A271A6300FB8DAD /* EaseToolBar.m in Sources */ = {isa = PBXBuildFile; fileRef = 4E15C7D51A271A6300FB8DAD /* EaseToolBar.m */; };
12+
4E2906A21A403B7D008A5B97 /* FileDownloadView.m in Sources */ = {isa = PBXBuildFile; fileRef = 4E2906A11A403B7D008A5B97 /* FileDownloadView.m */; };
13+
4E35A99F1A3EC47E00CE35F1 /* FileViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4E35A99E1A3EC47E00CE35F1 /* FileViewController.m */; };
1214
4E3AD3CF1A273A4A005C749B /* FAImageView.m in Sources */ = {isa = PBXBuildFile; fileRef = 4E3AD3C11A273A4A005C749B /* FAImageView.m */; };
1315
4E3AD3D01A273A4A005C749B /* NSString+FontAwesome.m in Sources */ = {isa = PBXBuildFile; fileRef = 4E3AD3C31A273A4A005C749B /* NSString+FontAwesome.m */; };
1416
4E3AD3D11A273A4A005C749B /* FontAwesome.ttf in Resources */ = {isa = PBXBuildFile; fileRef = 4E3AD3C51A273A4A005C749B /* FontAwesome.ttf */; };
@@ -483,6 +485,10 @@
483485
4E15C7CF1A26D2F000FB8DAD /* FolderToMoveViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FolderToMoveViewController.m; sourceTree = "<group>"; };
484486
4E15C7D41A271A6300FB8DAD /* EaseToolBar.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EaseToolBar.h; sourceTree = "<group>"; };
485487
4E15C7D51A271A6300FB8DAD /* EaseToolBar.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EaseToolBar.m; sourceTree = "<group>"; };
488+
4E2906A01A403B7D008A5B97 /* FileDownloadView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FileDownloadView.h; sourceTree = "<group>"; };
489+
4E2906A11A403B7D008A5B97 /* FileDownloadView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FileDownloadView.m; sourceTree = "<group>"; };
490+
4E35A99D1A3EC47E00CE35F1 /* FileViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FileViewController.h; sourceTree = "<group>"; };
491+
4E35A99E1A3EC47E00CE35F1 /* FileViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FileViewController.m; sourceTree = "<group>"; };
486492
4E3AD3C01A273A4A005C749B /* FAImageView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FAImageView.h; sourceTree = "<group>"; };
487493
4E3AD3C11A273A4A005C749B /* FAImageView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FAImageView.m; sourceTree = "<group>"; };
488494
4E3AD3C21A273A4A005C749B /* NSString+FontAwesome.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSString+FontAwesome.h"; sourceTree = "<group>"; };
@@ -1891,6 +1897,8 @@
18911897
4EA6791F1A15AB9F001A0324 /* FileListViewController.m */,
18921898
4E15C7CE1A26D2F000FB8DAD /* FolderToMoveViewController.h */,
18931899
4E15C7CF1A26D2F000FB8DAD /* FolderToMoveViewController.m */,
1900+
4E35A99D1A3EC47E00CE35F1 /* FileViewController.h */,
1901+
4E35A99E1A3EC47E00CE35F1 /* FileViewController.m */,
18941902
);
18951903
path = Controllers;
18961904
sourceTree = "<group>";
@@ -2329,6 +2337,8 @@
23292337
8E59F0F01A00F38A009A905F /* ProjectFolderListView.m */,
23302338
8EA6D12319E240C40076D59C /* UIMessageInputView.h */,
23312339
8EA6D12419E240C40076D59C /* UIMessageInputView.m */,
2340+
4E2906A01A403B7D008A5B97 /* FileDownloadView.h */,
2341+
4E2906A11A403B7D008A5B97 /* FileDownloadView.m */,
23322342
);
23332343
path = Views;
23342344
sourceTree = "<group>";
@@ -2871,6 +2881,7 @@
28712881
8EA6D13819E240C40076D59C /* Message_RootViewController.m in Sources */,
28722882
8EF643B219FF7E2900F7EEB0 /* Input_GlobalKey_Cell.m in Sources */,
28732883
8EA6D1BD19E240C40076D59C /* QcTask.m in Sources */,
2884+
4E35A99F1A3EC47E00CE35F1 /* FileViewController.m in Sources */,
28742885
4ECE8B001A3946C10021E29C /* CodingNetAPIClient.m in Sources */,
28752886
8EF643B119FF7E2900F7EEB0 /* ConversationCell.m in Sources */,
28762887
8EF643D019FF7E2900F7EEB0 /* TweetSendImagesCell.m in Sources */,
@@ -2914,6 +2925,7 @@
29142925
4E6BA2E11A1EE6AF005FD721 /* AFNetworkActivityIndicatorManager.m in Sources */,
29152926
8EA6D1B619E240C40076D59C /* Login.m in Sources */,
29162927
8EF643C419FF7E2900F7EEB0 /* TitleDisclosureCell.m in Sources */,
2928+
4E2906A21A403B7D008A5B97 /* FileDownloadView.m in Sources */,
29172929
4E6BA2DC1A1EE6AF005FD721 /* AFNetworkReachabilityManager.m in Sources */,
29182930
8E59F0E31A0098BA009A905F /* UIScrollView+SVPullToRefresh.m in Sources */,
29192931
8EA6D13419E240C40076D59C /* ProjectMemberListViewController.m in Sources */,

Coding_iOS/Controllers/CodeViewController.m

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,8 @@ - (void)refreshCodeViewData{
9090

9191
#pragma mark UIWebViewDelegate
9292
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
93-
NSString *strLink = request.URL.absoluteString;
94-
NSLog(@"strLink=[%@]",strLink);
95-
if ([strLink rangeOfString:@"about:blank"].location != NSNotFound) {
96-
return YES;
97-
}else{
98-
return YES;
99-
// return NO;
100-
}
93+
NSLog(@"strLink=[%@]",request.URL.absoluteString);
94+
return YES;
10195
}
10296
- (void)webViewDidStartLoad:(UIWebView *)webView{
10397
[_activityIndicator startAnimating];

Coding_iOS/Controllers/FileListViewController.m

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
#import "BasicPreviewItem.h"
1919
#import "SettingTextViewController.h"
2020
#import "FolderToMoveViewController.h"
21+
#import "FileViewController.h"
2122

22-
@interface FileListViewController () <QLPreviewControllerDataSource, QLPreviewControllerDelegate,SWTableViewCellDelegate>
23+
@interface FileListViewController () <QLPreviewControllerDataSource, QLPreviewControllerDelegate, SWTableViewCellDelegate>
2324
@property (nonatomic, strong) UITableView *myTableView;
2425
@property (nonatomic, strong) ODRefreshControl *refreshControl;
2526
@property (strong, nonatomic) ProjectFiles *myFiles;
@@ -48,6 +49,13 @@ - (void)didReceiveMemoryWarning
4849
// Dispose of any resources that can be recreated.
4950
}
5051

52+
- (void)viewWillAppear:(BOOL)animated{
53+
[super viewWillAppear:animated];
54+
if (self.myTableView) {
55+
[self.myTableView reloadData];
56+
}
57+
}
58+
5159
- (void)loadView{
5260
[super loadView];
5361
self.title = self.curFolder.name;
@@ -198,8 +206,11 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
198206
ProjectFolder *clickedFolder = [_curFolder.sub_folders objectAtIndex:indexPath.row];;
199207
[self goToVCWithFolder:clickedFolder inProject:self.curProject];
200208
}else{
201-
FileListFileCell *cell = (FileListFileCell *)[tableView cellForRowAtIndexPath:indexPath];
202-
[cell clickedByUser];
209+
// FileListFileCell *cell = (FileListFileCell *)[tableView cellForRowAtIndexPath:indexPath];
210+
// [cell clickedByUser];
211+
212+
ProjectFile *file = [_myFiles.list objectAtIndex:(indexPath.row - _curFolder.sub_folders.count)];
213+
[self goToFileVC:file];
203214
}
204215
}
205216

@@ -434,6 +445,12 @@ - (void)goToVCWithFolder:(ProjectFolder *)folder inProject:(Project *)project{
434445
[self.navigationController pushViewController:vc animated:YES];
435446
}
436447

448+
- (void)goToFileVC:(ProjectFile *)file{
449+
FileViewController *vc = [[FileViewController alloc] init];
450+
vc.curFile = file;
451+
[self.navigationController pushViewController:vc animated:YES];
452+
}
453+
437454
- (void)showDiskFile:(NSURL *)fileUrl{
438455
QLPreviewController *previewController = [[QLPreviewController alloc] init];
439456
previewController.dataSource = self;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// FileViewController.h
3+
// Coding_iOS
4+
//
5+
// Created by Ease on 14/12/15.
6+
// Copyright (c) 2014年 Coding. All rights reserved.
7+
//
8+
#import <QuickLook/QuickLook.h>
9+
#import "BaseViewController.h"
10+
#import "ProjectFolder.h"
11+
#import "ProjectFolders.h"
12+
#import "ProjectFile.h"
13+
14+
@interface FileViewController : BaseViewController
15+
@property (strong, nonatomic) ProjectFile *curFile;
16+
@end
Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
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

Comments
 (0)