YMPersonalDynamicCell.m 18 KB

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