ProfileTableView.m 10.9 KB
//
//  ProfileTableView.m
//  DreamSleep
//
//  Created by peter on 2022/5/18.
//

#import "ProfileTableView.h"
#import "ProfileHeaderView.h"

#pragma mark - ProfileModel
@interface ProfileModel : NSObject
@property (nonatomic, copy) NSString *leftIcon;
@property (nonatomic, copy) NSString *dk_leftIcon;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *rightIcon;
@property (nonatomic, copy) NSString *dk_rightIcon;
@end

@implementation ProfileModel

+ (NSArray *)getGroupDataArr {
    NSArray *leftIcon_group1 = @[@"ic_person_fankui_normal", @"ic_person_shezhi_normal", @"ic_person_yaoqing_normal", @"ic_person_guanyuwomen_normal"];
    NSArray *dk_leftIcon_group1 = @[@"dk_ic_person_fankui_normal", @"dk_ic_person_shezhi_normal", @"dk_ic_person_yaoqing_normal", @"dk_ic_person_guanyuwomen_normal"];
    NSArray *title_group1 = @[@"意见反馈", @"系统设置", @"邀请好友", @"关于我们"];
    
    NSArray *leftIcon_group2 = @[@"ic_person_xiaochengxu_normal", @"ic_person_gongzhonghao_normal", @"ic_person_kefu_normal"];
    NSArray *dk_leftIcon_group2 = @[@"dk_ic_person_xiaochengxu_normal", @"dk_ic_person_gongzhonghao_normal", @"dk_ic_person_kefu_normal"];
    NSArray *title_group2 = @[@"前往小程序", @"关注公众号", @"添加客服微信"];
    
    NSMutableArray *group1Arr = [NSMutableArray array];
    for (int i = 0; i < leftIcon_group1.count; i++) {
        ProfileModel *model = [ProfileModel new];
        model.leftIcon = leftIcon_group1[i];
        model.dk_leftIcon = dk_leftIcon_group1[i];
        model.title = title_group1[i];
        model.rightIcon = @"moreIcon";
        model.dk_rightIcon = @"moreIcon";
        [group1Arr addObject:model];
    }
    NSMutableArray *group2Arr = [NSMutableArray array];
    for (int i = 0; i < leftIcon_group2.count; i++) {
        ProfileModel *model = [ProfileModel new];
        model.leftIcon = leftIcon_group2[i];
        model.dk_leftIcon = dk_leftIcon_group2[i];
        model.title = title_group2[i];
        model.rightIcon = @"moreIcon";
        model.dk_rightIcon = @"moreIcon";
        [group2Arr addObject:model];
    }
    return @[[group1Arr copy], [group2Arr copy]];
}

@end

#pragma mark - ProfileCell
@interface ProfileCell : UITableViewCell
@property (nonatomic, strong) ProfileModel *model;
@property (nonatomic, strong) UIImageView *leftIV;
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UIImageView *rightIV;
@end

@implementation ProfileCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = DSClearColor;
        [self.contentView addSubview:self.leftIV];
        [self.contentView addSubview:self.titleLab];
        [self.contentView addSubview:self.rightIV];
        
        [self.leftIV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(23);
            make.centerY.equalTo(self.contentView);
            make.width.height.equalTo(@30);
        }];
        [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.leftIV.mas_right).offset(8);
            make.centerY.equalTo(self.contentView);
            make.right.equalTo(self.rightIV.mas_left).offset(-8);
        }];
        [self.rightIV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView).offset(-23);
            make.centerY.equalTo(self.contentView);
            make.width.height.equalTo(@30);
        }];
    }
    return self;
}

- (void)setModel:(ProfileModel *)model {
    _model = model;
    self.leftIV.dk_imagePicker = DKImagePickerWithNames(model.leftIcon, model.dk_leftIcon, model.leftIcon);
    self.titleLab.text = model.title;
    self.rightIV.dk_imagePicker = DKImagePickerWithNames(model.rightIcon, model.dk_rightIcon, model.rightIcon);
}

- (UIImageView *)leftIV {
    if (!_leftIV) {
        _leftIV = [UIImageView new];
    }
    return _leftIV;
}

- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [UILabel labWithFont:SysFont(15)];
        _titleLab.dk_textColorPicker = DKColorPickerWithColors(MainTextColor, DkTitleColor, DkTitleColor);
    }
    return _titleLab;
}

- (UIImageView *)rightIV {
    if (!_rightIV) {
        _rightIV = [UIImageView new];
    }
    return _rightIV;
}

@end

#pragma mark - ProfileTableView
@interface ProfileTableView () <UITableViewDataSource, UITableViewDelegate, ProfileHeaderViewDelegate>
@property (nonatomic, strong) NSArray *profileGroupArr;
@property (nonatomic, strong) ProfileHeaderView *profileHeaderView;
@property (nonatomic, strong) UIView *footerView;
@property (nonatomic, strong) UIImageView *introduceIV;
@property (nonatomic, strong) NSURL *introduceUrl;
@property (nonatomic, strong) ExceptionDefaultView *exceptionView;
@end

@implementation ProfileTableView

- (instancetype)initWithDelegate:(id<ProfileTableViewDelegate>)profileDelegate {
    if (self = [super initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kTabBarHeight) style:UITableViewStyleGrouped]) {
        _profileDelegate = profileDelegate;
        self.backgroundColor = DSClearColor;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.showsVerticalScrollIndicator = NO;
        self.delegate = self;
        self.dataSource = self;
        self.tableHeaderView = self.profileHeaderView;
        self.tableFooterView = self.footerView;
        self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        [self registerClass:[ProfileCell class] forCellReuseIdentifier:NSStringFromClass([ProfileCell class])];        
    }
    return self;
}

#pragma mark - UITableViewDataSource && UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.profileGroupArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *dataArr = self.profileGroupArr[section];
    return dataArr.count;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    NSArray *dataArr = self.profileGroupArr[indexPath.section];
    ProfileModel *model = dataArr[indexPath.row];
    ProfileCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ProfileCell class]) forIndexPath:indexPath];
    cell.model = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 48;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0.001)];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 0) {
        UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 8)];
        footView.dk_backgroundColorPicker = DKColorPickerWithColors(ColorFromHex(0xF0F0F0), AlertDarkColor, DSWhite);
        return footView;
    } else {
        return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0.001)];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return  (section == 0) ? 8 : 0.001;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.profileDelegate && [self.profileDelegate respondsToSelector:@selector(didSelectedIndexPath:)]) {
        [self.profileDelegate didSelectedIndexPath:indexPath];
    }
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView*)scrollView {
    CGFloat offset = scrollView.contentOffset.y;
    if (offset >= 0) {
        if (self.profileDelegate && [self.profileDelegate respondsToSelector:@selector(showNaviBar:)]) {
            [self.profileDelegate showNaviBar:offset/kTopHeight(0)];
        }
    }
    if (self.profileDelegate && [self.profileDelegate respondsToSelector:@selector(isUpdateStatusStyle:)]) {
        [self.profileDelegate isUpdateStatusStyle:(offset >= 2 ? YES : NO)];
    }
}

#pragma mark - Action
- (void)tapCBTIAction {
    [self.ds_viewController.navigationController pushViewController:[[DsWebController alloc] initWithLink:MYCBTI isShowNavi:NO] animated:YES];
}

#pragma mark - ProfileHeaderViewDelegate
- (void)didClickModifyUserBtn {
    if (self.profileDelegate && [self.profileDelegate respondsToSelector:@selector(modifyUserInfoAction)] ) {
        [self.profileDelegate modifyUserInfoAction];
    }
}

- (void)didClickScoreTaskBtn:(NSInteger)index {
    if (self.profileDelegate && [self.profileDelegate respondsToSelector:@selector(dealScoreTaskAction:)] ) {
        [self.profileDelegate dealScoreTaskAction:index];
    }
}

#pragma mark - lazy
- (NSArray *)profileGroupArr {
    if (!_profileGroupArr) {
        _profileGroupArr = [ProfileModel getGroupDataArr];
    }
    return _profileGroupArr;
}

- (ProfileHeaderView *)profileHeaderView {
    if (!_profileHeaderView) {
        _profileHeaderView = [[ProfileHeaderView alloc] initWithDelegate:self];
    }
    return _profileHeaderView;
}

- (UIView *)footerView {
    if (!_footerView) {
        CGFloat width = kScreenWidth - 30;
        CGFloat height = 260*width/654.0;
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, height + 27)];
        UIImageView *introduceIV = [[UIImageView alloc] initWithFrame:CGRectMake(15, 15, width, height)];
        [introduceIV cornerRadius:12.0];
        introduceIV.userInteractionEnabled = YES;
        [_footerView addSubview:introduceIV];
        UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapCBTIAction)];
        [introduceIV addGestureRecognizer:tapGR];
        self.introduceIV = introduceIV;
        
        [self loadImg];
    }
    return _footerView;
}

- (ExceptionDefaultView *)exceptionView {
    if (!_exceptionView) {
        WS(weakSelf);
        _exceptionView = [[ExceptionDefaultView alloc] initWithType:ExceptionTypeSmall block:^{
            weakSelf.exceptionView.hidden = YES;
            [weakSelf loadImg];
        } superView:self.footerView];
    }
    return _exceptionView;
}

- (void)loadImg {
    WS(weakSelf);
    [self.introduceIV yy_setImageWithURL:[NSURL URLWithString:@"https://img2.ydniu.com/sleep_ssmain/cbti_cover.png"] placeholder:[UIImage imageNamed:@"bannerPlaceholder"] options:YYWebImageOptionShowNetworkActivity completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
        weakSelf.exceptionView.hidden = error ? NO : YES;
        weakSelf.introduceIV.hidden = !weakSelf.exceptionView.hidden;
    }];
}

@end