mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-06-23 02:00:20 +07:00
fix: iPad startup crash fix (#2071)
This commit is contained in:
@@ -11,13 +11,7 @@ class ScreenProtection {
|
|||||||
import UIKit
|
import UIKit
|
||||||
|
|
||||||
public func toggleScreenshots(_ isEnabled: Bool) {
|
public func toggleScreenshots(_ isEnabled: Bool) {
|
||||||
let window = UIApplication.shared.keyWindows.first!
|
ScreenProtection.shared.setScreenshotsEnabled(isEnabled)
|
||||||
|
|
||||||
if isEnabled {
|
|
||||||
ScreenProtection.shared.disable(for: window.rootViewController!.view)
|
|
||||||
} else {
|
|
||||||
ScreenProtection.shared.enable(for: window.rootViewController!.view)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extension UIApplication {
|
extension UIApplication {
|
||||||
@@ -45,6 +39,45 @@ class ScreenProtection {
|
|||||||
|
|
||||||
private var blurView: UIVisualEffectView?
|
private var blurView: UIVisualEffectView?
|
||||||
private var recordingObservation: NSKeyValueObservation?
|
private var recordingObservation: NSKeyValueObservation?
|
||||||
|
private var desiredScreenshotsEnabled: Bool?
|
||||||
|
private var retryCount = 0
|
||||||
|
private var retryWorkItem: DispatchWorkItem?
|
||||||
|
|
||||||
|
public func setScreenshotsEnabled(_ isEnabled: Bool) {
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
self.desiredScreenshotsEnabled = isEnabled
|
||||||
|
self.applyScreenshotsSettingOrRetry()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func applyScreenshotsSettingOrRetry() {
|
||||||
|
assert(Thread.isMainThread)
|
||||||
|
|
||||||
|
guard let desiredScreenshotsEnabled else { return }
|
||||||
|
guard let window = UIApplication.shared.keyWindows.first,
|
||||||
|
let rootView = window.rootViewController?.view else {
|
||||||
|
retryCount += 1
|
||||||
|
guard retryCount <= 50 else { return } // ~5s total
|
||||||
|
|
||||||
|
retryWorkItem?.cancel()
|
||||||
|
let item = DispatchWorkItem { [weak self] in
|
||||||
|
self?.applyScreenshotsSettingOrRetry()
|
||||||
|
}
|
||||||
|
retryWorkItem = item
|
||||||
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: item)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
retryWorkItem?.cancel()
|
||||||
|
retryWorkItem = nil
|
||||||
|
retryCount = 0
|
||||||
|
|
||||||
|
if desiredScreenshotsEnabled {
|
||||||
|
disable(for: rootView)
|
||||||
|
} else {
|
||||||
|
enable(for: rootView)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public func enable(for view: UIView) {
|
public func enable(for view: UIView) {
|
||||||
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
|
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user