Skip to content

Commit

Permalink
1.0.0 (#27)
Browse files Browse the repository at this point in the history
Bundle implemented
  • Loading branch information
y9san9 committed Jun 12, 2021
2 parents 62af940 + 280ca91 commit 72f2b84
Show file tree
Hide file tree
Showing 17 changed files with 348 additions and 10 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
/files/build/
/local-storage/build/
/shared-preferences/build/
/bundle/build/
/json/build/
/json/json-files/build/
/json/json-local-storage/build/
/json/json-shared-preferences/build
/json/json-shared-preferences/build/
/json/json-bundle/

/.gradle/
/buildSrc/.gradle/
Expand Down
41 changes: 37 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,32 @@ fun main() {
}
```

### Android Bundle State
You can also store android app state with the library
```kotlin
class MainActivity : Activity() {
private val state = object : KBundleDataStorage() {
var score by int { 0 } // This will be automatically saved and restored
}

override fun onCreate(bundle: Bundle?) = state.fillState(bundle) {
super.onCreate(bundle)
}
// OR
override fun onCreate(bundle: Bundle?) {
super.onCreate(bundle)
state.restoreInstanceState(bundle)
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
state.saveInstanceState(outState)
}
}
```
`property` are also allowed there with serialization to string followed by `bundle.putString`


### Mutate Example
There is also an API to use mutable objects
```kotlin
Expand All @@ -60,15 +86,15 @@ object MainStorage : ... {
}

// Launches an asynchronous commit after block()
fun addItem() = MainStorage.mutate {
fun editItem() = MainStorage.mutate {
item.foo = ...
}
// Suspends until commit
suspend fun addItem() = MainStorage.mutateCommit {
suspend fun editItem() = MainStorage.mutateCommit {
item.foo = ...
}
// Blocking mutation
fun addItem() = MainStorage.mutateBlocking {
fun editItem() = MainStorage.mutateBlocking {
item.foo = ...
}

Expand Down Expand Up @@ -127,7 +153,14 @@ All `kds` packages are located at repository [maven.kotlingang.fun](https://mave
**Platforms**: ![android][badge-android] <br>
**Dependency**: `fun.kotlingang.kds:json-shared-preferences:$version` <br>
**Example**: Github [repo](https://github.com/kotlingang/kds-android-example)
**Example**: GitHub [repo](https://github.com/kotlingang/kds-android-example)

### KBundleDataStorage
> KDataStorage sync [implementation](json/json-bundle) with android `Bundle`
**Platforms**: ![android][badge-android] <br>
**Dependency**: `fun.kotlingang.kds:json-bundle:$version` <br>
**Example**: GitHub [repo](https://github.com/kotlingang/kds-android-example)

### Custom
There **are** plans for other implementations (bundle, ns-user-default, etc.), but if you want to create your implementation, take a look at the following dependencies
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/AppInfo.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
object AppInfo {
const val PACKAGE = "fun.kotlingang.kds"
const val VERSION = "5.3.0"
const val VERSION = "1.0.0"
const val NAME = "Kotlin Data Storage"
const val DESCRIPTION = "Multiplatform Coroutine-Based Kotlin Library for storing data via kotlinx.serialization"
}
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/Modules.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ val DependencyHandler.json get() = project(":json")
val DependencyHandler.core get() = project(":core")
val DependencyHandler.`shared-preferences` get() = project(":shared-preferences")
val DependencyHandler.`android-app-provider` get() = project(":android-app-provider")
val DependencyHandler.bundle get() = project(":bundle")
31 changes: 31 additions & 0 deletions bundle/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import `fun`.kotlingang.deploy.DeployEntity

plugins {
id(plugin.androidLibrary)
kotlin(plugin.android)
}

configure<DeployEntity> {
componentName = "release"
}

android {
// Workaround since explicitApi() does not work for android
kotlinOptions.freeCompilerArgs += "-Xexplicit-api=strict"
kotlinOptions.freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn"

compileSdk = Version.COMPILE_SDK

defaultConfig {
targetSdk = Version.COMPILE_SDK
minSdk = Version.MIN_SDK
}

buildFeatures {
buildConfig = false
}
}

dependencies {
api(core)
}
1 change: 1 addition & 0 deletions bundle/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest package="fun.kotlingang.kds"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package `fun`.kotlingang.kds

import android.os.Bundle


public interface InstanceStateManager {
public fun restoreInstanceState(bundle: Bundle?)
public fun saveInstanceState(bundle: Bundle)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package `fun`.kotlingang.kds

import `fun`.kotlingang.kds.annotation.RawSetterGetter
import `fun`.kotlingang.kds.extensions.bundle.contains
import `fun`.kotlingang.kds.storage.PrimitiveDataStorage
import android.os.Bundle


public open class KPrimitiveBundleDataStorage : PrimitiveDataStorage, InstanceStateManager {
private val bundle = Bundle()

override fun restoreInstanceState(bundle: Bundle?) {
if(bundle != null)
this.bundle.putAll(bundle)
}
override fun saveInstanceState(bundle: Bundle) {
bundle.putAll(this.bundle)
}

@RawSetterGetter
final override fun putString(key: String, value: String) {
bundle.putString(key, value)
}

@RawSetterGetter
final override fun putInt(key: String, value: Int) {
bundle.putInt(key, value)
}

@RawSetterGetter
final override fun putLong(key: String, value: Long) {
bundle.putLong(key, value)
}

@RawSetterGetter
final override fun putFloat(key: String, value: Float) {
bundle.putFloat(key, value)
}

@RawSetterGetter
final override fun putDouble(key: String, value: Double) {
bundle.putDouble(key, value)
}

@RawSetterGetter
final override fun putBoolean(key: String, value: Boolean) {
bundle.putBoolean(key, value)
}

@RawSetterGetter
final override fun getString(key: String): String? =
if(key in bundle)
bundle.getString(key)
else
null

@RawSetterGetter
final override fun getInt(key: String): Int? =
if(key in bundle)
bundle.getInt(key)
else
null

@RawSetterGetter
final override fun getLong(key: String): Long? =
if(key in bundle)
bundle.getLong(key)
else
null

@RawSetterGetter
final override fun getFloat(key: String): Float? =
if(key in bundle)
bundle.getFloat(key)
else
null

@RawSetterGetter
final override fun getDouble(key: String): Double? =
if(key in bundle)
bundle.getDouble(key)
else
null

@RawSetterGetter
final override fun getBoolean(key: String): Boolean? =
if(key in bundle)
bundle.getBoolean(key)
else
null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package `fun`.kotlingang.kds.extensions.bundle

import android.os.Bundle


internal operator fun Bundle.contains(key: String) = containsKey(key)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package `fun`.kotlingang.kds.extensions.instance_state_manager

import `fun`.kotlingang.kds.InstanceStateManager
import android.os.Bundle


public inline fun InstanceStateManager.fillState(bundle: Bundle?, block: () -> Unit) {
restoreInstanceState(bundle)
block()
}
32 changes: 32 additions & 0 deletions json/json-bundle/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import `fun`.kotlingang.deploy.DeployEntity

plugins {
id(plugin.androidLibrary)
kotlin(plugin.android)
}

configure<DeployEntity> {
componentName = "release"
}

android {
// Workaround since explicitApi() does not work for android
kotlinOptions.freeCompilerArgs += "-Xexplicit-api=strict"
kotlinOptions.freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn"

compileSdk = Version.COMPILE_SDK

defaultConfig {
targetSdk = Version.COMPILE_SDK
minSdk = Version.MIN_SDK
}

buildFeatures {
buildConfig = false
}
}

dependencies {
api(json)
api(bundle)
}
1 change: 1 addition & 0 deletions json/json-bundle/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest package="fun.kotlingang.kds"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package `fun`.kotlingang.kds

import `fun`.kotlingang.kds.annotation.DelicateKDSApi
import `fun`.kotlingang.kds.annotation.RawSetterGetter
import `fun`.kotlingang.kds.storage.PrimitiveDataStorage
import android.os.Bundle
import kotlinx.serialization.json.Json


public open class KBundleDataStorage private constructor (
json: Json,
private val storage: KJsonElementDataStorage
): KJsonDataStorage(json, storage), PrimitiveDataStorage, InstanceStateManager {
public constructor (
json: Json = Json
) : this(json, KJsonElementDataStorage(json))

final override fun restoreInstanceState(bundle: Bundle?) {
storage.restoreInstanceState(bundle)
}
final override fun saveInstanceState(bundle: Bundle) {
storage.saveInstanceState(bundle)
}

@RawSetterGetter
final override fun putString(key: String, value: String) {
storage.putString(key, value)
}

@RawSetterGetter
final override fun putInt(key: String, value: Int) {
storage.putInt(key, value)
}

@RawSetterGetter
final override fun putLong(key: String, value: Long) {
storage.putLong(key, value)
}

@RawSetterGetter
final override fun putFloat(key: String, value: Float) {
storage.putFloat(key, value)
}

@RawSetterGetter
final override fun putDouble(key: String, value: Double) {
storage.putDouble(key, value)
}

@RawSetterGetter
final override fun putBoolean(key: String, value: Boolean) {
storage.putBoolean(key, value)
}

@RawSetterGetter
final override fun getString(key: String): String? {
return storage.getString(key)
}

@RawSetterGetter
final override fun getInt(key: String): Int? {
return storage.getInt(key)
}

@RawSetterGetter
final override fun getLong(key: String): Long? {
return storage.getLong(key)
}

@RawSetterGetter
final override fun getFloat(key: String): Float? {
return storage.getFloat(key)
}

@RawSetterGetter
final override fun getDouble(key: String): Double? {
return storage.getDouble(key)
}

@RawSetterGetter
final override fun getBoolean(key: String): Boolean? {
return storage.getBoolean(key)
}

@OptIn(DelicateKDSApi::class, RawSetterGetter::class)
override fun commitBlocking() {
encodeReferences().forEach { (k, v) ->
storage.putJsonElement(k, v)
}
}

final override fun setupBlocking() {
super.setupBlocking()
}
}
Loading

0 comments on commit 72f2b84

Please sign in to comment.