@matiks/rn-app-state 1.0.0
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.
- package/FLOW.md +239 -0
- package/NitroAppState.podspec +31 -0
- package/README.md +72 -0
- package/android/CMakeLists.txt +29 -0
- package/android/build.gradle +140 -0
- package/android/fix-prefab.gradle +51 -0
- package/android/gradle.properties +5 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/cpp/cpp-adapter.cpp +6 -0
- package/android/src/main/java/com/margelo/nitro/appstate/HybridAppState.kt +188 -0
- package/android/src/main/java/com/margelo/nitro/appstate/NitroAppStatePackage.kt +18 -0
- package/ios/HybridAppState.swift +224 -0
- package/nitro.json +24 -0
- package/nitrogen/generated/.gitattributes +1 -0
- package/nitrogen/generated/android/NitroAppState+autolinking.cmake +81 -0
- package/nitrogen/generated/android/NitroAppState+autolinking.gradle +27 -0
- package/nitrogen/generated/android/NitroAppStateOnLoad.cpp +48 -0
- package/nitrogen/generated/android/NitroAppStateOnLoad.hpp +25 -0
- package/nitrogen/generated/android/c++/JAppStateChangeEvent.hpp +57 -0
- package/nitrogen/generated/android/c++/JAppStateListener.hpp +67 -0
- package/nitrogen/generated/android/c++/JFunc_void.hpp +75 -0
- package/nitrogen/generated/android/c++/JFunc_void_AppStateChangeEvent.hpp +78 -0
- package/nitrogen/generated/android/c++/JHybridAppStateModuleSpec.cpp +84 -0
- package/nitrogen/generated/android/c++/JHybridAppStateModuleSpec.hpp +68 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/appstate/AppStateChangeEvent.kt +38 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/appstate/AppStateListener.kt +42 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/appstate/Func_void.kt +80 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/appstate/Func_void_AppStateChangeEvent.kt +80 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/appstate/HybridAppStateModuleSpec.kt +72 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/appstate/NitroAppStateOnLoad.kt +35 -0
- package/nitrogen/generated/ios/NitroAppState+autolinking.rb +60 -0
- package/nitrogen/generated/ios/NitroAppState-Swift-Cxx-Bridge.cpp +49 -0
- package/nitrogen/generated/ios/NitroAppState-Swift-Cxx-Bridge.hpp +112 -0
- package/nitrogen/generated/ios/NitroAppState-Swift-Cxx-Umbrella.hpp +51 -0
- package/nitrogen/generated/ios/NitroAppStateAutolinking.mm +33 -0
- package/nitrogen/generated/ios/NitroAppStateAutolinking.swift +26 -0
- package/nitrogen/generated/ios/c++/HybridAppStateModuleSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridAppStateModuleSpecSwift.hpp +106 -0
- package/nitrogen/generated/ios/swift/AppStateChangeEvent.swift +29 -0
- package/nitrogen/generated/ios/swift/AppStateListener.swift +37 -0
- package/nitrogen/generated/ios/swift/Func_void.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_AppStateChangeEvent.swift +46 -0
- package/nitrogen/generated/ios/swift/HybridAppStateModuleSpec.swift +57 -0
- package/nitrogen/generated/ios/swift/HybridAppStateModuleSpec_cxx.swift +172 -0
- package/nitrogen/generated/shared/c++/AppStateChangeEvent.hpp +83 -0
- package/nitrogen/generated/shared/c++/AppStateListener.hpp +83 -0
- package/nitrogen/generated/shared/c++/HybridAppStateModuleSpec.cpp +24 -0
- package/nitrogen/generated/shared/c++/HybridAppStateModuleSpec.hpp +70 -0
- package/package.json +54 -0
- package/src/index.ts +2 -0
- package/src/specs/AppState.nitro.ts +57 -0
- package/src/useNitroAppState/index.ts +2 -0
- package/src/useNitroAppState/types.ts +21 -0
- package/src/useNitroAppState/useNitroAppState.native.ts +142 -0
- package/src/useNitroAppState/useNitroAppState.web.ts +56 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// Func_void_AppStateChangeEvent.swift
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
import NitroModules
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Wraps a Swift `(_ event: AppStateChangeEvent) -> Void` as a class.
|
|
12
|
+
* This class can be used from C++, e.g. to wrap the Swift closure as a `std::function`.
|
|
13
|
+
*/
|
|
14
|
+
public final class Func_void_AppStateChangeEvent {
|
|
15
|
+
public typealias bridge = margelo.nitro.appstate.bridge.swift
|
|
16
|
+
|
|
17
|
+
private let closure: (_ event: AppStateChangeEvent) -> Void
|
|
18
|
+
|
|
19
|
+
public init(_ closure: @escaping (_ event: AppStateChangeEvent) -> Void) {
|
|
20
|
+
self.closure = closure
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
@inline(__always)
|
|
24
|
+
public func call(event: AppStateChangeEvent) -> Void {
|
|
25
|
+
self.closure(event)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Casts this instance to a retained unsafe raw pointer.
|
|
30
|
+
* This acquires one additional strong reference on the object!
|
|
31
|
+
*/
|
|
32
|
+
@inline(__always)
|
|
33
|
+
public func toUnsafe() -> UnsafeMutableRawPointer {
|
|
34
|
+
return Unmanaged.passRetained(self).toOpaque()
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Casts an unsafe pointer to a `Func_void_AppStateChangeEvent`.
|
|
39
|
+
* The pointer has to be a retained opaque `Unmanaged<Func_void_AppStateChangeEvent>`.
|
|
40
|
+
* This removes one strong reference from the object!
|
|
41
|
+
*/
|
|
42
|
+
@inline(__always)
|
|
43
|
+
public static func fromUnsafe(_ pointer: UnsafeMutableRawPointer) -> Func_void_AppStateChangeEvent {
|
|
44
|
+
return Unmanaged<Func_void_AppStateChangeEvent>.fromOpaque(pointer).takeRetainedValue()
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// HybridAppStateModuleSpec.swift
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
import NitroModules
|
|
9
|
+
|
|
10
|
+
/// See ``HybridAppStateModuleSpec``
|
|
11
|
+
public protocol HybridAppStateModuleSpec_protocol: HybridObject {
|
|
12
|
+
// Properties
|
|
13
|
+
var wasKilledLastSession: Bool { get }
|
|
14
|
+
|
|
15
|
+
// Methods
|
|
16
|
+
func getCurrentState() throws -> String
|
|
17
|
+
func addListener(onStateChange: @escaping (_ event: AppStateChangeEvent) -> Void) throws -> AppStateListener
|
|
18
|
+
func getStateOnNotification() throws -> String
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public extension HybridAppStateModuleSpec_protocol {
|
|
22
|
+
/// Default implementation of ``HybridObject.toString``
|
|
23
|
+
func toString() -> String {
|
|
24
|
+
return "[HybridObject AppStateModule]"
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/// See ``HybridAppStateModuleSpec``
|
|
29
|
+
open class HybridAppStateModuleSpec_base {
|
|
30
|
+
private weak var cxxWrapper: HybridAppStateModuleSpec_cxx? = nil
|
|
31
|
+
public init() { }
|
|
32
|
+
public func getCxxWrapper() -> HybridAppStateModuleSpec_cxx {
|
|
33
|
+
#if DEBUG
|
|
34
|
+
guard self is any HybridAppStateModuleSpec else {
|
|
35
|
+
fatalError("`self` is not a `HybridAppStateModuleSpec`! Did you accidentally inherit from `HybridAppStateModuleSpec_base` instead of `HybridAppStateModuleSpec`?")
|
|
36
|
+
}
|
|
37
|
+
#endif
|
|
38
|
+
if let cxxWrapper = self.cxxWrapper {
|
|
39
|
+
return cxxWrapper
|
|
40
|
+
} else {
|
|
41
|
+
let cxxWrapper = HybridAppStateModuleSpec_cxx(self as! any HybridAppStateModuleSpec)
|
|
42
|
+
self.cxxWrapper = cxxWrapper
|
|
43
|
+
return cxxWrapper
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* A Swift base-protocol representing the AppStateModule HybridObject.
|
|
50
|
+
* Implement this protocol to create Swift-based instances of AppStateModule.
|
|
51
|
+
* ```swift
|
|
52
|
+
* class HybridAppStateModule : HybridAppStateModuleSpec {
|
|
53
|
+
* // ...
|
|
54
|
+
* }
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
public typealias HybridAppStateModuleSpec = HybridAppStateModuleSpec_protocol & HybridAppStateModuleSpec_base
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// HybridAppStateModuleSpec_cxx.swift
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
import NitroModules
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A class implementation that bridges HybridAppStateModuleSpec over to C++.
|
|
12
|
+
* In C++, we cannot use Swift protocols - so we need to wrap it in a class to make it strongly defined.
|
|
13
|
+
*
|
|
14
|
+
* Also, some Swift types need to be bridged with special handling:
|
|
15
|
+
* - Enums need to be wrapped in Structs, otherwise they cannot be accessed bi-directionally (Swift bug: https://github.com/swiftlang/swift/issues/75330)
|
|
16
|
+
* - Other HybridObjects need to be wrapped/unwrapped from the Swift TCxx wrapper
|
|
17
|
+
* - Throwing methods need to be wrapped with a Result<T, Error> type, as exceptions cannot be propagated to C++
|
|
18
|
+
*/
|
|
19
|
+
open class HybridAppStateModuleSpec_cxx {
|
|
20
|
+
/**
|
|
21
|
+
* The Swift <> C++ bridge's namespace (`margelo::nitro::appstate::bridge::swift`)
|
|
22
|
+
* from `NitroAppState-Swift-Cxx-Bridge.hpp`.
|
|
23
|
+
* This contains specialized C++ templates, and C++ helper functions that can be accessed from Swift.
|
|
24
|
+
*/
|
|
25
|
+
public typealias bridge = margelo.nitro.appstate.bridge.swift
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Holds an instance of the `HybridAppStateModuleSpec` Swift protocol.
|
|
29
|
+
*/
|
|
30
|
+
private var __implementation: any HybridAppStateModuleSpec
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Holds a weak pointer to the C++ class that wraps the Swift class.
|
|
34
|
+
*/
|
|
35
|
+
private var __cxxPart: bridge.std__weak_ptr_HybridAppStateModuleSpec_
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Create a new `HybridAppStateModuleSpec_cxx` that wraps the given `HybridAppStateModuleSpec`.
|
|
39
|
+
* All properties and methods bridge to C++ types.
|
|
40
|
+
*/
|
|
41
|
+
public init(_ implementation: any HybridAppStateModuleSpec) {
|
|
42
|
+
self.__implementation = implementation
|
|
43
|
+
self.__cxxPart = .init()
|
|
44
|
+
/* no base class */
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Get the actual `HybridAppStateModuleSpec` instance this class wraps.
|
|
49
|
+
*/
|
|
50
|
+
@inline(__always)
|
|
51
|
+
public func getHybridAppStateModuleSpec() -> any HybridAppStateModuleSpec {
|
|
52
|
+
return __implementation
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Casts this instance to a retained unsafe raw pointer.
|
|
57
|
+
* This acquires one additional strong reference on the object!
|
|
58
|
+
*/
|
|
59
|
+
public func toUnsafe() -> UnsafeMutableRawPointer {
|
|
60
|
+
return Unmanaged.passRetained(self).toOpaque()
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Casts an unsafe pointer to a `HybridAppStateModuleSpec_cxx`.
|
|
65
|
+
* The pointer has to be a retained opaque `Unmanaged<HybridAppStateModuleSpec_cxx>`.
|
|
66
|
+
* This removes one strong reference from the object!
|
|
67
|
+
*/
|
|
68
|
+
public class func fromUnsafe(_ pointer: UnsafeMutableRawPointer) -> HybridAppStateModuleSpec_cxx {
|
|
69
|
+
return Unmanaged<HybridAppStateModuleSpec_cxx>.fromOpaque(pointer).takeRetainedValue()
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Gets (or creates) the C++ part of this Hybrid Object.
|
|
74
|
+
* The C++ part is a `std::shared_ptr<HybridAppStateModuleSpec>`.
|
|
75
|
+
*/
|
|
76
|
+
public func getCxxPart() -> bridge.std__shared_ptr_HybridAppStateModuleSpec_ {
|
|
77
|
+
let cachedCxxPart = self.__cxxPart.lock()
|
|
78
|
+
if Bool(fromCxx: cachedCxxPart) {
|
|
79
|
+
return cachedCxxPart
|
|
80
|
+
} else {
|
|
81
|
+
let newCxxPart = bridge.create_std__shared_ptr_HybridAppStateModuleSpec_(self.toUnsafe())
|
|
82
|
+
__cxxPart = bridge.weakify_std__shared_ptr_HybridAppStateModuleSpec_(newCxxPart)
|
|
83
|
+
return newCxxPart
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Get the memory size of the Swift class (plus size of any other allocations)
|
|
91
|
+
* so the JS VM can properly track it and garbage-collect the JS object if needed.
|
|
92
|
+
*/
|
|
93
|
+
@inline(__always)
|
|
94
|
+
public var memorySize: Int {
|
|
95
|
+
return MemoryHelper.getSizeOf(self.__implementation) + self.__implementation.memorySize
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Compares this object with the given [other] object for reference equality.
|
|
100
|
+
*/
|
|
101
|
+
@inline(__always)
|
|
102
|
+
public func equals(other: HybridAppStateModuleSpec_cxx) -> Bool {
|
|
103
|
+
return self.__implementation === other.__implementation
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Call dispose() on the Swift class.
|
|
108
|
+
* This _may_ be called manually from JS.
|
|
109
|
+
*/
|
|
110
|
+
@inline(__always)
|
|
111
|
+
public func dispose() {
|
|
112
|
+
self.__implementation.dispose()
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Call toString() on the Swift class.
|
|
117
|
+
*/
|
|
118
|
+
@inline(__always)
|
|
119
|
+
public func toString() -> String {
|
|
120
|
+
return self.__implementation.toString()
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Properties
|
|
124
|
+
public final var wasKilledLastSession: Bool {
|
|
125
|
+
@inline(__always)
|
|
126
|
+
get {
|
|
127
|
+
return self.__implementation.wasKilledLastSession
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Methods
|
|
132
|
+
@inline(__always)
|
|
133
|
+
public final func getCurrentState() -> bridge.Result_std__string_ {
|
|
134
|
+
do {
|
|
135
|
+
let __result = try self.__implementation.getCurrentState()
|
|
136
|
+
let __resultCpp = std.string(__result)
|
|
137
|
+
return bridge.create_Result_std__string_(__resultCpp)
|
|
138
|
+
} catch (let __error) {
|
|
139
|
+
let __exceptionPtr = __error.toCpp()
|
|
140
|
+
return bridge.create_Result_std__string_(__exceptionPtr)
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
@inline(__always)
|
|
145
|
+
public final func addListener(onStateChange: bridge.Func_void_AppStateChangeEvent) -> bridge.Result_AppStateListener_ {
|
|
146
|
+
do {
|
|
147
|
+
let __result = try self.__implementation.addListener(onStateChange: { () -> (AppStateChangeEvent) -> Void in
|
|
148
|
+
let __wrappedFunction = bridge.wrap_Func_void_AppStateChangeEvent(onStateChange)
|
|
149
|
+
return { (__event: AppStateChangeEvent) -> Void in
|
|
150
|
+
__wrappedFunction.call(__event)
|
|
151
|
+
}
|
|
152
|
+
}())
|
|
153
|
+
let __resultCpp = __result
|
|
154
|
+
return bridge.create_Result_AppStateListener_(__resultCpp)
|
|
155
|
+
} catch (let __error) {
|
|
156
|
+
let __exceptionPtr = __error.toCpp()
|
|
157
|
+
return bridge.create_Result_AppStateListener_(__exceptionPtr)
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
@inline(__always)
|
|
162
|
+
public final func getStateOnNotification() -> bridge.Result_std__string_ {
|
|
163
|
+
do {
|
|
164
|
+
let __result = try self.__implementation.getStateOnNotification()
|
|
165
|
+
let __resultCpp = std.string(__result)
|
|
166
|
+
return bridge.create_Result_std__string_(__resultCpp)
|
|
167
|
+
} catch (let __error) {
|
|
168
|
+
let __exceptionPtr = __error.toCpp()
|
|
169
|
+
return bridge.create_Result_std__string_(__exceptionPtr)
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// AppStateChangeEvent.hpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#if __has_include(<NitroModules/JSIConverter.hpp>)
|
|
11
|
+
#include <NitroModules/JSIConverter.hpp>
|
|
12
|
+
#else
|
|
13
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
14
|
+
#endif
|
|
15
|
+
#if __has_include(<NitroModules/NitroDefines.hpp>)
|
|
16
|
+
#include <NitroModules/NitroDefines.hpp>
|
|
17
|
+
#else
|
|
18
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
19
|
+
#endif
|
|
20
|
+
#if __has_include(<NitroModules/JSIHelpers.hpp>)
|
|
21
|
+
#include <NitroModules/JSIHelpers.hpp>
|
|
22
|
+
#else
|
|
23
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
24
|
+
#endif
|
|
25
|
+
#if __has_include(<NitroModules/PropNameIDCache.hpp>)
|
|
26
|
+
#include <NitroModules/PropNameIDCache.hpp>
|
|
27
|
+
#else
|
|
28
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
29
|
+
#endif
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
#include <string>
|
|
34
|
+
|
|
35
|
+
namespace margelo::nitro::appstate {
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* A struct which can be represented as a JavaScript object (AppStateChangeEvent).
|
|
39
|
+
*/
|
|
40
|
+
struct AppStateChangeEvent final {
|
|
41
|
+
public:
|
|
42
|
+
std::string state SWIFT_PRIVATE;
|
|
43
|
+
|
|
44
|
+
public:
|
|
45
|
+
AppStateChangeEvent() = default;
|
|
46
|
+
explicit AppStateChangeEvent(std::string state): state(state) {}
|
|
47
|
+
|
|
48
|
+
public:
|
|
49
|
+
friend bool operator==(const AppStateChangeEvent& lhs, const AppStateChangeEvent& rhs) = default;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
} // namespace margelo::nitro::appstate
|
|
53
|
+
|
|
54
|
+
namespace margelo::nitro {
|
|
55
|
+
|
|
56
|
+
// C++ AppStateChangeEvent <> JS AppStateChangeEvent (object)
|
|
57
|
+
template <>
|
|
58
|
+
struct JSIConverter<margelo::nitro::appstate::AppStateChangeEvent> final {
|
|
59
|
+
static inline margelo::nitro::appstate::AppStateChangeEvent fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
|
|
60
|
+
jsi::Object obj = arg.asObject(runtime);
|
|
61
|
+
return margelo::nitro::appstate::AppStateChangeEvent(
|
|
62
|
+
JSIConverter<std::string>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "state")))
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::appstate::AppStateChangeEvent& arg) {
|
|
66
|
+
jsi::Object obj(runtime);
|
|
67
|
+
obj.setProperty(runtime, PropNameIDCache::get(runtime, "state"), JSIConverter<std::string>::toJSI(runtime, arg.state));
|
|
68
|
+
return obj;
|
|
69
|
+
}
|
|
70
|
+
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
|
|
71
|
+
if (!value.isObject()) {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
jsi::Object obj = value.getObject(runtime);
|
|
75
|
+
if (!nitro::isPlainObject(runtime, obj)) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
if (!JSIConverter<std::string>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "state")))) return false;
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
} // namespace margelo::nitro
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// AppStateListener.hpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#if __has_include(<NitroModules/JSIConverter.hpp>)
|
|
11
|
+
#include <NitroModules/JSIConverter.hpp>
|
|
12
|
+
#else
|
|
13
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
14
|
+
#endif
|
|
15
|
+
#if __has_include(<NitroModules/NitroDefines.hpp>)
|
|
16
|
+
#include <NitroModules/NitroDefines.hpp>
|
|
17
|
+
#else
|
|
18
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
19
|
+
#endif
|
|
20
|
+
#if __has_include(<NitroModules/JSIHelpers.hpp>)
|
|
21
|
+
#include <NitroModules/JSIHelpers.hpp>
|
|
22
|
+
#else
|
|
23
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
24
|
+
#endif
|
|
25
|
+
#if __has_include(<NitroModules/PropNameIDCache.hpp>)
|
|
26
|
+
#include <NitroModules/PropNameIDCache.hpp>
|
|
27
|
+
#else
|
|
28
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
29
|
+
#endif
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
#include <functional>
|
|
34
|
+
|
|
35
|
+
namespace margelo::nitro::appstate {
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* A struct which can be represented as a JavaScript object (AppStateListener).
|
|
39
|
+
*/
|
|
40
|
+
struct AppStateListener final {
|
|
41
|
+
public:
|
|
42
|
+
std::function<void()> remove SWIFT_PRIVATE;
|
|
43
|
+
|
|
44
|
+
public:
|
|
45
|
+
AppStateListener() = default;
|
|
46
|
+
explicit AppStateListener(std::function<void()> remove): remove(remove) {}
|
|
47
|
+
|
|
48
|
+
public:
|
|
49
|
+
// AppStateListener is not equatable because these properties are not equatable: remove
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
} // namespace margelo::nitro::appstate
|
|
53
|
+
|
|
54
|
+
namespace margelo::nitro {
|
|
55
|
+
|
|
56
|
+
// C++ AppStateListener <> JS AppStateListener (object)
|
|
57
|
+
template <>
|
|
58
|
+
struct JSIConverter<margelo::nitro::appstate::AppStateListener> final {
|
|
59
|
+
static inline margelo::nitro::appstate::AppStateListener fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
|
|
60
|
+
jsi::Object obj = arg.asObject(runtime);
|
|
61
|
+
return margelo::nitro::appstate::AppStateListener(
|
|
62
|
+
JSIConverter<std::function<void()>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "remove")))
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::appstate::AppStateListener& arg) {
|
|
66
|
+
jsi::Object obj(runtime);
|
|
67
|
+
obj.setProperty(runtime, PropNameIDCache::get(runtime, "remove"), JSIConverter<std::function<void()>>::toJSI(runtime, arg.remove));
|
|
68
|
+
return obj;
|
|
69
|
+
}
|
|
70
|
+
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
|
|
71
|
+
if (!value.isObject()) {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
jsi::Object obj = value.getObject(runtime);
|
|
75
|
+
if (!nitro::isPlainObject(runtime, obj)) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
if (!JSIConverter<std::function<void()>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "remove")))) return false;
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
} // namespace margelo::nitro
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// HybridAppStateModuleSpec.cpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#include "HybridAppStateModuleSpec.hpp"
|
|
9
|
+
|
|
10
|
+
namespace margelo::nitro::appstate {
|
|
11
|
+
|
|
12
|
+
void HybridAppStateModuleSpec::loadHybridMethods() {
|
|
13
|
+
// load base methods/properties
|
|
14
|
+
HybridObject::loadHybridMethods();
|
|
15
|
+
// load custom methods/properties
|
|
16
|
+
registerHybrids(this, [](Prototype& prototype) {
|
|
17
|
+
prototype.registerHybridGetter("wasKilledLastSession", &HybridAppStateModuleSpec::getWasKilledLastSession);
|
|
18
|
+
prototype.registerHybridMethod("getCurrentState", &HybridAppStateModuleSpec::getCurrentState);
|
|
19
|
+
prototype.registerHybridMethod("addListener", &HybridAppStateModuleSpec::addListener);
|
|
20
|
+
prototype.registerHybridMethod("getStateOnNotification", &HybridAppStateModuleSpec::getStateOnNotification);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
} // namespace margelo::nitro::appstate
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// HybridAppStateModuleSpec.hpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#if __has_include(<NitroModules/HybridObject.hpp>)
|
|
11
|
+
#include <NitroModules/HybridObject.hpp>
|
|
12
|
+
#else
|
|
13
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
14
|
+
#endif
|
|
15
|
+
|
|
16
|
+
// Forward declaration of `AppStateListener` to properly resolve imports.
|
|
17
|
+
namespace margelo::nitro::appstate { struct AppStateListener; }
|
|
18
|
+
// Forward declaration of `AppStateChangeEvent` to properly resolve imports.
|
|
19
|
+
namespace margelo::nitro::appstate { struct AppStateChangeEvent; }
|
|
20
|
+
|
|
21
|
+
#include <string>
|
|
22
|
+
#include "AppStateListener.hpp"
|
|
23
|
+
#include "AppStateChangeEvent.hpp"
|
|
24
|
+
#include <functional>
|
|
25
|
+
|
|
26
|
+
namespace margelo::nitro::appstate {
|
|
27
|
+
|
|
28
|
+
using namespace margelo::nitro;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* An abstract base class for `AppStateModule`
|
|
32
|
+
* Inherit this class to create instances of `HybridAppStateModuleSpec` in C++.
|
|
33
|
+
* You must explicitly call `HybridObject`'s constructor yourself, because it is virtual.
|
|
34
|
+
* @example
|
|
35
|
+
* ```cpp
|
|
36
|
+
* class HybridAppStateModule: public HybridAppStateModuleSpec {
|
|
37
|
+
* public:
|
|
38
|
+
* HybridAppStateModule(...): HybridObject(TAG) { ... }
|
|
39
|
+
* // ...
|
|
40
|
+
* };
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
class HybridAppStateModuleSpec: public virtual HybridObject {
|
|
44
|
+
public:
|
|
45
|
+
// Constructor
|
|
46
|
+
explicit HybridAppStateModuleSpec(): HybridObject(TAG) { }
|
|
47
|
+
|
|
48
|
+
// Destructor
|
|
49
|
+
~HybridAppStateModuleSpec() override = default;
|
|
50
|
+
|
|
51
|
+
public:
|
|
52
|
+
// Properties
|
|
53
|
+
virtual bool getWasKilledLastSession() = 0;
|
|
54
|
+
|
|
55
|
+
public:
|
|
56
|
+
// Methods
|
|
57
|
+
virtual std::string getCurrentState() = 0;
|
|
58
|
+
virtual AppStateListener addListener(const std::function<void(const AppStateChangeEvent& /* event */)>& onStateChange) = 0;
|
|
59
|
+
virtual std::string getStateOnNotification() = 0;
|
|
60
|
+
|
|
61
|
+
protected:
|
|
62
|
+
// Hybrid Setup
|
|
63
|
+
void loadHybridMethods() override;
|
|
64
|
+
|
|
65
|
+
protected:
|
|
66
|
+
// Tag for logging
|
|
67
|
+
static constexpr auto TAG = "AppStateModule";
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
} // namespace margelo::nitro::appstate
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@matiks/rn-app-state",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Nitro-powered native app state module for React Native",
|
|
5
|
+
"main": "lib/index",
|
|
6
|
+
"module": "lib/index",
|
|
7
|
+
"types": "lib/index.d.ts",
|
|
8
|
+
"react-native": "src/index",
|
|
9
|
+
"source": "src/index",
|
|
10
|
+
"files": [
|
|
11
|
+
"src",
|
|
12
|
+
"lib",
|
|
13
|
+
"nitrogen",
|
|
14
|
+
"android/build.gradle",
|
|
15
|
+
"android/gradle.properties",
|
|
16
|
+
"android/fix-prefab.gradle",
|
|
17
|
+
"android/CMakeLists.txt",
|
|
18
|
+
"android/src",
|
|
19
|
+
"ios/**/*.swift",
|
|
20
|
+
"ios/**/*.m",
|
|
21
|
+
"ios/**/*.mm",
|
|
22
|
+
"cpp/**/*.{hpp,cpp}",
|
|
23
|
+
"nitro.json",
|
|
24
|
+
"*.podspec",
|
|
25
|
+
"README.md",
|
|
26
|
+
"FLOW.md"
|
|
27
|
+
],
|
|
28
|
+
"scripts": {
|
|
29
|
+
"typecheck": "tsc --noEmit",
|
|
30
|
+
"clean": "rm -rf android/build lib",
|
|
31
|
+
"specs": "npx nitrogen@0.33.9 --logLevel=\"debug\""
|
|
32
|
+
},
|
|
33
|
+
"keywords": [
|
|
34
|
+
"react-native",
|
|
35
|
+
"nitro",
|
|
36
|
+
"app-state",
|
|
37
|
+
"lifecycle"
|
|
38
|
+
],
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "git+https://github.com/matiks-com/rn-app-state.git"
|
|
42
|
+
},
|
|
43
|
+
"author": "Matiks",
|
|
44
|
+
"license": "MIT",
|
|
45
|
+
"bugs": {
|
|
46
|
+
"url": "https://github.com/matiks-com/rn-app-state/issues"
|
|
47
|
+
},
|
|
48
|
+
"homepage": "https://github.com/matiks-com/rn-app-state#readme",
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"react": "*",
|
|
51
|
+
"react-native": "*",
|
|
52
|
+
"react-native-nitro-modules": "*"
|
|
53
|
+
}
|
|
54
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { HybridObject } from 'react-native-nitro-modules';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Event payload for app state change notifications.
|
|
5
|
+
* Nitro requires event payloads to be non-empty interfaces.
|
|
6
|
+
*/
|
|
7
|
+
export interface AppStateChangeEvent {
|
|
8
|
+
state: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Listener handle returned by addListener.
|
|
13
|
+
* Call remove() to unsubscribe.
|
|
14
|
+
*/
|
|
15
|
+
export interface AppStateListener {
|
|
16
|
+
remove: () => void;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* HybridObject spec for the native AppState module.
|
|
21
|
+
*
|
|
22
|
+
* Provides synchronous access to the current app lifecycle state
|
|
23
|
+
* and a listener-based API for state change notifications.
|
|
24
|
+
* Works on both iOS (Swift) and Android (Kotlin).
|
|
25
|
+
*/
|
|
26
|
+
export interface AppStateModule
|
|
27
|
+
extends HybridObject<{ ios: 'swift'; android: 'kotlin' }> {
|
|
28
|
+
/**
|
|
29
|
+
* Get the current app state synchronously.
|
|
30
|
+
* Returns 'active' or 'background'.
|
|
31
|
+
*/
|
|
32
|
+
getCurrentState(): string;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Whether the app was killed/terminated in the previous session.
|
|
36
|
+
* This is determined on cold start by checking if the previous
|
|
37
|
+
* session ended cleanly (via a persisted flag).
|
|
38
|
+
*/
|
|
39
|
+
readonly wasKilledLastSession: boolean;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Register a listener for app state changes.
|
|
43
|
+
* The callback receives the new state as a string ('active' | 'background').
|
|
44
|
+
* Returns a Listener with a remove() method to unsubscribe.
|
|
45
|
+
*/
|
|
46
|
+
addListener(
|
|
47
|
+
onStateChange: (event: AppStateChangeEvent) => void,
|
|
48
|
+
): AppStateListener;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Called from JS/native when a push notification is received.
|
|
52
|
+
* Returns the current app state at the moment of the call.
|
|
53
|
+
* Useful for knowing if a notification arrived while app was
|
|
54
|
+
* in foreground or background.
|
|
55
|
+
*/
|
|
56
|
+
getStateOnNotification(): string;
|
|
57
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The possible app lifecycle states.
|
|
3
|
+
*
|
|
4
|
+
* - `active`: App is in the foreground and interactive
|
|
5
|
+
* - `background`: App is in the background (or inactive on iOS)
|
|
6
|
+
* - `killed`: App was terminated/killed in the previous session
|
|
7
|
+
* (only reported briefly on cold start before transitioning to `active`)
|
|
8
|
+
*/
|
|
9
|
+
export type AppStateType = 'active' | 'background' | 'killed'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Return type for the useNitroAppState hook.
|
|
13
|
+
*/
|
|
14
|
+
export interface NitroAppStateResult {
|
|
15
|
+
/** Current app lifecycle state. Reactive -- triggers re-renders on change. */
|
|
16
|
+
appState: AppStateType
|
|
17
|
+
/** Whether the app was killed/terminated in the previous session. */
|
|
18
|
+
wasKilledLastSession: boolean
|
|
19
|
+
/** Get the current state at the moment of a notification receipt. */
|
|
20
|
+
getStateOnNotification: () => AppStateType
|
|
21
|
+
}
|