YMDynamicDetailInfoCell.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. //
  2. // YMDynamicDetailInfoCell.m
  3. // MSYOUPAI
  4. //
  5. // Created by YoMi on 2024/3/21.
  6. // Copyright © 2024 MS. All rights reserved.
  7. //
  8. #import "YMDynamicDetailInfoCell.h"
  9. #import "YMDynamicDetailInfoCellViewModel.h"
  10. #import "YMDynamicDetailInfoAlbumCell.h"
  11. #import "CHTCollectionViewWaterfallLayout.h"
  12. @interface YMDynamicDetailInfoCell ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout,CHTCollectionViewDelegateWaterfallLayout,YBImageBrowserDelegate>
  13. /// ViewModel
  14. @property (nonatomic, strong) YMDynamicDetailInfoCellViewModel *viewModel;
  15. /// 基础视图
  16. @property (nonatomic, strong) UIView *baseView;
  17. /// 用户头像视图
  18. @property (nonatomic, strong) UIImageView *userAvatarView;
  19. /// 用户昵称标签
  20. @property (nonatomic, strong) UILabel *userNicknameLb;
  21. /// 用户描述标签
  22. @property (nonatomic, strong) UILabel *userDescLb;
  23. /// VIP图标
  24. @property (nonatomic, strong) UIImageView *userVIPIcon;
  25. /// 心动按钮
  26. @property (nonatomic, strong) UIButton *heartbeatBtn;
  27. /// 用户内容标签
  28. @property (nonatomic, strong) UILabel *userContentLb;
  29. /// 用户相册排版
  30. @property (nonatomic, strong) CHTCollectionViewWaterfallLayout *albumLayout;
  31. /// 用户相册容器列表
  32. @property (nonatomic, strong) UICollectionView *albumCollectionView;
  33. /// 日期标签
  34. @property (nonatomic, strong) UILabel *dateLb;
  35. /// 评论按钮
  36. @property (nonatomic, strong) UIButton *commentsBtn;
  37. /// 点赞按钮
  38. @property (nonatomic, strong) UIButton *likesBtn;
  39. @end
  40. @implementation YMDynamicDetailInfoCell
  41. - (void)awakeFromNib {
  42. [super awakeFromNib];
  43. // Initialization code
  44. }
  45. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  46. [super setSelected:selected animated:animated];
  47. // Configure the view for the selected state
  48. }
  49. - (void)ym_setupViews{
  50. self.contentView.backgroundColor = UIColor.clearColor;
  51. self.backgroundColor = UIColor.clearColor;
  52. [self.contentView addSubview:self.baseView];
  53. [self.baseView addSubview:self.userAvatarView];
  54. [self.baseView addSubview:self.userNicknameLb];
  55. [self.baseView addSubview:self.userDescLb];
  56. [self.baseView addSubview:self.userVIPIcon];
  57. [self.baseView addSubview:self.heartbeatBtn];
  58. [self.baseView addSubview:self.userContentLb];
  59. [self.baseView addSubview:self.albumCollectionView];
  60. [self.baseView addSubview:self.dateLb];
  61. [self.baseView addSubview:self.commentsBtn];
  62. [self.baseView addSubview:self.likesBtn];
  63. [self setNeedsUpdateConstraints];
  64. [self updateConstraintsIfNeeded];
  65. }
  66. - (void)updateConstraints {
  67. [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
  68. make.top.equalTo(self.contentView).offset(adapt(10));
  69. make.left.equalTo(self.contentView);
  70. make.right.equalTo(self.contentView);
  71. make.bottom.equalTo(self.contentView);
  72. }];
  73. [self.userAvatarView mas_makeConstraints:^(MASConstraintMaker *make) {
  74. make.top.equalTo(self.baseView).offset(adapt(15));
  75. make.left.equalTo(self.baseView).offset(adapt(15));
  76. make.width.height.mas_equalTo(adapt(40));
  77. }];
  78. [self.userNicknameLb mas_makeConstraints:^(MASConstraintMaker *make) {
  79. make.top.equalTo(self.userAvatarView.mas_top);
  80. make.left.equalTo(self.userAvatarView.mas_right).offset(adapt(10));
  81. }];
  82. [self.userDescLb mas_makeConstraints:^(MASConstraintMaker *make) {
  83. make.top.equalTo(self.userNicknameLb.mas_bottom).offset(adapt(5));
  84. make.left.equalTo(self.userAvatarView.mas_right).offset(adapt(10));
  85. }];
  86. [self.userVIPIcon mas_makeConstraints:^(MASConstraintMaker *make) {
  87. make.centerY.equalTo(self.userNicknameLb.mas_centerY);
  88. make.left.equalTo(self.userNicknameLb.mas_right).offset(adapt(10));
  89. make.width.mas_equalTo(adapt(40));
  90. make.height.mas_equalTo(adapt(15));
  91. }];
  92. [self.heartbeatBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  93. make.centerY.equalTo(self.userNicknameLb.mas_centerY);
  94. make.right.equalTo(self.baseView).offset(adapt(-15));
  95. make.width.mas_equalTo(adapt(70));
  96. make.height.mas_equalTo(adapt(25));
  97. }];
  98. [self.userContentLb mas_makeConstraints:^(MASConstraintMaker *make) {
  99. make.top.equalTo(self.userDescLb.mas_bottom).offset(adapt(20));
  100. make.left.equalTo(self.baseView).offset(adapt(15));
  101. make.right.equalTo(self.baseView).offset(adapt(-15));
  102. }];
  103. [self.albumCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  104. make.top.equalTo(self.userContentLb.mas_bottom).offset(adapt(10));
  105. make.left.equalTo(self.userAvatarView.mas_right).offset(adapt(10));
  106. make.right.equalTo(self.baseView).offset(adapt(-15));
  107. }];
  108. [self.dateLb mas_makeConstraints:^(MASConstraintMaker *make) {
  109. make.top.equalTo(self.albumCollectionView.mas_bottom).offset(adapt(10));
  110. make.left.equalTo(self.albumCollectionView.mas_left);
  111. }];
  112. [self.commentsBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  113. make.centerY.equalTo(self.likesBtn.mas_centerY);
  114. make.right.equalTo(self.likesBtn.mas_left).offset(adapt(-10));
  115. make.width.mas_equalTo(adapt(70));
  116. make.height.mas_equalTo(adapt(30));
  117. }];
  118. [self.likesBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  119. make.top.equalTo(self.dateLb.mas_bottom).offset(adapt(10));
  120. make.right.equalTo(self.baseView).offset(adapt(-15));
  121. make.bottom.equalTo(self.baseView).offset(adapt(-15)).priorityHigh();
  122. make.width.mas_equalTo(adapt(70));
  123. make.height.mas_equalTo(adapt(30));
  124. }];
  125. [super updateConstraints];
  126. }
  127. - (void)ym_bindViewModel:(YMDynamicDetailInfoCellViewModel*)viewModel{
  128. if (!viewModel) {
  129. return;
  130. }
  131. _viewModel = viewModel;
  132. [self.userAvatarView sd_setImageWithURL:[LCTools getImageUrlWithAddress:self.viewModel.userAvatar]];
  133. self.userNicknameLb.text = self.viewModel.userNickname;
  134. self.userNicknameLb.textColor = self.viewModel.userNicknameColor;
  135. self.userDescLb.text = self.viewModel.userDesc;
  136. self.userVIPIcon.hidden = self.viewModel.isHideVIPIcon;
  137. self.heartbeatBtn.hidden = self.viewModel.isHideHeartbeatButton;
  138. self.userContentLb.text = self.viewModel.userContent;
  139. self.dateLb.text = self.viewModel.date;
  140. [self.commentsBtn setTitle:self.viewModel.commentsNumber forState:UIControlStateNormal];
  141. [self.commentsBtn setImage:self.viewModel.commentsImage forState:UIControlStateNormal];
  142. [self.likesBtn setTitle:self.viewModel.likesNumber forState:UIControlStateNormal];
  143. [self.likesBtn setImage:self.viewModel.likesImage forState:UIControlStateNormal];
  144. [self.albumCollectionView reloadData];
  145. [self.albumCollectionView layoutIfNeeded];
  146. [self.baseView layoutIfNeeded];
  147. }
  148. #pragma mark - UICollectionViewDataSource
  149. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  150. return 1;
  151. }
  152. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  153. return self.viewModel.albumDataArray.count;
  154. }
  155. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  156. YMDynamicDetailInfoAlbumCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([YMDynamicDetailInfoAlbumCell class]) forIndexPath:indexPath];
  157. [cell ym_bindViewModel:self.viewModel.albumDataArray[indexPath.item]];
  158. return cell;
  159. }
  160. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
  161. UICollectionReusableView *reusableView = nil;
  162. reusableView.backgroundColor = [UIColor clearColor];
  163. return reusableView;
  164. }
  165. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
  166. return CGSizeZero;
  167. }
  168. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
  169. return CGSizeZero;
  170. }
  171. #pragma mark - UICollectionViewDelegate
  172. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  173. NSMutableArray *tempArr = [NSMutableArray array];
  174. for (YMDynamicListAlbumCellViewModel *model in self.viewModel.albumDataArray) {
  175. if ([model.albumType isEqualToString:@"mp4"]) {
  176. YBIBVideoData *data = [YBIBVideoData new];
  177. data.videoURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",[LCSaveData getImageUrl]?[LCSaveData getImageUrl]:BaseImgUrl,model.albumUrl]];
  178. [collectionView cellForItemAtIndexPath:indexPath];
  179. [tempArr addObject:data];
  180. } else {
  181. YBIBImageData *data = [YBIBImageData new];
  182. data.imageURL = [LCTools getImageUrlWithAddress:model.albumUrl];
  183. data.projectiveView = [collectionView cellForItemAtIndexPath:indexPath];
  184. [tempArr addObject:data];
  185. }
  186. }
  187. YBImageBrowser *browser = [YBImageBrowser new];
  188. browser.delegate = self;
  189. browser.dataSourceArray = tempArr;
  190. browser.currentPage = indexPath.item;
  191. [browser show];
  192. }
  193. #pragma mark - CHTCollectionViewDelegateWaterfallLayout
  194. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  195. if (self.viewModel.albumDataArray.count >= 2) {
  196. return CGSizeMake(adapt(100), adapt(100));
  197. } else {
  198. CGSize albumSize = self.viewModel.albumDataArray[indexPath.item].albumSize;
  199. return CGSizeMake(adapt(albumSize.width), adapt(albumSize.height));
  200. }
  201. }
  202. - (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize withHorizontalFittingPriority:(UILayoutPriority)horizontalFittingPriority verticalFittingPriority:(UILayoutPriority)verticalFittingPriority {
  203. CGSize size = [super systemLayoutSizeFittingSize:targetSize withHorizontalFittingPriority:horizontalFittingPriority verticalFittingPriority:verticalFittingPriority];
  204. self.baseView.frame = CGRectMake(0, 0, targetSize.width, adapt(44));
  205. [self.baseView layoutIfNeeded];
  206. self.albumCollectionView.frame = CGRectMake(0, 0, targetSize.width - CGRectGetWidth(self.userAvatarView.frame) - adapt(10) - adapt(15)*2, adapt(44));
  207. [self.albumCollectionView layoutIfNeeded];
  208. CGSize collectionViewSize = self.albumCollectionView.collectionViewLayout.collectionViewContentSize;
  209. return CGSizeMake(size.width, size.height + collectionViewSize.height);
  210. }
  211. - (UIView *)baseView{
  212. if (!_baseView) {
  213. _baseView = [[UIView alloc]init];
  214. _baseView.backgroundColor = HexColorFromRGB(0xFFFFFF);
  215. }
  216. return _baseView;
  217. }
  218. - (UIImageView *)userAvatarView{
  219. if (!_userAvatarView) {
  220. _userAvatarView = [[UIImageView alloc]init];
  221. _userAvatarView.backgroundColor = UIColor.lightGrayColor;
  222. _userAvatarView.contentMode = UIViewContentModeScaleAspectFill;
  223. _userAvatarView.layer.cornerRadius = adapt(40)/2;
  224. _userAvatarView.clipsToBounds = YES;
  225. _userAvatarView.userInteractionEnabled = YES;
  226. WS(weakSelf)
  227. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
  228. [_userAvatarView addGestureRecognizer:tap];
  229. [[[tap rac_gestureSignal] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) {
  230. [weakSelf.viewModel.gotoPersonalPageSubject sendNext:@(weakSelf.viewModel.userId)];
  231. }];
  232. }
  233. return _userAvatarView;
  234. }
  235. - (UILabel *)userNicknameLb{
  236. if (!_userNicknameLb) {
  237. _userNicknameLb = [[UILabel alloc]init];
  238. _userNicknameLb.font = LCFont(15);
  239. _userNicknameLb.textColor = HexColorFromRGB(0x333333);
  240. _userNicknameLb.textAlignment = NSTextAlignmentLeft;
  241. _userNicknameLb.text = @"******";
  242. }
  243. return _userNicknameLb;
  244. }
  245. - (UILabel *)userDescLb{
  246. if (!_userDescLb) {
  247. _userDescLb = [[UILabel alloc]init];
  248. _userDescLb.font = LCFont(12);
  249. _userDescLb.textColor = HexColorFromRGB(0x9c9c9c);
  250. _userDescLb.textAlignment = NSTextAlignmentLeft;
  251. _userDescLb.text = @"******";
  252. }
  253. return _userDescLb;
  254. }
  255. - (UIImageView *)userVIPIcon{
  256. if (!_userVIPIcon) {
  257. _userVIPIcon = [[UIImageView alloc]init];
  258. _userVIPIcon.image = ImageByName(@"ym_dynamic_vip_icon");
  259. }
  260. return _userVIPIcon;
  261. }
  262. - (UIButton *)heartbeatBtn{
  263. if (!_heartbeatBtn) {
  264. _heartbeatBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  265. _heartbeatBtn.titleLabel.font = LCFont(13);
  266. [_heartbeatBtn setTitleColor:HexColorFromRGB(0xFFFFFF) forState:UIControlStateNormal];
  267. [_heartbeatBtn setTitle:@"心动" forState:UIControlStateNormal];
  268. [_heartbeatBtn setImage:ImageByName(@"ym_common_heartbeat_or_accost_icon") forState:UIControlStateNormal];
  269. _heartbeatBtn.backgroundColor = HexColorFromRGB(0xfd7bc5);
  270. _heartbeatBtn.layer.cornerRadius = adapt(25)/2;
  271. _heartbeatBtn.layer.masksToBounds = YES;
  272. _heartbeatBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
  273. [_heartbeatBtn setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];
  274. CGFloat margin = 5;
  275. _heartbeatBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -margin, 0, margin);
  276. // WS(weakSelf)
  277. [[[_heartbeatBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) {
  278. }];
  279. }
  280. return _heartbeatBtn;
  281. }
  282. - (UILabel *)userContentLb{
  283. if (!_userContentLb) {
  284. _userContentLb = [[UILabel alloc]init];
  285. _userContentLb.font = LCFont(14);
  286. _userContentLb.textColor = HexColorFromRGB(0x333333);
  287. _userContentLb.textAlignment = NSTextAlignmentLeft;
  288. _userContentLb.numberOfLines = 0;
  289. _userContentLb.text = @"******";
  290. }
  291. return _userContentLb;
  292. }
  293. - (CHTCollectionViewWaterfallLayout *)albumLayout{
  294. if (!_albumLayout) {
  295. _albumLayout = [[CHTCollectionViewWaterfallLayout alloc] init];
  296. _albumLayout.columnCount = 3;
  297. _albumLayout.sectionInset = UIEdgeInsetsMake(adapt(5), adapt(5), adapt(5), adapt(5));
  298. }
  299. return _albumLayout;
  300. }
  301. - (UICollectionView *)albumCollectionView{
  302. if (!_albumCollectionView) {
  303. _albumCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.albumLayout];
  304. _albumCollectionView.delegate = self;
  305. _albumCollectionView.dataSource = self;
  306. _albumCollectionView.showsVerticalScrollIndicator = NO;
  307. _albumCollectionView.showsHorizontalScrollIndicator = NO;
  308. _albumCollectionView.backgroundColor = UIColor.whiteColor;
  309. [_albumCollectionView registerClass:[YMDynamicDetailInfoAlbumCell class] forCellWithReuseIdentifier:NSStringFromClass([YMDynamicDetailInfoAlbumCell class])];
  310. }
  311. return _albumCollectionView;
  312. }
  313. - (UILabel *)dateLb{
  314. if (!_dateLb) {
  315. _dateLb = [[UILabel alloc]init];
  316. _dateLb.font = LCFont(12);
  317. _dateLb.textColor = HexColorFromRGB(0x9c9c9c);
  318. _dateLb.textAlignment = NSTextAlignmentLeft;
  319. _dateLb.text = @"****-**-**";
  320. }
  321. return _dateLb;
  322. }
  323. - (UIButton *)commentsBtn{
  324. if (!_commentsBtn) {
  325. _commentsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  326. _commentsBtn.titleLabel.font = LCFont(13);
  327. [_commentsBtn setTitleColor:HexColorFromRGB(0x9c9c9c) forState:UIControlStateNormal];
  328. [_commentsBtn setTitle:@"评论" forState:UIControlStateNormal];
  329. [_commentsBtn setImage:ImageByName(@"ym_dynamic_comments_icon") forState:UIControlStateNormal];
  330. _commentsBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  331. [_commentsBtn setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];
  332. CGFloat margin = 5;
  333. _commentsBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -margin, 0, margin);
  334. // WS(weakSelf)
  335. [[[_commentsBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) {
  336. }];
  337. }
  338. return _commentsBtn;
  339. }
  340. - (UIButton *)likesBtn{
  341. if (!_likesBtn) {
  342. _likesBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  343. _likesBtn.titleLabel.font = LCFont(13);
  344. [_likesBtn setTitleColor:HexColorFromRGB(0x9c9c9c) forState:UIControlStateNormal];
  345. [_likesBtn setTitle:@"点赞" forState:UIControlStateNormal];
  346. [_likesBtn setImage:ImageByName(@"ym_dynamic_likes_normal_icon") forState:UIControlStateNormal];
  347. _likesBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  348. [_likesBtn setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];
  349. CGFloat margin = 5;
  350. _likesBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -margin, 0, margin);
  351. // WS(weakSelf)
  352. [[[_likesBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) {
  353. }];
  354. }
  355. return _likesBtn;
  356. }
  357. @end