YOUPAILZHomeActiveVC.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. //
  2. // YOUPAILZHomeActiveVC.m
  3. // MSYOUPAI
  4. //
  5. // Created by CY on 2022/2/28.
  6. // Copyright © 2022 MS. All rights reserved.
  7. //
  8. #import "YOUPAILZHomeActiveVC.h"
  9. #import "UIScrollView+LZRefresh.h"
  10. #import "YOUPAILZHomeActiveCell.h"
  11. #import "FSAudioStream.h"
  12. #import "YOUPAILZUserShowVC.h"
  13. #import "YOUPAILCIMSessionVC.h"
  14. @interface YOUPAILZHomeActiveVC ()<UITableViewDelegate,UITableViewDataSource>
  15. @property (nonatomic, strong) NSMutableArray <YOUPAILCHomeListModel *>*youpaipdataSource;
  16. @property (nonatomic, strong) NSMutableDictionary *youpaipuserDict;
  17. @property (nonatomic, assign) NSInteger page;
  18. @property (nonatomic, weak) UITableView *youpaiptableView;
  19. // 语音播放器
  20. @property (nonatomic, strong)FSAudioStream *youpaipaudioStream;
  21. @property (nonatomic, strong)NSTimer *youpaiptimer;
  22. @property (nonatomic, strong)YOUPAILCHomeListModel *youpaipCurrentPlayModel;
  23. @end
  24. @implementation YOUPAILZHomeActiveVC
  25. - (void)viewWillDisappear:(BOOL)animated{
  26. [super viewWillDisappear:animated];
  27. [self youpaifstopPlay];
  28. }
  29. - (void)viewDidLoad {
  30. [super viewDidLoad];
  31. [self youpaifsetupUI];
  32. [self youpaifrequestListWithStatus:kRefreshNormal];
  33. }
  34. - (void)youpaifsetupUI{
  35. UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
  36. tableView.dataSource = self;
  37. tableView.delegate = self;
  38. tableView.rowHeight = 80.0f;
  39. tableView.estimatedRowHeight = 80.0f;
  40. tableView.estimatedSectionHeaderHeight = 0.0f;
  41. tableView.estimatedSectionFooterHeight = 0.0f;
  42. tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  43. tableView.backgroundColor = [UIColor clearColor];
  44. [self.view addSubview:tableView];
  45. self.youpaiptableView = tableView;
  46. [tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
  47. make.top.left.bottom.right.offset(0.0f);
  48. }];
  49. @weakify(self);
  50. [tableView setRefreshHeaderWithBlock:^{
  51. @strongify(self);
  52. [self youpaifstopPlay];
  53. [self youpaifrequestListWithStatus:kRefreshHeader];
  54. }];
  55. }
  56. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  57. return self.youpaipdataSource.count;
  58. }
  59. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  60. return 1;
  61. }
  62. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  63. YOUPAILZHomeActiveCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(YOUPAILZHomeActiveCell.class)];
  64. if (cell == nil) {
  65. cell = [[YOUPAILZHomeActiveCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass(YOUPAILZHomeActiveCell.class)];
  66. }
  67. [cell youpaifreloadWithModel:self.youpaipdataSource[indexPath.section]];
  68. @weakify(self);
  69. [cell setYoupaipAudioClickBlock:^(YOUPAILCHomeListModel * _Nonnull model, BOOL isPlay) {
  70. @strongify(self);
  71. [self youpaifplayAudioWithModel:model isPlay:isPlay];
  72. }];
  73. [cell setYoupaipChatClickBlock:^(YOUPAILCHomeListModel * _Nonnull model) {
  74. @strongify(self);
  75. [self youpaifChatClickWithModel:model];
  76. }];
  77. return cell;
  78. }
  79. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  80. return CGFLOAT_MIN;
  81. }
  82. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
  83. return 8.0f;
  84. }
  85. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  86. YOUPAILCHomeListModel *model = self.youpaipdataSource[indexPath.section];
  87. YOUPAILZUserShowVC *userShow = [[YOUPAILZUserShowVC alloc] init];
  88. userShow.youpaipuserId = model.youpaipuserid;
  89. [self.navigationController pushViewController:userShow animated:YES];
  90. }
  91. - (void)youpaifinitAudioStream{
  92. self.youpaipaudioStream = [[FSAudioStream alloc] init];
  93. @weakify(self);
  94. self.youpaipaudioStream.onFailure = ^(FSAudioStreamError error,NSString *description){
  95. @strongify(self);
  96. [self youpaifstopPlay];
  97. };
  98. self.youpaipaudioStream.onCompletion = ^(){
  99. @strongify(self);
  100. [self youpaifstopPlay];
  101. };
  102. [self.youpaipaudioStream setVolume:0.7];//设置声音
  103. self.youpaipaudioStream.strictContentTypeChecking = NO;
  104. self.youpaipaudioStream.defaultContentType = @"audio/mpeg";
  105. }
  106. /// 私信 搭讪 心动
  107. - (void)youpaifChatClickWithModel:(YOUPAILCHomeListModel *)model{
  108. if (![LCTools isGirlsRealNameAuthentication]) {
  109. return;
  110. }
  111. if(model.youpaipis_beckon){//心动 true 已心动过,false 未心动过
  112. @weakify(self);
  113. NIMSession *session = [NIMSession session:model.youpaipuserid type:NIMSessionTypeP2P];
  114. if (session) {
  115. [ZCHUDHelper show];
  116. [[[NIMSDK sharedSDK] userManager] fetchUserInfos:@[session.sessionId] completion:^(NSArray<NIMUser *> * _Nullable users, NSError * _Nullable error) {
  117. @strongify(self);
  118. [ZCHUDHelper dismiss];
  119. YOUPAILCIMSessionVC *vc = [[YOUPAILCIMSessionVC alloc] initWithSession:session];
  120. vc.type = @"1";
  121. [self.navigationController pushViewController:vc animated:YES];
  122. }];
  123. }
  124. }else{
  125. [UMengRecordTool umengEventCountWithId:ClickToChatHome];//友盟首页点击搭讪
  126. NSString *str = [NSString stringWithFormat:@"[%@]",model.youpaipuserid];
  127. @weakify(self);
  128. [LCHttpHelper requestWithURLString:HomeBeckonSend parameters:@{@"user_ids":str} needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) {
  129. @strongify(self);
  130. NSDictionary* dict = (NSDictionary*)responseObject;
  131. NSInteger code = [[dict objectForKey:@"code"] integerValue];
  132. NSDictionary* dict1 = dict[@"data"];
  133. if (code==0) {//成功
  134. if (dict1[@"boole"]) {
  135. model.youpaipis_beckon = YES;
  136. [UIView performWithoutAnimation:^{
  137. [self.youpaiptableView reloadSections:[NSIndexSet indexSetWithIndex:[self.youpaipdataSource indexOfObject:model]] withRowAnimation:UITableViewRowAnimationNone];
  138. }];
  139. }
  140. }else{
  141. [ZCHUDHelper showTitle:[dict objectForKey:@"message"]];
  142. }
  143. } failure:^(NSError *error) {
  144. }];
  145. }
  146. }
  147. /// 播放or暂停
  148. - (void)youpaifplayAudioWithModel:(YOUPAILCHomeListModel *)model isPlay:(BOOL)isPlay{
  149. [self youpaifstopPlay];
  150. self.youpaipCurrentPlayModel = model;
  151. if (isPlay) {
  152. [self youpaifstartPlay];
  153. [self youpaifstartTimer];
  154. }else{
  155. [self youpaifstopPlay];
  156. }
  157. }
  158. /// 开始播放语音
  159. - (void)youpaifstartPlay{
  160. if (self.youpaipaudioStream == nil) {
  161. [self youpaifinitAudioStream];
  162. }
  163. NSURL *url = [LCTools getImageUrlWithAddress:self.youpaipCurrentPlayModel.youpaipmp3];
  164. self.youpaipaudioStream.url = url;
  165. [self.youpaipaudioStream play];
  166. [[self youpaifLoadCurrentPlayCell] youpaifreloadCurrentPlayTime];
  167. }
  168. /// 停止播放语音
  169. - (void)youpaifstopPlay{
  170. // 停止播放
  171. if (self.youpaipaudioStream != nil || [self.youpaipaudioStream isPlaying]) {
  172. [self.youpaipaudioStream stop];
  173. self.youpaipaudioStream = nil;
  174. }
  175. self.youpaipCurrentPlayModel.youpaipisPlay = NO;
  176. self.youpaipCurrentPlayModel.youpaipCurrentPlayTime = 0;
  177. [self youpaifstopTimer];
  178. [[self youpaifLoadCurrentPlayCell] youpaifstopPlay];
  179. }
  180. - (void)youpaiftimerAction{
  181. self.youpaipCurrentPlayModel.youpaipCurrentPlayTime = self.youpaipaudioStream.currentTimePlayed.playbackTimeInSeconds;
  182. [[self youpaifLoadCurrentPlayCell] youpaifreloadCurrentPlayTime];
  183. }
  184. // 开始计时
  185. - (void)youpaifstartTimer{
  186. self.youpaiptimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(youpaiftimerAction) userInfo:nil repeats:YES];
  187. [[NSRunLoop currentRunLoop] addTimer:self.youpaiptimer forMode:NSRunLoopCommonModes];
  188. }
  189. // 结束计时
  190. - (void)youpaifstopTimer{
  191. [self.youpaiptimer invalidate];
  192. self.youpaiptimer = nil;
  193. }
  194. - (YOUPAILZHomeActiveCell *)youpaifLoadCurrentPlayCell{
  195. if ([self.youpaipdataSource containsObject:self.youpaipCurrentPlayModel]) {
  196. NSInteger index = [self.youpaipdataSource indexOfObject:self.youpaipCurrentPlayModel];
  197. YOUPAILZHomeActiveCell *cell = [self.youpaiptableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:index]];
  198. return cell;
  199. }
  200. return nil;
  201. }
  202. /// 设置加载更多
  203. - (void)youpaifrefreshFooterWithHidden:(BOOL)hidden{
  204. @weakify(self);
  205. [self.youpaiptableView setRefreshFooter:hidden withBlock:^{
  206. @strongify(self);
  207. [self youpaifrequestListWithStatus:kRefreshFooter];
  208. }];
  209. }
  210. /// 获取列表数据
  211. - (void)youpaifrequestListWithStatus:(kRefreshStatus)status{
  212. if (status == kRefreshFooter) {
  213. self.page ++;
  214. }else{
  215. self.page = 1;
  216. }
  217. @weakify(self);
  218. [LCHttpHelper requestWithURLString:self.youpaiprequestUrl parameters:@{@"page":@(self.page)} needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) {
  219. @strongify(self);
  220. [self.youpaiptableView endRefreshing:kRefreshAll];
  221. NSDictionary* dict = (NSDictionary*)responseObject;
  222. NSInteger code = [[dict objectForKey:@"code"] integerValue];
  223. if (code == 0) {//成功
  224. if (status != kRefreshFooter) {
  225. [self.youpaipdataSource removeAllObjects];
  226. [self.youpaipuserDict removeAllObjects];
  227. }
  228. NSInteger totalPage = [[[[dict objectForKey:@"data"] objectForKey:@"list"]objectForKey:@"total_page"]integerValue];
  229. NSArray* array = [[[dict objectForKey:@"data"]objectForKey:@"list"]objectForKey:@"list"];
  230. NSArray <YOUPAILCHomeListModel *>*list = [YOUPAILCHomeListModel mj_objectArrayWithKeyValuesArray:array];
  231. for (YOUPAILCHomeListModel *model in list) {
  232. if (![self.youpaipuserDict objectForKey:model.youpaipuserid]) {
  233. [self.youpaipdataSource addObject:model];
  234. [self.youpaipuserDict setObject:@"1" forKey:model.youpaipuserid];
  235. }
  236. }
  237. // [self.dataSource addObjectsFromArray:list];
  238. NSInteger currentPage = [[[[dict objectForKey:@"data"] objectForKey:@"list"]objectForKey:@"page"]integerValue];
  239. if(currentPage>0&&currentPage%5==0)
  240. {
  241. [[SDImageCache sharedImageCache]clearMemory];
  242. }
  243. [self.youpaiptableView reloadData];
  244. [self youpaifrefreshFooterWithHidden:self.page >= totalPage];
  245. if (self.youpaipdataSource.count != 0) {
  246. [self.youpaiptableView lz_hideEmptyView];
  247. }else{
  248. [self.youpaiptableView lz_showEmptyViewWithImage:[UIImage imageNamed:@"vqu_images_not_home_data"] content:@"暂无查到相关数据\n看看为您推荐的热搜小姐姐吧~"];
  249. }
  250. if (self.showUserInfoImperfectBlock != nil) {
  251. BOOL is_info = [[[[dict objectForKey:@"data"] objectForKey:@"activity"] objectForKey:@"is_info"] boolValue];
  252. self.showUserInfoImperfectBlock(!is_info);
  253. }
  254. }
  255. } failure:^(NSError *error) {
  256. [self.youpaiptableView endRefreshing:kRefreshAll];
  257. }];
  258. }
  259. #pragma mark - getter and setter
  260. - (NSMutableArray<YOUPAILCHomeListModel *> *)youpaipdataSource{
  261. if (!_youpaipdataSource) {
  262. _youpaipdataSource = [NSMutableArray array];
  263. }
  264. return _youpaipdataSource;
  265. }
  266. - (NSMutableDictionary *)youpaipuserDict{
  267. if (!_youpaipuserDict) {
  268. _youpaipuserDict = [NSMutableDictionary dictionary];
  269. }
  270. return _youpaipuserDict;
  271. }
  272. #pragma mark - JXCategoryListContentViewDelegate
  273. - (UIView *)listView{
  274. return self.view;
  275. }
  276. @end