Skip to content

Commit e5e2716

Browse files
committed
Do not crash on incorrect user input of numbers
1 parent 38f15f5 commit e5e2716

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

app/src/main/kotlin/com/njlabs/showjava/utils/UserPreferences.kt

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,31 @@ class UserPreferences(private val prefs: SharedPreferences) {
6363
get() = prefs.getBoolean("showSystemApps", DEFAULTS.SHOW_SYSTEM_APPS)
6464

6565
val chunkSize: Int
66-
get() = prefs.getString("chunkSize", DEFAULTS.CHUNK_SIZE.toString().trim())?.toInt()
67-
?: DEFAULTS.CHUNK_SIZE
66+
get() = try {
67+
prefs.getString("chunkSize", DEFAULTS.CHUNK_SIZE.toString())?.trim()?.toInt()
68+
?: DEFAULTS.CHUNK_SIZE
69+
} catch (ignored: Exception) {
70+
DEFAULTS.CHUNK_SIZE
71+
}
6872

6973
val maxAttempts: Int
70-
get() = prefs.getString("maxAttempts", DEFAULTS.MAX_ATTEMPTS.toString().trim())?.toInt()
71-
?: DEFAULTS.MAX_ATTEMPTS
74+
get() = try {
75+
prefs.getString("maxAttempts", DEFAULTS.MAX_ATTEMPTS.toString())?.trim()?.toInt()
76+
?: DEFAULTS.MAX_ATTEMPTS
77+
} catch (ignored: Exception) {
78+
DEFAULTS.MAX_ATTEMPTS
79+
}
7280

7381
val memoryThreshold: Int
74-
get() = prefs.getString("memoryThreshold", DEFAULTS.MEMORY_THRESHOLD.toString().trim())?.toInt()
75-
?: DEFAULTS.MEMORY_THRESHOLD
82+
get() = try {
83+
prefs.getString(
84+
"memoryThreshold",
85+
DEFAULTS.MEMORY_THRESHOLD.toString()
86+
)?.trim()?.toInt()
87+
?: DEFAULTS.MEMORY_THRESHOLD
88+
} catch (ignored: Exception) {
89+
DEFAULTS.MEMORY_THRESHOLD
90+
}
7691

7792
val consentStatus: Int
7893
get() = prefs.getInt("consentStatus", ConsentStatus.UNKNOWN.ordinal)

0 commit comments

Comments
 (0)