Skip to content

Commit a3142d5

Browse files
author
DevelopLab
committed
1.Finished all settings and function
2.Fix SettingsViewController.swift cell reuse bug.
1 parent 8423f0e commit a3142d5

4 files changed

Lines changed: 125 additions & 11 deletions

File tree

Resources/en.lproj/Localizable.strings

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"Show_Alert_Before_Starting_text" = "Show alert before starting";
2727
"Countdown_Title_text" = "Countdown for %@";
2828
"Countdown_Message_text" = "%d seconds remaining";
29-
"Timer_Alert_text" = "TODO";
29+
"Automatic_Timer_Alert_text" = "The current selection will immediately automatically execute the operation you choose when the Application is opened next time, with no any alert.\nIf you want to disable it, you can long press the application icon to use %@ %@";
30+
"Automatically_Action_hint" = "'%@' and '%@' can not enable together.";
3031
"Version_text" = "Version";
3132
"Unknown_text" = "Unknown";
3233
"Special_Thanks_text" = "Special thanks to Xiaobovlog\nProvides core code for reboot";

Resources/zh-Hans.lproj/Localizable.strings

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"Show_Alert_Before_Starting_text" = "开始操作前显示提醒";
2727
"Countdown_Title_text" = "即将进行%@";
2828
"Countdown_Message_text" = "剩余%d秒";
29-
"Timer_Alert_text" = "TODO";
29+
"Automatic_Timer_Alert_text" = "当前的选择在下一次打开App时会立即自动执行您选择的操作,没有任何提示\n如果需要关闭可以长按图标使用%@%@";
30+
"Automatically_Action_hint" = "'%@'和'%@'无法同时开启";
3031
"Version_text" = "版本";
3132
"Unknown_text" = "未知";
3233
"Special_Thanks_text" = "特别感谢肖博vlog\n提供重启设备核心代码";

RootViewController.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ class RootViewController: UIViewController {
3939
// 检查权限
4040
checkPermissionLabel.translatesAutoresizingMaskIntoConstraints = false
4141
checkPermissionLabel.textAlignment = .center // 设置文本居中
42-
checkPermissionLabel.isHidden = !settingsUtils.getShowRootText()
4342

4443
let enable = settingsUtils.checkInstallPermission()
4544

4645
if(enable) {
4746
checkPermissionLabel.text = NSLocalizedString("Install_With_TrollStore_text", comment: "")
4847
checkPermissionLabel.textColor = UIColor.green
48+
checkPermissionLabel.isHidden = !settingsUtils.getShowRootText()
4949
} else {
5050
checkPermissionLabel.text = NSLocalizedString("Need_Install_With_TrollStore_text", comment: "")
5151
checkPermissionLabel.textColor = UIColor.red
@@ -63,6 +63,11 @@ class RootViewController: UIViewController {
6363
rebootButton.clipsToBounds = true
6464
rebootButton.setTitle(NSLocalizedString("Reboot_Device_text", comment: ""), for: .normal)
6565
rebootButton.setTitleColor(.white, for: .normal)
66+
if !enable {
67+
// 禁用状态按钮的样子 照顾一下iOS14 用户 但是又没权限的情况,貌似基本不存在
68+
rebootButton.backgroundColor = UIColor.lightGray
69+
rebootButton.setTitleColor(.darkGray, for: .normal)
70+
}
6671
}
6772

6873
rebootButton.translatesAutoresizingMaskIntoConstraints = false
@@ -333,11 +338,11 @@ class RootViewController: UIViewController {
333338
settingsUtils.setEnableAction(value: false)
334339
// 显示一个弹窗 提示用户倒计时器已被禁用
335340
let alertController = UIAlertController(title: nil, message: NSLocalizedString("Timer_Closed_text", comment: ""), preferredStyle: .alert)
336-
let doneAction = UIAlertAction(title: NSLocalizedString("Dismiss_text", comment: ""), style: .cancel) { _ in
341+
let dismissAction = UIAlertAction(title: NSLocalizedString("Dismiss_text", comment: ""), style: .cancel) { _ in
337342
//
338343
}
339344
// 添加按钮到 UIAlertController
340-
alertController.addAction(doneAction)
345+
alertController.addAction(dismissAction)
341346
// 显示弹窗
342347
self.present(alertController, animated: true, completion: nil)
343348
settingsUtils.configQuickActions(application: UIApplication.shared)

SettingsViewController.swift

Lines changed: 113 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)