Skip to content

Commit

Permalink
Enhance Storage impl and add some evil things into util module
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus-corregiari committed Jun 13, 2024
1 parent 6046b20 commit 88d076f
Show file tree
Hide file tree
Showing 24 changed files with 432 additions and 441 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ internal class ToolkitPublishPlugin : Plugin<Project> {
val mapOfConfigurations = mapOf(
"runtime" to "implementation",
"compile" to "api",
"provided" to "runtimeOnly"
"provided" to "compileOnly"
).mapNotNull { (scope, configuration) ->
configurations.findByName(configuration)?.let { scope to it }
}.toMap()
Expand Down Expand Up @@ -208,7 +208,7 @@ internal class ToolkitPublishPlugin : Plugin<Project> {
node.appendNode("groupId", group)
node.appendNode("artifactId", name)
node.appendNode("version", version)
node.appendNode("scope", "runtime")
node.appendNode("scope", scope)
}
}
}
4 changes: 4 additions & 0 deletions samples/github-list-project/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ android {
}
}

androidComponents.beforeVariants {
it.enable = it.name == "debug"
}

dependencies {
// Other Modules
implementation(project(":toolkit:delegate"))
Expand Down
1 change: 1 addition & 0 deletions samples/playground-android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ dependencies {
implementation(libraries.androidx.recycler)
implementation(libraries.androidx.constraint)
implementation(libraries.square.timber)
implementation(libraries.google.gson)
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
package br.com.arch.toolkit.sample.playground.storage

import br.com.arch.toolkit.storage.StorageCreator
import br.com.arch.toolkit.storage.Storage
import br.com.arch.toolkit.storage.delegate.keyValueStorage
import kotlin.reflect.KClass

class SampleData<T : Any>(private val name: String, default: T, kClass: KClass<T>) {
private var nullableValue: T? by keyValueStorage(
name = "nullable-$name",
classToParse = kClass
)
private var value: T by keyValueStorage(
name = name,
default = default,
classToParse = kClass
)
private var nullableListOfValue: List<T>? by keyValueStorage(
name = "nullable-list-$name",
)
private var listOfValue: List<T> by keyValueStorage(
name = "list-$name",
default = emptyList()
)
private var nullableMapOfValue: Map<String, T>? by keyValueStorage(
name = "nullable-map-$name",
)
private var mapOfValue: Map<String, T> by keyValueStorage(
name = "map-$name",
default = emptyMap()
)

private var nullableValue: T? by keyValueStorage(kClass, "nullable-$name")
private var value: T by keyValueStorage<T>(kClass, name).required(default)

private var nullableListOfValue: List<T>? by keyValueStorage("nullable-list-$name")
private var listOfValue: List<T> by keyValueStorage<List<T>>("list-$name")
.required(listOf(default))

private var nullableMapOfValue: Map<String, T>? by keyValueStorage("nullable-map-$name")
private var mapOfValue: Map<String, T> by keyValueStorage<Map<String, T>>("map-$name")
.required(emptyMap())

fun asNewData() = NewData(
nullableValue,
Expand All @@ -48,12 +36,13 @@ class SampleData<T : Any>(private val name: String, default: T, kClass: KClass<T
}

fun delete() {
StorageCreator.defaultStorage().apply {
nullableValue = null
nullableListOfValue = null
nullableMapOfValue = null

Storage.Settings.keyValue.apply {
remove(this@SampleData.name)
remove("nullable-${this@SampleData.name}")
remove("nullable-list-${this@SampleData.name}")
remove("list-${this@SampleData.name}")
remove("nullable-map-${this@SampleData.name}")
remove("map-${this@SampleData.name}")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import androidx.lifecycle.lifecycleScope
import br.com.arch.toolkit.delegate.viewProvider
import br.com.arch.toolkit.sample.playground.R
import br.com.arch.toolkit.sample.playground.statemachine.BaseActivity
import br.com.arch.toolkit.storage.StorageCreator
import br.com.arch.toolkit.storage.ComplexDataParser
import br.com.arch.toolkit.storage.Storage
import com.google.gson.Gson
import kotlinx.coroutines.launch
import java.util.UUID
import kotlin.random.Random
import kotlin.reflect.KClass
import kotlin.time.Duration.Companion.minutes

class StorageSampleActivity : BaseActivity(R.layout.activity_storage_sample) {
Expand All @@ -24,7 +27,7 @@ class StorageSampleActivity : BaseActivity(R.layout.activity_storage_sample) {
private val booleanData = SampleData("boolean", false, Boolean::class)

// Complex
private val enumData = SampleData("TestEnum", TestEnum.FOUR, TestEnum::class)
private val enumData = SampleData("TestEnum", TestEnum.ONE, TestEnum::class)
private val classData = SampleData("TestClass", TestClass("test"), TestClass::class)

//region Views
Expand All @@ -41,8 +44,16 @@ class StorageSampleActivity : BaseActivity(R.layout.activity_storage_sample) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

StorageCreator.setDefaultStorage(StorageCreator::encryptedSharedPref)
StorageCreator.setDefaultThreshold(2.minutes)
Storage.Settings.setDefaultStorage(Storage.KeyValue.encrypted)
Storage.Settings.setDefaultThreshold(2.minutes)
Storage.Settings.setComplexDataParser(object : ComplexDataParser {
val gson = Gson()

override fun <T : Any> fromJson(json: String, classToParse: KClass<T>) =
gson.fromJson(json, classToParse.java)

override fun <T : Any> toJson(data: T): String = gson.toJson(data)
})

lifecycleScope.launch {
// Primitive
Expand Down Expand Up @@ -113,7 +124,7 @@ class StorageSampleActivity : BaseActivity(R.layout.activity_storage_sample) {
)
}

enum class TestEnum { FOUR }
enum class TestEnum { ONE }
data class TestClass(val value: String)

private fun <T> Boolean.ifTrue(block: () -> T) = if (this) {
Expand Down
3 changes: 1 addition & 2 deletions toolkit/storage/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ dependencies {
// JetBrains
implementation(libraries.jetbrains.stdlib.jdk8)
implementation(libraries.jetbrains.coroutines.core)
implementation(libraries.jetbrains.reflect)

// Androidx
implementation(libraries.androidx.security)
implementation(libraries.androidx.startup)

// Tools
implementation(libraries.square.timber)
implementation(libraries.google.gson)

}
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
}
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
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import android.content.Context
import androidx.startup.Initializer

internal class StorageInitializer : Initializer<Unit> {
override fun create(context: Context) = StorageCreator.init(context)
override fun create(context: Context) = Storage.KeyValue.init(context)
override fun dependencies() = mutableListOf<Class<out Initializer<*>>>()
}
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
}
Loading

0 comments on commit 88d076f

Please sign in to comment.