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

DROID-3409 Primitives | Refactor Object Layout Mapping Logic for Readability and Legacy Key Handling (#2300)

This commit is contained in:
Konstantin Ivanov 2025-04-12 18:04:55 +02:00 committed by GitHub
parent 800c82ceed
commit b74d77a9df
Signed by: github
GPG key ID: B5690EEEBB952194
4 changed files with 66 additions and 15 deletions

View file

@ -51,11 +51,16 @@ sealed class ObjectWrapper {
val links: List<Id> get() = getValues(Relations.LINKS)
val layout: ObjectType.Layout?
get() = when (val value = map[Relations.LAYOUT]) {
is Double -> ObjectType.Layout.entries.singleOrNull { layout ->
layout.code == value.toInt()
get() {
// Try legacy layout first, then fallback to resolved layout.
val layoutValue = when {
map[Relations.LEGACY_LAYOUT] is Double -> map[Relations.LEGACY_LAYOUT] as Double
map[Relations.LAYOUT] is Double -> map[Relations.LAYOUT] as Double
else -> null
}
return layoutValue?.let { value ->
ObjectType.Layout.entries.singleOrNull { it.code == value.toInt() }
}
else -> null
}
val id: Id by default
@ -182,12 +187,18 @@ sealed class ObjectWrapper {
else -> ObjectType.Layout.BASIC
}
val layout: ObjectType.Layout?
get() = when (val value = map[Relations.LAYOUT]) {
is Double -> ObjectType.Layout.entries.singleOrNull { layout ->
layout.code == value.toInt()
get() {
// Try legacy layout first, then fallback to resolved layout.
val layoutValue = when {
map[Relations.LEGACY_LAYOUT] is Double -> map[Relations.LEGACY_LAYOUT] as Double
map[Relations.LAYOUT] is Double -> map[Relations.LAYOUT] as Double
else -> null
}
return layoutValue?.let { value ->
ObjectType.Layout.entries.singleOrNull { it.code == value.toInt() }
}
else -> null
}
val defaultTemplateId: Id? by default
val restrictions: List<ObjectRestriction>

View file

@ -88,11 +88,17 @@ fun ObjectView.toObjectPermissions(
}
val isTemplateObject = (typeUniqueKey == ObjectTypeIds.TEMPLATE)
val currentLayout = when (val value = details[root]?.getOrDefault(Relations.LAYOUT, null)) {
is Double -> ObjectType.Layout.entries.singleOrNull { layout ->
layout.code == value.toInt()
}
val detailsForRoot = details[root]
val rawLayoutValue = if (detailsForRoot?.containsKey(Relations.LEGACY_LAYOUT) == true) {
detailsForRoot[Relations.LEGACY_LAYOUT]
} else {
detailsForRoot?.get(Relations.LAYOUT)
}
val currentLayout = when (rawLayoutValue) {
is Double -> ObjectType.Layout.entries.singleOrNull { layout ->
layout.code == rawLayoutValue.toInt()
}
else -> null
}

View file

@ -6858,9 +6858,6 @@ class EditorViewModel(
return getObjectTypeUniqueKeyFromDetails() == ObjectTypeIds.TEMPLATE
}
fun Struct?.isObjectParticipant(): Boolean =
this?.getOrDefault(Relations.LAYOUT, null) == ObjectType.Layout.PARTICIPANT.code.toDouble()
fun onSelectTemplateClicked() {
viewModelScope.launch {
sendAnalyticsSelectTemplateEvent(analytics)

View file

@ -1,6 +1,7 @@
package com.anytypeio.anytype.presentation.mapper
import com.anytypeio.anytype.core_models.ObjectType
import com.anytypeio.anytype.core_models.ObjectType.Layout
import com.anytypeio.anytype.core_models.ObjectWrapper
import com.anytypeio.anytype.core_models.Relations
import com.anytypeio.anytype.domain.debugging.Logger
@ -190,6 +191,42 @@ class ObjectWrapperExtensionsKtTest {
)
}
@Test
fun testLegacyLayoutTakesPrecedence() {
val details = mapOf(
Relations.ID to "root",
Relations.LEGACY_LAYOUT to 1.0, // should map to Layout.PROFILE
Relations.LAYOUT to 3.0 // would map to Set, but legacy should win
)
val result = ObjectWrapper.Basic(details)
val resultType = ObjectWrapper.Type(details)
assertEquals(Layout.PROFILE, result.layout)
assertEquals(Layout.PROFILE, resultType.layout)
}
@Test
fun testResolvedLayoutUsedWhenLegacyAbsent() {
val details = mapOf(
Relations.ID to "root",
Relations.LAYOUT to 3.0, // should map to Layout.Set
)
val result = ObjectWrapper.Basic(details)
val resultType = ObjectWrapper.Type(details)
assertEquals(Layout.SET, result.layout)
assertEquals(Layout.SET, resultType.layout)
}
@Test
fun testNonDoubleValueReturnsNull() {
val details = mapOf(
Relations.LEGACY_LAYOUT to "invalid" // invalid value type
)
val result = ObjectWrapper.Basic(details)
val resultType = ObjectWrapper.Type(details)
assertNull(result.layout)
assertNull(resultType.layout)
}
fun stubUrlBuilder(targetObjectId: String) {
urlBuilder.stub {
on { large(targetObjectId) } doReturn URL + targetObjectId