348 lines
13 KiB
Objective-C
348 lines
13 KiB
Objective-C
//
|
|
// CPCard.m
|
|
// YuMi
|
|
//
|
|
// Created by P on 2024/9/18.
|
|
//
|
|
|
|
#import "CPCard.h"
|
|
#import <SVGA.h>
|
|
#import "RelationUserVO.h"
|
|
|
|
@interface CPCard ()
|
|
|
|
@property (nonatomic, strong) RelationUserVO *relationUser;
|
|
|
|
@property (nonatomic, strong) UIImageView *backgroundImage;
|
|
@property (nonatomic, strong) UIButton *breakYourHeartButton;
|
|
@property (nonatomic, strong) NetImageView *avatarView;
|
|
@property (nonatomic, strong) SVGAImageView *headWearSvgaImageView;
|
|
@property (nonatomic, strong) NetImageView *avatarView_cp;
|
|
@property (nonatomic, strong) SVGAImageView *headWearSvgaImageView_cp;
|
|
@property (nonatomic, strong) UILabel *bottomLabel;
|
|
@property (nonatomic, strong) UILabel *topLabel;
|
|
@property (nonatomic, strong) NetImageView *bottomImageView;
|
|
@property (nonatomic, strong) UILabel *progressUpgradeLabel;
|
|
@property (nonatomic, strong) UILabel *progressNumLabel;
|
|
@property (nonatomic, strong) UIProgressView *progressView;
|
|
|
|
@end
|
|
|
|
@implementation CPCard
|
|
|
|
- (instancetype)init {
|
|
if (self = [super init]) {
|
|
[self setupUI];
|
|
|
|
#if DEBUG
|
|
self.topLabel.text = @"365 天";
|
|
self.bottomLabel.text = @"Waiting for your love";
|
|
self.bottomImageView.image = kImage(@"cp_relationship_lv_1");
|
|
|
|
[self.progressView setProgress:0.5 animated:NO];
|
|
#endif
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (void)setupUI {
|
|
UIImageView *backgroundImage = [[UIImageView alloc] initWithImage:kImage(@"cp_bg")];
|
|
backgroundImage.contentMode = UIViewContentModeScaleAspectFit;
|
|
[self addSubview:backgroundImage];
|
|
[backgroundImage mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.leading.mas_equalTo(kGetScaleWidth(15));
|
|
make.trailing.mas_equalTo(kGetScaleWidth(-15));
|
|
make.bottom.mas_equalTo(self);
|
|
make.top.mas_equalTo(self);
|
|
}];
|
|
_backgroundImage = backgroundImage;
|
|
|
|
UIImageView *heart = [[UIImageView alloc] initWithImage:kImage(@"cp_heart")];
|
|
heart.contentMode = UIViewContentModeScaleAspectFit;
|
|
[self addSubview:heart];
|
|
[heart mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.top.mas_equalTo(kGetScaleWidth(34));
|
|
make.centerX.mas_equalTo(backgroundImage);
|
|
make.size.mas_equalTo(CGSizeMake(kGetScaleWidth(125), kGetScaleWidth(60)));
|
|
}];
|
|
|
|
UIImageView *avatar_placeholder = [[UIImageView alloc] initWithImage:kImage(@"cp_empty_avatar")];
|
|
avatar_placeholder.contentMode = UIViewContentModeScaleAspectFit;
|
|
[self addSubview:avatar_placeholder];
|
|
[avatar_placeholder mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.top.mas_equalTo(kGetScaleWidth(43));
|
|
make.trailing.mas_equalTo(-56);
|
|
make.size.mas_equalTo(CGSizeMake(kGetScaleWidth(56), kGetScaleWidth(56)));
|
|
}];
|
|
|
|
[self addSubview:self.avatarView];
|
|
[self.avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.top.mas_equalTo(kGetScaleWidth(43));
|
|
make.leading.mas_equalTo(56);
|
|
make.size.mas_equalTo(CGSizeMake(kGetScaleWidth(56), kGetScaleWidth(56)));
|
|
}];
|
|
[self addSubview:self.headWearSvgaImageView];
|
|
[self.headWearSvgaImageView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.center.mas_equalTo(self.avatarView);
|
|
make.size.mas_equalTo(CGSizeMake(kGetScaleWidth(85), kGetScaleWidth(85)));
|
|
}];
|
|
|
|
[self addSubview:self.avatarView_cp];
|
|
[self.avatarView_cp mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.center.mas_equalTo(avatar_placeholder);
|
|
make.size.mas_equalTo(CGSizeMake(kGetScaleWidth(56), kGetScaleWidth(56)));
|
|
}];
|
|
[self addSubview:self.headWearSvgaImageView_cp];
|
|
[self.headWearSvgaImageView_cp mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.center.mas_equalTo(self.avatarView_cp);
|
|
make.size.mas_equalTo(CGSizeMake(kGetScaleWidth(85), kGetScaleWidth(85)));
|
|
}];
|
|
|
|
[self addSubview:self.topLabel];
|
|
[self.topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.centerX.mas_equalTo(self);
|
|
make.height.mas_equalTo(18);
|
|
make.top.mas_equalTo(29);
|
|
}];
|
|
|
|
[self addSubview:self.bottomLabel];
|
|
[self.bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.centerX.mas_equalTo(self);
|
|
make.height.mas_equalTo(18);
|
|
make.top.mas_equalTo(heart.mas_bottom);
|
|
}];
|
|
|
|
[self addSubview:self.bottomImageView];
|
|
[self.bottomImageView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.centerX.mas_equalTo(self);
|
|
make.size.mas_equalTo(CGSizeMake(100, 19));
|
|
make.top.mas_equalTo(90);
|
|
}];
|
|
|
|
[self addSubview:self.progressView];
|
|
[self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.top.mas_equalTo(self.headWearSvgaImageView.mas_bottom).offset(4);
|
|
make.leading.mas_equalTo(self.backgroundImage).offset(22);
|
|
make.trailing.mas_equalTo(self.backgroundImage).offset(-22);
|
|
make.height.mas_equalTo(4);
|
|
}];
|
|
|
|
[self addSubview:self.breakYourHeartButton];
|
|
[self.breakYourHeartButton mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.top.mas_equalTo(27);
|
|
make.trailing.mas_equalTo(-28);
|
|
make.size.mas_equalTo(22);
|
|
}];
|
|
|
|
[self addSubview:self.progressNumLabel];
|
|
[self.progressNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.top.mas_equalTo(self.progressView.mas_bottom).offset(4);
|
|
make.trailing.mas_equalTo(self.progressView);
|
|
}];
|
|
|
|
[self addSubview:self.progressUpgradeLabel];
|
|
[self.progressUpgradeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.top.mas_equalTo(self.progressView.mas_bottom).offset(4);
|
|
make.leading.mas_equalTo(self.progressView);
|
|
}];
|
|
}
|
|
|
|
- (void)setIsListItem:(BOOL)isListItem {
|
|
_isListItem = isListItem;
|
|
self.progressView.hidden = !isListItem;
|
|
self.progressNumLabel.hidden = !isListItem;
|
|
self.progressUpgradeLabel.hidden = !isListItem;
|
|
if (isListItem) {
|
|
self.backgroundImage.image = kImage(@"cp_relationship_bg");
|
|
} else {
|
|
|
|
}
|
|
}
|
|
|
|
- (void)setRelationUser:(RelationUserVO *)relationUser {
|
|
_relationUser = relationUser;
|
|
|
|
self.topLabel.hidden = [relationUser isEmptyRelation];
|
|
self.avatarView_cp.hidden = [relationUser isEmptyRelation];
|
|
self.bottomLabel.hidden = ![relationUser isEmptyRelation];
|
|
self.bottomImageView.hidden = [relationUser isEmptyRelation];
|
|
|
|
self.avatarView.imageUrl = relationUser.avatar;
|
|
self.avatarView_cp.imageUrl = relationUser.cpAvatar;
|
|
self.topLabel.text = [NSString stringWithFormat:@"%ld days", (long)relationUser.cpDay];
|
|
|
|
NSString *imagePath = [NSString stringWithFormat:@"cp_relationship_lv_%ld", self.relationUser.cpLevel];
|
|
self.bottomImageView.image = kImage(imagePath);
|
|
[self playSVGA:self.relationUser.cpLevel];
|
|
}
|
|
|
|
- (void)updateForUserPage:(RelationUserVO *)model {
|
|
self.relationUser = model;
|
|
|
|
if ([model isEmptyRelation]) {
|
|
self.avatarView.imageUrl = self.usersAvatar;
|
|
self.bottomLabel.hidden = NO;
|
|
}
|
|
self.progressView.hidden = YES;
|
|
self.progressNumLabel.hidden = YES;
|
|
self.progressUpgradeLabel.hidden = YES;
|
|
self.breakYourHeartButton.hidden = YES;
|
|
}
|
|
|
|
- (void)updateForCPList:(RelationUserVO *)model showBreakHeart:(BOOL)showBreakHeart {
|
|
self.relationUser = model;
|
|
|
|
self.breakYourHeartButton.hidden = !showBreakHeart;
|
|
|
|
[self.backgroundImage mas_updateConstraints:^(MASConstraintMaker *make) {
|
|
make.bottom.mas_equalTo(self).offset(-12);
|
|
}];
|
|
|
|
self.progressView.progress = self.relationUser.currentExp / self.relationUser.nextLevelExp;
|
|
self.progressUpgradeLabel.text = [NSString stringWithFormat:@"Need %ld to upgrade", (long)(self.relationUser.nextLevelExp - self.relationUser.currentExp)];
|
|
self.progressNumLabel.text = [NSString stringWithFormat:@"(%ld/%ld)", (long)self.relationUser.currentExp, (long)self.relationUser.endExp];
|
|
}
|
|
|
|
- (void)playSVGA:(NSInteger)level {
|
|
if (level == 0) {
|
|
return;
|
|
}
|
|
|
|
@kWeakify(self);
|
|
SVGAParser *parser = [SVGAParser new];
|
|
[parser parseWithNamed:@(level).stringValue
|
|
inBundle:[NSBundle mainBundle]
|
|
completionBlock:^(SVGAVideoEntity * _Nonnull videoItem) {
|
|
@kStrongify(self);
|
|
self.headWearSvgaImageView.videoItem = videoItem;
|
|
[self.headWearSvgaImageView startAnimation];
|
|
self.headWearSvgaImageView_cp.videoItem = videoItem;
|
|
[self.headWearSvgaImageView_cp startAnimation];
|
|
} failureBlock:^(NSError * _Nonnull error) {
|
|
|
|
}];
|
|
}
|
|
|
|
- (void)didTapBreakButton {
|
|
if (self.breakTheHeart) {
|
|
self.breakTheHeart(self.relationUser);
|
|
}
|
|
}
|
|
|
|
#pragma mark -
|
|
- (NetImageView *)avatarView {
|
|
if (!_avatarView) {
|
|
NetImageConfig * config = [[NetImageConfig alloc] init];
|
|
config.placeHolder = [UIImageConstant defaultAvatarPlaceholder];
|
|
config.imageType = ImageTypeUserIcon;
|
|
_avatarView = [[NetImageView alloc]initWithConfig:config];
|
|
_avatarView.layer.cornerRadius = kGetScaleWidth(56/2);
|
|
_avatarView.layer.masksToBounds = YES;
|
|
_avatarView.layer.borderWidth = 1;
|
|
_avatarView.userInteractionEnabled = YES;
|
|
_avatarView.layer.borderColor = [UIColor whiteColor].CGColor;
|
|
}
|
|
return _avatarView;
|
|
}
|
|
|
|
- (NetImageView *)avatarView_cp {
|
|
if (!_avatarView_cp) {
|
|
NetImageConfig * config = [[NetImageConfig alloc] init];
|
|
config.placeHolder = [UIImageConstant defaultAvatarPlaceholder];
|
|
config.imageType = ImageTypeUserIcon;
|
|
_avatarView_cp = [[NetImageView alloc]initWithConfig:config];
|
|
_avatarView_cp.layer.cornerRadius = kGetScaleWidth(56/2);
|
|
_avatarView_cp.layer.masksToBounds = YES;
|
|
_avatarView_cp.layer.borderWidth = 1;
|
|
_avatarView_cp.userInteractionEnabled = YES;
|
|
_avatarView_cp.layer.borderColor = [UIColor whiteColor].CGColor;
|
|
}
|
|
return _avatarView_cp;
|
|
}
|
|
|
|
- (UILabel *)topLabel {
|
|
if (!_topLabel) {
|
|
_topLabel = [UILabel labelInitWithText:@"" font:kFontMedium(12) textColor:[UIColor whiteColor]];
|
|
}
|
|
return _topLabel;
|
|
}
|
|
|
|
- (UILabel *)bottomLabel {
|
|
if (!_bottomLabel) {
|
|
_bottomLabel = [UILabel labelInitWithText:@"" font:kFontRegular(12) textColor:[UIColor colorWithWhite:1 alpha:0.8]];
|
|
}
|
|
return _bottomLabel;
|
|
}
|
|
|
|
- (NetImageView *)bottomImageView {
|
|
if (!_bottomImageView) {
|
|
_bottomImageView = [[NetImageView alloc] init];
|
|
}
|
|
return _bottomImageView;
|
|
}
|
|
|
|
- (UILabel *)progressUpgradeLabel {
|
|
if (!_progressUpgradeLabel) {
|
|
_progressUpgradeLabel = [UILabel labelInitWithText:@"" font:kFontRegular(12) textColor:[UIColor whiteColor]];
|
|
}
|
|
return _progressUpgradeLabel;
|
|
}
|
|
|
|
- (UILabel *)progressNumLabel {
|
|
if (!_progressNumLabel) {
|
|
_progressNumLabel = [UILabel labelInitWithText:@"" font:kFontRegular(12) textColor:[UIColor whiteColor]];
|
|
}
|
|
return _progressNumLabel;
|
|
}
|
|
|
|
- (UIProgressView *)progressView {
|
|
if (!_progressView) {
|
|
_progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
|
|
|
|
// 设置背景色和前景色
|
|
_progressView.progressTintColor = UIColorFromRGB(0xF77300);
|
|
_progressView.trackTintColor = UIColorFromRGB(0x7E1E00);
|
|
|
|
// 设置高度 4 和圆角 1
|
|
_progressView.transform = CGAffineTransformMakeScale(1.0, 4.0 / _progressView.frame.size.height); // 调整高度
|
|
_progressView.layer.cornerRadius = 2.0; // 设置圆角
|
|
_progressView.layer.masksToBounds = YES; // 裁剪超出边界的部分
|
|
}
|
|
return _progressView;
|
|
}
|
|
|
|
- (SVGAImageView *)headWearSvgaImageView {
|
|
if (!_headWearSvgaImageView) {
|
|
_headWearSvgaImageView = [[SVGAImageView alloc]init];
|
|
_headWearSvgaImageView.backgroundColor = [UIColor clearColor];
|
|
_headWearSvgaImageView.frame = self.bounds;
|
|
_headWearSvgaImageView.userInteractionEnabled = YES;
|
|
_headWearSvgaImageView.autoPlay = YES;
|
|
}
|
|
return _headWearSvgaImageView;
|
|
}
|
|
|
|
- (SVGAImageView *)headWearSvgaImageView_cp {
|
|
if (!_headWearSvgaImageView_cp) {
|
|
_headWearSvgaImageView_cp = [[SVGAImageView alloc]init];
|
|
_headWearSvgaImageView_cp.backgroundColor = [UIColor clearColor];
|
|
_headWearSvgaImageView_cp.frame = self.bounds;
|
|
_headWearSvgaImageView_cp.userInteractionEnabled = YES;
|
|
_headWearSvgaImageView_cp.autoPlay = YES;
|
|
}
|
|
return _headWearSvgaImageView_cp;
|
|
}
|
|
|
|
|
|
- (UIButton *)breakYourHeartButton {
|
|
if (!_breakYourHeartButton) {
|
|
_breakYourHeartButton = [UIButton buttonWithType:UIButtonTypeCustom];
|
|
[_breakYourHeartButton setImage:kImage(@"cp_break_heart") forState:UIControlStateNormal];
|
|
[_breakYourHeartButton addTarget:self
|
|
action:@selector(didTapBreakButton)
|
|
forControlEvents:UIControlEventTouchUpInside];
|
|
}
|
|
return _breakYourHeartButton;
|
|
}
|
|
@end
|