XYGiveRewardAlertViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. //
  2. // XYGiveRewardAlertViewController.m
  3. // Timi
  4. //
  5. // Created by 翟玉磊 on 2021/11/16.
  6. //
  7. #import "XYGiveRewardAlertViewController.h"
  8. #import "XYGiftAPIManager.h"
  9. #import "XYGiveRewardAlertGiftItemCollectionViewCell.h"
  10. #import "XYGiveRewardGiftModel.h"
  11. #import "XYUserDynamicListCellModel.h"
  12. #import "XYRechargeItemsViewController.h"
  13. /// 左右距离父屏幕边距
  14. #define Left_Spacing 48.0f
  15. /// 内容view的宽度
  16. #define ContentView_Width (SCREEN_WIDTH - Left_Spacing * 2)
  17. typedef void(^SelectedDidBlock)(BOOL success, XYUserDynamicListCellModel *dynamicModel);
  18. @interface XYGiveRewardAlertViewController ()<UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>
  19. @property (nonatomic, readwrite, strong) UIView *bgView;
  20. @property (nonatomic, readwrite, strong) UIView *contentView;
  21. @property (nonatomic, readwrite, strong) UILabel *titleLabel;
  22. @property (nonatomic, readwrite, strong) UILabel *messageLabel;
  23. @property (nonatomic, readwrite, strong) UICollectionView *collectionView;
  24. @property (nonatomic, readwrite, strong) UIButton *cancelButton;
  25. @property (nonatomic, readwrite, strong) UIButton *sureButton;
  26. @property (nonatomic, strong) XYUserDynamicListCellModel *dynamicModel;
  27. @property (nonatomic, readwrite, copy) SelectedDidBlock selectedDidBlock;
  28. @property (nonatomic, strong) NSMutableArray *dataSource;
  29. @end
  30. @implementation XYGiveRewardAlertViewController
  31. #pragma mark — Publiv
  32. + (instancetype)showWithDynamicModel:(XYUserDynamicListCellModel *)dynamicModel parentController:(UIViewController *)parentController completeBlock:(void(^)(BOOL success, XYUserDynamicListCellModel *dynamicModel))block {
  33. XYGiveRewardAlertViewController *alertVC = XYGiveRewardAlertViewController.new;
  34. alertVC.dynamicModel = dynamicModel;
  35. alertVC.selectedDidBlock = block;
  36. alertVC.view.hidden = YES;
  37. // 添加控制器
  38. [parentController addChildViewController:alertVC];
  39. [parentController.view addSubview:alertVC.view];
  40. [alertVC didMoveToParentViewController:parentController];
  41. return alertVC;
  42. }
  43. - (void)show {
  44. self.view.hidden = NO;
  45. [self showAnimation];
  46. [self showBackground];
  47. }
  48. - (void)dismiss {
  49. dispatch_async(dispatch_get_main_queue(), ^{
  50. [self hiddenAnimation];
  51. });
  52. }
  53. /// 重写init方法,配置你想要的属性
  54. - (instancetype)init
  55. {
  56. self = [super init];
  57. if (self) {
  58. }
  59. return self;
  60. }
  61. - (void)viewDidLoad {
  62. [super viewDidLoad];
  63. /// 设置
  64. [self _setup];
  65. /// 设置导航栏
  66. [self _setupNavigationItem];
  67. /// 设置子控件
  68. [self _setupSubViews];
  69. /// 布局子空间
  70. [self _makeSubViewsConstraints];
  71. }
  72. #pragma mark — Override
  73. - (void)bindViewModel {
  74. }
  75. - (void)requestRemoteData {
  76. [[XYGiftAPIManager new] getWishListSuccessHandler:^(ZYLResponseModel *responseModel) {
  77. [self.dataSource removeAllObjects];
  78. for (NSDictionary *dict in responseModel.data[@"list"]) {
  79. XYGiveRewardGiftModel *model = XYGiveRewardGiftModel.new;
  80. [model yy_modelSetWithDictionary:dict];
  81. [self.dataSource addObject:model];
  82. }
  83. // 刷新frame
  84. [self updateCollectionFrame];
  85. [self.collectionView reloadData];
  86. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  87. // 默认选中状态
  88. [self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionNone];
  89. if ([self.collectionView.delegate respondsToSelector:@selector(collectionView:didSelectItemAtIndexPath:)]) {
  90. [self.collectionView.delegate collectionView:self.collectionView didSelectItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
  91. }
  92. });
  93. [self show];
  94. } failureHandler:^(ZYLNetworkError *error) {
  95. [SVProgressHUD showInfoWithStatus:error.domain];
  96. [self dismiss];
  97. }];
  98. }
  99. - (void)updateCollectionFrame {
  100. NSInteger count = self.dataSource.count;
  101. CGFloat itemWidth = 72.0f;
  102. CGFloat itemSpacing = 8.0f;
  103. CGFloat leftAndRight = 24.0f;
  104. CGFloat allWidth = leftAndRight + itemWidth * count + itemSpacing * (count - 1) + leftAndRight;
  105. if (allWidth > self.contentView.f_width) {
  106. // 不居中
  107. [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
  108. make.left.equalTo(self.contentView).offset(24.0f);
  109. make.right.equalTo(self.contentView).offset(-24.0f);
  110. }];
  111. }else {
  112. // 居中
  113. CGFloat x = (self.contentView.f_width - (itemWidth * count + itemSpacing * (count - 1)))/2;
  114. [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
  115. make.left.equalTo(self.contentView).offset(x);
  116. make.right.equalTo(self.contentView).offset(-x);
  117. }];
  118. }
  119. }
  120. #pragma mark - 事件处理Or辅助方法
  121. - (void)cancelButtonAction:(id)sender {
  122. if (self.selectedDidBlock) {
  123. self.selectedDidBlock(NO, self.dynamicModel);
  124. }
  125. [self dismiss];
  126. }
  127. - (void)sureButtonAction:(id)sender {
  128. NSString *giftId = @"";
  129. for (XYGiveRewardGiftModel *model in self.dataSource) {
  130. if (model.status) {
  131. giftId = [model.giftId copy];
  132. break;
  133. }
  134. }
  135. if (StringIsEmpty(giftId)) {
  136. [SVProgressHUD showInfoWithStatus:@"请选择礼物"];
  137. return;
  138. }
  139. [SVProgressHUD show];
  140. [[XYGiftAPIManager new] sendRewardGiftWithFeedId:self.dynamicModel.feedId giftId:giftId quantity:1 SuccessHandler:^(ZYLResponseModel *responseModel) {
  141. NSString *balance = [BaseMethod toString:responseModel.data[@"balance"]];
  142. if ([balance integerValue] > 0) {
  143. [SVProgressHUD showSuccessWithStatus:@"赠送成功"];
  144. NSString *totalPrice = [BaseMethod toString:responseModel.data[@"totalPrice"]];
  145. self.dynamicModel.sendGiftNum = totalPrice;
  146. self.dynamicModel.hasSendGift = YES;
  147. if (self.selectedDidBlock) {
  148. self.selectedDidBlock(YES, self.dynamicModel);
  149. }
  150. }else {
  151. [SVProgressHUD dismiss];
  152. // 余额不足
  153. CustomActionAlertController *al = [[CustomActionAlertController alloc] initWithTitle:kLocalizedString(@"温馨提示") message:kLocalizedString(@"账户余额不足,请前去充值") sure:kLocalizedString(@"去充值") cancel:kLocalizedString(@"取消") selctedBlock:^(NSInteger index, NSString * _Nonnull title) {
  154. if ([title isEqualToString:kLocalizedString(@"去充值")]) {
  155. [[ApplicationDelegate getCurrentController].navigationController pushViewController:XYRechargeItemsViewController.new animated:YES];
  156. }
  157. }];
  158. [al show];
  159. }
  160. [self dismiss];
  161. } failureHandler:^(ZYLNetworkError *error) {
  162. [SVProgressHUD showInfoWithStatus:error.domain];
  163. }];
  164. }
  165. #pragma mark - Animation  提示框弹出时的动画效果
  166. - (void)showAnimation {
  167. CAKeyframeAnimation *alertViewshowAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
  168. alertViewshowAnimation .duration = 0.4;
  169. alertViewshowAnimation .values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.01f, 0.01f, 1.0f)],
  170. [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1f, 1.1f, 1.0f)],
  171. [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9f, 0.9f, 1.0f)],
  172. [NSValue valueWithCATransform3D:CATransform3DIdentity]];
  173. alertViewshowAnimation.keyTimes = @[@0.2f, @0.5f, @0.75f, @1.0f];
  174. alertViewshowAnimation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
  175. [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
  176. [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
  177. [self.contentView.layer addAnimation:alertViewshowAnimation forKey:nil];
  178. }
  179. - (void)showBackground
  180. {
  181. self.bgView.alpha = 0;
  182. [UIView beginAnimations:@"fadeIn" context:nil];
  183. [UIView setAnimationDuration:0.35];
  184. self.bgView.alpha = 0.35;
  185. [UIView commitAnimations];
  186. }
  187. - (void)hiddenAnimation {
  188. [UIView animateWithDuration:.25 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
  189. self.contentView.transform = CGAffineTransformMakeScale(0.00, 0.00);
  190. self.bgView.alpha = 0;
  191. } completion:^(BOOL finished) {
  192. // 移除控制器
  193. [self willMoveToParentViewController:nil];
  194. [self.view removeFromSuperview];
  195. [self removeFromParentViewController];
  196. }];
  197. }
  198. #pragma mark - UICollectionViewDelegateFlowLayout, UICollectionViewDataSource
  199. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  200. return self.dataSource.count;
  201. }
  202. // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
  203. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  204. XYGiveRewardAlertGiftItemCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:XYGiveRewardAlertGiftItemCollectionViewCell.className forIndexPath:indexPath];
  205. [cell configureModel:self.dataSource[indexPath.row]];
  206. return cell;
  207. }
  208. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  209. // 修改源数据状态
  210. for (NSInteger i = 0; i < self.dataSource.count; i++) {
  211. XYGiveRewardGiftModel *model = self.dataSource[i];
  212. if (indexPath.row == i) {
  213. model.status = YES;
  214. }else {
  215. model.status = NO;
  216. }
  217. }
  218. }
  219. #pragma mark - 初始化
  220. - (void)_setup{
  221. self.view.backgroundColor = Color_Clear;
  222. }
  223. #pragma mark - 设置导航栏
  224. - (void)_setupNavigationItem{
  225. }
  226. #pragma mark - 设置子控件
  227. - (void)_setupSubViews{
  228. [self.view addSubview:self.bgView];
  229. [self.view addSubview:self.contentView];
  230. [self.contentView addSubview:self.titleLabel];
  231. [self.contentView addSubview:self.messageLabel];
  232. [self.contentView addSubview:self.collectionView];
  233. [self.contentView addSubview:self.cancelButton];
  234. [self.contentView addSubview:self.sureButton];
  235. [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  236. make.top.equalTo(self.contentView).offset(24.0f);
  237. make.left.equalTo(self.contentView).offset(24.0f);
  238. make.right.equalTo(self.contentView).offset(-24.0f);
  239. make.height.equalTo(@22.0f);
  240. }];
  241. [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  242. make.top.equalTo(self.titleLabel.mas_bottom).offset(4.0f);
  243. make.left.equalTo(self.contentView).offset(24.0f);
  244. make.right.equalTo(self.contentView).offset(-24.0f);
  245. make.height.equalTo(@17.0f);
  246. }];
  247. [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  248. make.left.equalTo(self.contentView).offset(24.0f);
  249. make.right.equalTo(self.contentView).offset(-24.0f);
  250. make.top.equalTo(self.messageLabel.mas_bottom).offset(24.0f);
  251. make.height.equalTo(@96.0f);
  252. }];
  253. [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
  254. make.left.equalTo(self.contentView).offset(24.0f);
  255. make.top.equalTo(self.collectionView.mas_bottom).offset(34.0f);
  256. make.height.equalTo(@36.0f);
  257. }];
  258. [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
  259. make.left.equalTo(self.cancelButton.mas_right).offset(16.0f);
  260. make.top.equalTo(self.collectionView.mas_bottom).offset(34.0f);
  261. make.height.equalTo(@36.0f);
  262. make.right.equalTo(self.contentView).offset(-24.0f);
  263. make.width.equalTo(self.cancelButton);
  264. }];
  265. // 居中
  266. self.contentView.center = self.view.center;
  267. [self.contentView addViewBorder:Color_Clear redian:8];
  268. [self.cancelButton addViewBorder:Color_Clear redian:18];
  269. [self.sureButton addViewBorder:Color_Clear redian:18];
  270. }
  271. #pragma mark - 布局子控件
  272. - (void)_makeSubViewsConstraints{
  273. }
  274. #pragma mark - Setter & Getter
  275. - (UIView *)bgView {
  276. if (!_bgView) {
  277. _bgView = [[UIView alloc] initWithFrame:SCREEN_BOUNDS];
  278. _bgView.backgroundColor = ColorFromHexStringWithAlpha(@"#1D1C1F", 0.35f);
  279. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelButtonAction:)];
  280. [_bgView addGestureRecognizer:tap];
  281. }
  282. return _bgView;
  283. }
  284. - (UIView *)contentView {
  285. if (!_contentView) {
  286. _contentView = [[UIView alloc] initWithFrame:CGRectMake(Left_Spacing, (SCREEN_HEIGHT-280.0f)/2, ContentView_Width, 280.0f)];
  287. _contentView.backgroundColor = Color_White;
  288. }
  289. return _contentView;;
  290. }
  291. - (UILabel *)titleLabel {
  292. if (!_titleLabel) {
  293. _titleLabel = [UILabel createLabelTextColor:Color_TextFont fount:Font_B(16)];
  294. _titleLabel.textAlignment = NSTextAlignmentCenter;
  295. _titleLabel.text = @"温馨提示";
  296. }
  297. return _titleLabel;
  298. }
  299. - (UILabel *)messageLabel {
  300. if (!_messageLabel) {
  301. _messageLabel = [UILabel createLabelTextColor:ColorFromHexString(@"#FD8C3B") fount:Font(12)];
  302. _messageLabel.textAlignment = NSTextAlignmentCenter;
  303. _messageLabel.text = @"打赏一下有助于提高好感度哦";
  304. }
  305. return _messageLabel;
  306. }
  307. - (UICollectionView *)collectionView {
  308. if (_collectionView == nil) {
  309. UICollectionViewFlowLayout *flowLayout = UICollectionViewFlowLayout.new;
  310. flowLayout.itemSize = CGSizeMake(72.0f, 96.0f);
  311. flowLayout.minimumLineSpacing = 8.0f;
  312. flowLayout.minimumInteritemSpacing = 8.0f;
  313. flowLayout.sectionInset = UIEdgeInsetsMake(0, 0.0f, 0, 0.0f);
  314. flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  315. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
  316. _collectionView.delegate = self;
  317. _collectionView.dataSource = self;
  318. _collectionView.backgroundColor = Color_Clear;
  319. [_collectionView registerCell:XYGiveRewardAlertGiftItemCollectionViewCell.className];
  320. }
  321. return _collectionView;
  322. }
  323. - (UIButton *)cancelButton {
  324. if (!_cancelButton) {
  325. _cancelButton = [UIButton createButtonTextColor:Color_TextGray textFont:Font(14)];
  326. [_cancelButton setBackgroundColor:Color_Background];
  327. [_cancelButton addTarget:self action:@selector(cancelButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  328. [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
  329. }
  330. return _cancelButton;
  331. }
  332. - (UIButton *)sureButton {
  333. if (!_sureButton) {
  334. _sureButton = [UIButton createButtonTextColor:Color_White textFont:Font(16)];
  335. [_sureButton setBackgroundImage:[UIImage gradientColorImageFromColors:@[ColorFromHexString(@"#5D26FF"), ColorFromHexString(@"#8359FF")] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake((ContentView_Width - 16.0f - 24.0f * 2)/2, 36.0f)] forState:UIControlStateNormal];
  336. [_sureButton addTarget:self action:@selector(sureButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  337. [_sureButton setTitle:@"赠送" forState:UIControlStateNormal];
  338. }
  339. return _sureButton;
  340. }
  341. - (NSMutableArray *)dataSource {
  342. if (_dataSource == nil) {
  343. _dataSource = [NSMutableArray array];
  344. }
  345. return _dataSource;
  346. }
  347. @end