-
Notifications
You must be signed in to change notification settings - Fork 1
KVStore Demo #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
livotov
wants to merge
6
commits into
feature/oskit-compose-4.1.0
Choose a base branch
from
dev/demo_kvstore
base: feature/oskit-compose-4.1.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
KVStore Demo #2
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ef4302d
kvstore demo backend and UI skeleton
fd06c76
simple todo list for KV store demo UI and interactor
ee3fba4
docs updated
f7c42d6
workaround for KVStore not emitting initial state of the key
e95b59c
PR feedback
8ce0c10
Merge branch 'feature/oskit-compose-4.1.0' into dev/demo_kvstore
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
23 changes: 23 additions & 0 deletions
23
composeApp/src/commonMain/kotlin/service/kvstoreDemo/IKVStoreDemoService.kt
This file contains hidden or 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,23 @@ | ||
| package service.kvstoreDemo | ||
|
|
||
| import com.outsidesource.oskitkmp.filesystem.KmpFsRef | ||
| import com.outsidesource.oskitkmp.filesystem.KmpFsType | ||
| import com.outsidesource.oskitkmp.outcome.Outcome | ||
| import kotlinx.coroutines.flow.Flow | ||
| import kotlinx.serialization.Serializable | ||
| import ui.kvstoreDemo.KVStoreDemoScreenViewState | ||
|
|
||
| interface IKVStoreDemoService { | ||
| suspend fun observeTodos(): Flow<List<TodoItem>?> | ||
| suspend fun addTodoItem(title: String): Outcome<TodoItem, Any> | ||
| suspend fun removeTodoItem(id: String): Boolean | ||
| suspend fun changeState(id: String, completed: Boolean): Boolean | ||
| suspend fun rename(id: String, name: String): Boolean | ||
| } | ||
|
|
||
| @Serializable | ||
| data class TodoItem( | ||
| val id: String, | ||
| val name: String, | ||
| val completed: Boolean | ||
| ) |
112 changes: 112 additions & 0 deletions
112
composeApp/src/commonMain/kotlin/service/kvstoreDemo/KVStoreDemoService.kt
This file contains hidden or 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,112 @@ | ||
| package service.kvstoreDemo | ||
|
|
||
| import com.outsidesource.oskitkmp.outcome.Outcome | ||
| import com.outsidesource.oskitkmp.outcome.unwrapOrNull | ||
| import com.outsidesource.oskitkmp.storage.IKmpKvStore | ||
| import com.outsidesource.oskitkmp.storage.IKmpKvStoreNode | ||
| import kotlinx.coroutines.* | ||
| import kotlinx.coroutines.flow.Flow | ||
| import kotlinx.coroutines.flow.emptyFlow | ||
| import kotlinx.serialization.builtins.ListSerializer | ||
|
|
||
| private val scope = CoroutineScope(Dispatchers.Default + SupervisorJob()) | ||
|
|
||
| private const val KEY_ITEMS = "items" | ||
|
|
||
|
|
||
| /** | ||
| * A simple KVStore demo service that demonstrates how to use KmpKvStore to store and retrieve data in a reactive | ||
| * way. Note, the implementation here is simplified for the demo purposes. | ||
| * | ||
| * The KVStoreDemoService is a simple service that stores and retrieves TodoItems in a reactive way. It uses a single | ||
| * KmpKvStore node to store the serialized list of [TodoItem]. The [KmpKvStore] node is opened asynchronously when the service is | ||
| * initialized. The [KmpKvStore] node is closed when the service is destroyed. | ||
| * | ||
| * The [KVStoreDemoService] provides a flow of a list of [TodoItem] that can be observed using the observeTodos() function. | ||
| * | ||
| * The [KVStoreDemoService] provides functions to add, remove, update, and rename TodoItems using the addTodoItem(), | ||
| * removeTodoItem(), changeState(), and rename() functions, respectively. | ||
| * | ||
| * This service is being consumed in the [ui.kvstoreDemo.KVStoreDemoScreenViewInteractor]. | ||
| */ | ||
| class KVStoreDemoService( | ||
| private val storage: IKmpKvStore, | ||
| ) : IKVStoreDemoService { | ||
|
|
||
| private val node = CompletableDeferred<IKmpKvStoreNode?>() | ||
|
|
||
| init { | ||
| scope.launch { | ||
| node.complete(storage.openNode("kvstoredemo").unwrapOrNull()) | ||
| } | ||
| } | ||
|
|
||
| override suspend fun observeTodos(): Flow<List<TodoItem>?> { | ||
| return node.await()?.observeSerializable(KEY_ITEMS, ListSerializer(TodoItem.serializer())) ?: emptyFlow() | ||
| } | ||
|
|
||
| override suspend fun addTodoItem(title: String): Outcome<TodoItem, Any> { | ||
| val data = readItemsSnapshot() | ||
| val entity = TodoItem(title, title, false) | ||
| val res = writeItemsSnapshot(data.toMutableList().apply { add(entity) }) | ||
|
|
||
| return when { | ||
| res != null && res is Outcome.Ok -> Outcome.Ok(entity) | ||
| res is Outcome.Error -> Outcome.Error(res.error) | ||
| else -> Outcome.Error(IllegalStateException("Node is null")) | ||
| } | ||
| } | ||
|
|
||
| override suspend fun removeTodoItem(id: String): Boolean { | ||
| val data = readItemsSnapshot().toMutableList() | ||
| val item = data.find { it.id == id } | ||
|
|
||
| return if (item != null) { | ||
| data.remove(item) | ||
| writeItemsSnapshot(data) | ||
| true | ||
| } else { | ||
| false | ||
| } | ||
| } | ||
|
|
||
| override suspend fun changeState(id: String, completed: Boolean): Boolean { | ||
| val data = readItemsSnapshot().toMutableList() | ||
| val item = data.find { it.id == id } | ||
|
|
||
| return if (item != null) { | ||
| val index = data.indexOf(item) | ||
| data[index] = item.copy(completed = completed) | ||
| writeItemsSnapshot(data) | ||
| true | ||
| } else { | ||
| false | ||
| } | ||
| } | ||
|
|
||
| override suspend fun rename(id: String, name: String): Boolean { | ||
| val data = readItemsSnapshot().toMutableList() | ||
| val item = data.find { it.id == id } | ||
|
|
||
| return if (item != null) { | ||
| val index = data.indexOf(item) | ||
| data[index] = item.copy(name = name) | ||
| writeItemsSnapshot(data) | ||
| true | ||
| } else { | ||
| false | ||
| } | ||
| } | ||
|
|
||
| private suspend fun readItemsSnapshot(): List<TodoItem> { | ||
| return node.await()?.getSerializable( | ||
| KEY_ITEMS, | ||
| ListSerializer(TodoItem.serializer()) | ||
| ).orEmpty().toMutableList() | ||
| } | ||
|
|
||
| private suspend fun writeItemsSnapshot(data: List<TodoItem>): Outcome<Unit, Any>? { | ||
| return node.await()?.putSerializable(KEY_ITEMS, data, ListSerializer(TodoItem.serializer())) | ||
| } | ||
|
|
||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.