YMDynamicListCell.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. //
  2. // YMDynamicListCell.m
  3. // MSYOUPAI
  4. //
  5. // Created by YoMi on 2024/2/26.
  6. // Copyright © 2024 MS. All rights reserved.
  7. //
  8. #import "YMDynamicListCell.h"
  9. #import "YMDynamicListCellViewModel.h"
  10. #import "YMDynamicListAlbumCell.h"
  11. #import "CHTCollectionViewWaterfallLayout.h"
  12. @interface YMDynamicListCell ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout,CHTCollectionViewDelegateWaterfallLayout,YBImageBrowserDelegate>
  13. /// ViewModel
  14. @property (nonatomic, strong) YMDynamicListCellViewModel *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 YMDynamicListCell
  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.userAvatarView.mas_right).offset(adapt(10));
  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:(YMDynamicListCellViewModel*)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. YMDynamicListAlbumCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([YMDynamicListAlbumCell 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. YBIBImageData *data = [YBIBImageData new];
  176. data.imageURL = [LCTools getImageUrlWithAddress:model.albumUrl];
  177. data.projectiveView = [collectionView cellForItemAtIndexPath:indexPath];
  178. [tempArr addObject:data];
  179. }
  180. YBImageBrowser *browser = [YBImageBrowser new];
  181. browser.delegate = self;
  182. browser.dataSourceArray = tempArr;
  183. browser.currentPage = indexPath.item;
  184. [browser show];
  185. }
  186. #pragma mark - CHTCollectionViewDelegateWaterfallLayout
  187. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  188. if (self.viewModel.albumDataArray.count >= 2) {
  189. return CGSizeMake(adapt(100), adapt(100));
  190. } else {
  191. CGSize imageSize = self.viewModel.albumDataArray[indexPath.item].albumSize;
  192. return CGSizeMake(adapt(imageSize.width), adapt(imageSize.height));
  193. }
  194. }
  195. - (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize withHorizontalFittingPriority:(UILayoutPriority)horizontalFittingPriority verticalFittingPriority:(UILayoutPriority)verticalFittingPriority {
  196. CGSize size = [super systemLayoutSizeFittingSize:targetSize withHorizontalFittingPriority:horizontalFittingPriority verticalFittingPriority:verticalFittingPriority];
  197. self.baseView.frame = CGRectMake(0, 0, targetSize.width, adapt(44));
  198. [self.baseView layoutIfNeeded];
  199. self.albumCollectionView.frame = CGRectMake(0, 0, targetSize.width - CGRectGetWidth(self.userAvatarView.frame) - adapt(10) - adapt(15)*2, adapt(44));
  200. [self.albumCollectionView layoutIfNeeded];
  201. CGSize collectionViewSize = self.albumCollectionView.collectionViewLayout.collectionViewContentSize;
  202. return CGSizeMake(size.width, size.height + collectionViewSize.height);
  203. }
  204. - (UIView *)baseView{
  205. if (!_baseView) {
  206. _baseView = [[UIView alloc]init];
  207. _baseView.backgroundColor = HexColorFromRGB(0xFFFFFF);
  208. }
  209. return _baseView;
  210. }
  211. - (UIImageView *)userAvatarView{
  212. if (!_userAvatarView) {
  213. _userAvatarView = [[UIImageView alloc]init];
  214. _userAvatarView.backgroundColor = UIColor.lightGrayColor;
  215. _userAvatarView.contentMode = UIViewContentModeScaleAspectFill;
  216. _userAvatarView.layer.cornerRadius = adapt(40)/2;
  217. _userAvatarView.clipsToBounds = YES;
  218. _userAvatarView.userInteractionEnabled = YES;
  219. WS(weakSelf)
  220. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
  221. [_userAvatarView addGestureRecognizer:tap];
  222. [[[tap rac_gestureSignal] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) {
  223. [weakSelf.viewModel.gotoPersonalPageSubject sendNext:@(weakSelf.viewModel.userId)];
  224. }];
  225. }
  226. return _userAvatarView;
  227. }
  228. - (UILabel *)userNicknameLb{
  229. if (!_userNicknameLb) {
  230. _userNicknameLb = [[UILabel alloc]init];
  231. _userNicknameLb.font = LCFont(15);
  232. _userNicknameLb.textColor = HexColorFromRGB(0x333333);
  233. _userNicknameLb.textAlignment = NSTextAlignmentLeft;
  234. _userNicknameLb.text = @"******";
  235. }
  236. return _userNicknameLb;
  237. }
  238. - (UILabel *)userDescLb{
  239. if (!_userDescLb) {
  240. _userDescLb = [[UILabel alloc]init];
  241. _userDescLb.font = LCFont(12);
  242. _userDescLb.textColor = HexColorFromRGB(0x9c9c9c);
  243. _userDescLb.textAlignment = NSTextAlignmentLeft;
  244. _userDescLb.text = @"******";
  245. }
  246. return _userDescLb;
  247. }
  248. - (UIImageView *)userVIPIcon{
  249. if (!_userVIPIcon) {
  250. _userVIPIcon = [[UIImageView alloc]init];
  251. _userVIPIcon.image = ImageByName(@"ym_dynamic_vip_icon");
  252. }
  253. return _userVIPIcon;
  254. }
  255. - (UIButton *)heartbeatBtn{
  256. if (!_heartbeatBtn) {
  257. _heartbeatBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  258. _heartbeatBtn.titleLabel.font = LCFont(13);
  259. [_heartbeatBtn setTitleColor:HexColorFromRGB(0xFFFFFF) forState:UIControlStateNormal];
  260. //[_heartbeatBtn setTitle:@"心动" forState:UIControlStateNormal];
  261. [_heartbeatBtn setImage:ImageByName(@"ym_common_heartbeat_or_accost_icon") forState:UIControlStateNormal];
  262. _heartbeatBtn.backgroundColor = HexColorFromRGB(0xfd7bc5);
  263. _heartbeatBtn.layer.cornerRadius = adapt(25)/2;
  264. _heartbeatBtn.layer.masksToBounds = YES;
  265. _heartbeatBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
  266. [_heartbeatBtn setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];
  267. CGFloat margin = 5;
  268. _heartbeatBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -margin, 0, margin);
  269. // WS(weakSelf)
  270. [[[_heartbeatBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) {
  271. }];
  272. }
  273. return _heartbeatBtn;
  274. }
  275. - (UILabel *)userContentLb{
  276. if (!_userContentLb) {
  277. _userContentLb = [[UILabel alloc]init];
  278. _userContentLb.font = LCFont(14);
  279. _userContentLb.textColor = HexColorFromRGB(0x333333);
  280. _userContentLb.textAlignment = NSTextAlignmentLeft;
  281. _userContentLb.numberOfLines = 0;
  282. _userContentLb.text = @"******";
  283. }
  284. return _userContentLb;
  285. }
  286. - (CHTCollectionViewWaterfallLayout *)albumLayout{
  287. if (!_albumLayout) {
  288. _albumLayout = [[CHTCollectionViewWaterfallLayout alloc] init];
  289. _albumLayout.columnCount = 3;
  290. _albumLayout.sectionInset = UIEdgeInsetsMake(adapt(5), adapt(5), adapt(5), adapt(5));
  291. }
  292. return _albumLayout;
  293. }
  294. - (UICollectionView *)albumCollectionView{
  295. if (!_albumCollectionView) {
  296. _albumCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.albumLayout];
  297. _albumCollectionView.delegate = self;
  298. _albumCollectionView.dataSource = self;
  299. _albumCollectionView.showsVerticalScrollIndicator = NO;
  300. _albumCollectionView.showsHorizontalScrollIndicator = NO;
  301. _albumCollectionView.backgroundColor = UIColor.whiteColor;
  302. [_albumCollectionView registerClass:[YMDynamicListAlbumCell class] forCellWithReuseIdentifier:NSStringFromClass([YMDynamicListAlbumCell class])];
  303. }
  304. return _albumCollectionView;
  305. }
  306. - (UILabel *)dateLb{
  307. if (!_dateLb) {
  308. _dateLb = [[UILabel alloc]init];
  309. _dateLb.font = LCFont(12);
  310. _dateLb.textColor = HexColorFromRGB(0x9c9c9c);
  311. _dateLb.textAlignment = NSTextAlignmentLeft;
  312. _dateLb.text = @"****-**-**";
  313. }
  314. return _dateLb;
  315. }
  316. - (UIButton *)commentsBtn{
  317. if (!_commentsBtn) {
  318. _commentsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  319. _commentsBtn.titleLabel.font = LCFont(13);
  320. [_commentsBtn setTitleColor:HexColorFromRGB(0x9c9c9c) forState:UIControlStateNormal];
  321. [_commentsBtn setTitle:@"评论" forState:UIControlStateNormal];
  322. [_commentsBtn setImage:ImageByName(@"ym_dynamic_comments_icon") forState:UIControlStateNormal];
  323. _commentsBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  324. [_commentsBtn setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];
  325. CGFloat margin = 5;
  326. _commentsBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -margin, 0, margin);
  327. WS(weakSelf)
  328. [[[_commentsBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) {
  329. }];
  330. }
  331. return _commentsBtn;
  332. }
  333. - (UIButton *)likesBtn{
  334. if (!_likesBtn) {
  335. _likesBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  336. _likesBtn.titleLabel.font = LCFont(13);
  337. [_likesBtn setTitleColor:HexColorFromRGB(0x9c9c9c) forState:UIControlStateNormal];
  338. [_likesBtn setTitle:@"点赞" forState:UIControlStateNormal];
  339. [_likesBtn setImage:ImageByName(@"ym_dynamic_likes_normal_icon") forState:UIControlStateNormal];
  340. _likesBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  341. [_likesBtn setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];
  342. CGFloat margin = 5;
  343. _likesBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -margin, 0, margin);
  344. WS(weakSelf)
  345. [[[_likesBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) {
  346. }];
  347. }
  348. return _likesBtn;
  349. }
  350. @end