@capawesome/capacitor-screen-orientation 2.0.1 → 2.0.2

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.
@@ -1,150 +1,171 @@
1
- import Foundation
2
- import UIKit
3
-
4
- @objc public class ScreenOrientation: NSObject {
5
- static var supportedInterfaceOrientations = UIInterfaceOrientationMask.all
6
- private let plugin: ScreenOrientationPlugin
7
- private var currentOrientationType: String?
8
- private var lastOrientationType: String?
9
-
10
- init(plugin: ScreenOrientationPlugin) {
11
- self.plugin = plugin
12
- super.init()
13
- NotificationCenter.default.addObserver(self, selector: #selector(self.handleOrientationChange), name: UIDevice.orientationDidChangeNotification, object: nil)
14
- }
15
-
16
- @objc public static func getSupportedInterfaceOrientations() -> UIInterfaceOrientationMask {
17
- return ScreenOrientation.supportedInterfaceOrientations
18
- }
19
-
20
- @objc public func lock(_ orientationType: String, completion: @escaping () -> Void) {
21
- DispatchQueue.main.async { [weak self] in
22
- guard let strongSelf = self else {
23
- return
24
- }
25
- let currentOrientationValue = UIDevice.current.orientation.rawValue
26
- let nextOrientationMask = strongSelf.convertOrientationTypeToMask(orientationType)
27
- let nextOrientationValue = strongSelf.convertOrientationTypeToValue(orientationType)
28
- UIDevice.current.setValue(nextOrientationValue, forKey: "orientation")
29
- ScreenOrientation.supportedInterfaceOrientations = nextOrientationMask
30
- UINavigationController.attemptRotationToDeviceOrientation()
31
- UIDevice.current.setValue(currentOrientationValue, forKey: "orientation")
32
- strongSelf.notifyOrientationChangeListeners(orientationType)
33
- completion()
34
- }
35
- }
36
-
37
- @objc public func unlock(completion: @escaping () -> Void) {
38
- DispatchQueue.main.async {
39
- ScreenOrientation.supportedInterfaceOrientations = UIInterfaceOrientationMask.all
40
- UINavigationController.attemptRotationToDeviceOrientation()
41
- guard let orientationType = self.lastOrientationType else {
42
- return
43
- }
44
- self.notifyOrientationChangeListeners(orientationType)
45
- completion()
46
- }
47
- }
48
-
49
- @objc public func getCachedCurrentOrientationType(completion: @escaping (String) -> Void) {
50
- guard let cachedOrientationType = self.currentOrientationType else {
51
- self.getUncachedCurrentOrientationType(completion: completion)
52
- return
53
- }
54
- completion(cachedOrientationType)
55
- }
56
-
57
- @objc private func getUncachedCurrentOrientationType(completion: @escaping (String) -> Void) {
58
- DispatchQueue.main.async {
59
- let orientationValue = UIDevice.current.orientation.rawValue
60
- let orientationType = self.convertOrientationValueToType(orientationValue)
61
- completion(orientationType)
62
- }
63
- }
64
-
65
- @objc private func isCurrentOrientationValid() -> Bool {
66
- return UIDevice.current.orientation.isValidInterfaceOrientation
67
- }
68
-
69
- @objc private func handleOrientationChange() {
70
- let isValid = self.isCurrentOrientationValid()
71
- guard isValid else {
72
- return
73
- }
74
- self.getUncachedCurrentOrientationType(completion: { orientationType in
75
- self.lastOrientationType = orientationType
76
- guard ScreenOrientation.supportedInterfaceOrientations == UIInterfaceOrientationMask.all else {
77
- return
78
- }
79
- self.notifyOrientationChangeListeners(orientationType)
80
- })
81
- }
82
-
83
- @objc private func notifyOrientationChangeListeners(_ orientationType: String) {
84
- self.currentOrientationType = orientationType
85
- self.plugin.notifyOrientationChangeListeners(orientationType)
86
- }
87
-
88
- @objc private func convertOrientationTypeToMask(_ orientationType: String) -> UIInterfaceOrientationMask {
89
- switch orientationType {
90
- case "landscape":
91
- return UIInterfaceOrientationMask.landscapeRight
92
- case "landscape-primary":
93
- return UIInterfaceOrientationMask.landscapeLeft
94
- case "landscape-secondary":
95
- return UIInterfaceOrientationMask.landscapeRight
96
- case "portrait":
97
- return UIInterfaceOrientationMask.portrait
98
- case "portrait-primary":
99
- return UIInterfaceOrientationMask.portrait
100
- case "portrait-secondary":
101
- return UIInterfaceOrientationMask.portraitUpsideDown
102
- default:
103
- return UIInterfaceOrientationMask.all
104
- }
105
- }
106
-
107
- @objc private func convertOrientationTypeToValue(_ orientationType: String) -> Int {
108
- switch orientationType {
109
- case "landscape":
110
- return UIInterfaceOrientation.landscapeRight.rawValue
111
- case "landscape-primary":
112
- return UIInterfaceOrientation.landscapeLeft.rawValue
113
- case "landscape-secondary":
114
- return UIInterfaceOrientation.landscapeRight.rawValue
115
- case "portrait":
116
- return UIInterfaceOrientation.portrait.rawValue
117
- case "portrait-primary":
118
- return UIInterfaceOrientation.portrait.rawValue
119
- case "portrait-secondary":
120
- return UIInterfaceOrientation.portraitUpsideDown.rawValue
121
- default:
122
- return UIInterfaceOrientation.unknown.rawValue
123
- }
124
- }
125
-
126
- @objc private func convertOrientationValueToType(_ orientationValue: Int) -> String {
127
- switch orientationValue {
128
- case UIInterfaceOrientation.landscapeLeft.rawValue:
129
- return "landscape-primary"
130
- case UIInterfaceOrientation.landscapeRight.rawValue:
131
- return "landscape-secondary"
132
- case UIInterfaceOrientation.portrait.rawValue:
133
- return "portrait-primary"
134
- case UIInterfaceOrientation.portraitUpsideDown.rawValue:
135
- return "portrait-secondary"
136
- default:
137
- var isPortrait = false
138
- if #available(iOS 13.0, *) {
139
- isPortrait = UIApplication.shared.windows
140
- .first?
141
- .windowScene?
142
- .interfaceOrientation
143
- .isPortrait ?? false
144
- } else {
145
- isPortrait = UIApplication.shared.statusBarOrientation.isPortrait
146
- }
147
- return isPortrait ? "portrait-primary" : "landscape-primary"
148
- }
149
- }
150
- }
1
+ import Foundation
2
+ import UIKit
3
+ import Capacitor
4
+
5
+ @objc public class ScreenOrientation: NSObject {
6
+ static var supportedInterfaceOrientations = UIInterfaceOrientationMask.all
7
+ private let plugin: ScreenOrientationPlugin
8
+ private var currentOrientationType: String?
9
+ private var lastOrientationType: String?
10
+
11
+ init(plugin: ScreenOrientationPlugin) {
12
+ self.plugin = plugin
13
+ super.init()
14
+ NotificationCenter.default.addObserver(self, selector: #selector(self.handleOrientationChange), name: UIDevice.orientationDidChangeNotification, object: nil)
15
+ }
16
+
17
+ @objc public static func getSupportedInterfaceOrientations() -> UIInterfaceOrientationMask {
18
+ return ScreenOrientation.supportedInterfaceOrientations
19
+ }
20
+
21
+ @objc public func lock(_ orientationType: String, completion: @escaping () -> Void) {
22
+ DispatchQueue.main.async { [weak self] in
23
+ guard let strongSelf = self else {
24
+ return
25
+ }
26
+ let currentOrientationValue = UIDevice.current.orientation.rawValue
27
+ let currentOrientationMask = strongSelf.convertOrientationValueToMask(currentOrientationValue)
28
+ let nextOrientationMask = strongSelf.convertOrientationTypeToMask(orientationType)
29
+ let nextOrientationValue = strongSelf.convertOrientationTypeToValue(orientationType)
30
+ strongSelf.requestGeometryUpdate(orientationValue: nextOrientationValue, orientationMask: nextOrientationMask)
31
+ ScreenOrientation.supportedInterfaceOrientations = nextOrientationMask
32
+ UINavigationController.attemptRotationToDeviceOrientation()
33
+ strongSelf.requestGeometryUpdate(orientationValue: currentOrientationValue, orientationMask: currentOrientationMask)
34
+ strongSelf.notifyOrientationChangeListeners(orientationType)
35
+ completion()
36
+ }
37
+ }
38
+
39
+ @objc public func unlock(completion: @escaping () -> Void) {
40
+ DispatchQueue.main.async {
41
+ ScreenOrientation.supportedInterfaceOrientations = UIInterfaceOrientationMask.all
42
+ UINavigationController.attemptRotationToDeviceOrientation()
43
+ guard let orientationType = self.lastOrientationType else {
44
+ return
45
+ }
46
+ self.notifyOrientationChangeListeners(orientationType)
47
+ completion()
48
+ }
49
+ }
50
+
51
+ @objc public func getCachedCurrentOrientationType(completion: @escaping (String) -> Void) {
52
+ guard let cachedOrientationType = self.currentOrientationType else {
53
+ self.getUncachedCurrentOrientationType(completion: completion)
54
+ return
55
+ }
56
+ completion(cachedOrientationType)
57
+ }
58
+
59
+ @objc private func requestGeometryUpdate(orientationValue: Int, orientationMask: UIInterfaceOrientationMask) {
60
+ if #available(iOS 16, *) {
61
+ let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
62
+ windowScene?.keyWindow?.rootViewController?.setNeedsUpdateOfSupportedInterfaceOrientations()
63
+ windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: orientationMask)) { error in
64
+ CAPLog.print("requestGeometryUpdate failed.", error)
65
+ }
66
+ } else {
67
+ UIDevice.current.setValue(orientationValue, forKey: "orientation")
68
+ }
69
+ }
70
+
71
+ @objc private func getUncachedCurrentOrientationType(completion: @escaping (String) -> Void) {
72
+ DispatchQueue.main.async {
73
+ let orientationValue = UIDevice.current.orientation.rawValue
74
+ let orientationType = self.convertOrientationValueToType(orientationValue)
75
+ completion(orientationType)
76
+ }
77
+ }
78
+
79
+ @objc private func isCurrentOrientationValid() -> Bool {
80
+ return UIDevice.current.orientation.isValidInterfaceOrientation
81
+ }
82
+
83
+ @objc private func handleOrientationChange() {
84
+ let isValid = self.isCurrentOrientationValid()
85
+ guard isValid else {
86
+ return
87
+ }
88
+ self.getUncachedCurrentOrientationType(completion: { orientationType in
89
+ self.lastOrientationType = orientationType
90
+ guard ScreenOrientation.supportedInterfaceOrientations == UIInterfaceOrientationMask.all else {
91
+ return
92
+ }
93
+ self.notifyOrientationChangeListeners(orientationType)
94
+ })
95
+ }
96
+
97
+ @objc private func notifyOrientationChangeListeners(_ orientationType: String) {
98
+ self.currentOrientationType = orientationType
99
+ self.plugin.notifyOrientationChangeListeners(orientationType)
100
+ }
101
+
102
+ @objc private func convertOrientationValueToMask(_ orientationValue: Int) -> UIInterfaceOrientationMask {
103
+ switch orientationValue {
104
+ case UIInterfaceOrientation.landscapeLeft.rawValue:
105
+ return UIInterfaceOrientationMask.landscapeLeft
106
+ case UIInterfaceOrientation.landscapeRight.rawValue:
107
+ return UIInterfaceOrientationMask.landscapeRight
108
+ case UIInterfaceOrientation.portrait.rawValue:
109
+ return UIInterfaceOrientationMask.portrait
110
+ case UIInterfaceOrientation.portraitUpsideDown.rawValue:
111
+ return UIInterfaceOrientationMask.portraitUpsideDown
112
+ default:
113
+ let isPortrait = UIApplication.shared.windows.first?.windowScene?.interfaceOrientation.isPortrait ?? false
114
+ return isPortrait ? UIInterfaceOrientationMask.portrait : UIInterfaceOrientationMask.landscapeLeft
115
+ }
116
+ }
117
+
118
+ @objc private func convertOrientationTypeToMask(_ orientationType: String) -> UIInterfaceOrientationMask {
119
+ switch orientationType {
120
+ case "landscape":
121
+ return UIInterfaceOrientationMask.landscapeRight
122
+ case "landscape-primary":
123
+ return UIInterfaceOrientationMask.landscapeLeft
124
+ case "landscape-secondary":
125
+ return UIInterfaceOrientationMask.landscapeRight
126
+ case "portrait":
127
+ return UIInterfaceOrientationMask.portrait
128
+ case "portrait-primary":
129
+ return UIInterfaceOrientationMask.portrait
130
+ case "portrait-secondary":
131
+ return UIInterfaceOrientationMask.portraitUpsideDown
132
+ default:
133
+ return UIInterfaceOrientationMask.all
134
+ }
135
+ }
136
+
137
+ @objc private func convertOrientationTypeToValue(_ orientationType: String) -> Int {
138
+ switch orientationType {
139
+ case "landscape":
140
+ return UIInterfaceOrientation.landscapeRight.rawValue
141
+ case "landscape-primary":
142
+ return UIInterfaceOrientation.landscapeLeft.rawValue
143
+ case "landscape-secondary":
144
+ return UIInterfaceOrientation.landscapeRight.rawValue
145
+ case "portrait":
146
+ return UIInterfaceOrientation.portrait.rawValue
147
+ case "portrait-primary":
148
+ return UIInterfaceOrientation.portrait.rawValue
149
+ case "portrait-secondary":
150
+ return UIInterfaceOrientation.portraitUpsideDown.rawValue
151
+ default:
152
+ return UIInterfaceOrientation.unknown.rawValue
153
+ }
154
+ }
155
+
156
+ @objc private func convertOrientationValueToType(_ orientationValue: Int) -> String {
157
+ switch orientationValue {
158
+ case UIInterfaceOrientation.landscapeLeft.rawValue:
159
+ return "landscape-primary"
160
+ case UIInterfaceOrientation.landscapeRight.rawValue:
161
+ return "landscape-secondary"
162
+ case UIInterfaceOrientation.portrait.rawValue:
163
+ return "portrait-primary"
164
+ case UIInterfaceOrientation.portraitUpsideDown.rawValue:
165
+ return "portrait-secondary"
166
+ default:
167
+ let isPortrait = UIApplication.shared.windows.first?.windowScene?.interfaceOrientation.isPortrait ?? false
168
+ return isPortrait ? "portrait-primary" : "landscape-primary"
169
+ }
170
+ }
171
+ }
@@ -1,10 +1,10 @@
1
- #import <UIKit/UIKit.h>
2
-
3
- //! Project version number for Plugin.
4
- FOUNDATION_EXPORT double PluginVersionNumber;
5
-
6
- //! Project version string for Plugin.
7
- FOUNDATION_EXPORT const unsigned char PluginVersionString[];
8
-
9
- // In this header, you should import all the public headers of your framework using statements like #import <Plugin/PublicHeader.h>
10
-
1
+ #import <UIKit/UIKit.h>
2
+
3
+ //! Project version number for Plugin.
4
+ FOUNDATION_EXPORT double PluginVersionNumber;
5
+
6
+ //! Project version string for Plugin.
7
+ FOUNDATION_EXPORT const unsigned char PluginVersionString[];
8
+
9
+ // In this header, you should import all the public headers of your framework using statements like #import <Plugin/PublicHeader.h>
10
+
@@ -1,11 +1,11 @@
1
- #import <Foundation/Foundation.h>
2
- #import <Capacitor/Capacitor.h>
3
-
4
- // Define the plugin using the CAP_PLUGIN Macro, and
5
- // each method the plugin supports using the CAP_PLUGIN_METHOD macro.
6
- CAP_PLUGIN(ScreenOrientationPlugin, "ScreenOrientation",
7
- CAP_PLUGIN_METHOD(lock, CAPPluginReturnPromise);
8
- CAP_PLUGIN_METHOD(unlock, CAPPluginReturnPromise);
9
- CAP_PLUGIN_METHOD(getCurrentOrientation, CAPPluginReturnPromise);
10
- CAP_PLUGIN_METHOD(removeAllListeners, CAPPluginReturnNone);
11
- )
1
+ #import <Foundation/Foundation.h>
2
+ #import <Capacitor/Capacitor.h>
3
+
4
+ // Define the plugin using the CAP_PLUGIN Macro, and
5
+ // each method the plugin supports using the CAP_PLUGIN_METHOD macro.
6
+ CAP_PLUGIN(ScreenOrientationPlugin, "ScreenOrientation",
7
+ CAP_PLUGIN_METHOD(lock, CAPPluginReturnPromise);
8
+ CAP_PLUGIN_METHOD(unlock, CAPPluginReturnPromise);
9
+ CAP_PLUGIN_METHOD(getCurrentOrientation, CAPPluginReturnPromise);
10
+ CAP_PLUGIN_METHOD(removeAllListeners, CAPPluginReturnNone);
11
+ )
@@ -1,47 +1,47 @@
1
- import Foundation
2
- import Capacitor
3
-
4
- /**
5
- * Please read the Capacitor iOS Plugin Development Guide
6
- * here: https://capacitorjs.com/docs/plugins/ios
7
- */
8
- @objc(ScreenOrientationPlugin)
9
- public class ScreenOrientationPlugin: CAPPlugin {
10
- public let screenOrientationChangeEvent = "screenOrientationChange"
11
- private var implementation: ScreenOrientation?
12
-
13
- override public func load() {
14
- self.implementation = ScreenOrientation(plugin: self)
15
- }
16
-
17
- deinit {
18
- NotificationCenter.default.removeObserver(self)
19
- }
20
-
21
- @objc func lock(_ call: CAPPluginCall) {
22
- let orientationType = call.getString("type") ?? ""
23
- implementation?.lock(orientationType, completion: {
24
- call.resolve()
25
- })
26
- }
27
-
28
- @objc func unlock(_ call: CAPPluginCall) {
29
- implementation?.unlock(completion: {
30
- call.resolve()
31
- })
32
- }
33
-
34
- @objc func getCurrentOrientation(_ call: CAPPluginCall) {
35
- implementation?.getCachedCurrentOrientationType(completion: { orientationType in
36
- call.resolve([
37
- "type": orientationType
38
- ])
39
- })
40
- }
41
-
42
- @objc func notifyOrientationChangeListeners(_ orientationType: String) {
43
- self.notifyListeners(self.screenOrientationChangeEvent, data: [
44
- "type": orientationType
45
- ])
46
- }
47
- }
1
+ import Foundation
2
+ import Capacitor
3
+
4
+ /**
5
+ * Please read the Capacitor iOS Plugin Development Guide
6
+ * here: https://capacitorjs.com/docs/plugins/ios
7
+ */
8
+ @objc(ScreenOrientationPlugin)
9
+ public class ScreenOrientationPlugin: CAPPlugin {
10
+ public let screenOrientationChangeEvent = "screenOrientationChange"
11
+ private var implementation: ScreenOrientation?
12
+
13
+ override public func load() {
14
+ self.implementation = ScreenOrientation(plugin: self)
15
+ }
16
+
17
+ deinit {
18
+ NotificationCenter.default.removeObserver(self)
19
+ }
20
+
21
+ @objc func lock(_ call: CAPPluginCall) {
22
+ let orientationType = call.getString("type") ?? ""
23
+ implementation?.lock(orientationType, completion: {
24
+ call.resolve()
25
+ })
26
+ }
27
+
28
+ @objc func unlock(_ call: CAPPluginCall) {
29
+ implementation?.unlock(completion: {
30
+ call.resolve()
31
+ })
32
+ }
33
+
34
+ @objc func getCurrentOrientation(_ call: CAPPluginCall) {
35
+ implementation?.getCachedCurrentOrientationType(completion: { orientationType in
36
+ call.resolve([
37
+ "type": orientationType
38
+ ])
39
+ })
40
+ }
41
+
42
+ @objc func notifyOrientationChangeListeners(_ orientationType: String) {
43
+ self.notifyListeners(self.screenOrientationChangeEvent, data: [
44
+ "type": orientationType
45
+ ])
46
+ }
47
+ }