-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e5c176b
commit 7615d97
Showing
5 changed files
with
52 additions
and
29 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
src/main/kotlin/dartzee/preferences/AbstractPreferenceService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package dartzee.preferences | ||
|
||
abstract class AbstractPreferenceService { | ||
abstract fun <T : Any> delete(preference: Preference<T>) | ||
|
||
protected abstract fun <T> getRaw(preference: Preference<T>): String | ||
|
||
abstract fun <T : Any> save(preference: Preference<T>, value: T) | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
fun <T : Any> get(preference: Preference<T>, useDefault: Boolean = false): T { | ||
if (useDefault) { | ||
return preference.default | ||
} | ||
|
||
val raw = getRaw(preference) | ||
return when (val desiredType = preference.default::class) { | ||
Boolean::class -> raw.toBoolean() as T | ||
Double::class -> raw.toDouble() as T | ||
Int::class -> raw.toInt() as T | ||
String::class -> raw as T | ||
else -> | ||
throw TypeCastException( | ||
"Unhandled type [${desiredType}] for preference ${preference.name}" | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
16 changes: 16 additions & 0 deletions
16
src/test/kotlin/dartzee/preferences/InMemoryPreferenceService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package dartzee.preferences | ||
|
||
class InMemoryPreferenceService : AbstractPreferenceService() { | ||
private val hmPreferences = mutableMapOf<String, String>() | ||
|
||
override fun <T : Any> delete(preference: Preference<T>) { | ||
hmPreferences.remove(preference.name) | ||
} | ||
|
||
override fun <T> getRaw(preference: Preference<T>) = | ||
hmPreferences.getOrDefault(preference.name, preference.default.toString()) | ||
|
||
override fun <T : Any> save(preference: Preference<T>, value: T) { | ||
hmPreferences[preference.name] = value.toString() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters