// // XPPermissionsViewController.m // xplan-ios // // Created by zu on 2021/12/28. // #import "XPPermissionsViewController.h" #import #import #import #import "XPMacro.h" #import "ThemeColor.h" #import "NSArray+Safe.h" @interface XPPermissionCell : UITableViewCell @property (nonatomic, strong) UIImageView * icon; @property (nonatomic, strong) UILabel * name; @property (nonatomic, strong) UILabel * tip; @end @implementation XPPermissionCell - (instancetype)init { if (self == [super init]) { self.selectionStyle = UITableViewCellSelectionStyleNone; self.backgroundColor = UIColor.clearColor; [self.contentView addSubview:self.icon]; [self.contentView addSubview:self.name]; [self.contentView addSubview:self.tip]; self.contentView.backgroundColor = ThemeColor.appCellBackgroundColor; self.contentView.layer.masksToBounds = YES; self.contentView.layer.cornerRadius = 8; [self.icon mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.mas_equalTo(self.contentView); make.left.mas_equalTo(self.contentView).offset(15); }]; [self.name mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.icon.mas_right).offset(8); make.top.mas_equalTo(self.contentView).offset(16); }]; [self.tip mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.icon.mas_right).offset(8); make.bottom.mas_equalTo(self.contentView).offset(-16); }]; } return self; } - (UIImageView *)icon { if (!_icon) { _icon = [[UIImageView alloc] init]; } return _icon; } - (UILabel *)name { if (!_name) { _name = [[UILabel alloc] init]; _name.textColor = ThemeColor.mainTextColor; _name.font = [UIFont systemFontOfSize:14]; } return _name; } - (UILabel *)tip { if (!_tip) { _tip = [[UILabel alloc] init]; _tip.textColor = ThemeColor.secondTextColor; _tip.font = [UIFont systemFontOfSize:13]; } return _tip; } @end @interface XPPermissionsViewController () @property (nonatomic,strong) UITableView * tableView; @property (nonatomic, strong) NSMutableArray * permissionIcons; @property (nonatomic, strong) NSMutableArray * permissionNames; @property (nonatomic, strong) NSMutableArray * permissionTips; @property (nonatomic, strong) UIView * empty; @property (nonatomic, strong) UIImageView * emptyImage; @property (nonatomic, strong) UILabel * emptyTip; @property (nonatomic, strong) UIView * footer; @property (nonatomic, strong) UILabel * footerTip; @property (nonatomic, strong) UIButton * gotoSetting; @end @implementation XPPermissionsViewController - (void)viewDidLoad { [super viewDidLoad]; self.title = @"系统权限管理"; [self.empty addSubview:self.emptyImage]; [self.empty addSubview:self.emptyTip]; [self.view addSubview:self.tableView]; [self.footer addSubview:self.footerTip]; [self.footer addSubview:self.gotoSetting]; [self.view addSubview:self.footer]; } - (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.top.right.bottom.mas_equalTo(self.view).insets(UIEdgeInsetsMake(16, 16, 16, 16)); }]; [self.emptyImage mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self.empty); make.top.mas_equalTo(self.empty).offset(200); make.size.mas_equalTo(CGSizeMake(100, 100)); }]; [self.emptyTip mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self.empty); make.top.mas_equalTo(self.emptyImage.mas_bottom).offset(16); }]; [self.footer mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.mas_equalTo(self.view); make.bottom.mas_equalTo(self.view).offset(-kSafeAreaBottomHeight); make.height.mas_equalTo(100); }]; [self.footerTip mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self.footer); make.top.mas_equalTo(self.footer).offset(24); }]; [self.gotoSetting mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self.footer); make.top.mas_equalTo(self.footerTip.mas_bottom).offset(40); }]; } - (void)viewWillAppear:(BOOL)animated { PHAuthorizationStatus phStatus = [PHPhotoLibrary authorizationStatus]; if (phStatus == PHAuthorizationStatusAuthorized) { [self.permissionNames addObject:@"照片"]; [self.permissionTips addObject:@"读取相册或者存储图片到相册"]; [self.permissionIcons addObject:@"mine_setting_permission_album"]; } AVAuthorizationStatus microPhoneStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio]; if (microPhoneStatus == AVAuthorizationStatusAuthorized) { [self.permissionNames addObject:@"麦克风"]; [self.permissionTips addObject:@"录制音频"]; [self.permissionIcons addObject:@"mine_setting_permission_voice"]; } AVAuthorizationStatus cameraStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; if (cameraStatus == AVAuthorizationStatusAuthorized) { [self.permissionNames addObject:@"相机"]; [self.permissionTips addObject:@"拍摄照片或录制视频"]; [self.permissionIcons addObject:@"mine_setting_permission_camera"]; } } - (void)gotoSettingClick:(UIButton *)sender { NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; if ([[UIApplication sharedApplication] canOpenURL:url]) { [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil]; } } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { tableView.backgroundView = self.permissionNames.count == 0 ? self.empty : nil; return self.permissionNames.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { if (section == 0) { return 0.001f; } return 16.f; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 70.f; } -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *view = [[UIView alloc] init]; view.backgroundColor = [UIColor clearColor]; return view; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSInteger section = indexPath.section; XPPermissionCell * cell = [[XPPermissionCell alloc] init]; cell.icon.image = [UIImage imageNamed:[self.permissionIcons safeObjectAtIndex1:section]]; cell.name.text = [self.permissionNames safeObjectAtIndex1:section]; cell.tip.text = [self.permissionTips safeObjectAtIndex1:section]; return cell; } - (UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; _tableView.delegate = self; _tableView.dataSource = self; _tableView.showsVerticalScrollIndicator = NO; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.backgroundColor = [UIColor clearColor]; [_tableView registerClass:[XPPermissionCell class] forCellReuseIdentifier:NSStringFromClass([XPPermissionCell class])]; } return _tableView; } - (NSMutableArray *)permissionIcons { if (!_permissionIcons) { _permissionIcons = [[NSMutableArray alloc] init]; } return _permissionIcons; } - (NSMutableArray *)permissionNames { if (!_permissionNames) { _permissionNames = [[NSMutableArray alloc] init]; } return _permissionNames; } - (NSMutableArray *)permissionTips { if (!_permissionTips) { _permissionTips = [[NSMutableArray alloc] init]; } return _permissionTips; } - (UIView *)empty { if (!_empty) { _empty = [[UIView alloc] init]; } return _empty; } - (UIImageView *)emptyImage { if (!_emptyImage) { _emptyImage = [[UIImageView alloc] init]; _emptyImage.contentMode = UIViewContentModeScaleAspectFit; _emptyImage.image = [UIImage imageNamed:@"common_empty"]; } return _emptyImage; } - (UILabel *)emptyTip { if (!_emptyTip) { _emptyTip = [[UILabel alloc] init]; _emptyTip.textColor = ThemeColor.secondTextColor; _emptyTip.font = [UIFont systemFontOfSize:12]; _emptyTip.text = @"未有已授权的系统权限"; } return _emptyTip; } - (UIView *)footer { if (!_footer) { _footer = [[UIView alloc] init]; } return _footer; } - (UILabel *)footerTip { if (!_footerTip) { _footerTip = [[UILabel alloc] init]; _footerTip.textColor = ThemeColor.mainTextColor; _footerTip.font = [UIFont systemFontOfSize:12]; _footerTip.text = @"—— 仅展示能查询到的已授权系统权限 ——"; } return _footerTip; } - (UIButton *)gotoSetting { if (!_gotoSetting) { _gotoSetting = [UIButton buttonWithType:UIButtonTypeCustom]; [_gotoSetting setTitle:@"前往系统设置>" forState:UIControlStateNormal]; [_gotoSetting setTitleColor:ThemeColor.appMainColor forState:UIControlStateNormal]; _gotoSetting.titleLabel.font = [UIFont systemFontOfSize:14]; _gotoSetting.titleLabel.adjustsFontSizeToFitWidth = YES; [_gotoSetting addTarget:self action:@selector(gotoSettingClick:) forControlEvents:UIControlEventTouchUpInside]; } return _gotoSetting; } @end