// // CheckPermissionGuideVC.swift // QuickLocation // import UIKit import RxSwift import RxCocoa final class CheckPermissionGuideVC: BaseViewController { enum Kind { case location case screenTime } private let kind: Kind init(kind: Kind) { self.kind = kind super.init(nibName: nil, bundle: nil) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func viewDidLoad() { super.viewDidLoad() setupUI() bindActions() refreshActionButton() } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) refreshActionButton() } private func bindActions() { if kind == .location { tipsLab.rx.tapGesture.subscribe { _ in AppRouter.push(Route.web, userInfo: ["url": URLManager.shared.privacyPolicyUrl]) }.disposed(by: disposeBag) } settingBtn.rx.tap.subscribe(onNext: { [weak self] in self?.handleAction() }).disposed(by: disposeBag) } private func handleAction() { switch kind { case .location: openSystemSettings() case .screenTime: requestScreenTimeAuthorization() } } private func openSystemSettings() { guard let settingsURL = URL(string: UIApplication.openSettingsURLString) else { return } if UIApplication.shared.canOpenURL(settingsURL) { UIApplication.shared.open(settingsURL, options: [:], completionHandler: nil) } } private func requestScreenTimeAuthorization() { guard #available(iOS 16.0, *) else { DLToast.show(text: "该功能需要 iOS 16 及以上系统") return } Task { @MainActor [weak self] in do { if !AppRestrictManager.shared.isAuthorized { try await AppRestrictManager.shared.requestAuthorization() } guard AppRestrictManager.shared.isAuthorized else { DLToast.show(text: "需要屏幕使用时间权限") self?.refreshActionButton() return } self?.refreshActionButton() } catch { DLToast.show(text: "授权失败,请在设置中开启屏幕使用时间权限") self?.refreshActionButton() } } } private func refreshActionButton() { switch kind { case .location: let authorized = CheckPermissionAuth.isLocationAuthorized settingBtn.setTitle(authorized ? "已设置" : "去设置", for: .normal) applyActionStyle(isEnabled: !authorized) case .screenTime: let authorized = CheckPermissionAuth.isScreenTimeAuthorized settingBtn.setTitle(authorized ? "开启成功" : "去设置", for: .normal) applyActionStyle(isEnabled: !authorized) } } private func applyActionStyle(isEnabled: Bool) { settingBtn.isEnabled = isEnabled if isEnabled { settingBtn.setTitleColor(.white, for: .normal) settingBtn.setTitleColor(.white, for: .disabled) settingBtn.setBackgroundImage(UIImage(named: "Common/button_bg_2"), for: .normal) settingBtn.setBackgroundImage(UIImage(named: "Common/button_bg_2"), for: .disabled) settingBtn.backgroundColor = .clear } else { settingBtn.setTitleColor(UIColor(hexStr: "#B5B5B5"), for: .normal) settingBtn.setTitleColor(UIColor(hexStr: "#B5B5B5"), for: .disabled) settingBtn.setBackgroundImage(nil, for: .normal) settingBtn.setBackgroundImage(nil, for: .disabled) settingBtn.backgroundColor = UIColor(hexStr: "#EDEDED") } } private func setupUI() { view.backgroundColor = UIColor(hexStr: "#FAFAFA") view.addSubview(navBgView) view.addSubview(navView) view.addSubview(scrollView) scrollView.addSubview(scrollContentView) scrollContentView.addSubview(stepImgView) view.addSubview(settingBtn) if kind == .location { view.addSubview(tipsLab) } navBgView.layoutChain .edges(excludingEdge: .bottom) .height(kNaviHeight) navView.layoutChain .edges(excludingEdge: .bottom) .height(kNaviHeight) settingBtn.layoutChain .edgesHorzontal(30) .bottom(kSafeBottomMargin + 13) .height(56) if kind == .location { tipsLab.layoutChain .edgesHorzontal(31) .bottomToTopOfView(settingBtn, offset: -20) scrollView.layoutChain .topToBottomOfView(navView, offset: 15) .edgesHorzontal() .bottomToTopOfView(tipsLab, offset: -10) } else { scrollView.layoutChain .topToBottomOfView(navView, offset: 15) .edgesHorzontal() .bottomToTopOfView(settingBtn, offset: -20) } scrollContentView.layoutChain .edges() .widthToView(scrollView) let imageRatio: CGFloat = kind == .location ? (1066.0 / 672.0) : (1014.0 / 672.0) stepImgView.layoutChain .top() .edgesHorzontal(20) .bottom() .heightToWidth(imageRatio) } lazy var navBgView: UIImageView = { let iv = UIImageView() iv.image = UIImage(named: "Common/navBar_bg_2") iv.contentMode = .scaleAspectFill return iv }() lazy var navView = BaseNavigationView(title: kind == .location ? "权限检测" : "屏幕使用时间") lazy var scrollView: UIScrollView = { let view = UIScrollView() view.backgroundColor = .clear view.showsVerticalScrollIndicator = false return view }() lazy var scrollContentView: UIView = { let view = UIView() view.backgroundColor = .clear return view }() lazy var stepImgView: UIImageView = { let name = kind == .location ? "CheckPermission/location" : "CheckPermission/screen" let view = UIImageView(image: UIImage(named: name)) view.contentMode = .scaleAspectFill view.clipsToBounds = true return view }() lazy var tipsLab: UILabel = { let label = UILabel() label.font = .systemFont(ofSize: 10, weight: .regular) label.textColor = UIColor(hexStr: "#333333") label.textAlignment = .center label.numberOfLines = 0 label.isUserInteractionEnabled = true let text = "根据我们的隐私政策和用户设置,您的位置数据也将与第三方共享,用于研究、定制广告和分析目的。" let attr = NSMutableAttributedString(string: text) let range = (text as NSString).range(of: "隐私政策") attr.addAttribute(.foregroundColor, value: UIColor(hexStr: "#16B3FF"), range: range) label.attributedText = attr return label }() lazy var settingBtn: UIButton = { let btn = UIButton(type: .custom) btn.setTitle("去设置", for: .normal) btn.titleLabel?.font = FontManager.boboBold(18) btn.setTitleColor(.white, for: .normal) btn.setBackgroundImage(UIImage(named: "Common/button_bg_2"), for: .normal) btn.cornerRadius = 20 btn.clipsToBounds = true return btn }() }