1
0
Fork 0
mirror of https://github.com/anyproto/anytype-kotlin.git synced 2025-06-08 05:47:05 +09:00

DROID-940 Relations | Enhancement | Added ability to track changes in global store of relations (#3005)

This commit is contained in:
Evgenii Kozlov 2023-03-13 18:30:01 +01:00 committed by GitHub
parent 1e3da54ae4
commit 4de4ca24f3
Signed by: github
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View file

@ -6,6 +6,9 @@ import com.anytypeio.anytype.core_models.ObjectWrapper
import com.anytypeio.anytype.core_models.Struct
import com.anytypeio.anytype.domain.`object`.amend
import com.anytypeio.anytype.domain.`object`.unset
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
@ -20,6 +23,13 @@ interface StoreOfRelations {
suspend fun set(target: Id, data: Struct)
suspend fun remove(target: Id)
suspend fun clear()
fun trackChanges() : Flow<TrackedEvent>
sealed class TrackedEvent {
object Init : TrackedEvent()
object Change: TrackedEvent()
}
}
class DefaultStoreOfRelations : StoreOfRelations {
@ -28,6 +38,8 @@ class DefaultStoreOfRelations : StoreOfRelations {
private val store = mutableMapOf<Id, ObjectWrapper.Relation>()
private val keysToIds = mutableMapOf<Key, Id>()
private val updates = MutableSharedFlow<StoreOfRelations.TrackedEvent>()
override val size: Int get() = store.size
override suspend fun getByKey(key: Key): ObjectWrapper.Relation? = mutex.withLock {
@ -55,6 +67,7 @@ class DefaultStoreOfRelations : StoreOfRelations {
store[o.id] = current.amend(o.map)
}
}
updates.emit(StoreOfRelations.TrackedEvent.Change)
}
override suspend fun amend(target: Id, diff: Map<Id, Any?>): Unit = mutex.withLock {
@ -64,6 +77,7 @@ class DefaultStoreOfRelations : StoreOfRelations {
} else {
store[target] = ObjectWrapper.Relation(diff).also { keysToIds[it.key] = target }
}
updates.emit(StoreOfRelations.TrackedEvent.Change)
}
override suspend fun set(
@ -71,6 +85,7 @@ class DefaultStoreOfRelations : StoreOfRelations {
data: Map<String, Any?>
): Unit = mutex.withLock {
store[target] = ObjectWrapper.Relation(data).also { keysToIds[it.key] = target }
updates.emit(StoreOfRelations.TrackedEvent.Change)
}
override suspend fun unset(
@ -81,6 +96,7 @@ class DefaultStoreOfRelations : StoreOfRelations {
if (current != null) {
store[target] = current.unset(keys)
}
updates.emit(StoreOfRelations.TrackedEvent.Change)
}
override suspend fun remove(target: Id) : Unit = mutex.withLock {
@ -89,10 +105,15 @@ class DefaultStoreOfRelations : StoreOfRelations {
keysToIds.remove(current.key)
store.remove(target)
}
updates.emit(StoreOfRelations.TrackedEvent.Change)
}
override suspend fun clear(): Unit = mutex.withLock {
keysToIds.clear()
store.clear()
}
override fun trackChanges(): Flow<StoreOfRelations.TrackedEvent> = updates.onStart {
emit(StoreOfRelations.TrackedEvent.Init)
}
}

View file

@ -65,9 +65,10 @@ class RelationListViewModel(
)
jobs += viewModelScope.launch {
combine(
storeOfRelations.trackChanges(),
relationListProvider.links,
relationListProvider.details
) { relationLinks, details ->
) { _, relationLinks, details ->
val relations = relationLinks.mapNotNull { storeOfRelations.getByKey(it.key) }
val detail = details.details[ctx]
val values = detail?.map ?: emptyMap()