@digitalshieldfe/react-native-backup-card-sdk 0.1.1 → 0.1.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.
@@ -50,8 +50,8 @@ fun ByteArray?.toWritableArrayOrNull(): WritableArray? {
50
50
  class BackupCardSdkModule(val reactContext: ReactApplicationContext) :
51
51
  NativeBackupCardSdkSpec(reactContext) ,LifecycleEventListener
52
52
  {
53
- private lateinit var backupCardSdk: BackupCardSdk
54
- private val tag = "BackupCardSdkModule"
53
+ private var backupCardSdk: BackupCardSdk? = null
54
+ private var listenersRegistered = false
55
55
  private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
56
56
  override fun invalidate() {
57
57
  super.invalidate()
@@ -61,18 +61,17 @@ class BackupCardSdkModule(val reactContext: ReactApplicationContext) :
61
61
  object : BaseActivityEventListener() {
62
62
  override fun onNewIntent(intent: Intent) {
63
63
  super.onNewIntent(intent)
64
- backupCardSdk.handleIntent(intent)
64
+ backupCardSdk?.handleIntent(intent)
65
65
  }
66
66
  }
67
67
 
68
- override fun initialize() {
69
- super.initialize()
68
+ private fun ensureInitialized(): Boolean {
69
+ if (backupCardSdk != null) {
70
+ return true
71
+ }
70
72
  Utils.init(reactContext)
71
73
  Utils.getActivityLifecycle()
72
- val activity = Utils.getTopActivity()
73
- if (activity == null) {
74
- return
75
- }
74
+ val activity = Utils.getTopActivity() ?: return false
76
75
  val apduLogger =
77
76
  object : ApduLogger {
78
77
  override fun log(message: String, isSent: Boolean, isSuccess: Boolean) {
@@ -98,55 +97,86 @@ class BackupCardSdkModule(val reactContext: ReactApplicationContext) :
98
97
  }
99
98
  }
100
99
  backupCardSdk = BackupCardSdk(activity, apduLogger, nfcTouchListener)
101
- reactContext.addActivityEventListener(mActivityEventListener)
102
- reactContext.addLifecycleEventListener(this)
100
+ if (!listenersRegistered) {
101
+ reactContext.addActivityEventListener(mActivityEventListener)
102
+ reactContext.addLifecycleEventListener(this)
103
+ listenersRegistered = true
104
+ }
105
+ return true
106
+ }
107
+
108
+ private fun requireSdk(promise: Promise): BackupCardSdk? {
109
+ if (!ensureInitialized()) {
110
+ promise.reject("E_NO_ACTIVITY", "BackupCardSdk requires a foreground Activity")
111
+ return null
112
+ }
113
+ return backupCardSdk
114
+ }
115
+
116
+ override fun initialize() {
117
+ super.initialize()
118
+ ensureInitialized()
103
119
  }
104
120
 
105
121
  companion object {
106
122
  const val NAME = NativeBackupCardSdkSpec.NAME
107
123
  }
108
124
 
109
- override fun getCardInfo(promise: Promise) =
110
- promise.launchSuspend(
111
- block = { backupCardSdk.getCardInfo() },
112
- transform = { info -> info?.toWritableMap() }
113
- )
125
+ override fun getCardInfo(promise: Promise) {
126
+ val sdk = requireSdk(promise) ?: return
127
+ promise.launchSuspend(
128
+ block = { sdk.getCardInfo() },
129
+ transform = { info -> info?.toWritableMap() }
130
+ )
131
+ }
114
132
 
115
- override fun resetCard(promise: Promise) =
116
- promise.launchSuspend(
117
- block = { backupCardSdk.resetCard() },
118
- transform = { result -> result }
119
- )
133
+ override fun resetCard(promise: Promise) {
134
+ val sdk = requireSdk(promise) ?: return
135
+ promise.launchSuspend(
136
+ block = { sdk.resetCard() },
137
+ transform = { result -> result }
138
+ )
139
+ }
120
140
 
121
- override fun activateCard(pwd: String, promise: Promise) =
122
- promise.launchSuspend(
123
- block = { backupCardSdk.activateCard(pwd) },
124
- transform = { result -> result }
125
- )
141
+ override fun activateCard(pwd: String, promise: Promise) {
142
+ val sdk = requireSdk(promise) ?: return
143
+ promise.launchSuspend(
144
+ block = { sdk.activateCard(pwd) },
145
+ transform = { result -> result }
146
+ )
147
+ }
126
148
 
127
- override fun changePin(oldPin: String, newPin: String, promise: Promise) =
128
- promise.launchSuspend(
129
- block = { backupCardSdk.changePin(oldPin, newPin) },
130
- transform = { result -> result }
131
- )
149
+ override fun changePin(oldPin: String, newPin: String, promise: Promise) {
150
+ val sdk = requireSdk(promise) ?: return
151
+ promise.launchSuspend(
152
+ block = { sdk.changePin(oldPin, newPin) },
153
+ transform = { result -> result }
154
+ )
155
+ }
132
156
 
133
- override fun checkSlotEmpty(slotId: Double, pwd: String, promise: Promise) =
134
- promise.launchSuspend(
135
- block = { backupCardSdk.checkSlotEmpty(slotId.toInt(), pwd) },
136
- transform = { result -> result }
137
- )
157
+ override fun checkSlotEmpty(slotId: Double, pwd: String, promise: Promise) {
158
+ val sdk = requireSdk(promise) ?: return
159
+ promise.launchSuspend(
160
+ block = { sdk.checkSlotEmpty(slotId.toInt(), pwd) },
161
+ transform = { result -> result }
162
+ )
163
+ }
138
164
 
139
- override fun writeSlot(slotIndex: Double, data: ReadableArray, pwd: String, promise: Promise) =
140
- promise.launchSuspend(
141
- block = { backupCardSdk.writeSlot(slotIndex.toInt(), data.toByteArray(), pwd) },
142
- transform = { result -> result }
143
- )
165
+ override fun writeSlot(slotIndex: Double, data: ReadableArray, pwd: String, promise: Promise) {
166
+ val sdk = requireSdk(promise) ?: return
167
+ promise.launchSuspend(
168
+ block = { sdk.writeSlot(slotIndex.toInt(), data.toByteArray(), pwd) },
169
+ transform = { result -> result }
170
+ )
171
+ }
144
172
 
145
- override fun readSlot(slotIndex: Double, pwd: String, promise: Promise) =
146
- promise.launchSuspend(
147
- block = { backupCardSdk.readSlot(slotIndex.toInt(), pwd) },
148
- transform = { result -> result.toWritableArrayOrNull() }
149
- )
173
+ override fun readSlot(slotIndex: Double, pwd: String, promise: Promise) {
174
+ val sdk = requireSdk(promise) ?: return
175
+ promise.launchSuspend(
176
+ block = { sdk.readSlot(slotIndex.toInt(), pwd) },
177
+ transform = { result -> result.toWritableArrayOrNull() }
178
+ )
179
+ }
150
180
 
151
181
 
152
182
 
@@ -170,14 +200,16 @@ class BackupCardSdkModule(val reactContext: ReactApplicationContext) :
170
200
  }
171
201
 
172
202
  override fun onHostResume() {
173
- backupCardSdk.onActivityResumed()
203
+ if (ensureInitialized()) {
204
+ backupCardSdk?.onActivityResumed()
205
+ }
174
206
  }
175
207
 
176
208
  override fun onHostPause() {
177
- backupCardSdk.onActivityPaused()
209
+ backupCardSdk?.onActivityPaused()
178
210
  }
179
211
 
180
212
  override fun onHostDestroy() {
181
- backupCardSdk.onActivityDestroyed()
213
+ backupCardSdk?.onActivityDestroyed()
182
214
  }
183
215
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@digitalshieldfe/react-native-backup-card-sdk",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "ds react-native backup-card sdk",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",