OpenFileViewController.m 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // OpenFileViewController.m
  3. // MusicGradeExam
  4. //
  5. // Created by Kyle on 2020/7/21.
  6. // Copyright © 2020 DayaMusic. All rights reserved.
  7. //
  8. #import "OpenFileViewController.h"
  9. #import <QuickLook/QuickLook.h>
  10. @interface OpenFileViewController ()<QLPreviewControllerDataSource, QLPreviewControllerDelegate>
  11. @property (nonatomic, strong) QLPreviewController *previewCtrl;
  12. @property (nonatomic, strong) NSMutableArray *fileArray;
  13. @end
  14. @implementation OpenFileViewController
  15. - (void)viewDidLoad {
  16. [super viewDidLoad];
  17. // Do any additional setup after loading the view.
  18. [self allocTitle:[NSString returnNoNullStringWithString:self.fileName]];
  19. [self addChildViewController:self.previewCtrl];
  20. }
  21. - (void)viewWillAppear:(BOOL)animated {
  22. [super viewWillAppear:animated];
  23. [self requestFileSource];
  24. [self.previewCtrl addObserver:self forKeyPath:@"currentPreviewItemIndex" options:NSKeyValueObservingOptionNew context:nil];
  25. }
  26. - (void)requestFileSource { // 如果缓存中有,不会再次下载
  27. [MBProgressHUD ksShowHUDWithText:@"数据加载中......"];
  28. NSArray *urlArray = [self.urlString containsString:@","] ? [self.urlString componentsSeparatedByString:@","] : @[self.urlString];
  29. [KSRequestManager mutiDownloadFileRequest:urlArray progress:^(int64_t bytesRead, int64_t totalBytes) {
  30. } success:^(NSArray * _Nonnull dics) {
  31. [MBProgressHUD ksHideHUD];
  32. self.fileArray = [NSMutableArray arrayWithArray:dics];
  33. [self.view addSubview:self.previewCtrl.view];
  34. [self.previewCtrl refreshCurrentPreviewItem];
  35. } faliure:^(NSError * _Nonnull error) {
  36. [MBProgressHUD ksHideHUD];
  37. }];
  38. }
  39. #pragma mark ------ QLPreviewControllerDataSource
  40. - (id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
  41. return self.fileArray[index];
  42. }
  43. - (BOOL)previewController:(QLPreviewController *)controller shouldOpenURL:(NSURL *)url forPreviewItem:(id<QLPreviewItem>)item {
  44. return NO;
  45. }
  46. - (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)previewController {
  47. return self.fileArray.count;
  48. }
  49. - (QLPreviewController *)previewCtrl {
  50. if (!_previewCtrl) {
  51. _previewCtrl = [[QLPreviewController alloc] init];
  52. _previewCtrl.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - kNaviBarHeight - iPhoneXSafeBottomMargin);
  53. _previewCtrl.delegate = self;
  54. _previewCtrl.dataSource = self;
  55. _previewCtrl.currentPreviewItemIndex = 0;
  56. _previewCtrl.navigationController.navigationBar.userInteractionEnabled = NO;
  57. }
  58. return _previewCtrl;
  59. }
  60. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
  61. NSInteger currentIndex = [[change objectForKey:@"new"] integerValue];
  62. self.navigationController.interactivePopGestureRecognizer.enabled = (currentIndex == 0);
  63. [self rightButtonTitle:[NSString stringWithFormat:@"%zd/%zd", (currentIndex+1),self.fileArray.count]];
  64. }
  65. - (void)dealloc {
  66. [self.previewCtrl removeObserver:self forKeyPath:@"currentPreviewItemIndex"];
  67. }
  68. /*
  69. #pragma mark - Navigation
  70. // In a storyboard-based application, you will often want to do a little preparation before navigation
  71. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  72. // Get the new view controller using [segue destinationViewController].
  73. // Pass the selected object to the new view controller.
  74. }
  75. */
  76. @end