PhotoListViewController.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // PhotoListViewController.m
  3. // LenzCameraNativeModuleForRN
  4. //
  5. // Created by 王昭威 on 2023/1/22.
  6. //
  7. #import "PhotoListViewController.h"
  8. #import "UIButton+Layout.h"
  9. #import "PCSButton.h"
  10. #import "PhotoCollectionViewCell.h"
  11. #import "PhotoListCellModel.h"
  12. #import "ImageCacheLRU.h"
  13. #import "AlbumCounterLabel.h"
  14. #import "QuitMultipleModeAlertViewController.h"
  15. #import "LenzHeader.h"
  16. @interface PhotoListViewController ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
  17. @property (nonatomic, strong) ImageCacheLRU* lru;
  18. @end
  19. @implementation PhotoListViewController
  20. #pragma mark - others
  21. - (void)setImagesInMemory:(NSArray<UIImage *> *)imagesInMemory andImageNamesOnDisk:(NSArray<NSString *> *)imageNamesOnDisk{
  22. NSMutableArray<__kindof PhotoListCellModel*>* models = [NSMutableArray array];
  23. [imageNamesOnDisk enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  24. [models addObject: [[PhotoListCellModel alloc] initWithName:obj andImage:nil]];
  25. }];
  26. [imagesInMemory enumerateObjectsUsingBlock:^(UIImage * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  27. [models addObject: [[PhotoListCellModel alloc] initWithName:nil andImage:obj]];
  28. }];
  29. self.cellModels = [[NSMutableArray alloc] initWithArray:models];
  30. }
  31. #pragma mark - Button
  32. - (void)backButtonTouchUpInside:(id)sender{
  33. [self dismissViewControllerAnimated:YES completion:^{}];
  34. }
  35. - (void)deletedCachedResourceSuccessfulAtIndex:(NSInteger)deletedPageIndex{
  36. [self deletedImageSuccessfulAtIndex:deletedPageIndex];
  37. }
  38. - (void)deletedImageSuccessfulAtIndex: (NSInteger)deletedPageIndex{
  39. if(deletedPageIndex >= 0 && deletedPageIndex < self.cellModels.count){
  40. NSInteger currIdx = [self currentPageIndex];
  41. [self.cellModels removeObjectAtIndex:deletedPageIndex];
  42. [self.photoCollectionView deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:deletedPageIndex inSection:0]]];
  43. currIdx = MIN(MAX(0, currIdx), MAX(self.cellModels.count, 1) - 1);
  44. [self updatePageIndexLabelWithCurrentIndex:currIdx];
  45. }
  46. [self.presentedViewController dismissViewControllerAnimated:NO completion:^{
  47. if(self.cellModels.count == 0){
  48. [self dismissViewControllerAnimated:YES completion:^{}];
  49. }
  50. }];
  51. }
  52. - (void)deleteButtonTouchUpInside:(id)sender{
  53. NSInteger currPageIdx = MAX(self.currentPageIndex, 0);
  54. [QuitMultipleModeAlertViewController show:self title:nil text:LOCALIZATION_STRING_KEY_DELETE_IMAGE_ALERT_TEXT leftBtnTitle:LOCALIZATION_STRING_KEY_DELETE_ALERT_BTN_TITLE_CANCEL rightBtnTitle:LOCALIZATION_STRING_KEY_DELETE_ALERT_BTN_TITLE_CONFIRM withLeftButtonCallBack:^(QuitMultipleModeAlertViewController * _Nonnull alertController) {
  55. [alertController dismissViewControllerAnimated:NO completion:^{}];
  56. } rightButtonCallBack:^(QuitMultipleModeAlertViewController * _Nonnull alertController) {
  57. if(self.cellModels.count > currPageIdx){
  58. PhotoListCellModel* cellModel = self.cellModels[currPageIdx];
  59. if(cellModel.name != nil){
  60. [self.delegate deleteImageWithName:cellModel.name completion:^(BOOL successful) {
  61. dispatch_async(dispatch_get_main_queue(), ^{
  62. if(successful){
  63. [self deletedImageSuccessfulAtIndex:currPageIdx];
  64. }
  65. });
  66. }];
  67. }
  68. else{
  69. [self.delegate deleteImage:cellModel.image completion:^(BOOL successful) {
  70. dispatch_async(dispatch_get_main_queue(), ^{
  71. if(successful){
  72. [self deletedImageSuccessfulAtIndex:currPageIdx];
  73. }
  74. });
  75. }];
  76. }
  77. }
  78. }];
  79. }
  80. - (void)updatePageIndexLabelWithCurrentIndex: (NSInteger)currIdx{
  81. self.counterLabel.text = [NSString stringWithFormat:@"第%@/%@张", @(currIdx + 1), @(self.cellModels.count)];
  82. }
  83. + (instancetype)makeViewController{
  84. UIStoryboard* sb = [UIStoryboard storyboardWithName:[PCSTools shared].mainStoryboardName bundle:[PCSTools sdkBundle]];
  85. return [sb instantiateViewControllerWithIdentifier:@"PhotoListVC"];
  86. }
  87. #pragma mark - Life circle
  88. - (void)viewDidLoad {
  89. [super viewDidLoad];
  90. // Do any additional setup after loading the view from its nib.
  91. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeNum:) name:@"changeNum" object:nil];
  92. self.lru = [[ImageCacheLRU alloc] init];
  93. [self.view layoutIfNeeded];
  94. [self updatePageIndexLabelWithCurrentIndex:0];
  95. }
  96. - (void)changeNum:(NSNotification *)notification{
  97. NSString *str = notification.object;
  98. NSInteger currentPage = [str integerValue];
  99. NSLog(@"====:%ld",(long)currentPage);
  100. [self updatePageIndexLabelWithCurrentIndex:currentPage];
  101. }
  102. - (NSInteger)currentPageIndex{
  103. CGFloat pageWidth = self.photoCollectionView.frame.size.width;
  104. NSInteger currentPage = round(self.photoCollectionView.contentOffset.x / pageWidth);
  105. return currentPage;
  106. }
  107. - (UIImage* _Nullable)loadImageForKey: (NSString*)key{
  108. UIImage* img = nil;
  109. NSString* imgName = key;
  110. NSData* imgData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:[[PCSTools shared].tmpPath stringByAppendingPathComponent:imgName]]];
  111. img = [UIImage imageWithData:imgData];
  112. return img;
  113. }
  114. #pragma mark - UICollectionViewDelegateFlowLayout
  115. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
  116. return CGSizeMake(CGRectGetWidth(self.photoCollectionView.bounds), CGRectGetHeight(self.photoCollectionView.bounds));
  117. }
  118. #pragma mark - UICollectionViewDelegate and DataSource
  119. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
  120. NSInteger currentPage = self.currentPageIndex;
  121. [self updatePageIndexLabelWithCurrentIndex:currentPage];
  122. }
  123. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
  124. return 1;
  125. }
  126. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  127. return self.cellModels.count;
  128. }
  129. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  130. PhotoCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
  131. if(self.cellModels.count > indexPath.item){
  132. PhotoListCellModel* cellModel = self.cellModels[indexPath.item];
  133. if(cellModel.image == nil && cellModel.name != nil){
  134. UIImage* img = [self.lru queryByKey: cellModel.name];
  135. if(img == nil){
  136. img = [self loadImageForKey:cellModel.name];
  137. cellModel.image = img;
  138. [self.lru insertByKey:cellModel.name image:img];
  139. }
  140. }
  141. cell.cellModel = cellModel;
  142. }
  143. return cell;
  144. }
  145. - (void)viewDidLayoutSubviews{
  146. [super viewDidLayoutSubviews];
  147. [self.backButton centerVertically];
  148. [self.deleteButton centerVertically];
  149. }
  150. /*
  151. #pragma mark - Navigation
  152. // In a storyboard-based application, you will often want to do a little preparation before navigation
  153. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  154. // Get the new view controller using [segue destinationViewController].
  155. // Pass the selected object to the new view controller.
  156. }
  157. */
  158. @end