-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathReactNativeDeviceUtils.swift
More file actions
94 lines (76 loc) · 3.04 KB
/
ReactNativeDeviceUtils.swift
File metadata and controls
94 lines (76 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import NitroModules
import UIKit
class ReactNativeDeviceUtils: HybridReactNativeDeviceUtilsSpec {
private static let userInterfaceStyleKey = "1k_user_interface_style"
override init() {
super.init()
restoreUserInterfaceStyle()
}
private func restoreUserInterfaceStyle() {
guard let style = UserDefaults.standard.string(forKey: ReactNativeDeviceUtils.userInterfaceStyleKey) else {
return
}
applyUserInterfaceStyle(style)
}
private func applyUserInterfaceStyle(_ style: String) {
DispatchQueue.main.async {
var uiStyle: UIUserInterfaceStyle = .unspecified
if style == "light" {
uiStyle = .light
} else if style == "dark" {
uiStyle = .dark
}
if #available(iOS 15.0, *) {
for scene in UIApplication.shared.connectedScenes {
if let windowScene = scene as? UIWindowScene {
for window in windowScene.windows {
window.overrideUserInterfaceStyle = uiStyle
}
}
}
} else {
for window in UIApplication.shared.windows {
window.overrideUserInterfaceStyle = uiStyle
}
}
}
}
public func isDualScreenDevice() throws -> Bool {
return false
}
public func initEventListeners() throws -> Void {
}
public func isSpanning() throws -> Bool {
return false
}
public func getWindowRects() throws -> Promise<[DualScreenInfoRect]> {
return Promise.resolved(withResult: [])
}
public func getHingeBounds() throws -> Promise<DualScreenInfoRect> {
return Promise.resolved(withResult: DualScreenInfoRect(x: 0, y: 0, width: 0, height: 0))
}
func addSpanningChangedListener(callback: @escaping (Bool) -> Void) throws -> Double {
return 0
}
func removeSpanningChangedListener(id: Double) throws -> Void {
}
public func setUserInterfaceStyle(style: UserInterfaceStyle) throws -> Void {
UserDefaults.standard.set(style.stringValue, forKey: ReactNativeDeviceUtils.userInterfaceStyleKey)
applyUserInterfaceStyle(style.stringValue)
}
public func changeBackgroundColor(r: Double, g: Double, b: Double, a: Double) throws -> Void {
DispatchQueue.main.async {
// Clamp color values to valid range [0, 255]
let red = max(0, min(255, r))
let green = max(0, min(255, g))
let blue = max(0, min(255, b))
let alpha = max(0, min(255, a))
let color = UIColor(red: red/255.0, green: green/255.0, blue: blue/255.0, alpha: alpha/255.0)
// Fix optional chaining syntax
if let window = UIApplication.shared.delegate?.window,
let rootViewController = window?.rootViewController {
rootViewController.view.backgroundColor = color
}
}
}
}