CNLiveMineVC.m 16.2 KB
//
//  CNLiveMineVC.m
//  CNLiveCMineModule
//
//  Created by CNLive on 2022/1/14.
//

#import "CNLiveMineVC.h"
#import "CNTopImageView.h"
#import "CNMinePresent.h"
#import "CNListCollectionView.h"
#import "CNActivityView.h"
#import "CNToolView.h"
#import "CNEvaluaveView.h"
#import "CNRecommendView.h"
#import "CNMineEvaAlertView.h"
#import "CNRevisionInstructionView.h"
#import <CNLiveBaseTools/UIImage+EditIcon.h>
#import <DSBaseModule/DSBaseModule.h>


//有新客服消息
static NSString *const CNSobotNotification_NewMessage = @"CNSobotNotification_NewMessage";

@interface CNLiveMineVC ()<UIScrollViewDelegate>
@property (nonatomic, assign) CGFloat ratio;
@property (nonatomic, strong) UIScrollView *bgScrollView;
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) CNTopImageView *topImageView;
@property (nonatomic, strong) UIImageView *topSmallView;
@property (nonatomic, strong) CNListCollectionView *orderView;
@property (nonatomic, strong) CNListCollectionView *packageView;
@property (nonatomic, strong) CNActivityView *activityView;
@property (nonatomic, strong) CNToolView *toolView;
@property (nonatomic, strong) UILabel *versionLabel;
@property (nonatomic, strong) CNEvaluaveView *evaluateView;
@property (nonatomic, strong) CNRecommendView *recommend;

@property (nonatomic, strong) NSString *carCount;

@end

@implementation CNLiveMineVC
- (void)loadData{
    [CNMinePresent requestMineDataSourceWithBlock:^(NSArray * _Nonnull activities, CNScoreModel * _Nonnull model) {
        [self.bgScrollView.mj_header endRefreshing];
        self.activityView.dataSource = activities;
        self.carCount = model.cart_count;
        self.orderView.model = [CNMinePresent dataSourceOrdersModel:model.pay_order_count willShip:model.send_order_count willReceipt:model.confirm_order_count];
        self.packageView.integral = model.integral;
        self.packageView.money = model.money;
        self.packageView.model = [CNMinePresent dataSourceWalletModel:model.integral couponNum:model.coupon_count balance:model.money];
        self.toolView.model = [CNMinePresent datasourceToolModel:CNToolDatasourceTypeFirst carNum:model.cart_count];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.carCount = @"0";
    self.ratio = RATIO;
    [self.view addSubview:self.bgScrollView];
    [self.bgScrollView addSubview:self.bgView];
    [self.bgScrollView addSubview:self.topImageView];
    [self.view addSubview:self.topSmallView];
    [self.view bringSubviewToFront:self.topSmallView];
    [self.bgScrollView addSubview:self.orderView];
    [self.bgScrollView addSubview:self.packageView];
    [self.bgScrollView addSubview:self.activityView];
    [self.bgScrollView addSubview:self.toolView];
    [self.bgScrollView addSubview:self.versionLabel];
    NSString *hiddenDate = [[NSUserDefaults standardUserDefaults] objectForKey:HIDDENDETE];
    if (![hiddenDate isEqualToString:@"down"]) {
        [self.view addSubview:self.evaluateView];
        [self.evaluateView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.view);
            make.bottom.equalTo(self.view).offset(-DS_adapt_length(20));
            make.width.mas_equalTo(DS_adapt_length(79));
            make.height.mas_equalTo(DS_adapt_length(75.5));
        }];
    }
    
    self.bgScrollView.frame = CGRectMake(0, -kStatusHeight, kScreenWidth, kScreenHeight + kStatusHeight);
    self.topSmallView.frame = CGRectMake(0, 0, kScreenWidth, kNavigationBarHeight);
    self.topImageView.frame = CGRectMake(0, 0, kScreenWidth, DS_adapt_length(273));
    self.orderView.frame = CGRectMake(DS_adapt_length(10), self.topImageView.bottom -DS_adapt_length(55), kScreenWidth - DS_adapt_length( 20), DS_adapt_length(80));
    self.packageView.frame = CGRectMake(self.orderView.left, self.orderView.bottom +DS_adapt_length(16), self.orderView.width , DS_adapt_length(106*RATIO));
    self.activityView.frame = CGRectMake(self.orderView.left, self.packageView.bottom + DS_adapt_length(16), self.orderView.width, DS_adapt_length(24+138 + 38 *RATIO + 10));
    self.toolView.frame = CGRectMake(self.orderView.left, self.activityView.bottom + DS_adapt_length(16), self.orderView.width, DS_adapt_length(145 + 42*RATIO));
    self.versionLabel.frame = CGRectMake(self.orderView.left, self.toolView.bottom + DS_adapt_length(16), kScreenWidth, DS_adapt_length(16));
    self.bgScrollView.contentSize = CGSizeMake(kScreenWidth, self.versionLabel.bottom + DS_adapt_length(8));
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addTolks) name:CNSobotNotification_NewMessage object:nil];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (self.ratio != RATIO) {
        self.ratio = RATIO;
        _versionLabel.font = UIFontCNMake(14);
        [self.topImageView uploadViews];
        [self.packageView uploadViews];
        [self.activityView uploadViews];
        [self.toolView uploadViews];
        self.packageView.height = DS_adapt_length(106*RATIO);
        self.activityView.top = self.packageView.bottom + DS_adapt_length(16);
        self.activityView.height = DS_adapt_length(24+138 + 38 *RATIO+ 10);
        self.toolView.top = self.activityView.bottom + DS_adapt_length(16);
        self.versionLabel.top = self.toolView.bottom + DS_adapt_length(16);
        self.bgScrollView.contentSize = CGSizeMake(kScreenWidth, self.versionLabel.bottom + DS_adapt_length(8));
    }
    [self loadData];
    
    NSString *currentDate = [[NSString stringWithFormat:@"%@",[NSDate date]] substringToIndex:10];
    NSString *hiddenDate = [[NSUserDefaults standardUserDefaults] objectForKey:HIDDENDETE];
    if (![hiddenDate isEqualToString:@"down"]&&![currentDate isEqualToString:hiddenDate]) {
        self.evaluateView.hidden = NO;
    }else{
        self.evaluateView.hidden = YES;
    }
    if (![[NSUserDefaults standardUserDefaults] objectForKey:@"showEvaliate"]&&![hiddenDate isEqualToString:@"down"]) {
        if ([[NSUserDefaults standardUserDefaults] integerForKey:@"showTimes"] == 1) {
            [[NSUserDefaults standardUserDefaults] setInteger:2 forKey:@"showTimes"];
        }else if ([[NSUserDefaults standardUserDefaults] integerForKey:@"showTimes"] == 2){
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"showTimes"];
            [CNMineEvaAlertView setUpShowEvaluateAlertViewDoneBlock:^(NSString * _Nonnull chooseStr) {
                [CNMinePresent requestFeedback:chooseStr secceedBlock:^{
                    [[NSUserDefaults standardUserDefaults] setObject:@"down" forKey:HIDDENDETE];
                    [self.evaluateView removeFromSuperview];
                }];
                
            } cancelBlock:^{
                if (![[NSUserDefaults standardUserDefaults] boolForKey:@"showRem"]) {
                    if (self.evaluateView.showRecommend) self.evaluateView.showRecommend();
                    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"showRem"];
                }
            }];
        }
    }
    
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"instruShown"]) {
        CNRevisionInstructionView *visinView = [[CNRevisionInstructionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, self.view.height)];
        visinView.removeBlock = ^{
            [self setNeedsStatusBarAppearanceUpdate];
        };
        [self.view addSubview:visinView];
        [self.view bringSubviewToFront:visinView];
        [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"showTimes"];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"instruShown"];
    }
}

- (BOOL)preferredNavigationBarHidden
{
    return YES;
}
- (void)addTolks{
    if ([[CNLiveCMineService shareMineProtocol] haveUnreadMessage]) {
        self.topImageView.codesView.model = [CNMinePresent dataSourceCodesModel:@"."];
    }else{
        self.topImageView.codesView.model = [CNMinePresent dataSourceCodesModel:@"0"];
    }
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat contentOffetY = scrollView.offsetY + kStatusHeight;
    if (contentOffetY <= DS_adapt_length(23)  && contentOffetY > 0) {
        self.topSmallView.alpha = contentOffetY/DS_adapt_length(23) ;
        [AppKeyWindow addSubview:self.topImageView.codesView];
        [AppKeyWindow addSubview:self.topImageView.iconImage];
        [self.topImageView.iconImage mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).offset(kStatusHeight+DS_adapt_length(23)  - contentOffetY);
            make.left.equalTo(self.view).offset(DS_adapt_length( 12));
            make.width.height.mas_equalTo(DS_adapt_length(60) - self.topSmallView.alpha * DS_adapt_length(28));
        }];
        self.topImageView.iconImage.layer.mask = [self maskLayerWithWidth:DS_adapt_length(60) - self.topSmallView.alpha * DS_adapt_length(28)];
        [self.topImageView.codesView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).offset(kStatusHeight);
            make.right.equalTo(self.view).offset(-DS_adapt_length(10));
            make.height.mas_equalTo(DS_adapt_length(30));
            make.width.mas_equalTo(DS_adapt_length(105));
        }];
    }else if (contentOffetY > DS_adapt_length(23)){
        self.topSmallView.alpha = 1;
        [self.topSmallView addSubview:self.topImageView.codesView];
        [self.topSmallView addSubview:self.topImageView.iconImage];
        [self.topImageView.iconImage mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).offset(kStatusHeight);
            make.left.equalTo(self.view).offset(DS_adapt_length(12));
            make.width.height.mas_equalTo(DS_adapt_length(32));
        }];
        self.topImageView.iconImage.layer.mask = [self maskLayerWithWidth:DS_adapt_length(32)];
        [self.topImageView.codesView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).offset(kStatusHeight);
            make.right.equalTo(self.view).offset(-DS_adapt_length(10));
            make.height.mas_equalTo(DS_adapt_length(30));
            make.width.mas_equalTo(DS_adapt_length(105));
        }];
    }else{
        self.topSmallView.alpha = 0;
        [self.topImageView addSubview:self.topImageView.codesView];
        [self.topImageView addSubview:self.topImageView.iconImage];
        [self.topImageView.iconImage mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.topImageView).offset(kStatusHeight+DS_adapt_length(23));
            make.left.equalTo(self.topImageView).offset(DS_adapt_length(12));
            make.width.height.mas_equalTo(DS_adapt_length(60));
        }];
        self.topImageView.iconImage.layer.mask = [self maskLayerWithWidth:DS_adapt_length(60)];
        [self.topImageView.codesView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.topImageView).offset(kStatusHeight);
            make.right.equalTo(self.topImageView).offset(-DS_adapt_length(10));
            make.height.mas_equalTo(DS_adapt_length(30));
            make.width.mas_equalTo(DS_adapt_length(105));
        }];
    }
}
-(CAShapeLayer *)maskLayerWithWidth:(CGFloat)width{
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, width, width) cornerRadius:(width)/2];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.path = path.CGPath;
    return maskLayer;
}
#pragma mark - Getting
- (UIScrollView *)bgScrollView
{
    if (!_bgScrollView) {
        _bgScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
        _bgScrollView.backgroundColor = RGB(32, 31, 36);
        _bgScrollView.showsVerticalScrollIndicator = NO;
        _bgScrollView.showsHorizontalScrollIndicator = NO;
        _bgScrollView.delegate = self;
        CNLiveRefreshHeader *header = [CNLiveRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
        header.isShowWhite = NO;
        _bgScrollView.mj_header =  header;
    }
    return _bgScrollView;
}
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, KScreenWidth*200.0/375.0, KScreenWidth, 2*KScreenHeight)];
        _bgView.backgroundColor = UIColorHex(F6F6F6);
    }
    return _bgView;
}

- (CNTopImageView *)topImageView
{
    if (!_topImageView) {
        _topImageView = [[CNTopImageView alloc] init];
        _topImageView.contentMode = UIViewContentModeScaleAspectFill;
        _topImageView.backgroundColor = UIColorHex(#F6F6F6);
        _topImageView.userInteractionEnabled = YES;
        UIImage *image = [UIImage getEditIconWithName:@"mine_bgImage"];
        if (!image) {
            image = [CNLiveCMineService imageWithMineName:@"mine_bgImage"];
        }
        _topImageView.image = image;
    }
    return _topImageView;
}
- (UIImageView *)topSmallView
{
    if (!_topSmallView) {
        _topSmallView = [[UIImageView alloc] init];
        _topSmallView.contentMode = UIViewContentModeScaleAspectFill;
        _topSmallView.backgroundColor = UIColorHex(#F6F6F6);
        _topSmallView.userInteractionEnabled = YES;
        UIImage *image = [UIImage getEditIconWithName:@"mine_bgImage_small"];
        if (!image) {
            image = [CNLiveCMineService imageWithMineName:@"mine_bgImage_small"];
        }
        _topSmallView.image = image;
        _topSmallView.alpha = 0;
    }
    return _topSmallView;
}
- (CNListCollectionView *)orderView
{
    if (!_orderView) {
        _orderView = [[CNListCollectionView alloc] init];
        _orderView.layer.cornerRadius = 8;
        _orderView.backgroundColor = kWhiteColor;
        _orderView.model = [CNMinePresent dataSourceOrdersModel:@"0" willShip:@"0" willReceipt:@"0"];
        [_orderView.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_orderView).offset(5);
        }];
        _orderView.showdivideImage = YES;
    }
    return _orderView;
}
- (CNListCollectionView *)packageView
{
    if (!_packageView) {
        _packageView = [[CNListCollectionView alloc] initWithFrame:CGRectZero];
        _packageView.layer.cornerRadius = 8;
        _packageView.layer.masksToBounds = YES;
        _packageView.backgroundColor = kWhiteColor;
        _packageView.model = [CNMinePresent dataSourceWalletModel:@"0" couponNum:@"0" balance:@"0"];
    }
    return _packageView;
}

- (CNActivityView *)activityView
{
    if (!_activityView) {
        _activityView = [[CNActivityView alloc] init];
        _activityView.layer.cornerRadius = 8;
        _activityView.backgroundColor = kWhiteColor;
    }
    return _activityView;
}
- (CNToolView *)toolView
{
    if (!_toolView) {
        _toolView = [[CNToolView alloc] init];
        _toolView.layer.cornerRadius = 8;
        _toolView.backgroundColor = kWhiteColor;
        _toolView.model = [CNMinePresent datasourceToolModel:CNToolDatasourceTypeFirst carNum:self.carCount];
    }
    return _toolView;
}
- (UILabel *)versionLabel
{
    if (!_versionLabel) {
        _versionLabel = [[UILabel alloc] init];
        _versionLabel.backgroundColor = kClearColor;
        _versionLabel.textColor = UIColorHex(#999999);
        _versionLabel.font = UIFontCNMake(14);
        _versionLabel.textAlignment = NSTextAlignmentCenter;
        _versionLabel.text = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    }
    return _versionLabel;
}

- (CNEvaluaveView *)evaluateView
{
    if (!_evaluateView) {
        _evaluateView = [[CNEvaluaveView alloc] init];
        weakself;
        _evaluateView.showRecommend = ^{
            strongself;
            if (!self->_evaluateView.hidden) {
                [self.view addSubview:self.recommend];
                [self.recommend mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.right.equalTo(self.evaluateView.mas_left);
                    make.centerY.equalTo(self.evaluateView).offset(-5);
                    make.width.mas_equalTo(103);
                    make.height.mas_equalTo(27);
                }];
                [self performBlock:^(id  _Nonnull selfPtr) {
                    [self.recommend removeFromSuperview];
                } afterDelay:3];
            }
        };
    }
    return _evaluateView;
}
- (CNRecommendView *)recommend
{
    if (!_recommend) {
        _recommend = [[CNRecommendView alloc] init];
        [_recommend setImage:[CNLiveCMineService imageWithMineName:@"recommend_bg"]];
    }
    return _recommend;
}
@end