diff --git a/core-models/src/main/java/com/anytypeio/anytype/core_models/InternalFlags.kt b/core-models/src/main/java/com/anytypeio/anytype/core_models/InternalFlags.kt index 3ff673478e..56e6059ff6 100644 --- a/core-models/src/main/java/com/anytypeio/anytype/core_models/InternalFlags.kt +++ b/core-models/src/main/java/com/anytypeio/anytype/core_models/InternalFlags.kt @@ -1,19 +1,19 @@ package com.anytypeio.anytype.core_models -sealed class InternalFlags { +sealed class InternalFlags(val code: Int) { /** * show the object type selection interface */ - object ShouldSelectType : InternalFlags() + object ShouldSelectType : InternalFlags(1) /** * Flag to remove the object from the account, in case of closing this object when it is empty */ - object ShouldEmptyDelete : InternalFlags() + object ShouldEmptyDelete : InternalFlags(0) /** * show the template selection interface */ - object ShouldSelectTemplate : InternalFlags() + object ShouldSelectTemplate : InternalFlags(2) } \ No newline at end of file diff --git a/core-models/src/main/java/com/anytypeio/anytype/core_models/ObjectWrapper.kt b/core-models/src/main/java/com/anytypeio/anytype/core_models/ObjectWrapper.kt index 86ff6f2a95..bd5275e76c 100644 --- a/core-models/src/main/java/com/anytypeio/anytype/core_models/ObjectWrapper.kt +++ b/core-models/src/main/java/com/anytypeio/anytype/core_models/ObjectWrapper.kt @@ -101,6 +101,26 @@ sealed class ObjectWrapper { val relationOptionColor: String? by default val relationReadonlyValue: Boolean? by default + + val internalFlags: List + get() = when (val value = map[Relations.INTERNAL_FLAGS]) { + is Double -> buildList { + when (value.toInt()) { + InternalFlags.ShouldSelectType.code -> InternalFlags.ShouldSelectType + InternalFlags.ShouldSelectTemplate.code -> InternalFlags.ShouldSelectTemplate + InternalFlags.ShouldEmptyDelete.code -> InternalFlags.ShouldEmptyDelete + } + } + is List<*> -> value.typeOf().mapNotNull { code -> + when (code.toInt()) { + InternalFlags.ShouldSelectType.code -> InternalFlags.ShouldSelectType + InternalFlags.ShouldSelectTemplate.code -> InternalFlags.ShouldSelectTemplate + InternalFlags.ShouldEmptyDelete.code -> InternalFlags.ShouldEmptyDelete + else -> null + } + } + else -> emptyList() + } } /** diff --git a/core-models/src/main/java/com/anytypeio/anytype/core_models/Relations.kt b/core-models/src/main/java/com/anytypeio/anytype/core_models/Relations.kt index b86bf8f254..7dde725fa0 100644 --- a/core-models/src/main/java/com/anytypeio/anytype/core_models/Relations.kt +++ b/core-models/src/main/java/com/anytypeio/anytype/core_models/Relations.kt @@ -42,7 +42,7 @@ object Relations { const val RELATION_DEFAULT_VALUE = "relationDefaultValue" const val RELATION_FORMAT_OBJECT_TYPES = "relationFormatObjectTypes" const val SOURCE_OBJECT = "sourceObject" - + const val INTERNAL_FLAGS = "internalFlags" const val PAGE_COVER = "pageCover" diff --git a/presentation/src/test/java/com/anytypeio/anytype/presentation/editor/editor/EditorInternalFlagsTest.kt b/presentation/src/test/java/com/anytypeio/anytype/presentation/editor/editor/EditorInternalFlagsTest.kt new file mode 100644 index 0000000000..d7325102fd --- /dev/null +++ b/presentation/src/test/java/com/anytypeio/anytype/presentation/editor/editor/EditorInternalFlagsTest.kt @@ -0,0 +1,102 @@ +package com.anytypeio.anytype.presentation.editor.editor + +import androidx.arch.core.executor.testing.InstantTaskExecutorRule +import com.anytypeio.anytype.core_models.Block +import com.anytypeio.anytype.core_models.InternalFlags +import com.anytypeio.anytype.core_models.ObjectType +import com.anytypeio.anytype.core_models.ObjectTypeIds +import com.anytypeio.anytype.core_models.ObjectWrapper +import com.anytypeio.anytype.core_models.Relations +import com.anytypeio.anytype.core_models.StubHeader +import com.anytypeio.anytype.core_models.StubSmartBlock +import com.anytypeio.anytype.core_models.StubTitle +import com.anytypeio.anytype.presentation.util.DefaultCoroutineTestRule +import kotlin.test.assertEquals +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.advanceUntilIdle +import kotlinx.coroutines.test.runTest +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.mockito.MockitoAnnotations + +@ExperimentalCoroutinesApi +class EditorInternalFlagsTest : EditorPresentationTestSetup() { + + @get:Rule + val rule = InstantTaskExecutorRule() + + @get:Rule + val coroutineTestRule = DefaultCoroutineTestRule() + + @Before + fun setup() { + MockitoAnnotations.openMocks(this) + } + + @Test + fun `should has all internal flags on object open`() = runTest { + val title = StubTitle() + val header = StubHeader(children = listOf(title.id)) + val page = StubSmartBlock(id = root, children = listOf(header.id)) + val document = listOf(page, header, title) + stubInterceptEvents() + + val detailsList = Block.Details( + details = mapOf( + root to Block.Fields( + mapOf( + Relations.TYPE to ObjectTypeIds.NOTE, + Relations.LAYOUT to ObjectType.Layout.NOTE.code.toDouble(), + Relations.INTERNAL_FLAGS to listOf(2.0, 0.0, 1.0) + ) + ) + ) + ) + stubOpenDocument(document = document, details = detailsList) + + val vm = buildViewModel() + + vm.onStart(root) + + advanceUntilIdle() + + val storedDetails = orchestrator.stores.details.current() + + val objectDetails = ObjectWrapper.Basic(storedDetails.details[root]?.map.orEmpty()) + + val expectedFlags = listOf( + InternalFlags.ShouldSelectTemplate, + InternalFlags.ShouldEmptyDelete, + InternalFlags.ShouldSelectType + ) + val actualFlags = objectDetails.internalFlags + + assertEquals(expected = expectedFlags, actual = actualFlags) + } + + @Test + fun `should hasn't internal flags on object open`() = runTest { + val title = StubTitle() + val header = StubHeader(children = listOf(title.id)) + val page = StubSmartBlock(id = root, children = listOf(header.id)) + val document = listOf(page, header, title) + stubInterceptEvents() + stubOpenDocument(document = document) + + val vm = buildViewModel() + + vm.onStart(root) + + advanceUntilIdle() + + val storedDetails = orchestrator.stores.details.current() + + val objectDetails = ObjectWrapper.Basic(storedDetails.details[root]?.map.orEmpty()) + + val expectedFlags = emptyList() + val actualFlags = objectDetails.internalFlags + + assertEquals(expected = expectedFlags, actual = actualFlags) + } +} \ No newline at end of file