Skip to content

Commit

Permalink
Rerun RN codegen to allow the config workingDir to be set as a subdir…
Browse files Browse the repository at this point in the history
…ectory of the application directory and reset breezServices when disconnect is called
  • Loading branch information
dangeross committed Oct 30, 2023
1 parent 6f68705 commit e5cc30f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,6 @@ class BreezSDKModule(reactContext: ReactApplicationContext) : ReactContextBaseJa
val res = defaultConfig(envTypeTmp, apiKey, nodeConfigTmp)
val workingDir = File(reactApplicationContext.filesDir.toString() + "/breezSdk")

if (!workingDir.exists()) {
workingDir.mkdirs()
}

res.workingDir = workingDir.absolutePath
promise.resolve(readableMapOf(res))
} catch (e: Exception) {
Expand Down Expand Up @@ -157,7 +153,18 @@ class BreezSDKModule(reactContext: ReactApplicationContext) : ReactContextBaseJa

try {
val configTmp = asConfig(config) ?: run { throw SdkException.Generic("Missing mandatory field config of type Config") }

if (!configTmp.workingDir.contains(reactApplicationContext.filesDir.toString())) {
promise.reject("Generic", "Mandatory field workingDir must contain application directory")
return
}

val emitter = reactApplicationContext.getJSModule(RCTDeviceEventEmitter::class.java)
val workingDir = File(configTmp.workingDir)

if (!workingDir.exists()) {
workingDir.mkdirs()
}

breezServices = connect(configTmp, asUByteList(seed), BreezSDKListener(emitter))
promise.resolve(readableMapOf("status" to "ok"))
Expand All @@ -172,6 +179,7 @@ class BreezSDKModule(reactContext: ReactApplicationContext) : ReactContextBaseJa
executor.execute {
try {
getBreezServices().disconnect()
breezServices = null
promise.resolve(readableMapOf("status" to "ok"))
} catch (e: Exception) {
promise.reject(e.javaClass.simpleName.replace("Exception", "Error"), e.message, e)
Expand Down
16 changes: 15 additions & 1 deletion libs/sdk-react-native/ios/RNBreezSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ class RNBreezSDK: RCTEventEmitter {

private var breezServices: BlockingBreezServices!

static var applicationDirectory: URL {
return FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
}

static var breezSdkDirectory: URL {
let applicationDirectory = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
let breezSdkDirectory = applicationDirectory.appendingPathComponent("breezSdk", isDirectory: true)

if !FileManager.default.fileExists(atPath: breezSdkDirectory.path) {
Expand Down Expand Up @@ -113,6 +116,16 @@ class RNBreezSDK: RCTEventEmitter {

do {
let configTmp = try BreezSDKMapper.asConfig(config: config)

if !configTmp.workingDir.contains(RNBreezSDK.applicationDirectory.path) {
reject("Generic", "Mandatory field workingDir must contain application directory", nil)
return
}

if !FileManager.default.fileExists(atPath: configTmp.workingDir) {
try! FileManager.default.createDirectory(atPath: configTmp.workingDir, withIntermediateDirectories: true)
}

breezServices = try BreezSDK.connect(config: configTmp, seed: seed, listener: BreezSDKListener(emitter: self))
resolve(["status": "ok"])
} catch let err {
Expand All @@ -124,6 +137,7 @@ class RNBreezSDK: RCTEventEmitter {
func disconnect(_ resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
do {
try getBreezServices().disconnect()
breezServices = nil
resolve(["status": "ok"])
} catch let err {
rejectErr(err: err, reject: reject)
Expand Down

0 comments on commit e5cc30f

Please sign in to comment.