@@ -6,6 +6,7 @@ class SettingsViewController: UIViewController, UITableViewDelegate, UITableView
66
77 let tableView = UITableView ( frame: . zero, style: . insetGrouped)
88 private let settingsUtils = SettingsUtils . instance
9+ private var hasRootPermission = false //用于存储是否是有Root权限
910
1011 // 每个分组的小标题
1112 let sectionTitles = [
@@ -38,6 +39,8 @@ class SettingsViewController: UIViewController, UITableViewDelegate, UITableView
3839
3940 // 设置视图标题
4041 self . title = NSLocalizedString ( " Settings_text " , comment: " " )
42+ // 检查Root权限
43+ hasRootPermission = settingsUtils. checkInstallPermission ( )
4144
4245 // 设置表格视图的代理和数据源
4346 tableView. delegate = self
@@ -71,6 +74,16 @@ class SettingsViewController: UIViewController, UITableViewDelegate, UITableView
7174
7275 func tableView( _ tableView: UITableView , cellForRowAt indexPath: IndexPath ) -> UITableViewCell {
7376 var cell = tableView. dequeueReusableCell ( withIdentifier: " cell " , for: indexPath)
77+
78+ // 2. 重置 Cell 状态 解决经典列表复用bug
79+ cell. textLabel? . text = nil
80+ cell. detailTextLabel? . text = nil
81+ cell. textLabel? . textColor = . label
82+ cell. accessoryView = nil
83+ cell. accessoryType = . none
84+ cell. selectionStyle = . none
85+ cell. isUserInteractionEnabled = true
86+
7487 // 设置单元格的文本
7588 cell. textLabel? . text = sections [ indexPath. section] [ indexPath. row]
7689 cell. textLabel? . numberOfLines = 0 // 允许文本过长时换行
@@ -81,13 +94,20 @@ class SettingsViewController: UIViewController, UITableViewDelegate, UITableView
8194 // 创建 UISwitch 控件
8295 let switchView = UISwitch ( frame: . zero)
8396 switchView. addTarget ( self , action: #selector( self . switchChanged ( _: ) ) , for: . valueChanged)
97+ switchView. isEnabled = true
8498 // 根据不同的设置项初始化开关状态
8599 if indexPath. section == 0 {
86100 if indexPath. row == 0 {
87101 switchView. tag = SwitchTag . ShowAlertBeforeAction. rawValue
88102 switchView. isOn = settingsUtils. getShowAlertBeforeAction ( )
89103 } else if indexPath. row == 1 {
90104 switchView. tag = SwitchTag . ShowRootText. rawValue
105+ if !hasRootPermission { // 没有Root权限的用户,不允许关闭无权限的提示
106+ settingsUtils. setShowRootText ( value: true ) // 强制显示 防止某些用户(可能只有我会这样测试)从有Root权限变成没有权限
107+ cell. textLabel? . textColor = . lightGray //文本变成灰色
108+ switchView. isEnabled = false // 禁用开关
109+ cell. isUserInteractionEnabled = false
110+ }
91111 switchView. isOn = settingsUtils. getShowRootText ( )
92112 }
93113 } else if indexPath. section == 1 {
@@ -96,7 +116,25 @@ class SettingsViewController: UIViewController, UITableViewDelegate, UITableView
96116 switchView. isOn = settingsUtils. getEnableRespringFunction ( )
97117 } else if indexPath. row == 1 {
98118 switchView. tag = SwitchTag . HomeQuickAction. rawValue
119+ if !hasRootPermission { // 检查用户是否有Root权限
120+ settingsUtils. setHomeQuickAction ( value: false ) // 无Root 强制关闭桌面快捷方式
121+ settingsUtils. configQuickActions ( application: UIApplication . shared) // 强制移除掉快捷方式 防止某些用户(可能只有我会这样测试)从有Root权限变成没有权限
122+ cell. textLabel? . textColor = . lightGray //文本变成灰色
123+ switchView. isEnabled = false // 禁用开关
124+ cell. isUserInteractionEnabled = false
125+ }
126+ if settingsUtils. getEnableAction ( ) && settingsUtils. getOpenApplicationAction ( ) { // 检查是否与其他功能冲突
127+ cell. textLabel? . textColor = . lightGray //文本变成灰色
128+ switchView. isEnabled = false //Switch开关禁用
129+ cell. isUserInteractionEnabled = false
130+ } else {
131+ cell. textLabel? . textColor = . label //文本正常
132+ switchView. isEnabled = true
133+ cell. isUserInteractionEnabled = true
134+ }
135+
99136 switchView. isOn = settingsUtils. getHomeQuickAction ( )
137+
100138 }
101139 }
102140
@@ -123,22 +161,50 @@ class SettingsViewController: UIViewController, UITableViewDelegate, UITableView
123161
124162 // 设置 UIStepper 为 cell 的 accessoryView
125163 cell. accessoryView = stepper
164+ cell. selectionStyle = . none //没有点击cell的效果
126165 } else if indexPath. row == 2 {
127166 let switchView = UISwitch ( frame: . zero)
128167 switchView. tag = SwitchTag . OpenApplicationAction. rawValue
129168 switchView. addTarget ( self , action: #selector( self . switchChanged ( _: ) ) , for: . valueChanged)
130- switchView. isOn = settingsUtils. getOpenApplicationAction ( )
169+ if !hasRootPermission {
170+ settingsUtils. setOpenApplicationAction ( value: false ) // 无Root权限 禁止打开app就执行操作
171+ cell. textLabel? . textColor = . lightGray //文本变成灰色
172+ switchView. isEnabled = false // 禁用开关
173+ cell. isUserInteractionEnabled = false
174+ } else {
175+ switchView. isOn = settingsUtils. getOpenApplicationAction ( )
176+ }
177+ if !settingsUtils. getEnableAction ( ) { // 判断是否打开了 启用 开关
178+ cell. textLabel? . textColor = . lightGray //文本变成灰色
179+ switchView. isEnabled = false // 禁用开关
180+ cell. isUserInteractionEnabled = false
181+ } else {
182+ cell. textLabel? . textColor = . label //文本变成灰色
183+ switchView. isEnabled = true // 禁用开关
184+ cell. isUserInteractionEnabled = true
185+ }
186+
131187 cell. accessoryView = switchView
132188 } else if indexPath. row == 3 {
133189 cell = UITableViewCell ( style: . value1, reuseIdentifier: " cell " )
134190 if settingsUtils. getEnableRespringFunction ( ) { // 判断是否开启了注销功能
135191 cell. accessoryType = . disclosureIndicator // 添加右侧箭头
192+ cell. detailTextLabel? . textColor = . secondaryLabel // 默认浅灰色
136193 } else {
137194 cell. selectionStyle = . none
138195 cell. accessoryType = . none
196+ cell. textLabel? . textColor = . lightGray //文本变成灰色
197+ }
198+ if !settingsUtils. getEnableAction ( ) { // 判断是否打开了 启用 开关
199+ cell. textLabel? . textColor = . lightGray //文本变成灰色
200+ cell. isUserInteractionEnabled = false
201+ cell. detailTextLabel? . textColor = . tertiaryLabel // 更浅的灰色
202+ } else {
203+ cell. textLabel? . textColor = . label //文本变成灰色
204+ cell. isUserInteractionEnabled = true
139205 }
140206 cell. textLabel? . text = sections [ indexPath. section] [ indexPath. row]
141- cell. isUserInteractionEnabled = settingsUtils. getOpenApplicationAction ( )
207+ // cell.isUserInteractionEnabled = settingsUtils.getOpenApplicationAction()
142208 // 设置当前选择的item
143209 let action = settingsUtils. getAction ( )
144210 switch action {
@@ -167,6 +233,8 @@ class SettingsViewController: UIViewController, UITableViewDelegate, UITableView
167233
168234 return cell
169235 }
236+
237+
170238
171239 // MARK: - UISwitch 事件处理
172240 @objc func switchChanged( _ sender: UISwitch ) {
@@ -178,6 +246,7 @@ class SettingsViewController: UIViewController, UITableViewDelegate, UITableView
178246 switch switchTag {
179247 case SwitchTag . ShowAlertBeforeAction:
180248 settingsUtils. setShowAlertBeforeAction ( value: sender. isOn)
249+ checkAutomaticaEnableAlert ( )
181250 case SwitchTag . ShowRootText:
182251 settingsUtils. setShowRootText ( value: sender. isOn)
183252 case SwitchTag . RespringFunction:
@@ -196,9 +265,16 @@ class SettingsViewController: UIViewController, UITableViewDelegate, UITableView
196265 case SwitchTag . EnableAction:
197266 settingsUtils. setEnableAction ( value: sender. isOn)
198267 settingsUtils. configQuickActions ( application: UIApplication . shared)
268+ checkFunctionConflict ( )
269+ checkAutomaticaEnableAlert ( )
270+ // 刷新子项目
271+ tableView. reloadRows ( at: [ IndexPath ( row: 2 , section: 2 ) ] , with: . none)
272+ tableView. reloadRows ( at: [ IndexPath ( row: 3 , section: 2 ) ] , with: . none)
199273 case SwitchTag . OpenApplicationAction:
200274 settingsUtils. setOpenApplicationAction ( value: sender. isOn)
201275 settingsUtils. configQuickActions ( application: UIApplication . shared)
276+ checkFunctionConflict ( )
277+ checkAutomaticaEnableAlert ( )
202278 }
203279 }
204280
@@ -207,14 +283,15 @@ class SettingsViewController: UIViewController, UITableViewDelegate, UITableView
207283 if let cell = sender. superview as? UITableViewCell {
208284 // 更新 detailTextLabel 显示当前值
209285 settingsUtils. setTime ( value: Int ( sender. value) )
286+ checkAutomaticaEnableAlert ( ) //检查是否是全自动化
210287 cell. detailTextLabel? . text = String . localizedStringWithFormat ( NSLocalizedString ( " Seconds_text " , comment: " " ) , Int ( sender. value) )
211288 }
212289 }
213290
214291 // MARK: - UITableViewDelegate 点击item的事件
215292 func tableView( _ tableView: UITableView , didSelectRowAt indexPath: IndexPath ) {
216293 if indexPath. section == 2 && indexPath. row == 3 {
217- if settingsUtils. getEnableRespringFunction ( ) {
294+ if settingsUtils. getEnableRespringFunction ( ) && settingsUtils . getEnableAction ( ) {
218295 let actionSheet = UIAlertController ( title: NSLocalizedString ( " Choose_An_Action_text " , comment: " " ) , message: nil , preferredStyle: . actionSheet)
219296 let options = [ NSLocalizedString ( " Reboot_Device_text " , comment: " " ) , NSLocalizedString ( " Respring_text " , comment: " " ) ]
220297
@@ -232,7 +309,9 @@ class SettingsViewController: UIViewController, UITableViewDelegate, UITableView
232309 }
233310
234311 // 添加取消按钮
235- let cancelAction = UIAlertAction ( title: NSLocalizedString ( " Cancel_text " , comment: " " ) , style: . cancel, handler: nil )
312+ let cancelAction = UIAlertAction ( title: NSLocalizedString ( " Cancel_text " , comment: " " ) , style: . cancel) { _ in
313+ tableView. reloadRows ( at: [ indexPath] , with: . none) // 取消时刷新单元格 取消动画
314+ }
236315 actionSheet. addAction ( cancelAction)
237316
238317 // 显示弹窗
@@ -265,12 +344,40 @@ class SettingsViewController: UIViewController, UITableViewDelegate, UITableView
265344 }
266345
267346 func tableView( _ tableView: UITableView , titleForFooterInSection section: Int ) -> String ? {
268- return nil // 可以为分组设置尾部文本,如果没有尾部可以返回 nil
347+ // 可以为分组设置尾部文本,如果没有尾部可以返回 nil
348+ if section == 2 {
349+ return String . localizedStringWithFormat ( NSLocalizedString ( " Automatically_Action_hint " , comment: " " ) , NSLocalizedString ( " When_Open_Application_To_Action_text " , comment: " " ) , NSLocalizedString ( " Home_Screen_Quick_Actions_text " , comment: " " ) )
350+ }
351+ return nil
269352 }
270353
271- // 界面关闭的时候让主界面知道设置改变了
354+ // 界面关闭的时候让主界面知道设置改变了 然后更新一下对应的功能是否开启
272355 override func viewWillDisappear( _ animated: Bool ) {
273356 super. viewWillDisappear ( animated)
274357 onSettingsChanged ? ( )
275358 }
359+
360+ private func checkFunctionConflict( ) {
361+ if settingsUtils. getEnableAction ( ) && settingsUtils. getOpenApplicationAction ( ) {
362+ settingsUtils. setHomeQuickAction ( value: false )
363+ settingsUtils. configQuickActions ( application: UIApplication . shared)
364+ }
365+ tableView. reloadRows ( at: [ IndexPath ( row: 1 , section: 1 ) ] , with: . none)
366+ }
367+
368+ private func checkAutomaticaEnableAlert( ) {
369+ if !settingsUtils. getShowAlertBeforeAction ( ) && settingsUtils. getEnableAction ( ) &&
370+ settingsUtils. getOpenApplicationAction ( ) && settingsUtils. getTime ( ) == 0 {
371+ // 显示一个弹窗 提示用户当前操作的取消方法
372+ let alertController = UIAlertController ( title: nil , message: String . localizedStringWithFormat ( NSLocalizedString ( " Automatic_Timer_Alert_text " , comment: " " ) , NSLocalizedString ( " Home_Screen_Quick_Actions_text " , comment: " " ) , NSLocalizedString ( " Cancel_Timer_text " , comment: " " ) ) , preferredStyle: . alert)
373+ let dismissAction = UIAlertAction ( title: NSLocalizedString ( " Dismiss_text " , comment: " " ) , style: . cancel) { _ in
374+ //
375+ }
376+ // 添加按钮到 UIAlertController
377+ alertController. addAction ( dismissAction)
378+ // 显示弹窗
379+ self . present ( alertController, animated: true , completion: nil )
380+ settingsUtils. configQuickActions ( application: UIApplication . shared)
381+ }
382+ }
276383}
0 commit comments