283 lines
8.0 KiB
Swift
283 lines
8.0 KiB
Swift
//
|
|
// CheckPermissionVC.swift
|
|
// QuickLocation
|
|
//
|
|
// Created by 八条 on 2026/6/12.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
enum CheckPermissionAuth {
|
|
static var isLocationAuthorized: Bool {
|
|
AuthorizeManager.manager(type: .locationAlways)?.authorizeStatus() == .authorized
|
|
}
|
|
|
|
static var isScreenTimeAuthorized: Bool {
|
|
if #available(iOS 16.0, *) {
|
|
return AppRestrictManager.shared.isAuthorized
|
|
}
|
|
return false
|
|
}
|
|
}
|
|
|
|
class CheckPermissionVC: BaseViewController {
|
|
|
|
private let locationCard = CheckPermissionItemCard()
|
|
private let screenCard = CheckPermissionItemCard()
|
|
private let pairCard = CheckPermissionItemCard()
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
setupUI()
|
|
bindActions()
|
|
refreshCards()
|
|
}
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
super.viewWillAppear(animated)
|
|
refreshCards()
|
|
}
|
|
|
|
private func bindActions() {
|
|
locationCard.onTap = { [weak self] in
|
|
self?.navigationController?.pushViewController(
|
|
CheckPermissionGuideVC(kind: .location),
|
|
animated: true
|
|
)
|
|
}
|
|
screenCard.onTap = { [weak self] in
|
|
self?.navigationController?.pushViewController(
|
|
CheckPermissionGuideVC(kind: .screenTime),
|
|
animated: true
|
|
)
|
|
}
|
|
pairCard.onTap = {
|
|
AppRouter.push(Route.appRestrict)
|
|
}
|
|
}
|
|
|
|
private func refreshCards() {
|
|
locationCard.configure(
|
|
icon: UIImage(named: "CheckPermission/permission_location_icon"),
|
|
title: "定位权限",
|
|
subtitle: "可以TA分享实时位置",
|
|
actionTitle: CheckPermissionAuth.isLocationAuthorized ? "已设置" : "去设置",
|
|
isConfigured: CheckPermissionAuth.isLocationAuthorized
|
|
)
|
|
screenCard.configure(
|
|
icon: UIImage(named: "CheckPermission/permission_screen_icon"),
|
|
title: "屏幕使用时间限制访问",
|
|
subtitle: "可以TA分享App使用状况",
|
|
actionTitle: CheckPermissionAuth.isScreenTimeAuthorized ? "已设置" : "去设置",
|
|
isConfigured: CheckPermissionAuth.isScreenTimeAuthorized
|
|
)
|
|
pairCard.configure(
|
|
icon: UIImage(named: "CheckPermission/permission_pair_icon"),
|
|
title: "设置配对App",
|
|
subtitle: "可以TA分享App锁定状态",
|
|
actionTitle: "去设置",
|
|
isConfigured: false
|
|
)
|
|
}
|
|
|
|
private func setupUI() {
|
|
view.backgroundColor = UIColor(hexStr: "#FAFAFA")
|
|
view.addSubview(navBgView)
|
|
view.addSubview(navView)
|
|
view.addSubview(warningView)
|
|
warningView.addSubview(warningIconView)
|
|
warningView.addSubview(warningLabel)
|
|
view.addSubview(stackView)
|
|
[locationCard, screenCard, pairCard].forEach { stackView.addArrangedSubview($0) }
|
|
|
|
navBgView.layoutChain
|
|
.edges(excludingEdge: .bottom)
|
|
.height(kNaviHeight)
|
|
|
|
navView.layoutChain
|
|
.edges(excludingEdge: .bottom)
|
|
.height(kNaviHeight)
|
|
|
|
warningView.layoutChain
|
|
.topToBottomOfView(navView, offset: 12)
|
|
.left(16)
|
|
.right(16)
|
|
.height(40)
|
|
|
|
warningIconView.layoutChain
|
|
.left(12)
|
|
.centerY()
|
|
.width(12)
|
|
.height(12)
|
|
|
|
warningLabel.layoutChain
|
|
.leftToRightOfView(warningIconView, offset: 6)
|
|
.right(12)
|
|
.centerY()
|
|
|
|
stackView.layoutChain
|
|
.topToBottomOfView(warningView, offset: 12)
|
|
.left(16)
|
|
.right(16)
|
|
}
|
|
|
|
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: "权限检测")
|
|
|
|
private lazy var warningView: UIView = {
|
|
let view = UIView()
|
|
view.backgroundColor = UIColor(hexStr: "#FFECEC")
|
|
view.cornerRadius = 12
|
|
return view
|
|
}()
|
|
|
|
private lazy var warningIconView: UIImageView = {
|
|
let iv = UIImageView(image: UIImage(named: "CheckPermission/info"))
|
|
iv.contentMode = .scaleAspectFit
|
|
return iv
|
|
}()
|
|
|
|
private lazy var warningLabel: UILabel = {
|
|
let label = UILabel()
|
|
label.text = "需要开启以下权限,否则会导致数据异常"
|
|
label.font = .systemFont(ofSize: 13, weight: .medium)
|
|
label.textColor = UIColor(hexStr: "#FF3B30")
|
|
label.adjustsFontSizeToFitWidth = true
|
|
label.minimumScaleFactor = 0.8
|
|
return label
|
|
}()
|
|
|
|
private lazy var stackView: UIStackView = {
|
|
let stack = UIStackView()
|
|
stack.axis = .vertical
|
|
stack.spacing = 12
|
|
stack.alignment = .fill
|
|
return stack
|
|
}()
|
|
}
|
|
|
|
final class CheckPermissionItemCard: UIControl {
|
|
|
|
var onTap: (() -> Void)?
|
|
|
|
private let iconWell: UIView = {
|
|
let view = UIView()
|
|
view.backgroundColor = .clear
|
|
view.isUserInteractionEnabled = false
|
|
return view
|
|
}()
|
|
|
|
private let iconView: UIImageView = {
|
|
let iv = UIImageView()
|
|
iv.contentMode = .scaleAspectFit
|
|
iv.isHidden = true
|
|
return iv
|
|
}()
|
|
|
|
private let titleLabel: UILabel = {
|
|
let label = UILabel()
|
|
label.font = .systemFont(ofSize: 16, weight: .medium)
|
|
label.textColor = UIColor(hexStr: "#293445")
|
|
label.adjustsFontSizeToFitWidth = true
|
|
label.minimumScaleFactor = 0.85
|
|
return label
|
|
}()
|
|
|
|
private let subtitleLabel: UILabel = {
|
|
let label = UILabel()
|
|
label.font = .systemFont(ofSize: 12, weight: .regular)
|
|
label.textColor = UIColor(hexStr: "#A0A7B2")
|
|
return label
|
|
}()
|
|
|
|
private let actionButton: UIButton = {
|
|
let btn = UIButton(type: .custom)
|
|
btn.titleLabel?.font = .systemFont(ofSize: 14, weight: .medium)
|
|
btn.cornerRadius = 9
|
|
btn.isUserInteractionEnabled = false
|
|
return btn
|
|
}()
|
|
|
|
override init(frame: CGRect) {
|
|
super.init(frame: frame)
|
|
backgroundColor = .white
|
|
cornerRadius = 20
|
|
addTarget(self, action: #selector(handleTap), for: .touchUpInside)
|
|
setupLayout()
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
func configure(
|
|
icon: UIImage?,
|
|
title: String,
|
|
subtitle: String,
|
|
actionTitle: String,
|
|
isConfigured: Bool
|
|
) {
|
|
iconView.image = icon
|
|
iconView.isHidden = icon == nil
|
|
titleLabel.text = title
|
|
subtitleLabel.text = subtitle
|
|
actionButton.setTitle(actionTitle, for: .normal)
|
|
if isConfigured {
|
|
actionButton.backgroundColor = UIColor(hexStr: "#EDEDED")
|
|
actionButton.setTitleColor(UIColor(hexStr: "#B5B5B5"), for: .normal)
|
|
} else {
|
|
actionButton.backgroundColor = UIColor(hexStr: "#16B3FF")
|
|
actionButton.setTitleColor(.white, for: .normal)
|
|
}
|
|
}
|
|
|
|
private func setupLayout() {
|
|
addSubview(iconWell)
|
|
iconWell.addSubview(iconView)
|
|
addSubview(titleLabel)
|
|
addSubview(subtitleLabel)
|
|
addSubview(actionButton)
|
|
|
|
layoutChain.height(80)
|
|
|
|
iconWell.layoutChain
|
|
.left(16)
|
|
.centerY()
|
|
.width(40)
|
|
.height(40)
|
|
|
|
iconView.layoutChain
|
|
.centerX()
|
|
.centerY()
|
|
.width(36)
|
|
.height(36)
|
|
|
|
actionButton.layoutChain
|
|
.right(16)
|
|
.centerY()
|
|
.width(72)
|
|
.height(32)
|
|
|
|
titleLabel.layoutChain
|
|
.leftToRightOfView(iconWell, offset: 12)
|
|
.rightToLeftOfView(actionButton, offset: -10)
|
|
.top(18)
|
|
|
|
subtitleLabel.layoutChain
|
|
.leftToView(titleLabel)
|
|
.rightToView(titleLabel)
|
|
.topToBottomOfView(titleLabel, offset: 4)
|
|
.bottom(18)
|
|
}
|
|
|
|
@objc private func handleTap() {
|
|
onTap?()
|
|
}
|
|
}
|