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

Tech | Refact | Make duplicate use-case work with several targets (#1756)

This commit is contained in:
Evgenii Kozlov 2021-08-27 12:00:22 +03:00 committed by GitHub
parent 3966b5a277
commit 40a1ea1210
Signed by: github
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 44 additions and 34 deletions

View file

@ -179,11 +179,12 @@ sealed class Command {
/**
* Command for block duplication
* @property context context id
* @property original id of the original block, which we need to duplicate
* @property blocks id of the target blocks, which we need to duplicate
*/
class Duplicate(
val context: Id,
val original: Id
val target: Id,
val blocks: List<Id>
)
/**

View file

@ -89,8 +89,8 @@ class BlockDataRepository(
override suspend fun duplicate(
command: Command.Duplicate
): Pair<Id, Payload> = factory.remote.duplicate(command).let { (id, payload) ->
Pair(id, payload)
): Pair<List<Id>, Payload> = factory.remote.duplicate(command).let { (ids, payload) ->
Pair(ids, payload)
}
override suspend fun createDocument(

View file

@ -6,7 +6,7 @@ interface BlockDataStore {
suspend fun create(command: Command.Create): Pair<Id, Payload>
suspend fun replace(command: Command.Replace): Pair<Id, Payload>
suspend fun duplicate(command: Command.Duplicate): Pair<Id, Payload>
suspend fun duplicate(command: Command.Duplicate): Pair<List<Id>, Payload>
suspend fun split(command: Command.Split): Pair<Id, Payload>
suspend fun merge(command: Command.Merge): Payload

View file

@ -6,7 +6,7 @@ interface BlockRemote {
suspend fun create(command: Command.Create): Pair<String, Payload>
suspend fun replace(command: Command.Replace): Pair<String, Payload>
suspend fun duplicate(command: Command.Duplicate): Pair<String, Payload>
suspend fun duplicate(command: Command.Duplicate): Pair<List<Id>, Payload>
suspend fun split(command: Command.Split): Pair<Id, Payload>
suspend fun merge(command: Command.Merge): Payload

View file

@ -78,7 +78,7 @@ class BlockRemoteDataStore(private val remote: BlockRemote) : BlockDataStore {
override suspend fun duplicate(
command: Command.Duplicate
): Pair<String, Payload> = remote.duplicate(command)
): Pair<List<Id>, Payload> = remote.duplicate(command)
override suspend fun unlink(
command: Command.Unlink

View file

@ -1,11 +1,11 @@
package com.anytypeio.anytype.domain.block.interactor
import com.anytypeio.anytype.domain.base.BaseUseCase
import com.anytypeio.anytype.domain.base.Either
import com.anytypeio.anytype.core_models.Command
import com.anytypeio.anytype.domain.block.repo.BlockRepository
import com.anytypeio.anytype.core_models.Id
import com.anytypeio.anytype.core_models.Payload
import com.anytypeio.anytype.domain.base.BaseUseCase
import com.anytypeio.anytype.domain.base.Either
import com.anytypeio.anytype.domain.block.repo.BlockRepository
/**
* Use-case for block duplication.
@ -13,13 +13,14 @@ import com.anytypeio.anytype.core_models.Payload
*/
open class DuplicateBlock(
private val repo: BlockRepository
) : BaseUseCase<Pair<Id, Payload>, DuplicateBlock.Params>() {
) : BaseUseCase<Pair<List<Id>, Payload>, DuplicateBlock.Params>() {
override suspend fun run(params: Params) = try {
repo.duplicate(
command = Command.Duplicate(
context = params.context,
original = params.original
target = params.target,
blocks = params.blocks
)
).let {
Either.Right(it)
@ -30,10 +31,11 @@ open class DuplicateBlock(
/**
* @property context context id
* @property original id of the original block id, which we need to duplicate
* @property blocks id of the target blocks, which we need to duplicate
*/
data class Params(
val context: Id,
val original: Id
val target: Id,
val blocks: List<Id>
)
}

View file

@ -22,7 +22,7 @@ interface BlockRepository {
* Duplicates target block
* @return id of the new block and payload events.
*/
suspend fun duplicate(command: Command.Duplicate): Pair<Id, Payload>
suspend fun duplicate(command: Command.Duplicate): Pair<List<Id>, Payload>
/**
* Creates a new block.

View file

@ -92,7 +92,7 @@ class BlockMiddleware(
override suspend fun duplicate(
command: Command.Duplicate
): Pair<String, Payload> = middleware.duplicate(command)
): Pair<List<Id>, Payload> = middleware.duplicate(command)
override suspend fun move(command: Command.Move): Payload {
return middleware.move(command)

View file

@ -478,18 +478,18 @@ class Middleware(
}
@Throws(Exception::class)
fun duplicate(command: Command.Duplicate): Pair<String, Payload> {
fun duplicate(command: Command.Duplicate): Pair<List<Id>, Payload> {
val request = BlockList.Duplicate.Request(
contextId = command.context,
targetId = command.original,
blockIds = listOf(command.original),
targetId = command.target,
blockIds = command.blocks,
position = Block.Position.Bottom
)
if (BuildConfig.DEBUG) logRequest(request)
val response = service.blockListDuplicate(request)
if (BuildConfig.DEBUG) logResponse(response)
return Pair(response.blockIds.first(), response.event.toPayload())
return Pair(response.blockIds, response.event.toPayload())
}
@Throws(Exception::class)

View file

@ -1546,7 +1546,7 @@ class EditorViewModel(
dispatch(Command.PopBackStack)
}
ActionItemType.Duplicate -> {
duplicateBlock(target = id)
duplicateBlock(blocks = listOf(id))
onExitActionMode()
dispatch(Command.PopBackStack)
}
@ -1675,12 +1675,13 @@ class EditorViewModel(
}
}
private fun duplicateBlock(target: String) {
private fun duplicateBlock(blocks: List<Id>) {
viewModelScope.launch {
orchestrator.proxies.intents.send(
Intent.CRUD.Duplicate(
context = context,
target = target
target = blocks.last(),
blocks = blocks
)
)
}
@ -4628,7 +4629,7 @@ class EditorViewModel(
proceedWithUnlinking(targetId)
}
SlashItem.Actions.Duplicate -> {
duplicateBlock(targetId)
duplicateBlock(listOf(targetId))
}
SlashItem.Actions.Move -> {
viewModelScope.launch {

View file

@ -65,7 +65,8 @@ sealed class Intent {
class Duplicate(
val context: Id,
val target: Id
val target: Id,
val blocks: List<Id>
) : CRUD()
class Unlink(

View file

@ -158,11 +158,12 @@ class Orchestrator(
duplicateBlock(
params = DuplicateBlock.Params(
context = intent.context,
original = intent.target
blocks = intent.blocks,
target = intent.target
)
).proceed(
failure = defaultOnError,
success = { (id, payload) ->
success = { (ids, payload) ->
val event = EventAnalytics.Anytype(
name = BLOCK_DUPLICATE,
props = Props.empty(),
@ -171,7 +172,7 @@ class Orchestrator(
middleware = System.currentTimeMillis()
)
)
stores.focus.update(Focus(id = id, cursor = Cursor.End))
stores.focus.update(Focus(id = ids.last(), cursor = Cursor.End))
proxies.payloads.send(payload)
sendEvent(event)
}

View file

@ -1629,8 +1629,9 @@ open class EditorViewModelTest {
verify(duplicateBlock, times(1)).invoke(
params = eq(
DuplicateBlock.Params(
original = child,
context = root
target = child,
context = root,
blocks = listOf(child)
)
)
)
@ -3434,7 +3435,8 @@ open class EditorViewModelTest {
params = eq(
DuplicateBlock.Params(
context = root,
original = paragraph.id
target = paragraph.id,
blocks = listOf(paragraph.id)
)
)
)
@ -3449,7 +3451,7 @@ open class EditorViewModelTest {
duplicateBlock.stub {
onBlocking { invoke(any()) } doReturn Either.Right(
Pair(
newBlockId,
listOf(newBlockId),
Payload(
context = root,
events = emptyList()

View file

@ -205,7 +205,8 @@ class EditorSlashWidgetActionsTest : EditorPresentationTestSetup() {
val params = DuplicateBlock.Params(
context = root,
original = block.id
target = block.id,
blocks = listOf(block.id)
)
verifyBlocking(duplicateBlock, times(1)) { invoke(params) }
}
@ -242,7 +243,8 @@ class EditorSlashWidgetActionsTest : EditorPresentationTestSetup() {
val params = DuplicateBlock.Params(
context = root,
original = block.id
target = block.id,
blocks = listOf(block.id)
)
verifyBlocking(duplicateBlock, times(1)) { invoke(params) }
}