Files
yinmeng-ios/xplan-ios/Main/Tabbar/View/VersionUpdate/XPUpgradeView.m

223 lines
7.5 KiB
Mathematica
Raw Normal View History

2022-12-05 18:56:02 +08:00
//
// XPUpgradeView.m
// xplan-ios
//
// Created by GreenLand on 2022/12/5.
//
#import "XPUpgradeView.h"
#import <Masonry/Masonry.h>
2022-12-15 15:16:22 +08:00
#import <SZTextView/SZTextView.h>
2022-12-05 18:56:02 +08:00
#import "UIImage+Utils.h"
#import "XPMacro.h"
#import "ThemeColor.h"
#import "UIView+Corner.h"
2022-12-15 15:16:22 +08:00
#import "TTPopup.h"
2022-12-05 18:56:02 +08:00
@interface XPUpgradeView()
2022-12-27 11:39:37 +08:00
///
@property (nonatomic,strong) UIView *contentView;
///
@property (nonatomic,strong) UIImageView *topImageView;
///
@property (nonatomic,strong) UIView *backView;
2022-12-15 15:16:22 +08:00
@property (nonatomic,strong) SZTextView *textView;
///
@property (nonatomic,strong) UIStackView *stackView;
2022-12-05 18:56:02 +08:00
@property (nonatomic,strong) UIButton *updateBtn;
@property (nonatomic,strong) UIButton *cancelBtn;
2022-12-27 11:39:37 +08:00
///
@property (nonatomic,strong) UILabel *versionLabel;
2022-12-05 18:56:02 +08:00
@end
@implementation XPUpgradeView
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if(self){
[self initSubViews];
[self initSubViewConstraints];
}
return self;
}
#pragma mark - Private Method
- (void)initSubViews {
2022-12-27 11:39:37 +08:00
[self addSubview:self.contentView];
2022-12-15 15:16:22 +08:00
2022-12-27 11:39:37 +08:00
[self.contentView addSubview:self.topImageView];
[self.contentView addSubview:self.backView];
[self.topImageView addSubview:self.versionLabel];
[self.backView addSubview:self.textView];
[self.backView addSubview:self.stackView];
2022-12-15 15:16:22 +08:00
[self.stackView addArrangedSubview:self.cancelBtn];
[self.stackView addArrangedSubview:self.updateBtn];
2022-12-05 18:56:02 +08:00
}
- (void)initSubViewConstraints {
2022-12-27 11:39:37 +08:00
CGFloat kwidth = (KScreenWidth - 100);
2022-12-15 18:48:35 +08:00
[self mas_makeConstraints:^(MASConstraintMaker *make) {
2022-12-27 11:39:37 +08:00
make.size.mas_equalTo(CGSizeMake(kwidth, 340));
2022-12-15 18:48:35 +08:00
}];
2022-12-27 11:39:37 +08:00
[self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
2022-12-15 18:48:35 +08:00
make.edges.equalTo(self);
2022-12-05 18:56:02 +08:00
}];
2022-12-15 15:16:22 +08:00
2022-12-27 11:39:37 +08:00
CGFloat kscale = 140.0 / 275.0;
[self.topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(kwidth, kwidth * kscale));
make.top.mas_equalTo(self.contentView);
make.centerX.mas_equalTo(self.contentView);
}];
[self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.bottom.mas_equalTo(self.contentView);
make.top.mas_equalTo(self.topImageView.mas_bottom);
}];
[self.versionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.topImageView).offset(25);
make.top.mas_equalTo(self.topImageView).offset(90 * kScreenScale);
}];
2022-12-15 15:16:22 +08:00
[self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
2022-12-27 11:39:37 +08:00
make.height.mas_equalTo(36);
make.centerX.mas_equalTo(self.contentView);
make.bottom.mas_equalTo(self.contentView).offset(-20);
2022-12-15 15:16:22 +08:00
}];
2022-12-05 18:56:02 +08:00
[self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
2022-12-27 11:39:37 +08:00
make.width.mas_equalTo((kwidth - 11 - 16 * 2) / 2.0);
2022-12-05 18:56:02 +08:00
}];
[self.updateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
2022-12-27 11:39:37 +08:00
make.width.mas_equalTo(self.cancelBtn);
2022-12-05 18:56:02 +08:00
}];
2022-12-15 15:16:22 +08:00
2022-12-05 18:56:02 +08:00
[self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
2022-12-27 11:39:37 +08:00
make.top.equalTo(self.topImageView.mas_bottom).mas_offset(3);
2022-12-15 15:16:22 +08:00
make.left.mas_equalTo(17);
make.right.mas_equalTo(-17);
make.bottom.equalTo(self.stackView.mas_top).offset(-5);
2022-12-05 18:56:02 +08:00
}];
}
-(void)setVersionModel:(XPVersionUpdateModel *)versionModel{
_versionModel = versionModel;
2022-12-27 11:39:37 +08:00
if (_versionModel.updateVersionDesc) {
NSMutableAttributedString * attribute = [[NSMutableAttributedString alloc] initWithString:_versionModel.updateVersionDesc attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12], NSForegroundColorAttributeName:[ThemeColor mainTextColor]}];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 5;
[attribute addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, _versionModel.updateVersionDesc.length)];
_textView.attributedText = attribute;
}
_versionLabel.text = [NSString stringWithFormat:@"V%@", _versionModel.updateVersion];
2022-12-05 18:56:02 +08:00
_cancelBtn.hidden = _versionModel.updateStatus == 3;
}
-(void)updateAction{
NSURL *url = [[NSURL alloc]initWithString:self.versionModel.updateDownloadLink];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
if(self.versionModel.updateStatus != 3){
2022-12-15 15:16:22 +08:00
[TTPopup dismiss];
2022-12-05 18:56:02 +08:00
}
}];
}
}
-(void)cancelAction{
2022-12-15 15:16:22 +08:00
[TTPopup dismiss];
2022-12-05 18:56:02 +08:00
}
#pragma mark -
2022-12-27 11:39:37 +08:00
- (UIView *)contentView {
if (!_contentView) {
_contentView = [[UIView alloc] init];
_contentView.backgroundColor = [UIColor clearColor];
_contentView.layer.cornerRadius = 12;
_contentView.layer.masksToBounds = YES;
}
return _contentView;
}
- (UIView *)backView {
if (!_backView) {
_backView = [[UIView alloc] init];
_backView.backgroundColor = [UIColor whiteColor];
}
return _backView;
}
2022-12-15 15:16:22 +08:00
- (UIStackView *)stackView {
if (!_stackView) {
_stackView = [[UIStackView alloc] init];
_stackView.axis = UILayoutConstraintAxisHorizontal;
_stackView.distribution = UIStackViewDistributionFill;
_stackView.alignment = UIStackViewAlignmentFill;
_stackView.spacing = 10;
}
return _stackView;
}
2022-12-05 18:56:02 +08:00
2022-12-27 11:39:37 +08:00
- (UIImageView *)topImageView{
if (!_topImageView){
_topImageView = [UIImageView new];
_topImageView.backgroundColor = [UIColor clearColor];
_topImageView.userInteractionEnabled = YES;
_topImageView.image = [UIImage imageNamed:@"version_update_top_bg"];
2022-12-05 18:56:02 +08:00
}
2022-12-27 11:39:37 +08:00
return _topImageView;
2022-12-05 18:56:02 +08:00
}
2022-12-15 15:16:22 +08:00
- (SZTextView *)textView {
if (!_textView) {
_textView = [[SZTextView alloc] init];
_textView.textColor = [ThemeColor mainTextColor];
_textView.font = [UIFont systemFontOfSize:12];
_textView.backgroundColor = [UIColor clearColor];
_textView.editable = NO;
}
return _textView;
2022-12-05 18:56:02 +08:00
}
2022-12-15 15:16:22 +08:00
2022-12-27 11:39:37 +08:00
- (UILabel *)versionLabel {
if (!_versionLabel) {
_versionLabel = [[UILabel alloc] init];
_versionLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];
_versionLabel.textColor = [UIColor whiteColor];
}
return _versionLabel;
}
2022-12-15 15:16:22 +08:00
2022-12-05 18:56:02 +08:00
-(UIButton *)updateBtn{
if (!_updateBtn){
2022-12-15 15:16:22 +08:00
UIImage *image = [UIImage gradientColorImageFromColors:@[[ThemeColor confirmButtonGradientStartColor],[ThemeColor confirmButtonGradientEndColor]] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(10, 10)];
_updateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
2022-12-15 18:48:35 +08:00
[_updateBtn setTitle:@"立即更新" forState:UIControlStateNormal];
2022-12-05 18:56:02 +08:00
_updateBtn.titleLabel.font = [UIFont systemFontOfSize:14];
2022-12-15 15:16:22 +08:00
_updateBtn.titleLabel.textColor = [ThemeColor confirmButtonTextColor];
2022-12-05 18:56:02 +08:00
[_updateBtn setBackgroundImage:image forState:UIControlStateNormal];
2022-12-27 11:39:37 +08:00
_updateBtn.layer.cornerRadius = 36 / 2;
2022-12-05 18:56:02 +08:00
_updateBtn.layer.masksToBounds = YES;
[_updateBtn addTarget:self action:@selector(updateAction) forControlEvents:UIControlEventTouchUpInside];
}
return _updateBtn;
}
-(UIButton *)cancelBtn{
if (!_cancelBtn){
2022-12-15 15:16:22 +08:00
_cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *image = [UIImage gradientColorImageFromColors:@[[ThemeColor cancelButtonGradientStartColor],[ThemeColor cancelButtonGradientEndColor]] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(10, 10)];
[_cancelBtn setTitle:@"下次再说" forState:UIControlStateNormal];
_cancelBtn.titleLabel.font = [UIFont systemFontOfSize:14];
2022-12-15 18:48:35 +08:00
[_cancelBtn setTitleColor:[ThemeColor cancelButtonTextColor] forState:UIControlStateNormal];
2022-12-15 15:16:22 +08:00
[_cancelBtn setBackgroundImage:image forState:UIControlStateNormal];
2022-12-27 11:39:37 +08:00
_cancelBtn.layer.cornerRadius = 36 / 2;
2022-12-15 15:16:22 +08:00
_cancelBtn.layer.masksToBounds = YES;
2022-12-05 18:56:02 +08:00
[_cancelBtn addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
}
return _cancelBtn;
}
@end