XYMarkingFriendsSkillListViewController.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // XYMarkingFriendsSkillListViewController.m
  3. // Starbuds
  4. //
  5. // Created by 翟玉磊 on 2020/12/7.
  6. // Copyright © 2020 翟玉磊. All rights reserved.
  7. //
  8. #import "XYMarkingFriendsSkillListViewController.h"
  9. #import "XYMarkingFriendsSkillListViewModel.h"
  10. #import "XYMarkingFriendsSkillListTableViewCell.h"
  11. #import "XYSkillUserModel.h"
  12. #import "XYSkillUserHomePageViewController.h"
  13. #import "XYSkillVoicePlayManager.h"
  14. @interface XYMarkingFriendsSkillListViewController ()<XYMarkingFriendsSkillListTableViewCellDelegate, XYSkillVoicePlayManagerDelegate>
  15. @property (nonatomic, strong) XYMarkingFriendsSkillListViewModel *viewModel;
  16. @property (nonatomic, strong) XYMarkingFriendsSkillListTableViewCell *playingCell;
  17. @end
  18. @implementation XYMarkingFriendsSkillListViewController
  19. - (instancetype)init
  20. {
  21. self = [super init];
  22. if (self) {
  23. self.prefersNavigationBarHidden = YES;
  24. }
  25. return self;
  26. }
  27. - (void)viewDidLoad {
  28. [super viewDidLoad];
  29. // Do any additional setup after loading the view.
  30. }
  31. - (void)viewWillAppear:(BOOL)animated {
  32. [super viewWillAppear:animated];
  33. [XYSkillVoicePlayManager sharedInstance].delegate = self;
  34. }
  35. - (void)viewWillDisappear:(BOOL)animated {
  36. [super viewWillDisappear:animated];
  37. [self stopPlay];
  38. [XYSkillVoicePlayManager sharedInstance].delegate = nil;
  39. }
  40. #pragma mark — Override
  41. - (void)bindViewModel {
  42. self.viewModel = XYMarkingFriendsSkillListViewModel.new;
  43. self.viewModel.skillId = [self.skillId copy];
  44. }
  45. - (UIEdgeInsets)contentInset {
  46. return UIEdgeInsetsMake(0, 0, STATUS_HEIGHT + 48.0f + 68.0f + TABBAR_HEIGHT, 0);
  47. }
  48. - (void)requestRemoteData {
  49. self.viewModel.pullDown = YES;
  50. [self.viewModel loadData:^(id _Nullable responseModel) {
  51. [self tableViewDidFinishTriggerHeader:YES reload:YES];
  52. [self.tableView zyl_configEmptyViewWithType:ZYLEmptyDataViewTypeDefault emptyInfo:@"" errorInfo:@"" offsetTop:DefaultOffsetTop hasData:self.viewModel.dataSource.count>0 hasError:NO reloadBlock:nil];
  53. } failure:^(ZYLNetworkError * _Nullable error) {
  54. [self tableViewDidFinishTriggerHeader:YES reload:NO];
  55. [self.tableView zyl_configEmptyViewWithType:ZYLEmptyDataViewTypeDefault emptyInfo:@"" errorInfo:error.domain offsetTop:DefaultOffsetTop hasData:self.viewModel.dataSource.count>0 hasError:error!=nil reloadBlock:nil];
  56. }];
  57. }
  58. - (void)tableViewDidTriggerFooterRefresh {
  59. [super tableViewDidTriggerFooterRefresh];
  60. }
  61. - (UITableViewCell *)tableView:(UITableView *)tableView dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath {
  62. return [XYMarkingFriendsSkillListTableViewCell cellWithTableView:tableView];
  63. }
  64. - (void)configureCell:(XYMarkingFriendsSkillListTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath withObject:(id)object {
  65. [cell configureModel:object];
  66. [cell setIndexPath:indexPath rowsInSection:self.viewModel.dataSource.count-1];
  67. cell.delegate = self;
  68. }
  69. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  70. return 94.0f;
  71. }
  72. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  73. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  74. XYSkillUserModel *model = self.viewModel.dataSource[indexPath.row];
  75. XYSkillUserHomePageViewController *controller = XYSkillUserHomePageViewController.new;
  76. controller.userId = [model.userId copy];
  77. controller.skillId = [model.skillId copy];
  78. [self.navigationController pushViewController:controller animated:YES];
  79. }
  80. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  81. /// 滚动时停止播放
  82. [self stopPlay];
  83. // 滑动到顶部,不允许继续下滑
  84. if (scrollView.contentOffset.y < 0) {
  85. [self.tableView setContentOffset:CGPointMake(0, 0)];
  86. }
  87. [[NSNotificationCenter defaultCenter] postNotificationName:@"SKILL_LIST_SCROLL_OFFSET_Y" object:@{@"offsetY":@(scrollView.contentOffset.y)}];
  88. }
  89. #pragma mark — XYSkillUserListTableViewCellDelegate
  90. - (void)cell:(XYMarkingFriendsSkillListTableViewCell *)cell didClickVoicePlayActionAtIndexPath:(NSIndexPath *)indexPath {
  91. XYSkillUserModel *model = self.viewModel.dataSource[indexPath.row];
  92. if (self.playingCell) {
  93. self.playingCell.voicePlayView.playing = NO;
  94. self.playingCell.voicePlayView.secondText = [NSString stringWithFormat:@"%ld''", [self.playingCell.skillModel.duration integerValue]];
  95. }
  96. self.playingCell = cell;
  97. // 播放/暂停
  98. [[XYSkillVoicePlayManager sharedInstance] playVoiceWithUrl:model.voice];
  99. }
  100. #pragma mark — XYSkillVoicePlayManagerDelegate
  101. /// 开始播放
  102. - (void)didStartPlayVoice {
  103. if (self.playingCell) {
  104. self.playingCell.voicePlayView.playing = YES;
  105. self.playingCell.voicePlayView.secondText = [NSString stringWithFormat:@"%ld''", [self.playingCell.skillModel.duration integerValue]];
  106. }
  107. }
  108. /// 播放中的时间回调
  109. - (void)playVoiceCurrtentTime:(NSInteger)time {
  110. if (self.playingCell) {
  111. self.playingCell.voicePlayView.secondText = [NSString stringWithFormat:@"%ld''", time];
  112. }
  113. }
  114. /// 播放完成或者暂停播放
  115. - (void)playbackCompleteOrPausedWithUrl:(NSString *)voiceUrl {
  116. if (self.playingCell) {
  117. self.playingCell.voicePlayView.playing = NO;
  118. self.playingCell.voicePlayView.secondText = [NSString stringWithFormat:@"%ld''", [self.playingCell.skillModel.duration integerValue]];
  119. }
  120. }
  121. #pragma mark — JXCategoryListContentViewDelegate
  122. /**
  123. 如果列表是VC,就返回VC.view
  124. 如果列表是View,就返回View自己
  125. @return 返回列表视图
  126. */
  127. - (UIView *)listView {
  128. return self.view;
  129. }
  130. - (void)stopPlay {
  131. XYSkillVoicePlayManager *playManager = [XYSkillVoicePlayManager sharedInstance];
  132. if (StringIsNotEmpty(playManager.currentVoiceUrl)) {
  133. [playManager resetStatus];
  134. }
  135. if (self.playingCell) {
  136. self.playingCell.voicePlayView.playing = NO;
  137. self.playingCell.voicePlayView.secondText = [NSString stringWithFormat:@"%ld''", [self.playingCell.skillModel.duration integerValue]];
  138. }
  139. }
  140. /*
  141. #pragma mark - Navigation
  142. // In a storyboard-based application, you will often want to do a little preparation before navigation
  143. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  144. // Get the new view controller using [segue destinationViewController].
  145. // Pass the selected object to the new view controller.
  146. }
  147. */
  148. @end