LKS_ExportManager.m 7.3 KB

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