@@ -2,6 +2,9 @@ import UIKit
22
33class 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
225347extension UIColor {
0 commit comments