MHCompleteBeautyView.m 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // MHCompleteBeautyView.m
  3. //一键美颜
  4. #import "MHCompleteBeautyView.h"
  5. #import "MHBeautyParams.h"
  6. #import "MHBeautiesModel.h"
  7. #import "MHBeautyMenuCell.h"
  8. @interface MHCompleteBeautyView()<UICollectionViewDelegate,UICollectionViewDataSource>
  9. @property (nonatomic, strong) UICollectionView *collectionView;
  10. @property (nonatomic, strong) NSMutableArray *array;
  11. @property (nonatomic, assign) NSInteger lastIndex;
  12. @end
  13. @implementation MHCompleteBeautyView
  14. - (instancetype)initWithFrame:(CGRect)frame {
  15. if (self = [super initWithFrame:frame]) {
  16. [self addSubview:self.collectionView];
  17. }
  18. return self;
  19. }
  20. - (void)cancelQuickBeautyEffect:(MHBeautiesModel *)selectedModel {
  21. for (int i = 0; i<self.array.count; i++) {
  22. MHBeautiesModel *model = self.array[i];
  23. if (i == 0) {
  24. model.isSelected = YES;
  25. }
  26. if ([model.beautyTitle isEqualToString:selectedModel.beautyTitle]) {
  27. model.isSelected = NO;
  28. }
  29. }
  30. [self.collectionView reloadData];
  31. self.lastIndex = 0;
  32. [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:kOneclickBeauty];
  33. [[NSUserDefaults standardUserDefaults] synchronize];
  34. }
  35. #pragma mark - dataSource
  36. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  37. return self.array.count;
  38. }
  39. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  40. MHBeautyMenuCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MHQuickBeautyCell" forIndexPath:indexPath];
  41. cell.quickModel = self.array[indexPath.row];
  42. return cell;
  43. }
  44. - (CGSize)collectionView:(UICollectionView *)collectionView
  45. layout:(UICollectionViewLayout *)collectionViewLayout
  46. sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  47. return CGSizeMake((window_width-20) /MHFilterItemColumn, MHFilterCellHeight);
  48. }
  49. #pragma mark - delegate
  50. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  51. if (self.lastIndex == indexPath.row) {
  52. return;
  53. }
  54. MHBeautiesModel *model = self.array[indexPath.row];
  55. model.isSelected = YES;
  56. [[NSUserDefaults standardUserDefaults] setObject:@(indexPath.row) forKey:kOneclickIndex];
  57. if ([self.delegate respondsToSelector:@selector(handleCompleteEffect:defaultSliderValue:)]) {
  58. [self.delegate handleCompleteEffect:model defaultSliderValue:model.defaultValue.integerValue];
  59. }
  60. if (self.lastIndex >= 0) {
  61. MHBeautiesModel *lastModel = self.array[self.lastIndex];
  62. lastModel.isSelected = NO;
  63. }
  64. if (indexPath.row == 0) {
  65. [[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:kMHSelectedQuickBeauty];
  66. [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:kOneclickBeauty];//清空保存的一键美颜数据
  67. } else {
  68. [[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:kMHSelectedQuickBeauty];
  69. if(cancelOtherBeauty){
  70. [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:kMHFaceTitle];
  71. [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:kMHBeautyTitle];
  72. }
  73. if (isSaveOneclickBeauty) {
  74. if (@available(iOS 11.0,*)) {
  75. NSData *data = [NSKeyedArchiver archivedDataWithRootObject:model requiringSecureCoding:YES error:nil];
  76. [[NSUserDefaults standardUserDefaults] setObject:data forKey:kOneclickBeauty];
  77. }else{
  78. NSData *data = [NSKeyedArchiver archivedDataWithRootObject:model];
  79. [[NSUserDefaults standardUserDefaults] setObject:data forKey:kOneclickBeauty];
  80. }
  81. }
  82. }
  83. [self.collectionView reloadData];
  84. self.lastIndex = indexPath.row;
  85. }
  86. #pragma mark - lazy
  87. -(NSMutableArray *)array {
  88. if (!_array) {
  89. NSMutableArray * selectedItemArray = [MHSDK shareInstance].completeBeautyArray;
  90. NSMutableArray * selectedItems = [NSMutableArray array];
  91. NSString *path = [[NSBundle mainBundle] pathForResource:@"MHQuickBeautyParams" ofType:@"plist"];
  92. NSArray *contentArr = [NSArray arrayWithContentsOfFile:path];
  93. NSArray *dataArr = contentArr.firstObject;
  94. for (int i = 0; i < selectedItemArray.count; i ++) {
  95. NSDictionary * selectedItemDic = selectedItemArray[i];
  96. NSString * selectedName = selectedItemDic[@"name"];
  97. for (int j = 0; j < dataArr.count; j++) {
  98. NSDictionary * itemDic = dataArr[j];
  99. NSString * itemName = itemDic[@"title"];
  100. if ([selectedName isEqual:itemName]) {
  101. [selectedItems addObject:itemDic];
  102. }
  103. }
  104. }
  105. _array = [NSMutableArray array];
  106. NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:kOneclickBeauty];
  107. MHBeautiesModel *lastmodel;
  108. NSSet *set = [NSSet setWithObjects:[NSString class],[NSNumber class],[MHBeautiesModel class], nil];
  109. for (int i = 0; i<selectedItems.count; i++) {
  110. NSDictionary *dic = selectedItems[i];
  111. MHBeautiesModel *model = [MHBeautiesModel mh_quickBeautyModelWithDictionary:dic];
  112. model.menuType = MHBeautyMenuType_QuickBeauty;
  113. if (!isSaveOneclickBeauty) {
  114. model.isSelected = i == 0 ? YES : NO;
  115. self.lastIndex = 0;
  116. }else{
  117. if (data && ![data isKindOfClass:[NSString class]]) {
  118. if (@available(iOS 11.0,*)) {
  119. lastmodel = [NSKeyedUnarchiver unarchivedObjectOfClasses:set fromData:data error:nil];
  120. }else{
  121. lastmodel = [NSKeyedUnarchiver unarchiveObjectWithData:data];
  122. }
  123. if ([model.beautyTitle isEqualToString:lastmodel.beautyTitle]) {
  124. model.isSelected = YES;
  125. self.lastIndex = i;
  126. }else{
  127. model.isSelected = NO;
  128. }
  129. }else{
  130. model.isSelected = i == 0 ? YES : NO;
  131. self.lastIndex = 0;
  132. }
  133. }
  134. [_array addObject:model];
  135. }
  136. }
  137. return _array;
  138. }
  139. - (UICollectionView *)collectionView {
  140. if (!_collectionView) {
  141. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  142. layout.minimumLineSpacing = 0;
  143. layout.minimumInteritemSpacing = 15;
  144. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  145. layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
  146. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, window_width,self.frame.size.height) collectionViewLayout:layout];
  147. ///修改MHUI
  148. _collectionView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:MHBlackAlpha];
  149. _collectionView.showsHorizontalScrollIndicator = NO;
  150. _collectionView.delegate = self;
  151. _collectionView.dataSource = self;
  152. [_collectionView registerClass:[MHBeautyMenuCell class] forCellWithReuseIdentifier:@"MHQuickBeautyCell"];
  153. }
  154. return _collectionView;
  155. }
  156. @end