@onekeyfe/react-native-device-utils 6.7.0-alpha.392 → 6.7.0-alpha.394

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -23,7 +23,9 @@ final class DualScreenDetector {
23
23
  private var spanning = false
24
24
  private var listeners: [Double: (Bool) -> Void] = [:]
25
25
  private var nextListenerId: Double = 1
26
- private var hingeObserver: AnyObject?
26
+ private var hingeInteraction: UIInteraction?
27
+ private var keyWindowObserver: NSObjectProtocol?
28
+ private var resignKeyWindowObserver: NSObjectProtocol?
27
29
 
28
30
  private init() {
29
31
  let modelIdentifier = DualScreenDetector.modelIdentifier()
@@ -89,46 +91,56 @@ final class DualScreenDetector {
89
91
  #if compiler(>=6.4)
90
92
  guard #available(iOS 27.1, *) else { return }
91
93
  DispatchQueue.main.async { [weak self] in
92
- self?.attachHingeInteraction()
94
+ guard let self else { return }
95
+ self.keyWindowObserver = NotificationCenter.default.addObserver(
96
+ forName: UIWindow.didBecomeKeyNotification,
97
+ object: nil,
98
+ queue: .main
99
+ ) { [weak self] notification in
100
+ guard let window = notification.object as? UIWindow else { return }
101
+ self?.attachHingeInteraction(in: window)
102
+ }
103
+ self.resignKeyWindowObserver = NotificationCenter.default.addObserver(
104
+ forName: UIWindow.didResignKeyNotification,
105
+ object: nil,
106
+ queue: .main
107
+ ) { [weak self] notification in
108
+ guard let self, let window = notification.object as? UIWindow,
109
+ self.hingeInteraction?.view === window else { return }
110
+ self.attachHingeInteraction(in: nil)
111
+ }
112
+ self.attachHingeInteraction(in: DualScreenDetector.keyWindow())
93
113
  }
94
114
  #endif
95
115
  }
96
116
 
97
117
  #if compiler(>=6.4)
98
118
  @available(iOS 27.1, *)
99
- private func attachHingeInteraction() {
100
- guard hingeObserver == nil else { return }
101
- guard let window = DualScreenDetector.keyWindow() else {
102
- // Native modules can initialize before the first window exists.
103
- hingeObserver = NotificationCenter.default.addObserver(
104
- forName: UIWindow.didBecomeKeyNotification,
105
- object: nil,
106
- queue: .main
107
- ) { [weak self] _ in
108
- guard let self, let observer = self.hingeObserver else { return }
109
- NotificationCenter.default.removeObserver(observer)
110
- self.hingeObserver = nil
111
- self.attachHingeInteraction()
112
- }
113
- return
119
+ private func attachHingeInteraction(in window: UIWindow?) {
120
+ if let interaction = hingeInteraction, interaction.view === window { return }
121
+ let previousInteraction = hingeInteraction
122
+ hingeInteraction = nil
123
+ if let interaction = previousInteraction {
124
+ interaction.view?.removeInteraction(interaction)
114
125
  }
115
- let interaction = UIHingeInteraction { [weak self] _, update in
116
- self?.handleHingeUpdate(update.hinge)
126
+ handleHingeUpdate(nil)
127
+ // A listener can change the key window while spanning is reset.
128
+ guard let window, window.isKeyWindow, hingeInteraction == nil else { return }
129
+ let interaction = UIHingeInteraction { [weak self] interaction, update in
130
+ guard let self, self.hingeInteraction === interaction else { return }
131
+ self.handleHingeUpdate(update.hinge)
117
132
  }
133
+ hingeInteraction = interaction
118
134
  window.addInteraction(interaction)
119
- hingeObserver = interaction
120
135
  }
121
136
 
122
137
  @available(iOS 27.1, *)
123
138
  private func handleHingeUpdate(_ hinge: UIHinge?) {
124
- // A nil hinge means the hierarchy no longer provides hinge updates;
125
- // keep the last known state.
126
- guard let hinge else { return }
127
- let nextSpanning = hinge.status == .fullyOpen || hinge.status == .partiallyOpen
139
+ let nextSpanning = hinge?.status == .fullyOpen || hinge?.status == .partiallyOpen
128
140
 
129
141
  lock.lock()
130
- let isNewlyDetected = !isDualScreen
131
- isDualScreen = true
142
+ let isNewlyDetected = hinge != nil && !isDualScreen
143
+ if hinge != nil { isDualScreen = true }
132
144
  let didChangeSpanning = spanning != nextSpanning
133
145
  spanning = nextSpanning
134
146
  let callbacks = Array(listeners.values)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/react-native-device-utils",
3
- "version": "6.7.0-alpha.392",
3
+ "version": "6.7.0-alpha.394",
4
4
  "description": "Native device, launch, system appearance and boot recovery utilities for OneKey",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",