From 42f9cf3640ae140c06d84b6e1f8fce45b4c84b49 Mon Sep 17 00:00:00 2001 From: ShubertMunthali Date: Thu, 28 Dec 2023 12:27:22 +0200 Subject: [PATCH] Adopt ARK storage for versions --- .../arkmemo/models/VersionsResult.kt | 1 + ...ersionStorage.kt => RootVersionStorage.kt} | 3 +-- .../repo/versions/RootVersionsStorage.kt | 23 +++++++++++++++++++ .../{models => repo/versions}/Version.kt | 4 +++- .../arkmemo/repo/versions/VersionStorage.kt | 7 +++--- .../repo/versions/VersionStorageRepo.kt | 4 ++-- .../arkmemo/repo/versions/Versions.kt | 20 ++++++++++++++++ .../arkmemo/repo/versions/VersionsStorage.kt | 15 ++++++++++++ .../repo/versions/VersionsStorageRepo.kt | 4 ++++ .../ui/viewmodels/VersionsViewModel.kt | 3 +-- .../arkbuilders/arkmemo/VersionStorageTest.kt | 6 ++--- 11 files changed, 76 insertions(+), 14 deletions(-) rename app/src/main/java/dev/arkbuilders/arkmemo/repo/versions/{PlainVersionStorage.kt => RootVersionStorage.kt} (98%) create mode 100644 app/src/main/java/dev/arkbuilders/arkmemo/repo/versions/RootVersionsStorage.kt rename app/src/main/java/dev/arkbuilders/arkmemo/{models => repo/versions}/Version.kt (59%) create mode 100644 app/src/main/java/dev/arkbuilders/arkmemo/repo/versions/Versions.kt create mode 100644 app/src/main/java/dev/arkbuilders/arkmemo/repo/versions/VersionsStorage.kt create mode 100644 app/src/main/java/dev/arkbuilders/arkmemo/repo/versions/VersionsStorageRepo.kt diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/models/VersionsResult.kt b/app/src/main/java/dev/arkbuilders/arkmemo/models/VersionsResult.kt index 9fc8704e..0ceb0139 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/models/VersionsResult.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/models/VersionsResult.kt @@ -1,6 +1,7 @@ package dev.arkbuilders.arkmemo.models import dev.arkbuilders.arklib.ResourceId +import dev.arkbuilders.arkmemo.repo.versions.Version data class VersionsResult ( val versions: List, diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/repo/versions/PlainVersionStorage.kt b/app/src/main/java/dev/arkbuilders/arkmemo/repo/versions/RootVersionStorage.kt similarity index 98% rename from app/src/main/java/dev/arkbuilders/arkmemo/repo/versions/PlainVersionStorage.kt rename to app/src/main/java/dev/arkbuilders/arkmemo/repo/versions/RootVersionStorage.kt index e9b53f8a..3202b757 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/repo/versions/PlainVersionStorage.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/repo/versions/RootVersionStorage.kt @@ -5,7 +5,6 @@ import dev.arkbuilders.arklib.ResourceId import dev.arkbuilders.arklib.arkFolder import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext -import dev.arkbuilders.arkmemo.models.Version import dev.arkbuilders.arkmemo.models.VersionsResult import space.taran.arkmemo.utils.arkVersions import java.nio.file.Files @@ -13,7 +12,7 @@ import java.nio.file.Path import java.nio.file.attribute.FileTime import kotlin.io.path.writeLines -class PlainVersionStorage(private val root: Path): VersionStorage { +class RootVersionStorage(private val root: Path): VersionStorage { private val storageFile = root.arkFolder().arkVersions() private var lastModified = FileTime.fromMillis(0L) diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/repo/versions/RootVersionsStorage.kt b/app/src/main/java/dev/arkbuilders/arkmemo/repo/versions/RootVersionsStorage.kt new file mode 100644 index 00000000..36ba7a84 --- /dev/null +++ b/app/src/main/java/dev/arkbuilders/arkmemo/repo/versions/RootVersionsStorage.kt @@ -0,0 +1,23 @@ +package dev.arkbuilders.arkmemo.repo.versions + +import dev.arkbuilders.arklib.ResourceId +import dev.arkbuilders.arklib.arkFolder +import dev.arkbuilders.arklib.data.storage.FileStorage +import kotlinx.coroutines.CoroutineScope +import space.taran.arkmemo.utils.arkVersions +import java.nio.file.Path + +class RootVersionsStorage( + private val scope: CoroutineScope, + private val root: Path +): + FileStorage("versions", scope, root.arkFolder().arkVersions(), VersionsMonoid), + VersionsStorage { + + override fun valueFromString(raw: String): Versions = + raw.split(",").filter { it.isNotEmpty() }.map { + ResourceId.fromString(it) + }.toSet() + + override fun valueToString(value: Versions): String = value.joinToString(",") +} diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/models/Version.kt b/app/src/main/java/dev/arkbuilders/arkmemo/repo/versions/Version.kt similarity index 59% rename from app/src/main/java/dev/arkbuilders/arkmemo/models/Version.kt rename to app/src/main/java/dev/arkbuilders/arkmemo/repo/versions/Version.kt index 83bd61e7..0b32cb77 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/models/Version.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/repo/versions/Version.kt @@ -1,7 +1,9 @@ -package dev.arkbuilders.arkmemo.models +package dev.arkbuilders.arkmemo.repo.versions import dev.arkbuilders.arklib.ResourceId +typealias Version2 = ResourceId + data class Version( val parent: ResourceId, val child: ResourceId diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/repo/versions/VersionStorage.kt b/app/src/main/java/dev/arkbuilders/arkmemo/repo/versions/VersionStorage.kt index 9fda9da9..28d0e9c2 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/repo/versions/VersionStorage.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/repo/versions/VersionStorage.kt @@ -1,9 +1,10 @@ package dev.arkbuilders.arkmemo.repo.versions import dev.arkbuilders.arklib.ResourceId -import dev.arkbuilders.arkmemo.models.Version +import dev.arkbuilders.arklib.data.storage.Storage + +interface VersionStorage: Storage { -interface VersionStorage { suspend fun add(version: Version) suspend fun forget(id: ResourceId) @@ -19,6 +20,4 @@ interface VersionStorage { fun childrenNotParents(): List fun isLatestResourceVersion(id: ResourceId): Boolean - - suspend fun persist() } \ No newline at end of file diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/repo/versions/VersionStorageRepo.kt b/app/src/main/java/dev/arkbuilders/arkmemo/repo/versions/VersionStorageRepo.kt index 6ae2cb03..77b515a7 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/repo/versions/VersionStorageRepo.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/repo/versions/VersionStorageRepo.kt @@ -8,12 +8,12 @@ class VersionStorageRepo @Inject constructor( private val memoPreferences: MemoPreferences ) { - private val storageByRoot = mutableMapOf() + private val storageByRoot = mutableMapOf() suspend fun provide(): VersionStorage { val root = memoPreferences.getNotesStorage() if (storageByRoot[root] == null) { - val versionStorage = PlainVersionStorage(root) + val versionStorage = RootVersionStorage(root) versionStorage.init() storageByRoot[root] = versionStorage } diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/repo/versions/Versions.kt b/app/src/main/java/dev/arkbuilders/arkmemo/repo/versions/Versions.kt new file mode 100644 index 00000000..a6d37b8e --- /dev/null +++ b/app/src/main/java/dev/arkbuilders/arkmemo/repo/versions/Versions.kt @@ -0,0 +1,20 @@ +package dev.arkbuilders.arkmemo.repo.versions + +import android.util.Log +import dev.arkbuilders.arklib.ResourceId +import dev.arkbuilders.arklib.data.storage.Monoid + +typealias Versions = Set + +object VersionsMonoid : Monoid { + + override val neutral: Versions = setOf() + + override fun combine(a: Versions, b: Versions): Versions { + val result = a.union(b) + Log.d(LOG_PREFIX, "merging $a and $b into $result") + return result + } +} + +internal val LOG_PREFIX: String = "[versions]" \ No newline at end of file diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/repo/versions/VersionsStorage.kt b/app/src/main/java/dev/arkbuilders/arkmemo/repo/versions/VersionsStorage.kt new file mode 100644 index 00000000..57ff8a02 --- /dev/null +++ b/app/src/main/java/dev/arkbuilders/arkmemo/repo/versions/VersionsStorage.kt @@ -0,0 +1,15 @@ +package dev.arkbuilders.arkmemo.repo.versions + +import dev.arkbuilders.arklib.ResourceId +import dev.arkbuilders.arklib.data.storage.Storage + +interface VersionsStorage: Storage { + + fun setParents(child: ResourceId, parents: Versions) = setValue(child, parents) + + fun getParents(child: ResourceId): Versions = getValue(child) + + fun removeParents(child: ResourceId) { + remove(child) + } +} \ No newline at end of file diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/repo/versions/VersionsStorageRepo.kt b/app/src/main/java/dev/arkbuilders/arkmemo/repo/versions/VersionsStorageRepo.kt new file mode 100644 index 00000000..a9c6dd1d --- /dev/null +++ b/app/src/main/java/dev/arkbuilders/arkmemo/repo/versions/VersionsStorageRepo.kt @@ -0,0 +1,4 @@ +package dev.arkbuilders.arkmemo.repo.versions + +class VersionsStorageRepo { +} \ No newline at end of file diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/ui/viewmodels/VersionsViewModel.kt b/app/src/main/java/dev/arkbuilders/arkmemo/ui/viewmodels/VersionsViewModel.kt index ea8afafa..acbfae16 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/ui/viewmodels/VersionsViewModel.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/ui/viewmodels/VersionsViewModel.kt @@ -1,6 +1,5 @@ package dev.arkbuilders.arkmemo.ui.viewmodels -import android.util.Log import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import dagger.hilt.android.lifecycle.HiltViewModel @@ -10,7 +9,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.launch -import dev.arkbuilders.arkmemo.models.Version +import dev.arkbuilders.arkmemo.repo.versions.Version import dev.arkbuilders.arkmemo.repo.versions.VersionStorage import dev.arkbuilders.arkmemo.repo.versions.VersionStorageRepo import javax.inject.Inject diff --git a/app/src/test/java/dev/arkbuilders/arkmemo/VersionStorageTest.kt b/app/src/test/java/dev/arkbuilders/arkmemo/VersionStorageTest.kt index 57d62bfe..bc041736 100644 --- a/app/src/test/java/dev/arkbuilders/arkmemo/VersionStorageTest.kt +++ b/app/src/test/java/dev/arkbuilders/arkmemo/VersionStorageTest.kt @@ -1,8 +1,8 @@ package space.taran.arkmemo import space.taran.arklib.ResourceId -import dev.arkbuilders.arkmemo.models.Version -import dev.arkbuilders.arkmemo.repo.versions.PlainVersionStorage +import dev.arkbuilders.arkmemo.repo.versions.Version +import dev.arkbuilders.arkmemo.repo.versions.RootVersionStorage import org.junit.Test import org.junit.Assert.* import java.io.File @@ -10,7 +10,7 @@ import java.io.File class VersionStorageTest { private val path = File(ROOT_PATH).toPath() - private val versionStorage = PlainVersionStorage(path) + private val versionStorage = RootVersionStorage(path) @Test fun versions_size_is_correct() {