Skip to content

Commit cc53a42

Browse files
1.Finished timer function.
2.Finished open application do action. 3.Finished Home Screen quick actions.
1 parent fd82caf commit cc53a42

6 files changed

Lines changed: 357 additions & 50 deletions

File tree

AppDelegate.swift

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,93 @@ import UIKit
44
class AppDelegate: UIResponder, UIApplicationDelegate {
55

66
var window: UIWindow?
7+
var mainViewController: RootViewController?
8+
// 加载配置文件
9+
let settingsUtils = SettingsUtils.instance
10+
// 用于存储启动时的快捷方式
11+
var pendingQuickAction: UIApplicationShortcutItem?
712

813
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
914
window = UIWindow(frame: UIScreen.main.bounds)
10-
window!.rootViewController = UINavigationController(rootViewController: RootViewController())
15+
mainViewController = RootViewController()
16+
guard let mainVC = mainViewController else {
17+
fatalError("Failed to initialize RootViewController")
18+
}
19+
window!.rootViewController = UINavigationController(rootViewController: mainVC)
1120
window!.makeKeyAndVisible()
12-
// 加载配置文件
13-
SettingsUtils.instance
21+
settingsUtils.configQuickActions(application: application)
22+
23+
// 检查是否通过快捷方式启动
24+
if let shortcutItem = launchOptions?[.shortcutItem] as? UIApplicationShortcutItem {
25+
pendingQuickAction = shortcutItem // 保存快捷方式
26+
}
27+
28+
1429
return true
1530
}
31+
32+
func applicationDidBecomeActive(_ application: UIApplication) {
33+
// 处理启动时的快捷方式
34+
if let shortcutItem = pendingQuickAction {
35+
handleQuickActionFromLaunch(shortcutItem)
36+
pendingQuickAction = nil // 清除状态
37+
}
38+
}
39+
40+
func application(
41+
_ application: UIApplication,
42+
performActionFor shortcutItem: UIApplicationShortcutItem,
43+
completionHandler: @escaping (Bool) -> Void
44+
) {
45+
guard let navController = window?.rootViewController as? UINavigationController else {
46+
completionHandler(false)
47+
return
48+
}
49+
50+
// 检查是否有模态视图被显示 主要用来处理设置界面打开的时候返回桌面再按快捷方式无效的问题
51+
if let presentedVC = navController.presentedViewController {
52+
print("Modal view detected. Dismissing before executing quick action.")
53+
presentedVC.dismiss(animated: true) { [weak self] in
54+
self?.handleQuickAction(shortcutItem.type, navController: navController, completionHandler: completionHandler)
55+
}
56+
} else {
57+
// 没有模态视图,直接执行快捷方式逻辑 在主界面 直接执行快捷操作
58+
handleQuickAction(shortcutItem.type, navController: navController, completionHandler: completionHandler)
59+
}
60+
}
61+
62+
private func handleQuickActionFromLaunch(_ shortcutItem: UIApplicationShortcutItem) {
63+
guard let navController = window?.rootViewController as? UINavigationController else {
64+
print("Failed to handle quick action: NavigationController not found.")
65+
return
66+
}
67+
68+
handleQuickAction(shortcutItem.type, navController: navController) { success in
69+
print("Quick action from launch handled: \(success)")
70+
}
71+
}
72+
73+
private func handleQuickAction(_ actionType: String, navController: UINavigationController, completionHandler: @escaping (Bool) -> Void) {
74+
guard let quickAction = SettingsUtils.QuickActionType(rawValue: actionType) else {
75+
completionHandler(false)
76+
return
77+
}
78+
79+
// 优先处理 CancelTimer 快捷方式 不然就没办法从桌面快捷方式取消计时器了
80+
if quickAction == .CancelTimer {
81+
SettingsUtils.instance.setEnableAction(value: false)
82+
SettingsUtils.instance.configQuickActions(application: UIApplication.shared)
83+
}
84+
85+
// 找到 RootViewController 并执行快捷方式
86+
if let rootVC = navController.topViewController as? RootViewController {
87+
rootVC.onClickQuickAction?(actionType)
88+
completionHandler(true)
89+
} else {
90+
completionHandler(false)
91+
}
92+
}
93+
94+
1695

1796
}

Resources/en.lproj/Localizable.strings

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
"Respring_text" = "Respring";
66
"Settings_text" = "Settings";
77
"Done_text" = "Done";
8+
"Dismiss_text" = "Dismiss";
89
"Action_Alert" = "Are you sure you want to %@?";
910
"Confirm_text" = "Confirm";
1011
"Cancel_text" = "Cancel";
12+
"Cancel_Timer_text" = "Disable timer";
13+
"Timer_Closed_text" = "The timer has been turned off.\nYou can reopen it in Settings";
1114
"General_text" = "General";
1215
"Timer_text" = "Timer";
1316
"About_text" = "About";
@@ -21,6 +24,9 @@
2124
"Choose_An_Action_text" = "Choose an action";
2225
"When_Open_Application_To_Action_text" = "Automatically perform operations when launching the Application";
2326
"Show_Alert_Before_Starting_text" = "Show alert before starting";
27+
"Countdown_Title_text" = "Countdown for %@";
28+
"Countdown_Message_text" = "%d seconds remaining";
29+
"Timer_Alert_text" = "TODO";
2430
"Version_text" = "Version";
2531
"Unknown_text" = "Unknown";
2632
"Special_Thanks_text" = "Special thanks to Xiaobovlog\nProvides core code for reboot";

Resources/zh-Hans.lproj/Localizable.strings

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
"Respring_text" = "注销";
66
"Settings_text" = "设置";
77
"Done_text" = "完成";
8+
"Dismiss_text" = "关闭";
89
"Action_Alert" = "确定%@?";
910
"Confirm_text" = "确定";
1011
"Cancel_text" = "取消";
12+
"Cancel_Timer_text" = "禁用倒计时器";
13+
"Timer_Closed_text" = "倒计时器已被禁用\n可以在设置中重新启用";
1114
"General_text" = "通用";
1215
"Timer_text" = "倒计时器";
1316
"About_text" = "关于";
@@ -21,6 +24,9 @@
2124
"Choose_An_Action_text" = "选择一个操作";
2225
"When_Open_Application_To_Action_text" = "启动App时自动进行操作";
2326
"Show_Alert_Before_Starting_text" = "开始操作前显示提醒";
27+
"Countdown_Title_text" = "即将进行%@";
28+
"Countdown_Message_text" = "剩余%d秒";
29+
"Timer_Alert_text" = "TODO";
2430
"Version_text" = "版本";
2531
"Unknown_text" = "未知";
2632
"Special_Thanks_text" = "特别感谢肖博vlog\n提供重启设备核心代码";

RootViewController.swift

Lines changed: 131 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import UIKit
22

33
class RootViewController: UIViewController {
44

5+
// 定义一个闭包(接口)当桌面快捷方式被点击的时候调用
6+
var onClickQuickAction: ((String) -> Void)?
7+
58
private let settingsUtils = SettingsUtils.instance
69
private let checkPermissionLabel = UILabel()
710
private let respringButton = UIButton(type: .system)
@@ -38,7 +41,7 @@ class RootViewController: UIViewController {
3841
checkPermissionLabel.textAlignment = .center // 设置文本居中
3942
checkPermissionLabel.isHidden = !settingsUtils.getShowRootText()
4043

41-
let enable = self.checkInstallPermission()
44+
let enable = settingsUtils.checkInstallPermission()
4245

4346
if(enable) {
4447
checkPermissionLabel.text = NSLocalizedString("Install_With_TrollStore_text", comment: "")
@@ -125,10 +128,37 @@ class RootViewController: UIViewController {
125128

126129
}
127130

128-
func checkInstallPermission() -> Bool {
129-
let path = "/var/mobile/Library/Preferences"
130-
let writeable = access(path, W_OK) == 0
131-
return writeable
131+
override func viewWillAppear(_ animated: Bool) {
132+
super.viewWillAppear(animated)
133+
134+
// 桌面快捷方式的回调
135+
onClickQuickAction = { [weak self] actionType in
136+
137+
guard let quickAction = SettingsUtils.QuickActionType(rawValue: actionType) else {
138+
NSLog("Invalid quick action type: \(actionType)")
139+
return
140+
}
141+
142+
switch quickAction {
143+
case .Reboot:
144+
self?.onClickRebootButton()
145+
case .Respring:
146+
self?.onClickRespringButton()
147+
case .CancelTimer:
148+
self?.onClickCloseTimerQuickAction()
149+
}
150+
}
151+
152+
// 打开app时的自动操作
153+
if settingsUtils.getEnableAction() && settingsUtils.getOpenApplicationAction() {
154+
let action = settingsUtils.getAction()
155+
switch action {
156+
case .Reboot:
157+
self.onClickRebootButton()
158+
case .Respring:
159+
self.onClickRespringButton()
160+
}
161+
}
132162
}
133163

134164
@objc func onClickRebootButton() {
@@ -139,8 +169,7 @@ class RootViewController: UIViewController {
139169

140170
// 创建 "确定" 按钮(红色)
141171
let confirmAction = UIAlertAction(title: NSLocalizedString("Confirm_text", comment: ""), style: .destructive) { _ in
142-
let deviceController = DeviceController()
143-
deviceController.rebootDevice()
172+
self.rebootDevice()
144173
}
145174

146175
// 创建 "取消" 按钮
@@ -154,6 +183,20 @@ class RootViewController: UIViewController {
154183

155184
// 显示弹窗
156185
self.present(alertController, animated: true, completion: nil)
186+
} else {
187+
self.rebootDevice()
188+
}
189+
190+
}
191+
192+
private func rebootDevice() {
193+
let time = settingsUtils.getTime()
194+
if settingsUtils.getEnableAction() && time > 0 {
195+
showCountdownAlert(actionName: NSLocalizedString("Reboot_Device_text", comment: ""), countdownSeconds: time) {
196+
//计时器结束的操作
197+
let deviceController = DeviceController()
198+
deviceController.rebootDevice()
199+
}
157200
} else {
158201
let deviceController = DeviceController()
159202
deviceController.rebootDevice()
@@ -169,8 +212,7 @@ class RootViewController: UIViewController {
169212

170213
// 创建 "确定" 按钮(红色)
171214
let confirmAction = UIAlertAction(title: NSLocalizedString("Confirm_text", comment: ""), style: .destructive) { _ in
172-
let deviceController = DeviceController()
173-
deviceController.respring()
215+
self.respring()
174216
}
175217

176218
// 创建 "取消" 按钮
@@ -184,6 +226,20 @@ class RootViewController: UIViewController {
184226

185227
// 显示弹窗
186228
self.present(alertController, animated: true, completion: nil)
229+
} else {
230+
self.respring()
231+
}
232+
233+
}
234+
235+
private func respring() {
236+
let time = settingsUtils.getTime()
237+
if settingsUtils.getEnableAction() && time > 0 {
238+
showCountdownAlert(actionName: NSLocalizedString("Respring_text", comment: ""), countdownSeconds: time) {
239+
//计时器结束的操作
240+
let deviceController = DeviceController()
241+
deviceController.respring()
242+
}
187243
} else {
188244
let deviceController = DeviceController()
189245
deviceController.respring()
@@ -216,10 +272,76 @@ class RootViewController: UIViewController {
216272
dismiss(animated: true, completion: nil)
217273
}
218274

275+
private func showCountdownAlert(actionName: String, countdownSeconds: Int, action: @escaping () -> Void) {
276+
guard countdownSeconds > 0 else {
277+
print("Invalid countdownSeconds: \(countdownSeconds). It should be greater than 0.")
278+
return
279+
}
280+
281+
// 初始化倒计时变量
282+
var remainingSeconds = countdownSeconds
283+
var timer: Timer?
284+
285+
// 创建 UIAlertController
286+
let alertController = UIAlertController(
287+
title: String.localizedStringWithFormat(NSLocalizedString("Countdown_Title_text", comment: ""), actionName),
288+
message: String.localizedStringWithFormat(NSLocalizedString("Countdown_Message_text", comment: ""), remainingSeconds),
289+
preferredStyle: .alert
290+
)
291+
292+
// 添加取消按钮
293+
let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel_text", comment: ""), style: .destructive) { _ in
294+
timer?.invalidate() // 停止计时器
295+
}
296+
alertController.addAction(cancelAction)
297+
298+
// 显示弹窗
299+
DispatchQueue.main.async {
300+
self.present(alertController, animated: true, completion: nil)
301+
}
302+
303+
// 创建 Timer,倒计时
304+
timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in
305+
guard remainingSeconds > 0 else { // 确保倒计时不为负数
306+
timer?.invalidate()
307+
return
308+
}
309+
310+
remainingSeconds -= 1
311+
DispatchQueue.main.async {
312+
alertController.message = String.localizedStringWithFormat(NSLocalizedString("Countdown_Message_text", comment: ""), remainingSeconds)
313+
}
314+
315+
// 倒计时结束,执行操作
316+
if remainingSeconds <= 0 {
317+
timer?.invalidate() // 停止计时器
318+
DispatchQueue.main.async {
319+
alertController.dismiss(animated: true, completion: nil) // 关闭弹窗
320+
action() // 执行操作
321+
}
322+
}
323+
}
324+
}
325+
326+
219327
private func updateUI() { // 因为设置更改 更新界面
220328
checkPermissionLabel.isHidden = !settingsUtils.getShowRootText()
221329
respringButton.isHidden = !settingsUtils.getEnableRespringFunction()
222330
}
331+
332+
private func onClickCloseTimerQuickAction() {
333+
settingsUtils.setEnableAction(value: false)
334+
// 显示一个弹窗 提示用户倒计时器已被禁用
335+
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
337+
//
338+
}
339+
// 添加按钮到 UIAlertController
340+
alertController.addAction(doneAction)
341+
// 显示弹窗
342+
self.present(alertController, animated: true, completion: nil)
343+
settingsUtils.configQuickActions(application: UIApplication.shared)
344+
}
223345
}
224346

225347
extension UIColor {

0 commit comments

Comments
 (0)