-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance Storage impl and add some evil things into util module
- Loading branch information
1 parent
6046b20
commit 88d076f
Showing
24 changed files
with
432 additions
and
441 deletions.
There are no files selected for viewing
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 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 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 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 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 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
16 changes: 16 additions & 0 deletions
16
toolkit/storage/src/main/kotlin/br/com/arch/toolkit/storage/ComplexDataParser.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 br.com.arch.toolkit.storage | ||
|
||
import kotlin.reflect.KClass | ||
|
||
interface ComplexDataParser { | ||
|
||
/** | ||
* Parses a json string into a data class | ||
*/ | ||
fun <T : Any> fromJson(json: String, classToParse: KClass<T>): T | ||
|
||
/** | ||
* Parses a data class into a json string | ||
*/ | ||
fun <T : Any> toJson(data: T): String | ||
} |
49 changes: 49 additions & 0 deletions
49
toolkit/storage/src/main/kotlin/br/com/arch/toolkit/storage/Storage.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,49 @@ | ||
package br.com.arch.toolkit.storage | ||
|
||
import android.content.Context | ||
import br.com.arch.toolkit.storage.keyValue.KeyValueStorage | ||
import br.com.arch.toolkit.storage.keyValue.MemoryStorage | ||
import br.com.arch.toolkit.storage.keyValue.SharedPrefStorage | ||
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.milliseconds | ||
|
||
sealed class Storage { | ||
|
||
data object KeyValue : Storage() { | ||
private var _encrypted: SharedPrefStorage.Encrypted? = null | ||
private var _regular: SharedPrefStorage.Regular? = null | ||
|
||
val memory = MemoryStorage("default-memory") | ||
val encrypted: SharedPrefStorage.Encrypted | ||
get() = requireNotNull(_encrypted) { "Not initialized, Be aware to call init() before use" } | ||
val regular: SharedPrefStorage.Regular | ||
get() = requireNotNull(_regular) { "Not initialized, Be aware to call init() before use" } | ||
|
||
fun init(context: Context) { | ||
_encrypted = SharedPrefStorage.Encrypted(context, "default-encrypted") | ||
_regular = SharedPrefStorage.Regular(context, "default-regular") | ||
} | ||
} | ||
|
||
data object Settings : Storage() { | ||
var threshold: Duration = 300.milliseconds | ||
private set | ||
var keyValue: KeyValueStorage = KeyValue.memory | ||
private set | ||
|
||
var complexDataParser: ComplexDataParser? = null | ||
private set | ||
|
||
fun setDefaultThreshold(threshold: Duration) = apply { | ||
this.threshold = threshold | ||
} | ||
|
||
fun setDefaultStorage(storage: KeyValueStorage) = apply { | ||
keyValue = storage | ||
} | ||
|
||
fun setComplexDataParser(parser: ComplexDataParser) = apply { | ||
complexDataParser = parser | ||
} | ||
} | ||
} |
118 changes: 0 additions & 118 deletions
118
toolkit/storage/src/main/kotlin/br/com/arch/toolkit/storage/StorageCreator.kt
This file was deleted.
Oops, something went wrong.
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
35 changes: 35 additions & 0 deletions
35
...torage/src/main/kotlin/br/com/arch/toolkit/storage/delegate/NonOptionalStorageDelegate.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,35 @@ | ||
package br.com.arch.toolkit.storage.delegate | ||
|
||
import br.com.arch.toolkit.storage.keyValue.KeyValueStorage | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.KProperty | ||
import kotlin.time.Duration | ||
|
||
data class NonOptionalStorageDelegate<T : Any> internal constructor( | ||
private val name: () -> String, | ||
private val storage: () -> KeyValueStorage, | ||
private val default: () -> T, | ||
private val threshold: Duration, | ||
private val classToParse: KClass<out T> | ||
) : StorageDelegate<T>() { | ||
|
||
private var savedData: T? by keyValueStorage(classToParse, name) | ||
|
||
operator fun getValue(thisRef: Any?, property: KProperty<*>): T = savedData | ||
?: default.get().also { | ||
log("[Storage] Delivering default value for field '${property.name}': \n\t Value -> $it") | ||
} | ||
|
||
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) { | ||
savedData = value | ||
} | ||
|
||
//region Storage Method modifiers | ||
fun storage(storage: KeyValueStorage) = storage { storage } | ||
fun storage(storage: () -> KeyValueStorage) = copy(storage = storage) | ||
//endregion | ||
|
||
//region Threshold Method modifiers | ||
fun threshold(threshold: Duration) = copy(threshold = threshold) | ||
//endregion | ||
} |
Oops, something went wrong.