MHActionView.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //
  2. // MHActionView.m
  3. // TXLiteAVDemo_UGC
  4. //
  5. // Created by Apple on 2021/4/13.
  6. // Copyright © 2021 Tencent. All rights reserved.
  7. //
  8. #import "MHActionView.h"
  9. #import "MHBeautyParams.h"
  10. #import "MHBeautyMenuCell.h"
  11. #import "MHBeautiesModel.h"
  12. #import <MHBeautySDK/MHBeautySDK.h>
  13. #define kBasicStickerURL @"aHR0cHM6Ly9kYXRhLmZhY2VnbC5jb20vYXBpL3Nkay92My9tb3Rpb24vaW5kZXg="
  14. @interface MHActionView ()<UICollectionViewDelegate,UICollectionViewDataSource>
  15. @property (nonatomic, strong) UICollectionView *collectionView;
  16. @property (nonatomic, strong) NSMutableArray *array;
  17. @property (nonatomic, assign) NSInteger lastIndex;
  18. @property (nonatomic, assign) NSInteger beautyType;
  19. @property (nonatomic, strong) NSMutableArray *arr;
  20. @property (nonatomic, strong) NSMutableArray * actionArray;
  21. @property (nonatomic, assign) int sdkLevelTYpe;///<sdk类型
  22. @property(nonatomic,assign)NSInteger selectedIndex;
  23. @end
  24. @implementation MHActionView
  25. - (instancetype)initWithFrame:(CGRect)frame {
  26. if (self = [super initWithFrame:frame]) {
  27. [self addSubview:self.collectionView];
  28. self.backgroundColor = [UIColor clearColor];
  29. self.lastIndex = 0;
  30. [self getSticks];
  31. }
  32. return self;
  33. }
  34. #pragma mark - 贴纸解析
  35. - (void)getSticks {
  36. __weak typeof(self) weakSelf = self;
  37. dispatch_async(dispatch_queue_create("com.suory.stickers", DISPATCH_QUEUE_SERIAL), ^{
  38. [[StickerManager sharedManager] requestStickersListWithUrl:kBasicStickerURL Success:^(NSArray<StickerDataListModel *> * _Nonnull stickerArray) {
  39. [weakSelf.actionArray removeAllObjects];
  40. NSMutableArray * arr = [NSMutableArray arrayWithArray:stickerArray];
  41. StickerDataListModel * model = arr[0];
  42. NSString *key = [NSString stringWithFormat:@"%@:%@",model.name,model.uptime];
  43. NSString *content = [[NSUserDefaults standardUserDefaults] objectForKey:key];
  44. //NSLog(@"stickercontent:---%@",content);
  45. if (content.length > 0) {
  46. model.is_downloaded = @"1";
  47. }else{
  48. model.is_downloaded = @"0";
  49. }
  50. [arr replaceObjectAtIndex:0 withObject:model];
  51. [weakSelf.actionArray addObjectsFromArray:arr];
  52. } Failed:^{
  53. }];
  54. });
  55. }
  56. - (void)clearAllActionEffects {
  57. if(self.lastIndex < self.array.count){
  58. MHBeautiesModel * model = self.array[self.lastIndex];
  59. model.isSelected = NO;
  60. [self.array replaceObjectAtIndex:self.lastIndex withObject:model];
  61. self.lastIndex = 0;
  62. [self.collectionView reloadData];
  63. }
  64. }
  65. #pragma mark - collectionView
  66. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  67. return self.array.count;
  68. }
  69. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  70. MHBeautyMenuCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MHBeautyMenuCell" forIndexPath:indexPath];
  71. cell.beautyModel = self.array[indexPath.row];
  72. return cell;
  73. }
  74. - (CGSize)collectionView:(UICollectionView *)collectionView
  75. layout:(UICollectionViewLayout *)collectionViewLayout
  76. sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  77. return CGSizeMake((window_width-40)/4, MHMeiyanMenusCellHeight);
  78. }
  79. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  80. if (self.actionArray.count == 0) {
  81. [self toastTip:@"资源下载中"];
  82. [self getSticks];
  83. return;
  84. }
  85. if (self.lastIndex == indexPath.row) {
  86. return;
  87. }
  88. MHBeautiesModel *currentModel1 = self.array[indexPath.row];
  89. currentModel1.isSelected = YES;
  90. if (self.lastIndex >= 0) {
  91. MHBeautiesModel *lastModel = self.array[self.lastIndex];
  92. lastModel.isSelected = NO;
  93. }
  94. self.lastIndex = indexPath.row;
  95. [self.collectionView reloadData];
  96. StickerDataListModel * currentModel;
  97. if (indexPath.row == 0) {
  98. [self.delegate handleStickerActionEffect:@"" sticker:nil action:(int)self.lastIndex];
  99. [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:kMHAction];
  100. return;
  101. }else if (indexPath.row == 1) {
  102. currentModel = self.actionArray[2];
  103. self.selectedIndex = 2;
  104. }else if(indexPath.row == 2){
  105. currentModel = self.actionArray[0];
  106. self.selectedIndex = 0;
  107. }else if(indexPath.row == 3){
  108. currentModel = self.actionArray[1];
  109. self.selectedIndex = 1;
  110. }
  111. if (currentModel.is_downloaded.boolValue == NO) {
  112. [[StickerManager sharedManager] downloadSticker:currentModel index:indexPath.row withSuccessed:^(StickerDataListModel * _Nonnull sticker, NSInteger index) {
  113. sticker.downloadState = MHStickerDownloadStateDownoadDone;
  114. sticker.isSelected = YES;
  115. [self.actionArray replaceObjectAtIndex:self.selectedIndex withObject:sticker];
  116. NSString *key = [NSString stringWithFormat:@"%@:%@",sticker.name,sticker.uptime];
  117. if ([self.delegate respondsToSelector:@selector(handleStickerActionEffect: sticker: action:)]) {
  118. [self.delegate handleStickerActionEffect:key sticker:sticker action:(int)self.lastIndex];
  119. if (isSaveActionSticker) {
  120. NSDictionary *info = @{@"content":key,
  121. @"kaction":@(self.lastIndex)};
  122. [[NSUserDefaults standardUserDefaults] setObject:info forKey:kMHAction];
  123. }
  124. }
  125. // }
  126. // dispatch_async(dispatch_get_main_queue(), ^{
  127. // if (collectionView) {
  128. // for (NSIndexPath *path in collectionView.indexPathsForVisibleItems) {
  129. // if (index == path.item) {
  130. // [collectionView reloadData];
  131. // break;
  132. // }
  133. // }
  134. //
  135. // }
  136. //
  137. // });
  138. // self.lastIndex = indexPath.item;
  139. } failed:^(StickerDataListModel * _Nonnull sticker, NSInteger index) {
  140. // sticker.isSelected = NO;
  141. // sticker.downloadState = MHStickerDownloadStateDownoadNot;
  142. // [self.actionArray replaceObjectAtIndex:self.selectedIndex withObject:sticker];
  143. // if (self.lastIndex >= 0) {
  144. // StickerDataListModel *lastModel = self.actionArray[1];
  145. // lastModel.isSelected = YES;
  146. // }
  147. dispatch_async(dispatch_get_main_queue(), ^{
  148. [collectionView reloadData];
  149. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"下载失败" message:@"请稍后重试" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
  150. [alert show];
  151. });
  152. }];
  153. } else {
  154. // for (StickerDataListModel * model in self.actionArray) {
  155. // if (model == currentModel) {
  156. // model.isSelected = YES;
  157. // }else{
  158. // model.isSelected = NO;
  159. // }
  160. // }
  161. NSString *key = [NSString stringWithFormat:@"%@:%@",currentModel.name,currentModel.uptime];
  162. currentModel.isSelected = YES;
  163. if ([self.delegate respondsToSelector:@selector(handleStickerActionEffect:sticker:action:)]) {
  164. [self.delegate handleStickerActionEffect:key sticker:currentModel action:(int)self.lastIndex];
  165. if (isSaveActionSticker) {
  166. NSDictionary *info = @{@"content":key,
  167. @"kaction":@(self.lastIndex)};
  168. [[NSUserDefaults standardUserDefaults] setObject:info forKey:kMHAction];
  169. }
  170. }
  171. // [collectionView reloadData];
  172. // self.lastIndex = indexPath.item;
  173. }
  174. }
  175. - (void)toastTip:(NSString*)toastInfo
  176. {
  177. CGRect frameRC = [self bounds];
  178. frameRC.origin.y = frameRC.size.height - 240;
  179. frameRC.size.height -= 100;
  180. __block UITextView * toastView = [[UITextView alloc] init];
  181. toastView.editable = NO;
  182. toastView.selectable = NO;
  183. frameRC.size.height = [toastView sizeThatFits:CGSizeMake(frameRC.size.width, MAXFLOAT)].height;
  184. toastView.frame = frameRC;
  185. toastView.text = toastInfo;
  186. toastView.textAlignment = NSTextAlignmentCenter;
  187. toastView.font = [UIFont systemFontOfSize:17];
  188. toastView.backgroundColor = [UIColor clearColor];
  189. [self addSubview:toastView];
  190. dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC);
  191. dispatch_after(popTime, dispatch_get_main_queue(), ^(){
  192. [toastView removeFromSuperview];
  193. toastView = nil;
  194. });
  195. }
  196. #pragma mark - lazy
  197. - (NSMutableArray *)array {
  198. if (!_array) {
  199. NSMutableArray * selectedItemArray = [MHSDK shareInstance].actionArray;
  200. NSString *path = [[NSBundle mainBundle] pathForResource:@"MHActionParams" ofType:@"plist"];
  201. NSArray *items = [NSArray arrayWithContentsOfFile:path];
  202. NSMutableArray * selectedItems = [NSMutableArray array];
  203. for (int i = 0; i < selectedItemArray.count; i ++) {
  204. NSDictionary * selectedItemDic = selectedItemArray[i];
  205. NSString * selectedName = selectedItemDic[@"name"];
  206. for (int j = 0; j < items.count; j++) {
  207. NSDictionary * itemDic = items[j];
  208. NSString * itemName = itemDic[@"name"];
  209. if ([selectedName isEqual:itemName]) {
  210. [selectedItems addObject:itemDic];
  211. }
  212. }
  213. }
  214. _array = [NSMutableArray array];
  215. for (int i = 0; i<selectedItems.count; i++) {
  216. NSDictionary * itemDic = selectedItems[i];
  217. MHBeautiesModel *model = [[MHBeautiesModel alloc] init];
  218. model.imgName = itemDic[@"imageName"];
  219. model.beautyTitle = itemDic[@"name"];
  220. model.type = [itemDic[@"type"] integerValue];
  221. model.menuType = MHBeautyMenuType_Action;
  222. if (isSaveActionSticker) {
  223. NSDictionary *info = [[NSUserDefaults standardUserDefaults] objectForKey:kMHAction];
  224. if ([info isKindOfClass:[NSDictionary class]]) {
  225. NSNumber *action = info[@"kaction"];
  226. if (i == action.intValue) {
  227. model.isSelected = YES;
  228. self.lastIndex = i;
  229. }else{
  230. model.isSelected = NO;
  231. }
  232. }else{
  233. model.isSelected = i==0? YES:NO;
  234. }
  235. }else{
  236. model.isSelected = i==0? YES:NO;
  237. }
  238. [_array addObject:model];
  239. }
  240. }
  241. return _array;
  242. }
  243. - (UICollectionView *)collectionView {
  244. if (!_collectionView) {
  245. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  246. layout.minimumLineSpacing = 0;
  247. layout.minimumInteritemSpacing = 0;
  248. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  249. layout.sectionInset = UIEdgeInsetsMake(20, 20,20,20);
  250. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, window_width,self.frame.size.height) collectionViewLayout:layout];
  251. ///修改MHUI
  252. _collectionView.backgroundColor = [UIColor clearColor];
  253. _collectionView.delegate = self;
  254. _collectionView.dataSource = self;
  255. [_collectionView registerClass:[MHBeautyMenuCell class] forCellWithReuseIdentifier:@"MHBeautyMenuCell"];
  256. }
  257. return _collectionView;
  258. }
  259. - (NSInteger)currentIndex{
  260. return _lastIndex;
  261. }
  262. - (NSMutableArray*)actionArray{
  263. if (!_actionArray) {
  264. _actionArray = [NSMutableArray array];
  265. }
  266. return _actionArray;
  267. }
  268. - (int)sdkLevelTYpe{
  269. return [[MHSDK shareInstance] getSDKLevel];
  270. }
  271. @end