-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathPortalsReactNative.swift
70 lines (58 loc) · 2.84 KB
/
PortalsReactNative.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import Foundation
import Capacitor
import IonicPortals
import IonicLiveUpdates
import React
@objc(IONPortalsReactNative)
public class PortalsReactNative: NSObject {
internal private(set) static var lum: LiveUpdateManager = .shared
let encoder = JSValueEncoder(optionalEncodingStrategy: .undefined)
let decoder = JSValueDecoder()
public override init() {
guard let configUrl = Bundle.main.url(forResource: "portals.config.json", withExtension: nil) else {
return
}
guard let configData = try? Data(contentsOf: configUrl),
let portalsConfig = try? JSONDecoder().decode(PortalsConfig.self, from: configData)
else { fatalError("Portals config data is malformed. Aborting.") }
if let registrationKey = portalsConfig.registrationKey {
PortalsRegistrationManager.shared.register(key: registrationKey)
}
if let publicKeyPath = portalsConfig.secureLiveUpdatesPublicKey {
guard let publicKeyUrl = Bundle.main.url(forResource: publicKeyPath, withExtension: nil) else { fatalError("Public key not found at \(publicKeyPath)") }
Self.lum = SecureLiveUpdateManager(named: "secure-updates", publicKeyUrl: publicKeyUrl)
}
}
@objc func register(_ key: String, resolver: RCTPromiseResolveBlock, rejector: RCTPromiseRejectBlock) {
PortalsRegistrationManager.shared.register(key: key)
resolver(())
}
@objc func enableSecureLiveUpdates(_ publicKeyPath: String, resolver: RCTPromiseResolveBlock, rejector: RCTPromiseRejectBlock) {
guard let publicKeyUrl = Bundle.main.url(forResource: publicKeyPath, withExtension: nil) else { fatalError("Public key not found at \(publicKeyPath)") }
Self.lum = SecureLiveUpdateManager(named: "secure-updates", publicKeyUrl: publicKeyUrl)
resolver(())
}
@objc func syncOne(_ appId: String, resolver: @escaping RCTPromiseResolveBlock, rejector: @escaping RCTPromiseRejectBlock) {
Task {
do {
let result = try await Self.lum.sync(appId: appId)
resolver(try? JSValueEncoder(optionalEncodingStrategy: .undefined).encode(result))
} catch {
rejector(nil, nil, error)
}
}
}
@objc func syncSome(_ appIds: [String], resolver: @escaping RCTPromiseResolveBlock, rejector: RCTPromiseRejectBlock) {
Task {
let syncResult = await Self.lum.syncSome(appIds)
resolver(try? syncResult.dict)
}
}
@objc func syncAll(_ resolver: @escaping RCTPromiseResolveBlock, rejector: RCTPromiseRejectBlock) {
Task {
let syncResult = await Self.lum.syncAll()
resolver(try? syncResult.dict)
}
}
@objc static func requiresMainQueueSetup() -> Bool { true }
}