LKS_ExportManager.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #ifdef SHOULD_COMPILE_LOOKIN_SERVER
  2. //
  3. // LKS_ExportManager.m
  4. // LookinServer
  5. //
  6. // Created by Li Kai on 2019/5/13.
  7. // https://lookin.work
  8. //
  9. #import "LKS_ExportManager.h"
  10. #import "UIViewController+LookinServer.h"
  11. #import "LookinHierarchyInfo.h"
  12. #import "LookinHierarchyFile.h"
  13. #import "LookinAppInfo.h"
  14. #import "LookinServerDefines.h"
  15. @interface LKS_ExportManagerMaskView : UIView
  16. @property(nonatomic, strong) UIView *tipsView;
  17. @property(nonatomic, strong) UILabel *firstLabel;
  18. @property(nonatomic, strong) UILabel *secondLabel;
  19. @property(nonatomic, strong) UILabel *thirdLabel;
  20. @end
  21. @implementation LKS_ExportManagerMaskView
  22. - (instancetype)initWithFrame:(CGRect)frame {
  23. if (self = [super initWithFrame:frame]) {
  24. self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.35];
  25. self.layer.lks_isLookinPrivateLayer = YES;
  26. self.layer.lks_avoidCapturing = YES;
  27. self.tipsView = [UIView new];
  28. self.tipsView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.88];
  29. self.tipsView.layer.cornerRadius = 6;
  30. self.tipsView.layer.masksToBounds = YES;
  31. [self addSubview:self.tipsView];
  32. self.firstLabel = [UILabel new];
  33. self.firstLabel.text = LKS_Localized(@"Creating File…");
  34. self.firstLabel.textColor = [UIColor whiteColor];
  35. self.firstLabel.font = [UIFont boldSystemFontOfSize:14];
  36. self.firstLabel.textAlignment = NSTextAlignmentCenter;
  37. self.firstLabel.numberOfLines = 0;
  38. [self.tipsView addSubview:self.firstLabel];
  39. self.secondLabel = [UILabel new];
  40. self.secondLabel.text = LKS_Localized(@"May take 8 or more seconds according to the UI complexity.");
  41. self.secondLabel.textColor = [UIColor colorWithRed:173/255.0 green:180/255.0 blue:190/255.0 alpha:1];
  42. self.secondLabel.font = [UIFont systemFontOfSize:12];
  43. self.secondLabel.textAlignment = NSTextAlignmentLeft;
  44. self.secondLabel.numberOfLines = 0;
  45. [self.tipsView addSubview:self.secondLabel];
  46. self.thirdLabel = [UILabel new];
  47. self.thirdLabel.text = LKS_Localized(@"The file can be opend by Lookin.app in macOS.");
  48. self.thirdLabel.textColor = [UIColor colorWithRed:173/255.0 green:180/255.0 blue:190/255.0 alpha:1];
  49. self.thirdLabel.font = [UIFont systemFontOfSize:12];
  50. self.thirdLabel.textAlignment = NSTextAlignmentCenter;
  51. self.thirdLabel.numberOfLines = 0;
  52. [self.tipsView addSubview:self.thirdLabel];
  53. }
  54. return self;
  55. }
  56. - (void)layoutSubviews {
  57. [super layoutSubviews];
  58. UIEdgeInsets insets = UIEdgeInsetsMake(8, 10, 8, 10);
  59. CGFloat maxLabelWidth = self.bounds.size.width * .8 - insets.left - insets.right;
  60. CGSize firstSize = [self.firstLabel sizeThatFits:CGSizeMake(maxLabelWidth, CGFLOAT_MAX)];
  61. CGSize secondSize = [self.secondLabel sizeThatFits:CGSizeMake(maxLabelWidth, CGFLOAT_MAX)];
  62. CGSize thirdSize = [self.thirdLabel sizeThatFits:CGSizeMake(maxLabelWidth, CGFLOAT_MAX)];
  63. CGFloat tipsWidth = MAX(MAX(firstSize.width, secondSize.width), thirdSize.width) + insets.left + insets.right;
  64. self.firstLabel.frame = CGRectMake(tipsWidth / 2.0 - firstSize.width / 2.0, insets.top, firstSize.width, firstSize.height);
  65. self.secondLabel.frame = CGRectMake(tipsWidth / 2.0 - secondSize.width / 2.0, CGRectGetMaxY(self.firstLabel.frame) + 10, secondSize.width, secondSize.height);
  66. self.thirdLabel.frame = CGRectMake(tipsWidth / 2.0 - thirdSize.width / 2.0, CGRectGetMaxY(self.secondLabel.frame) + 5, thirdSize.width, thirdSize.height);
  67. self.tipsView.frame = ({
  68. CGFloat height = CGRectGetMaxY(self.thirdLabel.frame) + insets.bottom;
  69. CGRectMake(self.bounds.size.width / 2.0 - tipsWidth / 2.0, self.bounds.size.height / 2.0 - height / 2.0, tipsWidth, height);
  70. });
  71. }
  72. @end
  73. @interface LKS_ExportManager ()
  74. #if TARGET_OS_TV
  75. #else
  76. @property(nonatomic, strong) UIDocumentInteractionController *documentController;
  77. #endif
  78. @property(nonatomic, strong) LKS_ExportManagerMaskView *maskView;
  79. @end
  80. @implementation LKS_ExportManager
  81. + (instancetype)sharedInstance {
  82. static dispatch_once_t onceToken;
  83. static LKS_ExportManager *instance = nil;
  84. dispatch_once(&onceToken,^{
  85. instance = [[super allocWithZone:NULL] init];
  86. });
  87. return instance;
  88. }
  89. + (id)allocWithZone:(struct _NSZone *)zone{
  90. return [self sharedInstance];
  91. }
  92. #if TARGET_OS_TV
  93. - (void)exportAndShare {
  94. NSAssert(NO, @"not supported");
  95. }
  96. #else
  97. - (void)exportAndShare {
  98. UIViewController *visibleVc = [UIViewController lks_visibleViewController];
  99. if (!visibleVc) {
  100. NSLog(@"LookinServer - Failed to export because we didn't find any visible view controller.");
  101. return;
  102. }
  103. [[NSNotificationCenter defaultCenter] postNotificationName:@"Lookin_WillExport" object:nil];
  104. if (!self.maskView) {
  105. self.maskView = [LKS_ExportManagerMaskView new];
  106. }
  107. [visibleVc.view.window addSubview:self.maskView];
  108. self.maskView.frame = visibleVc.view.window.bounds;
  109. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  110. LookinHierarchyInfo *info = [LookinHierarchyInfo exportedInfo];
  111. LookinHierarchyFile *file = [LookinHierarchyFile new];
  112. file.serverVersion = info.serverVersion;
  113. file.hierarchyInfo = info;
  114. NSData *data = [NSKeyedArchiver archivedDataWithRootObject:file];
  115. if (!data) {
  116. return;
  117. }
  118. NSString *fileName = ({
  119. NSString *timeString = ({
  120. NSDate *date = [NSDate date];
  121. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  122. [formatter setDateFormat:@"MMddHHmm"];
  123. [formatter stringFromDate:date];
  124. });
  125. NSString *iOSVersion = ({
  126. NSString *str = info.appInfo.osDescription;
  127. NSUInteger dotIdx = [str rangeOfString:@"."].location;
  128. if (dotIdx != NSNotFound) {
  129. str = [str substringToIndex:dotIdx];
  130. }
  131. str;
  132. });
  133. [NSString stringWithFormat:@"%@_ios%@_%@.lookin", info.appInfo.appName, iOSVersion, timeString];
  134. });
  135. NSString *path = [NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), fileName];
  136. [data writeToFile:path atomically:YES];
  137. [self.maskView removeFromSuperview];
  138. if (!self.documentController) {
  139. self.documentController = [UIDocumentInteractionController new];
  140. }
  141. self.documentController.URL = [NSURL fileURLWithPath:path];
  142. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  143. [self.documentController presentOpenInMenuFromRect:CGRectMake(0, 0, 1, 1) inView:visibleVc.view animated:YES];
  144. } else {
  145. [self.documentController presentOpenInMenuFromRect:visibleVc.view.bounds inView:visibleVc.view animated:YES];
  146. }
  147. [[NSNotificationCenter defaultCenter] postNotificationName:@"Lookin_DidFinishExport" object:nil];
  148. // [self.documentController presentOptionsMenuFromRect:visibleVc.view.bounds inView:visibleVc.view animated:YES];
  149. // CFTimeInterval endTime = CACurrentMediaTime();
  150. // CFTimeInterval consumingTime = endTime - startTime;
  151. // NSLog(@"LookinServer - 导出 UI 结构耗时:%@", @(consumingTime));
  152. });
  153. }
  154. #endif
  155. @end
  156. #endif /* SHOULD_COMPILE_LOOKIN_SERVER */