// // TFSetViewController.m // WXReader // // Created by 谢腾飞 on 2020/12/2. // Copyright © 2020 xtfei_2011@126.com. All rights reserved. // #import "TFSetViewController.h" #import "TFPushSetViewController.h" #import "TFLanguageSetController.h" #import "TFSetSwitchTableViewCell.h" #import "TFSetLogoutTableViewCell.h" #import "TFSetNormalTableViewCell.h" @interface TFSetViewController () @property (nonatomic ,strong) NSArray *sourcesArray; @end @implementation TFSetViewController - (void)viewDidLoad { [super viewDidLoad]; [self initialize]; [self createSubviews]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self setStatusBarDefaultStyle]; } - (void)initialize { [self setNavigationBarTitle:TFLocalizedString(@"设置")]; self.view.backgroundColor = kGrayViewColor; } - (void)createSubviews { if (TFUserInfoManager.isLogin) { self.sourcesArray = @[ #if TF_Recharge_Mode @[TFLocalizedString(@"自动购买下一章")], #endif @[TFLocalizedString(@"通知管理")], @[TFLocalizedString(@"清除缓存"), TFLocalizedString(@"好评支持"), TFLocalizedString(@"多语言")], @[TFLocalizedString(@"退出登录")] ]; } else { self.sourcesArray = @[@[TFLocalizedString(@"清除缓存"), TFLocalizedString(@"好评支持"), TFLocalizedString(@"多语言")]]; } self.mainTableViewGroup.delegate = self; self.mainTableViewGroup.dataSource = self; [self.view addSubview:self.mainTableViewGroup]; [self.mainTableViewGroup mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(0); make.top.mas_equalTo(PUB_NAVBAR_HEIGHT); make.width.mas_equalTo(SCREEN_WIDTH); make.height.mas_equalTo(SCREEN_HEIGHT - PUB_NAVBAR_HEIGHT); }]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.sourcesArray.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [[self.sourcesArray objectOrNilAtIndex:section] count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (TFUserInfoManager.isLogin) { #if TF_Recharge_Mode if (indexPath.section == 0) { return [self createSettingSwitchCellWithTableView:tableView indexPath:indexPath]; } else if (indexPath.section == 3) { return [self createSettingLogoutCellWithTableView:tableView indexPath:indexPath]; } #else if (indexPath.section == 2) { return [self createSettingLogoutCellWithTableView:tableView indexPath:indexPath]; } #endif } return [self createSettingNormalCellWithTableView:tableView indexPath:indexPath]; } - (UITableViewCell *)createSettingSwitchCellWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath { TFSetSwitchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TFSetSwitchTableViewCell0"]; if (!cell) { cell = [[TFSetSwitchTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TFSetSwitchTableViewCell0"]; } cell.leftTitleText = [[self.sourcesArray objectOrNilAtIndex:indexPath.section] objectOrNilAtIndex:indexPath.row]; cell.hiddenEndLine = YES; if ([TFUserInfoManager shareInstance].auto_sub) { [cell.switchButton setDefaultOnState:YES]; } else { [cell.switchButton setDefaultOnState:NO]; } WS(weakSelf) cell.switchButton.didChangeHandler = ^(BOOL isOn) { [weakSelf autoSubNetRequest]; }; cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; } - (UITableViewCell *)createSettingLogoutCellWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath { TFSetLogoutTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TFSetLogoutTableViewCell"]; if (!cell) { cell = [[TFSetLogoutTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TFSetLogoutTableViewCell"]; } cell.leftTitleText = [[self.sourcesArray objectOrNilAtIndex:indexPath.section] objectOrNilAtIndex:indexPath.row]; cell.hiddenEndLine = [[self.sourcesArray objectOrNilAtIndex:indexPath.section] count] - 1 == indexPath.row; cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; } - (UITableViewCell *)createSettingNormalCellWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath { TFSetNormalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TFSetNormalTableViewCell"]; if (!cell) { cell = [[TFSetNormalTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TFSetNormalTableViewCell"]; } cell.leftTitleText = [[self.sourcesArray objectOrNilAtIndex:indexPath.section] objectOrNilAtIndex:indexPath.row]; cell.hiddenEndLine = NO; cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return kHalfMargin; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *view = [[UIView alloc] init]; view.backgroundColor = [UIColor clearColor]; return view; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return CGFLOAT_MIN; } - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { UIView *view = [[UIView alloc] init]; view.backgroundColor = [UIColor clearColor]; return view; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { TFSetBasicTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if ([cell.leftTitleText isEqualToString:TFLocalizedString(@"通知管理")]) { [self.navigationController pushViewController:[[TFPushSetViewController alloc] init] animated:YES]; return; } if ([cell.leftTitleText isEqualToString:TFLocalizedString(@"清除缓存")]) { TFAlertView *alert = [[TFAlertView alloc] init]; alert.alertDetailContent = TFLocalizedString(@"是否清除缓存信息"); alert.confirmButtonClickBlock = ^{ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [TFPromptManager showPromptViewWithStatus:TFPromptStatusSuccess promptTitle:TFLocalizedString(@"清除缓存成功")]; }); }; [alert showAlertView]; return; } if ([cell.leftTitleText isEqualToString:TFLocalizedString(@"好评支持")]) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:TF_EvaluationAddress] options:@{} completionHandler:nil]; return; } if ([cell.leftTitleText isEqualToString:TFLocalizedString(@"退出登录")]) { [self tableView:tableView settingLogoutdidSelectRowAtIndexPath:indexPath]; return; } if ([cell.leftTitleText isEqualToString:TFLocalizedString(@"多语言")]) { [self.navigationController pushViewController:[[TFLanguageSetController alloc] init] animated:YES]; return; } } - (void)tableView:(UITableView *)tableView settingLogoutdidSelectRowAtIndexPath:(NSIndexPath *)indexPath { [TFUserInfoManager logout]; [[NSNotificationCenter defaultCenter] postNotificationName:Notification_Login_Success object:nil]; [self.mainTableViewGroup reloadData]; [self popViewController]; } - (void)autoSubNetRequest { [TFNetworkTools POST:Auto_Sub_Chapter parameters:nil model:nil success:^(BOOL isSuccess, NSDictionary *_Nullable t_model, TFNetworkRequestModel *requestModel) { if (isSuccess) { BOOL autoSub = [t_model[@"data"][@"auto_sub"] boolValue]; [TFUserInfoManager updateWithDict:@{KEY_PATH(TFUserInfoManager.shareInstance, auto_sub):@(autoSub)}]; } } failure:nil]; } @end