Skip to content

Commit 376769a

Browse files
refactor: implement typed constants spec
1 parent 8aff58a commit 376769a

13 files changed

Lines changed: 112 additions & 47 deletions

File tree

android/src/main/java/com/mendix/mendixnative/react/MxConfiguration.kt

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package com.mendix.mendixnative.react
22

33
import com.facebook.react.bridge.ReactApplicationContext
4-
import com.facebook.react.bridge.WritableMap
5-
import com.facebook.react.bridge.WritableNativeMap
64
import com.mendix.mendixnative.MendixApplication
75
import com.mendix.mendixnative.config.AppUrl
86
import com.mendix.mendixnative.react.ota.getNativeDependencies
97
import com.mendix.mendixnative.react.ota.getOtaManifestFilepath
108

119
class MxConfiguration(val reactContext: ReactApplicationContext) {
1210

13-
fun getConstants(): WritableMap? {
11+
fun getConstants(): Map<String, Any> {
1412
val application = (reactContext.applicationContext as MendixApplication)
1513
if (runtimeUrl == null) {
1614
if (warningsFilter != WarningsFilter.none) {
@@ -22,31 +20,26 @@ class MxConfiguration(val reactContext: ReactApplicationContext) {
2220
Throwable("Without the runtime URL, the app cannot retrieve any data.\n\nPlease redeploy the app.")
2321
)
2422

25-
return WritableNativeMap()
23+
return emptyMap()
2624
}
2725

2826
throw IllegalStateException("Runtime URL not set in the MxConfiguration")
2927
}
3028

31-
val constants = WritableNativeMap()
32-
constants.putString("RUNTIME_URL", AppUrl.forRuntime(runtimeUrl))
33-
constants.putString("APP_NAME", defaultAppName)
34-
constants.putString("DATABASE_NAME", defaultDatabaseName)
35-
constants.putString(
36-
"FILES_DIRECTORY_NAME",
37-
defaultFilesDirectoryName
38-
) // Not to be removed as it is required for backwards compatibility.
39-
constants.putString("WARNINGS_FILTER_LEVEL", warningsFilter.toString())
40-
constants.putString("OTA_MANIFEST_PATH", getOtaManifestFilepath(reactContext))
41-
constants.putBoolean("IS_DEVELOPER_APP", application.getUseDeveloperSupport())
42-
constants.putInt("NATIVE_BINARY_VERSION", NATIVE_BINARY_VERSION)
43-
constants.putString("APP_SESSION_ID", application.getAppSessionId())
44-
45-
val dependencies = WritableNativeMap()
46-
getNativeDependencies(reactContext).forEach {
47-
dependencies.putString(it.key, it.value)
29+
val constants = mutableMapOf(
30+
"RUNTIME_URL" to AppUrl.forRuntime(runtimeUrl),
31+
"DATABASE_NAME" to defaultDatabaseName,
32+
"FILES_DIRECTORY_NAME" to defaultFilesDirectoryName,
33+
"WARNINGS_FILTER_LEVEL" to warningsFilter.toString(),
34+
"OTA_MANIFEST_PATH" to getOtaManifestFilepath(reactContext),
35+
"IS_DEVELOPER_APP" to application.getUseDeveloperSupport(),
36+
"NATIVE_BINARY_VERSION" to NATIVE_BINARY_VERSION,
37+
"APP_SESSION_ID" to application.getAppSessionId(),
38+
"NATIVE_DEPENDENCIES" to getNativeDependencies(reactContext)
39+
)
40+
defaultAppName?.let {
41+
constants.put("APP_NAME", it)
4842
}
49-
constants.putMap("NATIVE_DEPENDENCIES", dependencies)
5043

5144
return constants
5245
}

android/src/main/java/com/mendix/mendixnative/react/fs/NativeFsModule.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,12 @@ class NativeFsModule(private val reactContext: ReactApplicationContext) {
230230
}
231231
}
232232

233-
fun getConstants(): WritableMap {
234-
val constants = WritableNativeMap()
235-
constants.putString("DocumentDirectoryPath", filesDir)
236-
constants.putBoolean("SUPPORTS_DIRECTORY_MOVE", true) // Client uses this const to identify if functionality is supported
237-
constants.putBoolean("SUPPORTS_ENCRYPTION", true)
238-
return constants
233+
fun getConstants(): Map<String, Any> {
234+
return mapOf(
235+
"DocumentDirectoryPath" to filesDir,
236+
"SUPPORTS_DIRECTORY_MOVE" to true, // Client uses this const to identify if functionality is supported
237+
"SUPPORTS_ENCRYPTION" to true
238+
)
239239
}
240240

241241
@Throws(IOException::class)

android/src/main/java/com/mendixnative/configuration/MxConfigurationModule.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.mendixnative.configuration
22

33
import com.facebook.react.bridge.ReactApplicationContext
4-
import com.facebook.react.bridge.WritableMap
54
import com.facebook.react.module.annotations.ReactModule
65
import com.mendix.mendixnative.react.MxConfiguration
76
import com.mendixnative.NativeMxConfigurationSpec
@@ -14,7 +13,7 @@ class MxConfigurationModule(reactContext: ReactApplicationContext) :
1413

1514
override fun getName(): String = NAME
1615

17-
override fun getConfig(): WritableMap? {
16+
override fun getTypedExportedConstants(): Map<String, Any> {
1817
return configuration.getConstants()
1918
}
2019

android/src/main/java/com/mendixnative/fs/MxFileSystemModule.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.mendixnative.fs
33
import com.facebook.react.bridge.Promise
44
import com.facebook.react.bridge.ReactApplicationContext
55
import com.facebook.react.bridge.ReadableMap
6-
import com.facebook.react.bridge.WritableMap
76
import com.facebook.react.module.annotations.ReactModule
87
import com.mendix.mendixnative.react.fs.NativeFsModule
98
import com.mendixnative.NativeMxFileSystemSpec
@@ -16,7 +15,7 @@ class MxFileSystemModule(reactContext: ReactApplicationContext) :
1615

1716
override fun getName(): String = NAME
1817

19-
override fun constants(): WritableMap? {
18+
override fun getTypedExportedConstants(): Map<String, Any> {
2019
return fsModule.getConstants()
2120
}
2221

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import Foundation
2+
3+
@objcMembers
4+
public class MxConfigProxy: NSObject {
5+
public var runtimeUrl: String
6+
public var appName: String?
7+
public var databaseName: String
8+
public var filesDirectoryName: String
9+
public var warningsFilter: String
10+
public var otaManifestPath: String
11+
public var isDeveloperApp: NSNumber
12+
public var nativeDependencies: [String: Any]
13+
public var nativeBinaryVersion: NSNumber
14+
public var appSessionId: String?
15+
16+
init(runtimeUrl: String, appName: String?, databaseName: String, filesDirectoryName: String, warningsFilter: String, otaManifestPath: String, isDeveloperApp: NSNumber, nativeDependencies: [String : Any], nativeBinaryVersion: NSNumber, appSessionId: String?) {
17+
self.runtimeUrl = runtimeUrl
18+
self.appName = appName
19+
self.databaseName = databaseName
20+
self.filesDirectoryName = filesDirectoryName
21+
self.warningsFilter = warningsFilter
22+
self.otaManifestPath = otaManifestPath
23+
self.isDeveloperApp = isDeveloperApp
24+
self.nativeDependencies = nativeDependencies
25+
self.nativeBinaryVersion = nativeBinaryVersion
26+
self.appSessionId = appSessionId
27+
}
28+
29+
30+
public static func prepare() -> MxConfigProxy? {
31+
guard let runtimeUrl = MxConfiguration.runtimeUrl?.absoluteString else {
32+
let exception = NSException(
33+
name: NSExceptionName("RUNTIME_URL_MISSING"),
34+
reason: "Runtime URL was not set prior to launch.",
35+
userInfo: nil
36+
)
37+
exception.raise()
38+
return nil
39+
}
40+
41+
return MxConfigProxy(
42+
runtimeUrl: runtimeUrl,
43+
appName: MxConfiguration.appName,
44+
databaseName: MxConfiguration.databaseName,
45+
filesDirectoryName: MxConfiguration.filesDirectoryName,
46+
warningsFilter: MxConfiguration.warningsFilter.stringValue,
47+
otaManifestPath: OtaHelpers.getOtaManifestFilepath(),
48+
isDeveloperApp: NSNumber(booleanLiteral: MxConfiguration.isDeveloperApp),
49+
nativeDependencies: OtaHelpers.getNativeDependencies(),
50+
nativeBinaryVersion: NSNumber(integerLiteral: MxConfiguration.nativeBinaryVersion),
51+
appSessionId: MxConfiguration.appSessionId
52+
)
53+
}
54+
}

ios/Modules/MxConfiguration/MxConfiguration.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Foundation
33
@objcMembers
44
public class MxConfiguration: NSObject {
55

6-
private static let nativeBinaryVersion: Int = 32
6+
static let nativeBinaryVersion: Int = 32
77
private static let defaultDatabaseName = "default"
88
private static let defaultFilesDirectoryName = "files/default"
99

ios/Modules/NativeFsModule/NativeFsModule.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -268,12 +268,6 @@ public class NativeFsModule: NSObject {
268268
}
269269
}
270270

271-
public let constants: NSDictionary = [
272-
"DocumentDirectoryPath": NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first ?? "",
273-
"SUPPORTS_DIRECTORY_MOVE": true,
274-
"SUPPORTS_ENCRYPTION": true
275-
]
276-
277271
private func isWhiteListedPath(_ paths: String..., reject: RCTPromiseRejectBlock) -> Bool {
278272
do {
279273
try NativeFsModule.ensureWhiteListedPath(paths)

ios/TurboModules/MxConfiguration/MxConfigurationModule.mm

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,25 @@ @implementation MxConfigurationModule
1313
return std::make_shared<facebook::react::NativeMxConfigurationSpecJSI>(params);
1414
}
1515

16-
- (nonnull NSDictionary *)getConfig {
17-
return [[[MxConfiguration alloc] init] constants];
16+
- (nonnull facebook::react::ModuleConstants<JS::NativeMxConfiguration::Constants>)constantsToExport {
17+
return [self getConstants];
18+
}
19+
20+
- (nonnull facebook::react::ModuleConstants<JS::NativeMxConfiguration::Constants>)getConstants {
21+
MxConfigProxy *config = [MxConfigProxy prepare];
22+
return facebook::react::typedConstants<JS::NativeMxConfiguration::Constants>({
23+
.RUNTIME_URL = config.runtimeUrl,
24+
.APP_NAME = config.appName,
25+
.FILES_DIRECTORY_NAME = config.filesDirectoryName,
26+
.DATABASE_NAME = config.databaseName,
27+
.WARNINGS_FILTER_LEVEL = config.warningsFilter,
28+
.OTA_MANIFEST_PATH = config.otaManifestPath,
29+
.NATIVE_DEPENDENCIES = config.nativeDependencies,
30+
.IS_DEVELOPER_APP = config.isDeveloperApp,
31+
.CODE_PUSH_KEY= NULL,
32+
.NATIVE_BINARY_VERSION = [config.nativeBinaryVersion doubleValue],
33+
.APP_SESSION_ID = config.appSessionId
34+
});
1835
}
1936

2037
@end

ios/TurboModules/MxFileSystem/MxFileSystem.mm

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,17 @@ @implementation MxFileSystem
1313
return std::make_shared<facebook::react::NativeMxFileSystemSpecJSI>(params);
1414
}
1515

16-
- (nonnull NSDictionary *)constants {
17-
return [[[NativeFsModule alloc] init] constants];
16+
- (facebook::react::ModuleConstants<JS::NativeMxFileSystem::Constants>)constantsToExport {
17+
return [self getConstants];
18+
}
19+
20+
- (facebook::react::ModuleConstants<JS::NativeMxFileSystem::Constants>)getConstants {
21+
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
22+
return facebook::react::typedConstants<JS::NativeMxFileSystem::Constants>({
23+
.DocumentDirectoryPath = path ?: @"",
24+
.SUPPORTS_DIRECTORY_MOVE = true,
25+
.SUPPORTS_ENCRYPTION = true
26+
});
1827
}
1928

2029
- (void)save:(nonnull NSDictionary *)blob

src/file-system/NativeMxFileSystem.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type FsConstants = {
2020
};
2121

2222
export interface Spec extends TurboModule {
23-
constants(): FsConstants;
23+
readonly getConstants: () => FsConstants;
2424
save(blob: CodegenTypes.UnsafeObject, filePath: string): Promise<void>;
2525
read(filePath: string): Promise<BlobData>;
2626
move(filePath: string, newPath: string): Promise<void>;

0 commit comments

Comments
 (0)