Files
peko-ios/YuMi/Modules/YMRoom/View/AnimationView/LuckyGiftWinningBannerView.m
2024-09-12 18:49:42 +08:00

267 lines
9.5 KiB
Objective-C

//
// LuckyGiftWinningBannerView.m
// YuMi
//
// Created by P on 2024/9/10.
//
#import "LuckyGiftWinningBannerView.h"
#import "AttachmentModel.h"
#import "i18nGiftNameMap.h"
#import "XPRoomViewController.h"
#import "XCCurrentVCStackManager.h"
@interface LuckyGiftWinningBannerViewModel : PIBaseModel
@property (nonatomic, copy) NSString *times;
@property (nonatomic, copy) NSString *avatar;
@property (nonatomic, copy) NSString *coins;
@property (nonatomic, copy) NSDictionary *giftNameMap;
@property (nonatomic, copy) NSString *roomUid;
- (NSString *)giftName;
@end
@implementation LuckyGiftWinningBannerViewModel
+ (NSDictionary *)replacedKeyFromPropertyName {
return @{
@"avatar":@"sender.avatar"
};
}
- (NSString *)giftName {
if ([self.giftNameMap.allKeys containsObject:@"ar"] && isMSRTL()) {
return self.giftNameMap[@"ar"];
} else if ([self.giftNameMap.allKeys containsObject:@"zh"] && isMSZH()) {
return self.giftNameMap[@"zh"];
} else {
return self.giftNameMap[@"en"];
}
}
@end
@interface LuckyGiftWinningBannerView ()
@property (nonatomic, strong) LuckyGiftWinningBannerViewModel *model;
@property (nonatomic, strong) UIImageView *backgroundImageView;
@property (nonatomic, strong) UIImageView *ballImageView;
@property (nonatomic, strong) NetImageView *avatarImage;
@property (nonatomic, strong) UILabel *giftNameLabel;
@property (nonatomic, strong) UILabel *timesLabel;
@property (nonatomic, strong) UILabel *coinsLabel;
@property (nonatomic, assign) NSInteger currentRoomUid;
@property (nonatomic, copy) void(^completeDisplay)(void);
@property (nonatomic, copy) void(^exitCurrentRoom)(void);
@end
@implementation LuckyGiftWinningBannerView
+ (void)display:(UIView *)superView
inRoomUid:(NSInteger)roomUid
with:(AttachmentModel *)attachment
complete:(void(^)(void))complete
exitCurrentRoom:(void(^)(void))exit {
LuckyGiftWinningBannerViewModel *model = [LuckyGiftWinningBannerViewModel modelWithDictionary:attachment.data];
CGFloat width = kGetScaleWidth(346.5);
CGFloat height = kGetScaleWidth(82.5);
LuckyGiftWinningBannerView *bannerView = [[LuckyGiftWinningBannerView alloc] initWithFrame:CGRectMake(KScreenWidth, 80, width, height)];
bannerView.model = model;
bannerView.completeDisplay = complete;
bannerView.exitCurrentRoom = exit;
bannerView.currentRoomUid = roomUid;
[superView addSubview:bannerView];
@kWeakify(bannerView);
[UIView animateWithDuration:0.25 animations:^{
bannerView.center = CGPointMake(superView.center.x, height/2 + 80);
} completion:^(BOOL finished) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
bannerView.frame = CGRectMake(-KScreenWidth, 80, width, height);
} completion:^(BOOL finished) {
@kStrongify(bannerView);
[bannerView removeFromSuperview];
if (bannerView.completeDisplay) {
bannerView.completeDisplay();
}
}];
});
}];
}
- (void)setModel:(LuckyGiftWinningBannerViewModel *)model {
_model = model;
self.avatarImage.imageUrl = model.avatar;
self.giftNameLabel.text = [model giftName];
self.timesLabel.text = model.times;
self.coinsLabel.text = [NSString formatNumberToKOrM:model.coins];
}
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self setupUI];
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
[self addSubview:b];
[b mas_remakeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self);
}];
[b addTarget:self action:@selector(handelTap) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
- (void)handelTap {
if (self.model.roomUid.integerValue == self.currentRoomUid) {
return;
}
@kWeakify(self);
[TTPopup alertWithMessage:YMLocalizedString(@"Combo_10") confirmHandler:^{
@kStrongify(self);
if (self.exitCurrentRoom) {
self.exitCurrentRoom();
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[XPRoomViewController openRoom:self.model.roomUid
viewController:[XCCurrentVCStackManager shareManager].getCurrentVC];
});
} cancelHandler:^{}];
}
- (void)setupUI {
[self addSubview:self.backgroundImageView];
[self.backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self);
}];
[self addSubview:self.ballImageView];
[self.ballImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.trailing.bottom.top.mas_equalTo(self);
make.width.mas_equalTo(self.ballImageView.mas_height);
}];
[self addSubview:self.avatarImage];
[self.avatarImage mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(kGetScaleWidth(8));
make.top.mas_equalTo(kGetScaleWidth(24));
make.width.height.mas_equalTo(kGetScaleWidth(43));
}];
UILabel *titleLabel_1 = [UILabel labelInitWithText:YMLocalizedString(@"Combo_0") font:kFontSemibold(14) textColor:[UIColor whiteColor]];
[self addSubview:titleLabel_1];
[titleLabel_1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.avatarImage).offset(2);
make.leading.mas_equalTo(self.avatarImage.mas_trailing).offset(10);
}];
[self addSubview:self.giftNameLabel];
[self.giftNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(titleLabel_1);
make.leading.mas_equalTo(titleLabel_1.mas_trailing).offset(3);
}];
UILabel *titleLabel_2 = [UILabel labelInitWithText:YMLocalizedString(@"Combo_4") font:kFontSemibold(14) textColor:[UIColor whiteColor]];
[self addSubview:titleLabel_2];
[titleLabel_2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(self.avatarImage.mas_bottom).offset(-2);
make.leading.mas_equalTo(self.avatarImage.mas_trailing).offset(10);
}];
[self addSubview:self.timesLabel];
[self.timesLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(titleLabel_2);
make.leading.mas_equalTo(titleLabel_2.mas_trailing).offset(3);
}];
UILabel *titleLabel_3 = [UILabel labelInitWithText:YMLocalizedString(@"Combo_9") font:kFontSemibold(14) textColor:[UIColor whiteColor]];
[self addSubview:titleLabel_3];
[titleLabel_3 mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self.timesLabel);
make.leading.mas_equalTo(self.timesLabel.mas_trailing).offset(3);
}];
[self addSubview:self.coinsLabel];
[self.coinsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.ballImageView);
make.top.mas_equalTo(self).offset(kGetScaleWidth(18));
make.height.mas_equalTo(kGetScaleWidth(28));
make.width.mas_equalTo(self.ballImageView);
}];
UILabel *titleLabel_4 = [UILabel labelInitWithText:YMLocalizedString(@"Combo_5") font:kFontSemibold(14) textColor:[UIColor whiteColor]];
[self addSubview:titleLabel_4];
[titleLabel_4 mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.coinsLabel);
make.top.mas_equalTo(self.coinsLabel.mas_bottom);
}];
}
#pragma mark -
- (UIImageView *)backgroundImageView {
if (!_backgroundImageView) {
_backgroundImageView = [[UIImageView alloc] initWithImage:[kImage(@"luck_gift_flag") ms_SetImageForRTL]];
_backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
}
return _backgroundImageView;
}
- (UIImageView *)ballImageView {
if (!_ballImageView) {
_ballImageView = [[UIImageView alloc] initWithImage:kImage(@"luck_gift_flag_ball")];
_ballImageView.contentMode = UIViewContentModeScaleAspectFill;
}
return _ballImageView;
}
- (NetImageView *)avatarImage {
if (!_avatarImage) {
NetImageConfig * config = [[NetImageConfig alloc]init];
config.placeHolder = [UIImageConstant defaultAvatarPlaceholder];
config.imageType = ImageTypeCornerAvatar;
_avatarImage = [[NetImageView alloc] initWithConfig:config];
_avatarImage.backgroundColor = [UIColor clearColor];
_avatarImage.contentMode = UIViewContentModeScaleAspectFill;
_avatarImage.layer.cornerRadius = kGetScaleWidth(43/2);
_avatarImage.layer.masksToBounds = YES;
_avatarImage.clipsToBounds = YES;
}
return _avatarImage;
}
- (UILabel *)giftNameLabel {
if (!_giftNameLabel) {
_giftNameLabel = [UILabel labelInitWithText:@"Gift" font:kFontSemibold(14) textColor:UIColorFromRGB(0xffe375)];
}
return _giftNameLabel;
}
- (UILabel *)coinsLabel {
if (!_coinsLabel) {
_coinsLabel = [UILabel labelInitWithText:@"14K" font:kFontSemibold(20) textColor:UIColorFromRGB(0xffe375)];
_coinsLabel.adjustsFontSizeToFitWidth = YES;
_coinsLabel.minimumScaleFactor = 0.5;
_coinsLabel.textAlignment = NSTextAlignmentCenter;
}
return _coinsLabel;
}
- (UILabel *)timesLabel {
if (!_timesLabel) {
_timesLabel = [UILabel labelInitWithText:@"913" font:kFontSemibold(14) textColor:UIColorFromRGB(0xffe375)];
}
return _timesLabel;
}
@end