TUIFileViewController.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // FileViewController.m
  3. // TUIKitDemo
  4. //
  5. // Created by kennethmiao on 2018/11/12.
  6. // Copyright © 2018 Tencent. All rights reserved.
  7. //
  8. #import "TUIFileViewController.h"
  9. #import <QuickLook/QuickLook.h>
  10. #import <TIMCommon/TIMDefine.h>
  11. #import <TUICore/TUIGlobalization.h>
  12. #import <TUICore/TUIThemeManager.h>
  13. #import <TUICore/UIView+TUILayout.h>
  14. #import <TUICore/UIView+TUIToast.h>
  15. #import "ReactiveObjC/ReactiveObjC.h"
  16. @interface TUIFileViewController () <UIDocumentInteractionControllerDelegate>
  17. @property(nonatomic, strong) UIImageView *image;
  18. @property(nonatomic, strong) UILabel *name;
  19. @property(nonatomic, strong) UILabel *progress;
  20. @property(nonatomic, strong) UIButton *button;
  21. @property(nonatomic, strong) UIDocumentInteractionController *document;
  22. @end
  23. @implementation TUIFileViewController
  24. - (void)viewDidLoad {
  25. [super viewDidLoad];
  26. self.view.backgroundColor = [UIColor whiteColor];
  27. UILabel *titleLabel = [[UILabel alloc] init];
  28. titleLabel.text = TIMCommonLocalizableString(File);
  29. titleLabel.font = [UIFont boldSystemFontOfSize:17.0];
  30. titleLabel.textColor = TIMCommonDynamicColor(@"nav_title_text_color", @"#000000");
  31. titleLabel.textAlignment = isRTL()?NSTextAlignmentRight:NSTextAlignmentLeft;
  32. [titleLabel sizeToFit];
  33. self.navigationItem.titleView = titleLabel;
  34. // left
  35. UIImage *defaultImage = [UIImage imageNamed:TUIChatImagePath(@"back")];
  36. UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
  37. UIImage *formatImage = [TIMCommonDynamicImage(@"nav_back_img", defaultImage) rtl_imageFlippedForRightToLeftLayoutDirection];
  38. [leftButton addTarget:self action:@selector(onBack:) forControlEvents:UIControlEventTouchUpInside];
  39. [leftButton setImage:formatImage forState:UIControlStateNormal];
  40. UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
  41. self.navigationItem.leftBarButtonItem = leftItem;
  42. _image = [[UIImageView alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 80) * 0.5, NavBar_Height + StatusBar_Height + 50, 80, 80)];
  43. _image.contentMode = UIViewContentModeScaleAspectFit;
  44. _image.image = [UIImage imageNamed:TUIChatImagePath(@"msg_file")];
  45. [self.view addSubview:_image];
  46. _name = [[UILabel alloc] initWithFrame:CGRectMake(0, _image.frame.origin.y + _image.frame.size.height + 20, self.view.frame.size.width, 40)];
  47. _name.textColor = [UIColor blackColor];
  48. _name.font = [UIFont systemFontOfSize:15];
  49. _name.textAlignment = NSTextAlignmentCenter;
  50. _name.text = _data.fileName;
  51. [self.view addSubview:_name];
  52. _button = [[UIButton alloc] initWithFrame:CGRectMake(100, _name.frame.origin.y + _name.frame.size.height + 20, self.view.frame.size.width - 200, 40)];
  53. [_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  54. _button.backgroundColor = [UIColor colorWithRed:44 / 255.0 green:145 / 255.0 blue:247 / 255.0 alpha:1.0];
  55. _button.layer.cornerRadius = 5;
  56. [_button.layer setMasksToBounds:YES];
  57. [_button addTarget:self action:@selector(onOpen:) forControlEvents:UIControlEventTouchUpInside];
  58. [self.view addSubview:_button];
  59. @weakify(self);
  60. [RACObserve(_data, downladProgress) subscribeNext:^(NSNumber *x) {
  61. @strongify(self);
  62. int progress = [x intValue];
  63. if (progress < 100 && progress > 0) {
  64. [self.button setTitle:[NSString stringWithFormat:TIMCommonLocalizableString(TUIKitDownloadProgressFormat), progress] forState:UIControlStateNormal];
  65. } else {
  66. [self.button setTitle:TIMCommonLocalizableString(TUIKitOpenWithOtherApp) forState:UIControlStateNormal];
  67. }
  68. }];
  69. if ([_data isLocalExist]) {
  70. [self.button setTitle:TIMCommonLocalizableString(TUIKitOpenWithOtherApp) forState:UIControlStateNormal];
  71. } else {
  72. [self.button setTitle:TIMCommonLocalizableString(Download) forState:UIControlStateNormal];
  73. }
  74. }
  75. - (void)onOpen:(id)sender {
  76. BOOL isExist = NO;
  77. NSString *path = [_data getFilePath:&isExist];
  78. if (isExist) {
  79. NSURL *url = [NSURL fileURLWithPath:path];
  80. _document = [UIDocumentInteractionController interactionControllerWithURL:url];
  81. _document.delegate = self;
  82. [_document presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES];
  83. } else {
  84. [_data downloadFile];
  85. }
  86. }
  87. - (void)onBack:(id)sender {
  88. [self.navigationController popViewControllerAnimated:YES];
  89. }
  90. - (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller {
  91. return self.view;
  92. }
  93. - (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller {
  94. return self.view.frame;
  95. }
  96. - (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
  97. return self;
  98. }
  99. @end