Skip to content

Commit d0ec516

Browse files
committed
refactor: simplify enum parsing and map reading in Extensions
1 parent 1033ca9 commit d0ec516

1 file changed

Lines changed: 12 additions & 20 deletions

File tree

android/src/main/java/com/freeraspreactnative/utils/Extensions.kt

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -96,32 +96,24 @@ internal fun PackageInfo.toRNPackageInfo(context: ReactContext): RNPackageInfo {
9696
)
9797
}
9898

99+
private inline fun <reified T : Enum<T>> String?.toEnumOrDefault(default: T): T =
100+
if (this == null) default
101+
else try { enumValueOf(this) } catch (_: IllegalArgumentException) { default }
102+
99103
internal fun ReadableMap.toMalwareScanScope(): MalwareScanScope {
100-
val scanScope = try {
101-
ScopeType.valueOf(getString("scanScope") ?: "SIDELOADED_ONLY")
102-
} catch (_: IllegalArgumentException) {
103-
ScopeType.SIDELOADED_ONLY
104-
}
104+
val scanScope = getString("scanScope").toEnumOrDefault(ScopeType.SIDELOADED_ONLY)
105105
val trustedInstallSources = getArraySafe("trustedInstallSources").toList().ifEmpty { null }
106106
return MalwareScanScope(scanScope, trustedInstallSources)
107107
}
108108

109109
internal fun ReadableMap.toSuspiciousAppDetectionConfig(): SuspiciousAppDetectionConfig {
110-
val packageNames = this.getArraySafe("packageNames").toSet().ifEmpty { null }
111-
val hashes = this.getArraySafe("hashes").toSet().ifEmpty { null }
112-
val requestedPermissions = this.getNestedArraySafe("requestedPermissions")
113-
.map { it.toSet() }.toSet().ifEmpty { null }
114-
val grantedPermissions = this.getNestedArraySafe("grantedPermissions")
115-
.map { it.toSet() }.toSet().ifEmpty { null }
116-
val malwareScanScope = if (this.hasKey("malwareScanScope"))
117-
this.getMap("malwareScanScope")?.toMalwareScanScope() ?: MalwareScanScope(ScopeType.SIDELOADED_ONLY, null)
118-
else
119-
MalwareScanScope(ScopeType.SIDELOADED_ONLY, null)
120-
val reasonMode = try {
121-
ReasonMode.valueOf(this.getString("reasonMode") ?: "HIGHEST_CONFIDENCE")
122-
} catch (_: IllegalArgumentException) {
123-
ReasonMode.HIGHEST_CONFIDENCE
124-
}
110+
val packageNames = getArraySafe("packageNames").toSet().ifEmpty { null }
111+
val hashes = getArraySafe("hashes").toSet().ifEmpty { null }
112+
val requestedPermissions = getNestedArraySafe("requestedPermissions").map { it.toSet() }.toSet().ifEmpty { null }
113+
val grantedPermissions = getNestedArraySafe("grantedPermissions").map { it.toSet() }.toSet().ifEmpty { null }
114+
val malwareScanScope = getMap("malwareScanScope")?.toMalwareScanScope()
115+
?: MalwareScanScope(ScopeType.SIDELOADED_ONLY, null)
116+
val reasonMode = getString("reasonMode").toEnumOrDefault(ReasonMode.HIGHEST_CONFIDENCE)
125117
return SuspiciousAppDetectionConfig(packageNames, hashes, requestedPermissions, grantedPermissions, malwareScanScope, reasonMode)
126118
}
127119

0 commit comments

Comments
 (0)