Commit 53296024 cgx

社区动态列表、点赞逻辑处理

1 个父辈 b3ebe4bc
......@@ -57,6 +57,7 @@
D02D082A287BCEF3005C3A00 /* SRTipsView.m in Sources */ = {isa = PBXBuildFile; fileRef = D02D0829287BCEF3005C3A00 /* SRTipsView.m */; };
D02D082D287BD05D005C3A00 /* ReadyListController.m in Sources */ = {isa = PBXBuildFile; fileRef = D02D082C287BD05D005C3A00 /* ReadyListController.m */; };
D02E85882882A594002DAD7E /* PrepareItemsCell.m in Sources */ = {isa = PBXBuildFile; fileRef = D02E85872882A594002DAD7E /* PrepareItemsCell.m */; };
D030796F28E3E32A00ED96BF /* LikeButton.m in Sources */ = {isa = PBXBuildFile; fileRef = D030796E28E3E32A00ED96BF /* LikeButton.m */; };
D030C1B8284775930014946F /* RelaxTrainController.m in Sources */ = {isa = PBXBuildFile; fileRef = D030C1B7284775930014946F /* RelaxTrainController.m */; };
D0336163285035AB005573CF /* DailyBgView.m in Sources */ = {isa = PBXBuildFile; fileRef = D0336162285035AB005573CF /* DailyBgView.m */; };
D03559E42887DE4400168402 /* NSObject+AutoCancelRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = D03559E32887DE4400168402 /* NSObject+AutoCancelRequest.m */; };
......@@ -380,6 +381,8 @@
D02D082C287BD05D005C3A00 /* ReadyListController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ReadyListController.m; sourceTree = "<group>"; };
D02E85862882A594002DAD7E /* PrepareItemsCell.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PrepareItemsCell.h; sourceTree = "<group>"; };
D02E85872882A594002DAD7E /* PrepareItemsCell.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = PrepareItemsCell.m; sourceTree = "<group>"; };
D030796D28E3E32A00ED96BF /* LikeButton.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LikeButton.h; sourceTree = "<group>"; };
D030796E28E3E32A00ED96BF /* LikeButton.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LikeButton.m; sourceTree = "<group>"; };
D030C1B6284775930014946F /* RelaxTrainController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RelaxTrainController.h; sourceTree = "<group>"; };
D030C1B7284775930014946F /* RelaxTrainController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RelaxTrainController.m; sourceTree = "<group>"; };
D0336161285035AB005573CF /* DailyBgView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DailyBgView.h; sourceTree = "<group>"; };
......@@ -1163,6 +1166,15 @@
path = SleepReady;
sourceTree = "<group>";
};
D030796628E3D92700ED96BF /* LikeButton */ = {
isa = PBXGroup;
children = (
D030796D28E3E32A00ED96BF /* LikeButton.h */,
D030796E28E3E32A00ED96BF /* LikeButton.m */,
);
path = LikeButton;
sourceTree = "<group>";
};
D03559E12887DE4400168402 /* YRAutoCancelRequest */ = {
isa = PBXGroup;
children = (
......@@ -1449,6 +1461,7 @@
D0A3BB8D28D959F800F58781 /* View */ = {
isa = PBXGroup;
children = (
D030796628E3D92700ED96BF /* LikeButton */,
D02B7E5A28E2A28500218EF7 /* ComDynamicCell.h */,
D02B7E5B28E2A28500218EF7 /* ComDynamicCell.m */,
D0A3BB9028D95A2B00F58781 /* CommunityView.h */,
......@@ -2566,6 +2579,7 @@
D0A72E3F282B89AD00EED7BE /* DSHomeView.m in Sources */,
D01814E8280020F900583D4E /* CWPageControl.m in Sources */,
D027EE3027FB52DA004BBA61 /* UIImage+Extras.m in Sources */,
D030796F28E3E32A00ED96BF /* LikeButton.m in Sources */,
D037C49A284F4F0D000F3089 /* DailyTaskCell.m in Sources */,
D043DD55285B11860043A4CE /* VersionUpdateBox.m in Sources */,
D0A7510E282E2FEF00589B0E /* NoisePlayView.m in Sources */,
......
......@@ -7,13 +7,13 @@
#import "CommunityController.h"
#import "CommunityView.h"
#import "TestFlutterController.h"
#import "ComListViewModel.h"
#import "TestFlutterController.h"
@interface CommunityController () <CommunityViewDelegate>
@property (nonatomic, strong) CommunityView *communityView;
@property (nonatomic, strong) TestFlutterController *flutterEngine;
@property (nonatomic, strong) ComListViewModel *comListViewModel;
@property (nonatomic, assign) int offset;
@end
@implementation CommunityController
......@@ -27,7 +27,7 @@
[self setupUI];
self.comListViewModel = [ComListViewModel new];
self.offset = 1;
}
- (void)setupUI {
......@@ -43,20 +43,41 @@
}
- (void)messageAction {
if (![LoginUtils getUserLoginData]) {
[LoginUtils jumpToLoginControllerWithTarget:self];
return;
}
self.flutterEngine = [TestFlutterController new];
[self.navigationController pushViewController:self.flutterEngine animated:YES];
}
#pragma mark - CommunityViewDelegate
- (void)tapCommunityHeaderModule:(NSInteger)index {
if (![LoginUtils getUserLoginData]) {
[LoginUtils jumpToLoginControllerWithTarget:self];
return;
}
NSString *clsName = index == 1 ? @"ArticleController" : @"EvaluateController";
[self.navigationController pushViewController:[NSClassFromString(clsName) new] animated:YES];
}
- (void)getDynamicListRequest:(BOOL)loadMore {
[self.comListViewModel getComListWithCompletion:^(ComListViewModel * _Nonnull requestModel) {
if (loadMore == NO) { self.offset = 1; }
[ComListViewModel querySleepDynamicListWithOffset:self.offset completion:^(ComListViewModel * _Nonnull requestModel) {
if (requestModel.resCode == DSResCodeSuccess) {
if (loadMore) {
if (self.offset > requestModel.totalCount) {
[DSProgressHUD showToast:@"无更多动态"];
[self.communityView updateCommunityMoments:loadMore listArr:@[]];
return;
}
}
self.offset++;
[self.communityView updateCommunityMoments:loadMore listArr:requestModel.listArr];
} else {
[DSProgressHUD showToast:requestModel.errMessage];
[self.communityView endRefreshing:loadMore];
}
}];
}
......@@ -78,9 +99,47 @@
}
- (void)didSelectItem:(NSIndexPath *)indexPath {
if (![LoginUtils getUserLoginData]) {
[LoginUtils jumpToLoginControllerWithTarget:self];
return;
}
[self.navigationController pushViewController:[NSClassFromString(@"DynamicDetailController") new] animated:YES];
}
- (void)didTapLikeItem:(ComDynModel *)comDynModel cell:(ComDynamicCell *)cell {
if (![LoginUtils getUserLoginData]) {
[LoginUtils jumpToLoginControllerWithTarget:self];
return;
}
// 1、用户点赞或取消点赞直接响应UI变化
if (comDynModel.isLike) {
comDynModel.likeCount--;
} else {
comDynModel.likeCount++;
}
comDynModel.isLike = !comDynModel.isLike;
[cell fireLikeAnimate];
[cell updateLikeUI];
// 2、更新点赞或取消点赞请求
[ComListViewModel userDynamicPraiseWithTalkID:comDynModel.dynamicID completion:^(ComListViewModel * _Nonnull requestModel) {
// 3、如果请求失败,延迟执行回退
if (requestModel.resCode != DSResCodeSuccess) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 修改数据model
if (comDynModel.isLike) {
comDynModel.likeCount--;
} else {
comDynModel.likeCount++;
}
comDynModel.isLike = !comDynModel.isLike;
[cell updateLikeUI];
[DSProgressHUD showToast:requestModel.errMessage];
});
}
}];
}
#pragma mark - lazy
- (CommunityView *)communityView {
if (!_communityView) {
......
......@@ -19,6 +19,8 @@
<array>
<string>https://img2.ydniu.com/sleep_ssmain/relax/1653288028602_1yFqHL.jpg</string>
</array>
<key>isLike</key>
<false/>
<key>likeCount</key>
<integer>2</integer>
<key>remarkCount</key>
......@@ -38,6 +40,8 @@
<string>https://img2.ydniu.com/sleep_ssmain/relax/1653288005388_7wuK9n.jpg</string>
<string>https://img2.ydniu.com/sleep_ssmain/relax/1653277874846_BzEuFF.jpg</string>
</array>
<key>isLike</key>
<true/>
<key>likeCount</key>
<integer>33</integer>
<key>remarkCount</key>
......@@ -57,6 +61,8 @@
<string>https://img2.ydniu.com/sleep_ssmain/relax/1653288005388_7wuK9n.jpg</string>
<string>https://img2.ydniu.com/sleep_ssmain/relax/1653277874846_BzEuFF.jpg</string>
</array>
<key>isLike</key>
<true/>
<key>likeCount</key>
<integer>33</integer>
<key>remarkCount</key>
......@@ -77,6 +83,8 @@
<string>https://img2.ydniu.com/sleep_ssmain/relax/1653301114665_lwDNUG.jpg</string>
<string>https://img2.ydniu.com/sleep_ssmain/relax/1653301150570_zfQ0jW.jpg</string>
</array>
<key>isLike</key>
<false/>
<key>likeCount</key>
<integer>0</integer>
<key>remarkCount</key>
......@@ -97,6 +105,8 @@
<string>https://img2.ydniu.com/sleep_ssmain/relax/1653301114665_lwDNUG.jpg</string>
<string>https://img2.ydniu.com/sleep_ssmain/relax/1653301150570_zfQ0jW.jpg</string>
</array>
<key>isLike</key>
<false/>
<key>likeCount</key>
<integer>0</integer>
<key>remarkCount</key>
......@@ -117,6 +127,8 @@
<string>https://img2.ydniu.com/sleep_ssmain/relax/1653301114665_lwDNUG.jpg</string>
<string>https://img2.ydniu.com/sleep_ssmain/relax/1653301150570_zfQ0jW.jpg</string>
</array>
<key>isLike</key>
<false/>
<key>likeCount</key>
<integer>0</integer>
<key>remarkCount</key>
......@@ -131,6 +143,8 @@
<string>3天前</string>
<key>content</key>
<string>觉知当下,与家人建立和谐关系觉知当下,与家人建立和谐关系觉知当下,与家人建立和谐关系觉知当下,与家人建立和谐关系觉知当下,与家人建立和谐关系觉知当下,与家人建立和谐关系觉知当下,与家人建立和谐关系&quot;觉知当下,与家人建立和谐关系&quot;觉知当下,与家人建立和谐关系觉知当下,与家人建立和谐关系&quot;觉知当下,与家人建立和谐关系&quot;觉知当下,与家人建立和谐关系&quot;觉知当下,与家人建立和谐关系&quot;觉知当下,与家人建立和谐关系&quot;</string>
<key>isLike</key>
<false/>
<key>likeCount</key>
<integer>2</integer>
<key>remarkCount</key>
......@@ -149,6 +163,8 @@
<array>
<string>https://img2.ydniu.com/sleep_ssmain/relax/1653288028602_1yFqHL.jpg</string>
</array>
<key>isLike</key>
<false/>
<key>likeCount</key>
<integer>2</integer>
<key>remarkCount</key>
......@@ -167,6 +183,8 @@
<array>
<string>https://img2.ydniu.com/sleep_ssmain/relax/1653288028602_1yFqHL.jpg</string>
</array>
<key>isLike</key>
<false/>
<key>likeCount</key>
<integer>2</integer>
<key>remarkCount</key>
......
......@@ -11,6 +11,10 @@ NS_ASSUME_NONNULL_BEGIN
/// 社区动态列表数据model
@interface ComDynModel : NSObject
/// 动态ID
@property (nonatomic, assign) int dynamicID;
/// 用户ID
@property (nonatomic, assign) int userID;
/// 用户头像URL
@property (nonatomic, copy) NSString *userIcon;
/// 用户名
......@@ -20,7 +24,9 @@ NS_ASSUME_NONNULL_BEGIN
/// 动态内容
@property (nonatomic, copy) NSString *content;
/// 图片链接
@property (nonatomic, copy) NSArray *imgUrls;
@property (nonatomic, copy) NSString *imgUrls;
/// 自己是否点赞
@property (nonatomic, assign) BOOL isLike;
/// 点赞数
@property (nonatomic, assign) int likeCount;
/// 评论数
......@@ -32,6 +38,8 @@ NS_ASSUME_NONNULL_BEGIN
- (CGFloat)imgHeight;
/// 动态cell高度
- (CGFloat)cellHeight;
- (NSArray *)getImgUrlsArr:(NSString *)imgUrls;
@end
NS_ASSUME_NONNULL_END
......@@ -9,6 +9,27 @@
@implementation ComDynModel
+ (nullable NSDictionary<NSString *, id> *)modelCustomPropertyMapper {
return @{@"dynamicID" : @"id",
@"userID" : @"user_id",
@"userIcon" : @"user_profile",
@"userName" : @"nick_name",
@"time" : @"publish_time",
@"imgUrls" : @"imgs_url",
@"likeCount" : @"total_praise",
@"remarkCount" : @"total_comments",
@"isLike" : @"is_praise",
};
}
//- (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic {
// NSNumber *timestamp = dic[@"is_praise"];
// if (![timestamp isKindOfClass:[NSNumber class]]) return NO;
// _createdAt = [NSDate dateWithTimeIntervalSince1970:timestamp.floatValue];
// return YES;
//}
#pragma mark - public
- (CGFloat)contentHeight {
CGFloat contentW = kScreenWidth - 60;
NSString *content = self.content;
......@@ -21,29 +42,33 @@
if (iPhone5) {
return 100;
} else {
switch (self.imgUrls.count) {
case 1: case 2:
return 150;
break;
case 3:
return 95;
break;
default:
return 0;
break;
NSArray *imgUrls = [self getImgUrlsArr:self.imgUrls];
if (imgUrls.count == 1 || imgUrls.count == 2) {
return 150;
} else if (imgUrls.count == 3) {
return 95;
}
return 0;
}
}
return 0;
}
- (CGFloat)cellHeight {
NSArray *imgUrls = [self getImgUrlsArr:self.imgUrls];
CGFloat topH = 67;
CGFloat bottomH = (self.imgUrls && self.imgUrls.count) ? 52 : 56;
CGFloat bottomH = imgUrls.count ? 52 : 56;
CGFloat contentH = [self contentHeight];
CGFloat imgTextSpace = (self.imgUrls && self.imgUrls.count) ? 12 : 0;
CGFloat imgTextSpace = imgUrls.count ? 12 : 0;
CGFloat imgH = [self imgHeight];
return topH + bottomH + contentH + imgTextSpace + imgH;
}
- (NSArray *)getImgUrlsArr:(NSString *)imgUrls {
if (imgUrls && imgUrls.length) {
return [imgUrls componentsSeparatedByString:@"|"];
}
return @[];
}
@end
......@@ -8,11 +8,22 @@
#import <UIKit/UIKit.h>
#import "ComDynModel.h"
typedef void(^TapLikeBlock)(void);
typedef void(^TapRemarkBlock)(void);
NS_ASSUME_NONNULL_BEGIN
/// 社区动态Cell
@interface ComDynamicCell : UITableViewCell
@property (nonatomic, strong) ComDynModel *model;
@property (nonatomic, copy) TapLikeBlock tapLikeBlock;
@property (nonatomic, copy) TapRemarkBlock tapRemarkBlock;
/// 启动点赞动画
- (void)fireLikeAnimate;
/// 更新点赞UI
- (void)updateLikeUI;
@end
NS_ASSUME_NONNULL_END
......@@ -7,6 +7,7 @@
#import "ComDynamicCell.h"
#import "GKPhotoBrowser.h"
#import "LikeButton.h"
@interface ComDynamicCell ()
@property (nonatomic, strong) UIImageView *userIcon;
......@@ -15,7 +16,7 @@
@property (nonatomic, strong) UIButton *informBtn;
@property (nonatomic, strong) UIView *imgContainerView;
@property (nonatomic, strong) UILabel *contentLab;
@property (nonatomic, strong) UIButton *likeBtn;
@property (nonatomic, strong) LikeButton *likeBtn;
@property (nonatomic, strong) UIButton *remarkCountBtn;
@property (nonatomic, strong) NSMutableArray *photos;
@end
......@@ -71,6 +72,15 @@
return self;
}
#pragma mark - public
- (void)fireLikeAnimate {
[self.likeBtn executeAnimation];
}
- (void)updateLikeUI {
[self.likeBtn updateLikeBtnState:self.model.isLike likeCount:self.model.likeCount];
}
#pragma mark - setter
- (void)setModel:(ComDynModel *)model {
_model = model;
......@@ -79,7 +89,7 @@
self.userNameLab.text = model.userName;
self.timeLab.text = model.time;
self.contentLab.text = model.content;
[self.likeBtn setTitle:[NSString stringWithFormat:@"%d", model.likeCount] forState:UIControlStateNormal];
[self.likeBtn updateLikeBtnState:model.isLike likeCount:model.likeCount];
[self.remarkCountBtn setTitle:[NSString stringWithFormat:@"%d", model.remarkCount] forState:UIControlStateNormal];
[self.contentLab mas_remakeConstraints:^(MASConstraintMaker *make) {
......@@ -96,21 +106,22 @@
[self.photos removeAllObjects];
// 再添加
CGFloat imgContainerViewH = [model imgHeight];
[self.imgContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
[self.imgContainerView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.contentLab.mas_bottom).offset(12);
make.left.equalTo(self.contentView).offset(15);
make.right.equalTo(self.contentView).offset(-15);
make.height.equalTo(@(imgContainerViewH));
}];
if (imgContainerViewH > 0) {
CGFloat itemCount = model.imgUrls.count;
NSArray *imgUrlArr = [model getImgUrlsArr:model.imgUrls];
CGFloat itemCount = imgUrlArr.count;
CGFloat itemSpace = itemCount > 1 ? (kScreenWidth - 60 - imgContainerViewH * itemCount) / (itemCount - 1) : 0;
for (int index = 0; index < itemCount; index++) {
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(index*(imgContainerViewH + itemSpace), 0, imgContainerViewH, imgContainerViewH)];
[imgView cornerRadius:12];
imgView.tag = index;
imgView.userInteractionEnabled = YES;
[imgView yy_setImageWithURL:[NSURL URLWithString:model.imgUrls[index]] placeholder:[UIImage defaultPlaceholderWithSize:imgView.size]];
[imgView yy_setImageWithURL:[NSURL URLWithString:imgUrlArr[index]] placeholder:[UIImage defaultPlaceholderWithSize:imgView.size]];
imgView.dk_alphaPicker = DKAlphaPickerWithAlphas(1.f, 0.5f, 0.1f);
[self.imgContainerView addSubview:imgView];
......@@ -118,7 +129,7 @@
[imgView addGestureRecognizer:tapGR];
GKPhoto *photo = [GKPhoto new];
photo.url = [NSURL URLWithString:model.imgUrls[index]];
photo.url = [NSURL URLWithString:imgUrlArr[index]];
photo.sourceImageView = imgView;
[self.photos addObject:photo];
}
......@@ -187,12 +198,19 @@
return _contentLab;
}
- (UIButton *)likeBtn {
- (void)likeOrRemarkAction:(UIButton *)sender {
if (sender.tag == 1) {
if (self.tapLikeBlock) { self.tapLikeBlock(); }
} else {
if (self.tapRemarkBlock) { self.tapRemarkBlock(); }
}
}
- (LikeButton *)likeBtn {
if (!_likeBtn) {
_likeBtn = [UIButton btnWithTitle:@"0" font:SysFont(12)];
[_likeBtn dk_setTitleColorPicker:DKColorPickerWithColors(SmallTextColor, DSWhite, DSWhite) forState:UIControlStateNormal];
[_likeBtn setImage:[UIImage imageNamed:@"ic_shequ_dianza"] forState:UIControlStateNormal];
_likeBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 2, 0, 0);
_likeBtn = [[LikeButton alloc] initWithNormalImg:nil selectedImg:nil];
_likeBtn.tag = 1;
[_likeBtn addTarget:self action:@selector(likeOrRemarkAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _likeBtn;
}
......@@ -200,9 +218,11 @@
- (UIButton *)remarkCountBtn {
if (!_remarkCountBtn) {
_remarkCountBtn = [UIButton btnWithTitle:@"0" font:SysFont(12)];
_remarkCountBtn.tag = 2;
[_remarkCountBtn dk_setTitleColorPicker:DKColorPickerWithColors(SmallTextColor, DSWhite, DSWhite) forState:UIControlStateNormal];
[_remarkCountBtn setImage:[UIImage imageNamed:@"ic_shequ_pinlun"] forState:UIControlStateNormal];
_remarkCountBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 2, 0, 0);
[_remarkCountBtn addTarget:self action:@selector(likeOrRemarkAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _remarkCountBtn;
}
......
......@@ -6,6 +6,7 @@
//
#import <UIKit/UIKit.h>
#import "ComDynamicCell.h"
NS_ASSUME_NONNULL_BEGIN
......@@ -23,6 +24,11 @@ NS_ASSUME_NONNULL_BEGIN
/// 点击动态进入动态详情
/// @param indexPath indexPath
- (void)didSelectItem:(NSIndexPath *)indexPath;
/// 点赞按钮
/// @param comDynModel 动态数据model
/// @param cell cell
- (void)didTapLikeItem:(ComDynModel *)comDynModel cell:(ComDynamicCell *)cell;
@end
/// 社区主视图
......@@ -35,6 +41,9 @@ NS_ASSUME_NONNULL_BEGIN
/// 更新社区动态
- (void)updateCommunityMoments:(BOOL)loadMore listArr:(NSArray *)listArr;
/// 结束加载动画
- (void)endRefreshing:(BOOL)loadMore;
@end
NS_ASSUME_NONNULL_END
......@@ -6,7 +6,6 @@
//
#import "CommunityView.h"
#import "ComDynamicCell.h"
@interface CommunityView () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UIView *headView;
......@@ -42,16 +41,20 @@
#pragma mark - public
- (void)updateCommunityMoments:(BOOL)loadMore listArr:(NSArray *)listArr {
self.listArr = [NSMutableArray arrayWithArray:listArr];
if (loadMore) {
[self.listArr addObjectsFromArray:listArr];
[self.listView.mj_footer endRefreshing];
} else {
self.listArr = [NSMutableArray arrayWithArray:listArr];
[self.listView.mj_header endRefreshing];
}
[self.listView reloadData];
}
- (void)endRefreshing:(BOOL)loadMore {
loadMore ? [self.listView.mj_footer endRefreshing] : [self.listView.mj_header endRefreshing];
}
#pragma mark - Action
- (void)tapAction:(UITapGestureRecognizer *)tapGR {
if (self.delegate && [self.delegate respondsToSelector:@selector(tapCommunityHeaderModule:)]) {
......@@ -97,7 +100,18 @@
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ComDynamicCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ComDynamicCell class])];
cell.model = self.listArr[indexPath.section];
ComDynModel *model = self.listArr[indexPath.section];
cell.model = model;
WS(weakSelf);
__weak ComDynamicCell * weakCell = (ComDynamicCell *)cell;
cell.tapLikeBlock = ^{
if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(didTapLikeItem:cell:)]) {
[weakSelf.delegate didTapLikeItem:model cell:weakCell];
}
};
cell.tapRemarkBlock = ^{
[weakSelf tableView:tableView didSelectRowAtIndexPath:indexPath];
};
return cell;
}
......
//
// LikeButton.h
// DreamSleep
//
// Created by peter on 2022/9/28.
//
#import <UIKit/UIKit.h>
typedef void (^LikeBlock)(void);
/// 封装点赞按钮
@interface LikeButton : UIButton
@property (nonatomic, copy) LikeBlock likeBlock;
- (instancetype)initWithNormalImg:(UIImage *)normalImg selectedImg:(UIImage *)selectedImg;
- (void)updateLikeBtnState:(BOOL)isLike likeCount:(int)likeCount;
- (void)executeAnimation;
@end
//
// LikeButton.m
// DreamSleep
//
// Created by peter on 2022/9/28.
//
#import "LikeButton.h"
@interface LikeButton ()
@property (nonatomic, strong) UIButton *aniIcon;
@property (nonatomic, strong) UILabel *countLab;
@property (nonatomic, strong) CAEmitterLayer *emitterLayer;
@property (nonatomic, strong) UIView *explodeAnimationView;
@property (nonatomic, strong) UIImage *normalIcon;
@property (nonatomic, strong) UIImage *selectedIcon;
@end
@implementation LikeButton
- (instancetype)initWithNormalImg:(UIImage *)normalImg selectedImg:(UIImage *)selectedImg {
if (self = [super init]) {
self.normalIcon = normalImg ?: [UIImage imageNamed:@"ic_shequ_dianza"];
self.selectedIcon = selectedImg ?: [UIImage imageNamed:@"ic_shequ_yidianza"];
[self addSubview:self.aniIcon];
[self addSubview:self.countLab];
[self.aniIcon insertSubview:self.explodeAnimationView atIndex:0];
[self.aniIcon mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self).offset(26);
make.centerY.equalTo(self);
make.size.mas_equalTo(CGSizeMake(30, 30));
}];
[self.countLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.aniIcon.mas_right).offset(2);
make.centerY.equalTo(self.aniIcon);
}];
[self.explodeAnimationView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.bottom.right.equalTo(self.aniIcon);
}];
[self layoutIfNeeded];
self.emitterLayer.emitterPosition = self.explodeAnimationView.center;
}
return self;
}
#pragma mark - public
- (void)updateLikeBtnState:(BOOL)isLike likeCount:(int)likeCount {
self.aniIcon.selected = isLike;
UIColor *dayColor = self.aniIcon.selected ? ColorFromHex(0xF04B77) : SmallTextColor;
self.countLab.dk_textColorPicker = DKColorPickerWithColors(dayColor, DSWhite, DSWhite);
self.countLab.text = [NSString stringWithFormat:@"%d", likeCount];
}
- (void)executeAnimation {
if (self.aniIcon.selected) {
[self popInsideWithDuration:0.5];
} else {
[self popOutsideWithDuration:0.5];
[self emitAnimate];
};
}
#pragma mark - 点赞、取消点赞
- (void)popOutsideWithDuration:(NSTimeInterval)duringTime {
__weak typeof(self) weakSelf = self;
self.transform = CGAffineTransformIdentity;
[UIView animateKeyframesWithDuration: duringTime delay: 0 options: 0 animations: ^{
[UIView addKeyframeWithRelativeStartTime: 0
relativeDuration: 1 / 3.0
animations: ^{
typeof(self) strongSelf = weakSelf;
strongSelf.aniIcon.transform = CGAffineTransformMakeScale(1.5, 1.5);
}];
[UIView addKeyframeWithRelativeStartTime: 1 / 3.0
relativeDuration: 1 / 3.0
animations: ^{
typeof(self) strongSelf = weakSelf;
strongSelf.aniIcon.transform = CGAffineTransformMakeScale(0.8, 0.8);
}];
[UIView addKeyframeWithRelativeStartTime: 2 / 3.0
relativeDuration: 1 / 3.0
animations: ^{
typeof(self) strongSelf = weakSelf;
strongSelf.aniIcon.transform = CGAffineTransformMakeScale(1.0, 1.0);
}];
} completion:^(BOOL finished) {}];
}
- (void)popInsideWithDuration:(NSTimeInterval)duringTime {
__weak typeof(self) weakSelf = self;
self.aniIcon.transform = CGAffineTransformIdentity;
[UIView animateKeyframesWithDuration: duringTime delay: 0 options: 0 animations: ^{
[UIView addKeyframeWithRelativeStartTime: 0
relativeDuration: 1 / 2.0
animations: ^{
typeof(self) strongSelf = weakSelf;
strongSelf.aniIcon.transform = CGAffineTransformMakeScale(0.8, 0.8);
}];
[UIView addKeyframeWithRelativeStartTime: 1 / 2.0
relativeDuration: 1 / 2.0
animations: ^{
typeof(self) strongSelf = weakSelf;
strongSelf.aniIcon.transform = CGAffineTransformMakeScale(1.0, 1.0);
}];
} completion:^(BOOL finished) {}];
}
#pragma mark - 粒子动画
- (void)emitAnimate {
dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, 0.2 * NSEC_PER_SEC);
dispatch_after(delay, dispatch_get_main_queue(), ^{
self.emitterLayer.beginTime = CACurrentMediaTime();
CABasicAnimation *ani = [CABasicAnimation animationWithKeyPath: @"emitterCells.explosion.birthRate"];
ani.fromValue = @0;
ani.toValue = @500;
[self->_emitterLayer addAnimation: ani forKey: nil];
});
}
#pragma mark - lazy
- (UIButton *)aniIcon {
if (!_aniIcon) {
_aniIcon = [UIButton new];
_aniIcon.userInteractionEnabled = NO;
[_aniIcon setImage:self.normalIcon forState:UIControlStateNormal];
[_aniIcon setImage:self.selectedIcon forState: UIControlStateSelected];
}
return _aniIcon;
}
- (UILabel *)countLab {
if (!_countLab) {
_countLab = [UILabel labWithFont:SysFont(12)];
}
return _countLab;
}
- (UIView *)explodeAnimationView {
if (!_explodeAnimationView) {
_explodeAnimationView = [UIView new];
_explodeAnimationView.clipsToBounds = NO;
_explodeAnimationView.userInteractionEnabled = NO;
CAEmitterCell *emitter = [CAEmitterCell emitterCell];
emitter.contents = (id)self.selectedIcon.CGImage;
emitter.name = @"explosion";
emitter.alphaRange = 0.2f;
emitter.alphaSpeed = -1.f;
emitter.lifetime = 0.7f;
emitter.lifetimeRange = 0.3f;
emitter.birthRate = 0;
emitter.velocity = 40.0f;
emitter.velocityRange = 10.0f;
emitter.emissionRange = M_PI_4;
emitter.scale = 0.05f;
emitter.scaleRange = 0.02;
_emitterLayer = [CAEmitterLayer layer];
_emitterLayer.name = @"emitterLayer";
_emitterLayer.emitterShape = kCAEmitterLayerCircle;
_emitterLayer.emitterMode = kCAEmitterLayerOutline;
_emitterLayer.emitterPosition = self.center;
_emitterLayer.emitterSize = CGSizeMake(25, 0);
_emitterLayer.renderMode = kCAEmitterLayerOldestFirst;
_emitterLayer.masksToBounds = NO;
_emitterLayer.emitterCells = @[emitter];
_emitterLayer.frame = [UIScreen mainScreen].bounds;
[_explodeAnimationView.layer addSublayer: _emitterLayer];
}
return _explodeAnimationView;
}
@end
......@@ -11,10 +11,22 @@ NS_ASSUME_NONNULL_BEGIN
@interface ComListViewModel : DSNetworkTool
@property (nonatomic, assign) int offset;
@property (nonatomic, assign) int limit;
@property (nonatomic, assign) int totalCount;
@property (nonatomic, strong) NSArray *listArr;
- (void)getComListWithCompletion:(void (^)(ComListViewModel *requestModel))completion;
/// 社区动态查询列表接口
/// @param offset offset
/// @param completion completion
+ (NSURLSessionDataTask *)querySleepDynamicListWithOffset:(int)offset completion:(void (^)(ComListViewModel *requestModel))completion;
/// 用户点赞、取消点赞接口
/// @param talkID talkID
/// @param completion completion
+ (NSURLSessionDataTask *)userDynamicPraiseWithTalkID:(int)talkID completion:(void (^)(ComListViewModel *requestModel))completion;
@end
NS_ASSUME_NONNULL_END
......@@ -24,27 +24,47 @@
requestModel.listArr = [tmpArr copy];
DSLog(@"apiDic:%@", apiDic);
completion(requestModel);
// NSString *api = @"query_ssmian_knowledge_list";
// NSString *argStr = [NSString stringWithFormat:@"query{%@}", api];
// return [self httpPostBodyRequestWithAPI:api params:@{@"query" : argStr} view:nil hasNetActivity:YES loadingInfo:nil hasFailInfo:NO success:^(NSDictionary * _Nonnull apiDic) {
// DSLog(@"文章咨询数据接口apiDic:%@", apiDic);
// requestModel.resCode = DSResCodeSuccess;
// NSDictionary *resultDic = apiDic[@"result"];
//
// completion(requestModel);
// } failure:^(id _Nonnull failureInfo) {
// requestModel.resCode = [failureInfo[@"errorCode"] integerValue];
// requestModel.errMessage = failureInfo[@"errMessage"];
// completion(requestModel);
// }];
}
- (NSArray *)listArr {
if (!_listArr) {
_listArr = [NSArray array];
}
return _listArr;
+ (NSURLSessionDataTask *)querySleepDynamicListWithOffset:(int)offset completion:(void (^)(ComListViewModel *requestModel))completion {
ComListViewModel * requestModel = [[ComListViewModel alloc] init];
NSString *api = @"query_sleep_dynamic_list";
NSString *argStr = [NSString stringWithFormat:@"query{%@(offset:%d)}", api, offset];
return [self httpPostBodyRequestWithAPI:api params:@{@"query" : argStr} view:nil hasNetActivity:YES loadingInfo:nil hasFailInfo:NO success:^(NSDictionary * _Nonnull apiDic) {
DSLog(@"社区动态查询列表接口apiDic:%@", apiDic);
requestModel.resCode = DSResCodeSuccess;
NSDictionary *resultDic = apiDic[@"result"];
NSArray *data_list = resultDic[@"data_list"];
NSMutableArray *tmpArr = [NSMutableArray array];
for (int i = 0; i < data_list.count; i++) {
ComDynModel *model = [ComDynModel yy_modelWithDictionary:data_list[i]];
[tmpArr addObject:model];
}
requestModel.listArr = [tmpArr copy];
requestModel.offset = [resultDic[@"offset"] intValue];
requestModel.limit = [resultDic[@"limit"] intValue];
requestModel.totalCount = [resultDic[@"count"] intValue];
completion(requestModel);
} failure:^(id _Nonnull failureInfo) {
requestModel.resCode = [failureInfo[@"errorCode"] integerValue];
requestModel.errMessage = failureInfo[@"errMessage"];
completion(requestModel);
}];
}
+ (NSURLSessionDataTask *)userDynamicPraiseWithTalkID:(int)talkID completion:(void (^)(ComListViewModel *requestModel))completion {
ComListViewModel * requestModel = [[ComListViewModel alloc] init];
NSString *api = @"user_dynamic_praise";
NSString *argStr = [NSString stringWithFormat:@"mutation{%@(talk_id:%d)}", api, talkID];
return [self httpPostBodyRequestWithAPI:api params:@{@"query" : argStr} view:nil hasNetActivity:YES loadingInfo:nil hasFailInfo:NO success:^(NSDictionary * _Nonnull apiDic) {
DSLog(@"用户动态点赞、取消点赞接口apiDic:%@", apiDic);
requestModel.resCode = DSResCodeSuccess;
completion(requestModel);
} failure:^(id _Nonnull failureInfo) {
requestModel.resCode = [failureInfo[@"errorCode"] integerValue];
requestModel.errMessage = failureInfo[@"errMessage"];
completion(requestModel);
}];
}
@end
{
"images" : [
{
"filename" : "ic_shequ_yidianza.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "ic_shequ_yidianza@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "ic_shequ_yidianza@3x.png",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!