PCSTools.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // PCSTools.m
  3. // sampleSDK
  4. //
  5. // Created by 王昭威 on 2023/1/15.
  6. //
  7. #import "PCSBaseViewController.h"
  8. #import "PCSTools.h"
  9. #import "OperationNodeProtocol.h"
  10. #import "QuitMultipleModeAlertViewController.h"
  11. #import "SDKParameters.h"
  12. #import "DBManager.h"
  13. #import "LenzSDKConstant.h"
  14. #import "LenzCachedResourceModel.h"
  15. const NSNotificationName LenzSDKNotificationDiskIsFull = @"LENZ_SDK_NOTIFICATION_DISK_IS_FULL";
  16. const NSNotificationName CameraNotificationNotPermission = @"CameraNotificationNotPermission";
  17. const NSNotificationName MicrophoneNotificationNotPermission = @"MicrophoneNotificationNotPermission";
  18. inline CGFloat dgree_2_rad(CGFloat dgree){
  19. return dgree / 180.0 * M_PI;
  20. }
  21. void showAlertToDiscardCachesWithTitle(id<OperationNodeProtocol> node, NSString* title, NSString* description, WillQuitCompletionBlockType block){
  22. id<OperationNodeProtocol> curr = node;
  23. if(curr.controller == nil){
  24. return;
  25. }
  26. if ([SDKParameters shared].retainedMode == SDK_DATA_RETAINED_CLEAR) {
  27. block();
  28. }
  29. else if([SDKParameters shared].retainedMode == SDK_DATA_RETAINED_USER_CONFIRM && [curr respondsToSelector:@selector(numberOfCaches)] && curr.numberOfCaches > 0){
  30. [QuitMultipleModeAlertViewController show: node.controller withSaveButtonCallBack:^(QuitMultipleModeAlertViewController * _Nonnull alertController) {
  31. [alertController dismissViewControllerAnimated:NO completion:^{}];
  32. block();
  33. } discardButtonCallBack:^(QuitMultipleModeAlertViewController * _Nonnull alertController) {
  34. if([curr respondsToSelector:@selector(cleanOnCompleted:)]){
  35. [curr cleanOnCompleted:^{
  36. [alertController dismissViewControllerAnimated:NO completion:^{}];
  37. block();
  38. }];
  39. }
  40. }];
  41. }
  42. else{
  43. block();
  44. }
  45. }
  46. @implementation PCSTools
  47. - (void)cleanDiskCache{
  48. PCSTools* tool = [PCSTools shared];
  49. LenzDBManager* dbMgr = [LenzDBManager shared];
  50. NSArray<LenzCachedResourceModel*>* arr = [dbMgr fetchAll];
  51. NSMutableArray<LenzCachedResourceModel*>* invalids = [NSMutableArray array];
  52. NSFileManager* fileMgr = [NSFileManager defaultManager];
  53. [arr enumerateObjectsUsingBlock:^(LenzCachedResourceModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  54. switch (obj.type) {
  55. case SDKCameraCapturedResourceIndexMovie:
  56. {
  57. NSURL* desUrl = [tool.moviesDir URLByAppendingPathComponent: obj.name isDirectory:NO];
  58. NSError* err = nil;
  59. if([fileMgr fileExistsAtPath:desUrl.path] && [fileMgr removeItemAtURL:desUrl error:&err]){
  60. [invalids addObject:obj];
  61. }
  62. else if(![fileMgr fileExistsAtPath:desUrl.path]){
  63. [invalids addObject:obj];
  64. }
  65. }
  66. break;
  67. case SDKCameraCapturedResourceIndexPhoto:
  68. {
  69. NSString* path = [tool imagePathByName:obj.name];
  70. NSError* err = nil;
  71. if([fileMgr fileExistsAtPath:path] && [fileMgr removeItemAtPath:path error:&err]){
  72. [invalids addObject:obj];
  73. }
  74. else if(![fileMgr removeItemAtPath:path error:&err]){
  75. [invalids addObject:obj];
  76. }
  77. }
  78. break;
  79. default:
  80. break;
  81. }
  82. }];
  83. [dbMgr deleteModels:invalids];
  84. }
  85. - (NSString *)mainStoryboardName{
  86. return @"VC";
  87. }
  88. + (PCSTools *)shared{
  89. static PCSTools* tool = nil;
  90. static dispatch_once_t token;
  91. dispatch_once(&token, ^{
  92. tool = [[self alloc] init];
  93. });
  94. return tool;
  95. }
  96. - (NSString *)documentPath{
  97. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
  98. NSString *path = [paths objectAtIndex:0];
  99. return path;
  100. }
  101. - (NSString *)tmpPath{
  102. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);
  103. NSString *path = [paths objectAtIndex:0];
  104. return path;
  105. }
  106. - (NSURL *)moviesDir{
  107. return [[NSURL fileURLWithPath:self.tmpPath] URLByAppendingPathComponent:@"movies" isDirectory:YES];
  108. }
  109. - (NSString*)imagePathByName: (NSString*)name{
  110. NSString* tmpPath = [PCSTools shared].tmpPath;
  111. return [tmpPath stringByAppendingPathComponent:name];
  112. }
  113. - (NSString *)libraryPath{
  114. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,YES);
  115. NSString *path = [paths objectAtIndex:0];
  116. return path;
  117. }
  118. //+ (NSBundle*)resourceBundle{
  119. // NSBundle* classBundle = [NSBundle bundleForClass:self];
  120. // NSBundle* resourceBundle = [classBundle URLForResource:@"LenzCameraNativeModuleForRN" withExtension:@"bundle"];
  121. // return resourceBundle;
  122. //}
  123. + (NSBundle*)sdkBundle{
  124. NSBundle* classBundle = [NSBundle bundleForClass:self];
  125. NSURL* url = [classBundle URLForResource:@"LenzCameraNativeModuleForRN" withExtension:@"bundle"];
  126. if(url == nil){
  127. return nil;
  128. }
  129. NSBundle* resourceBundle = [NSBundle bundleWithURL:url];
  130. return resourceBundle;
  131. return [NSBundle bundleWithIdentifier:@"LenzCameraNativeModuleForRN"];
  132. }
  133. @end