Files
yinmeng-ios/xplan-ios/Main/Mine/View/Setting/XPMineSettingViewController.m
2022-12-23 10:43:58 +08:00

364 lines
12 KiB
Objective-C

//
// XPMineSettingViewController.m
// xplan-ios
//
// Created by 冯硕 on 2021/9/17.
//
#import "XPMineSettingViewController.h"
///Third
#import <Masonry/Masonry.h>
#import <SDWebImage/SDWebImageManager.h>
#import "SDWebImageManager.h"
#import <ReactiveObjC/ReactiveObjC.h>
///Tool
#import "ThemeColor.h"
#import "XPMacro.h"
#import "NSArray+Safe.h"
#import "XPHtmlUrl.h"
#import "YYUtility.h"
///Model
#import "XPMineSettingItemModel.h"
#import "UserInfoModel.h"
#import "XPVersionUpdateModel.h"
///View
#import "XPMineSettingTableViewCell.h"
#import "TTAlertConfig.h"
#import "TTPopup.h"
#import "XPUpgradeView.h"
///P
#import "XPMineSettingPresent.h"
#import "XPMineSettingProtocol.h"
///VC
#import "XPMineAboutUsViewController.h"
#import "XPMineFeedbackViewController.h"
#import "XPMineNotificaViewController.h"
#import "XPMinePayPwdViewController.h"
#import "XPMineModifPayPwdViewController.h"
#import "XPPrivacyViewController.h"
#import "XPMineLoginPasswordViewController.h"
#import "XPMineBlackListViewController.h"
///不同模块的VC
#import "XPWebViewController.h"
#import "XPLoginVerifBindPhoneViewController.h"
#import "XPLoginBindPhoneResultViewController.h"
@interface XPMineSettingViewController ()<UITableViewDelegate, UITableViewDataSource, XPMineSettingProtocol>
///
@property (nonatomic,strong) UITableView *tableView;
///数据源
@property (nonatomic,strong) NSArray<NSArray<XPMineSettingItemModel *> *> *datasource;
///退出登录
@property (nonatomic,strong) UIButton *logoutButton;
///底部的试图
@property (strong, nonatomic) UIView *footerView;
@end
@implementation XPMineSettingViewController
- (XPMineSettingPresent *)createPresenter {
return [[XPMineSettingPresent alloc] init];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self initSubViews];
[self initSubViewConstraints];
[self.presenter getMineSettingDataSourceWith:self.userInfo];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
///每次都需要刷新一个userinfo
[self.presenter getUserInfo];
}
#pragma mark - Private Method
- (void)initSubViews {
self.title = @"设置";
[self.view addSubview:self.tableView];
[self.footerView addSubview:self.logoutButton];
self.tableView.tableFooterView = self.footerView;
}
- (void)initSubViewConstraints {
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.bottom.mas_equalTo(self.view);
make.top.mas_equalTo(0);
}];
}
- (void)pushViewControllerWithType:(XPMineSettingItemType)type {
switch (type) {
case XPMineSettingItemType_Phone:
{
// 绑定手机
if (self.userInfo.isBindPhone) { // 如果已经绑定
XPLoginBindPhoneResultViewController *vc = [[XPLoginBindPhoneResultViewController alloc] init];
vc.userInfo = _userInfo;
[self.navigationController pushViewController:vc animated:YES];
}else {
XPLoginVerifBindPhoneViewController *vc = [[XPLoginVerifBindPhoneViewController alloc] init];
vc.bindingPhoneNumType = XPBindingPhoneNumTypeNormal;
vc.userInfo = _userInfo;
[self.navigationController pushViewController:vc animated:YES];
}
}
break;
case XPMineSettingItemType_Pay_Password:
{
if (!self.userInfo.isBindPhone) {
[self showErrorToast:@"请先绑定手机号码哦~"];
return;
}
//支付密码
if (self.userInfo.isBindPaymentPwd) {
XPMineModifPayPwdViewController *vc = [[XPMineModifPayPwdViewController alloc] init];
vc.userInfo = self.userInfo;
[self.navigationController pushViewController:vc animated:YES];
} else {
XPMinePayPwdViewController *vc = [[XPMinePayPwdViewController alloc] init];
vc.userInfo = self.userInfo;
[self.navigationController pushViewController:vc animated:YES];
}
}
break;
case XPMineSettingItemType_Login_Password:
{
if (!self.userInfo.isBindPhone) {
XPLoginVerifBindPhoneViewController *vc = [[XPLoginVerifBindPhoneViewController alloc] init];
vc.bindingPhoneNumType = XPBindingPhoneNumTypeNormal;
vc.userInfo = _userInfo;
[self.navigationController pushViewController:vc animated:YES];
return;
}
//支付密码
if (self.userInfo.isBindPasswd) {
XPMineLoginPasswordViewController *vc = [[XPMineLoginPasswordViewController alloc] init];
vc.userInfo = self.userInfo;
vc.isModifiPwd = YES;
[self.navigationController pushViewController:vc animated:YES];
} else {
XPMineLoginPasswordViewController *vc = [[XPMineLoginPasswordViewController alloc] init];
vc.userInfo = self.userInfo;
vc.isModifiPwd = NO;
[self.navigationController pushViewController:vc animated:YES];
}
}
break;
case XPMineSettingItemType_Black_Manager:
{
XPMineBlackListViewController * blackMangerVC = [[XPMineBlackListViewController alloc] init];
[self.navigationController pushViewController:blackMangerVC animated:YES];
}
break;
case XPMineSettingItemType_Notification_Remind:
{
XPMineNotificaViewController * notiVC = [[XPMineNotificaViewController alloc] init];
[self.navigationController pushViewController:notiVC animated:YES];
}
break;
case XPMineSettingItemType_Permission:
{
XPPrivacyViewController * pvc = [[XPPrivacyViewController alloc] init];
[self.navigationController pushViewController:pvc animated:YES];
}
break;
case XPMineSettingItemType_Helper:
{
[self pushWebViewWIthUrl:URLWithType(kFAQURL)];
}
break;
case XPMineSettingItemType_Feedback:
{
XPMineFeedbackViewController * feedbackVC = [[XPMineFeedbackViewController alloc] init];
[self.navigationController pushViewController:feedbackVC animated:YES];
}
break;
case XPMineSettingItemType_Clear_Memory:
{
TTAlertConfig *config = [[TTAlertConfig alloc] init];
config.title = @"清除缓存";
config.message = @"要清除缓存吗?";
[TTPopup alertWithConfig:config confirmHandler:^{
NSSet *websiteDataTypes = [WKWebsiteDataStore allWebsiteDataTypes];
NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
@weakify(self);
[[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
@strongify(self);
[self showSuccessToast:@"清除缓存完成"];
}];
[[SDWebImageManager sharedManager].imageCache clearWithCacheType:SDImageCacheTypeAll completion:nil];
} cancelHandler:^{
}];
}
break;
case XPMineSettingItemType_About_Us:
{
XPMineAboutUsViewController * aboutusVC = [[XPMineAboutUsViewController alloc] init];
[self.navigationController pushViewController:aboutusVC animated:YES];
}
break;
case XPMineSettingItemType_CheckUpdate:
{
[self showLoading];
[self.presenter getVersionUpdate];
}
break;
default:
break;
}
}
- (void)pushWebViewWIthUrl:(NSString *)url {
XPWebViewController * webVC = [[XPWebViewController alloc] init];
webVC.url = url;
[self.navigationController pushViewController:webVC animated:YES];
}
#pragma mark - UITableViewDelegate And UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 45;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.datasource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.datasource[section].count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
XPMineSettingTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPMineSettingTableViewCell class])];
if (cell == nil) {
cell = [[XPMineSettingTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([XPMineSettingTableViewCell class])];
}
NSArray * array = [self.datasource safeObjectAtIndex1:indexPath.section];
XPMineSettingItemModel * model = [array safeObjectAtIndex1:indexPath.row];
cell.itemModel = model;
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if (section != 0) {
return 15;
}
return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView * view = [[UIView alloc] init];
view.frame = CGRectMake(0, 0, KScreenWidth, 15);
view.backgroundColor = [UIColor clearColor];
return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView * view = [[UIView alloc] init];
view.frame = CGRectMake(0, 0, KScreenWidth, 0.01f);
view.backgroundColor = [UIColor clearColor];
return view;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (self.datasource.count > 0) {
XPMineSettingItemModel * model = [[self.datasource safeObjectAtIndex1:indexPath.section] safeObjectAtIndex1:indexPath.row];
[self pushViewControllerWithType:model.type];
}
}
#pragma mark - XPMineSettingProtocol
- (void)getMineSettingDatasourceSuccess:(NSArray *)array {
self.datasource = array;
[self.tableView reloadData];
}
- (void)onGetUserInfoSuccess:(UserInfoModel *)userInfo {
self.userInfo = userInfo;
[self.presenter getMineSettingDataSourceWith:userInfo];
}
- (void)logoutCurrentAccountSuccess {
[self.navigationController popToRootViewControllerAnimated:NO];
}
- (void)getVersionUpdateFail{
[self hideHUD];
}
///版本更新
-(void)getVersionUpdate:(XPVersionUpdateModel *)model{
[self hideHUD];
if(model.updateVersion.length == 0){
[self showErrorToast:@"目前已更新到最新版本"];
return;
}
NSString *appVersion = [YYUtility appVersion];
NSArray *appVersionList = [appVersion componentsSeparatedByString:@"."];
int appVersionNum = 0;
if(appVersionList. count == 3){
appVersionNum = [appVersionList[0]intValue] * 100 + [appVersionList[1]intValue] * 10 + [appVersionList[2]intValue];
}
NSString *updateVersion = model.updateVersion;
NSArray *updateVersionList = [updateVersion componentsSeparatedByString:@"."];
int updateVersionNum = 0;
if(updateVersionList. count == 3){
updateVersionNum = [updateVersionList[0] intValue] * 100 + [updateVersionList[1] intValue] * 10 + [updateVersionList[2] intValue];
}
if(updateVersionNum <= appVersionNum){
[self showErrorToast:@"目前已更新到最新版本"];
return;
}
XPUpgradeView *view = [[XPUpgradeView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
view.versionModel = model;
[[UIApplication sharedApplication].keyWindow addSubview:view];
}
#pragma mark - Event Response
- (void)logoutButtonAction:(UIButton *)sender {
[self.presenter logoutCurrentAccount];
}
#pragma mark - Getters And Setters
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.showsVerticalScrollIndicator = NO;
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.backgroundColor = [UIColor clearColor];
[_tableView registerClass:[XPMineSettingTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XPMineSettingTableViewCell class])];
}
return _tableView;
}
- (UIView *)footerView {
if (!_footerView) {
_footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 168)];
}
return _footerView;
}
- (UIButton *)logoutButton {
if (!_logoutButton) {
_logoutButton = [UIButton buttonWithType:UIButtonTypeCustom];
_logoutButton.frame = CGRectMake(32, 40, KScreenWidth - 32* 2, 50);
_logoutButton.backgroundColor = [ThemeColor appCellBackgroundColor];
_logoutButton.layer.masksToBounds = YES;
_logoutButton.layer.cornerRadius = 50/2;
_logoutButton.titleLabel.font = [UIFont systemFontOfSize:16];
[_logoutButton setTitleColor:[ThemeColor mainTextColor] forState:UIControlStateNormal];
[_logoutButton setTitle:@"退出当前账号" forState:UIControlStateNormal];
[_logoutButton addTarget:self action:@selector(logoutButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _logoutButton;
}
@end