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

Protocol | Api for creating objects with template (#2190)

This commit is contained in:
Evgenii Kozlov 2022-04-19 16:06:20 +03:00 committed by GitHub
parent 98b5bce45d
commit 29e55a0779
Signed by: github
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 95 additions and 28 deletions

View file

@ -139,6 +139,7 @@ sealed class Command {
* @property emoji random emoji for new page
* @property type object type
* @property layout object layout
* @property [template] id of the template for this object (optional)
*/
class CreateDocument(
val context: Id,
@ -146,7 +147,8 @@ sealed class Command {
val position: Position,
val emoji: String?,
val type: String?,
val layout: ObjectType.Layout?
val layout: ObjectType.Layout?,
val template: Id? = null
)
/**

View file

@ -26,7 +26,7 @@ class BlockDataRepository(
Result.Success(factory.remote.openPage(id))
} catch (e: BackwardCompatilityNotSupportedException) {
Result.Failure(Error.BackwardCompatibility)
} catch (e : NotFoundObjectException) {
} catch (e: NotFoundObjectException) {
Result.Failure(Error.NotFoundObject)
}
@ -37,7 +37,7 @@ class BlockDataRepository(
Result.Success(factory.remote.openObjectSet(id))
} catch (e: BackwardCompatilityNotSupportedException) {
Result.Failure(Error.BackwardCompatibility)
} catch (e : NotFoundObjectException) {
} catch (e: NotFoundObjectException) {
Result.Failure(Error.NotFoundObject)
}
@ -49,11 +49,18 @@ class BlockDataRepository(
command: Command.UpdateAlignment
): Payload = factory.remote.updateAlignment(command)
override suspend fun createPage(ctx: Id?, emoji: String?, isDraft: Boolean?, type: String?) = factory.remote.createPage(
override suspend fun createPage(
ctx: Id?,
emoji: String?,
isDraft: Boolean?,
type: String?,
template: Id?
) = factory.remote.createPage(
ctx = ctx,
emoji = emoji,
isDraft = isDraft,
type = type
type = type,
template = template
)
override suspend fun closePage(id: String) {
@ -485,7 +492,8 @@ class BlockDataRepository(
)
override suspend fun debugSync(): String = factory.remote.debugSync()
override suspend fun debugLocalStore(path: String): String = factory.remote.debugLocalStore(path)
override suspend fun debugLocalStore(path: String): String =
factory.remote.debugLocalStore(path)
override suspend fun updateDetail(
ctx: Id,
@ -534,7 +542,8 @@ class BlockDataRepository(
isArchived = isArchived
)
override suspend fun deleteObjects(targets: List<Id>) = factory.remote.deleteObjects(targets = targets)
override suspend fun deleteObjects(targets: List<Id>) =
factory.remote.deleteObjects(targets = targets)
override suspend fun setObjectLayout(ctx: Id, layout: ObjectType.Layout): Payload =
factory.remote.setObjectLayout(ctx, layout)

View file

@ -26,7 +26,13 @@ interface BlockDataStore {
suspend fun move(command: Command.Move): Payload
suspend fun unlink(command: Command.Unlink): Payload
suspend fun getConfig(): Config
suspend fun createPage(ctx: Id?, emoji: String?, isDraft: Boolean?, type: String?): Id
suspend fun createPage(
ctx: Id?,
emoji: String?,
isDraft: Boolean?,
type: String?,
template: Id?
): Id
suspend fun openPage(id: String): Payload
suspend fun openObjectSet(id: String): Payload
suspend fun openProfile(id: String): Payload

View file

@ -23,7 +23,13 @@ interface BlockRemote {
suspend fun updateCheckbox(command: Command.UpdateCheckbox): Payload
suspend fun move(command: Command.Move): Payload
suspend fun getConfig(): Config
suspend fun createPage(ctx: Id?, emoji: String?, isDraft: Boolean?, type: String?): Id
suspend fun createPage(
ctx: Id?,
emoji: String?,
isDraft: Boolean?,
type: String?,
template: Id?
): Id
suspend fun createPage(command: Command.CreateNewDocument): String
suspend fun openPage(id: String): Payload
suspend fun openProfile(id: String): Payload

View file

@ -19,8 +19,15 @@ class BlockRemoteDataStore(private val remote: BlockRemote) : BlockDataStore {
ctx: Id?,
emoji: String?,
isDraft: Boolean?,
type: String?
): Id = remote.createPage(ctx = ctx, emoji = emoji, isDraft = isDraft, type = type)
type: String?,
template: Id?
): Id = remote.createPage(
ctx = ctx,
emoji = emoji,
isDraft = isDraft,
type = type,
template = template
)
override suspend fun openPage(id: String): Payload = remote.openPage(id)
override suspend fun openProfile(id: String): Payload = remote.openProfile(id)

View file

@ -69,7 +69,13 @@ interface BlockRepository {
suspend fun getConfig(): Config
suspend fun createPage(ctx: Id?, emoji: String?, isDraft: Boolean?, type: String?): Id
suspend fun createPage(
ctx: Id?,
emoji: String?,
isDraft: Boolean?,
type: String?,
template: Id?
): Id
suspend fun openPage(id: String): Result<Payload>

View file

@ -1,8 +1,8 @@
package com.anytypeio.anytype.domain.page
import com.anytypeio.anytype.core_models.Command
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.domain.icon.DocumentEmojiIconProvider
@ -27,6 +27,10 @@ class CreateNewDocument(
)
}
/**
* [name] name for new object
* [type] type for new object
*/
data class Params(
val name: String,
val type: String?

View file

@ -19,7 +19,8 @@ class CreateObject(
position = params.position,
emoji = null,
type = params.type,
layout = params.layout
layout = params.layout,
template = params.template
)
).let { (id, target, payload) ->
Either.Right(
@ -39,13 +40,15 @@ class CreateObject(
* @property context id of the context of the block (i.e. page, dashboard or something else)
* @property target id of the block associated with the block we need to create
* @property position position of the block that we need to create in relation with the target block
* @property [template] id of the template for this object (optional)
*/
data class Params(
val context: Id,
val target: Id,
val position: Position,
val type: String,
val layout: ObjectType.Layout
val layout: ObjectType.Layout,
val template: Id? = null
)
/**
@ -55,8 +58,8 @@ class CreateObject(
* @property payload payload of events
*/
data class Result(
val id: String,
val target: String,
val id: Id,
val target: Id,
val payload: Payload
)
}

View file

@ -17,7 +17,8 @@ class CreatePage(
ctx = params.ctx,
emoji = null,
isDraft = params.isDraft,
type = params.type
type = params.type,
template = params.template
)
}
@ -25,11 +26,13 @@ class CreatePage(
* @property [ctx] context (parent) for this new page.
* @property [type] type of created object
* @property [isDraft] should this object be in Draft state
* @property [template] id of the template for this object (optional)
*/
data class Params(
val ctx: Id?,
val type: String?,
val emoji: String?,
val isDraft: Boolean?
val isDraft: Boolean?,
val template: Id? = null
)
}

View file

@ -23,11 +23,22 @@ class BlockMiddleware(
}
override suspend fun createPage(
ctx: Id?, emoji: String?, isDraft: Boolean?, type: String?
): String = middleware.createPage(ctx = ctx, emoji = emoji, isDraft = isDraft, type = type)
ctx: Id?,
emoji: String?,
isDraft: Boolean?,
type: String?,
template: Id?
): String = middleware.createPage(
ctx = ctx,
emoji = emoji,
isDraft = isDraft,
type = type,
template = template
)
override suspend fun createPage(command: Command.CreateNewDocument): String =
middleware.createPage(command)
override suspend fun createPage(
command: Command.CreateNewDocument
): String = middleware.createPage(command)
override suspend fun openPage(id: String): Payload = middleware.openBlock(id)
override suspend fun openProfile(id: String): Payload = middleware.openBlock(id)

View file

@ -212,7 +212,13 @@ class Middleware(
}
@Throws(Exception::class)
fun createPage(ctx: Id?, emoji: String?, isDraft: Boolean?, type: String?): Id {
fun createPage(
ctx: Id?,
emoji: String?,
isDraft: Boolean?,
type: String?,
template: Id?
): Id {
val details: MutableMap<String, Any> = mutableMapOf()
emoji?.let { details[iconEmojiKey] = it }
@ -222,7 +228,8 @@ class Middleware(
val request = Rpc.Block.CreatePage.Request(
contextId = ctx.orEmpty(),
details = details,
position = Block.Position.Inner
position = Block.Position.Inner,
templateId = template.orEmpty()
)
if (BuildConfig.DEBUG) logRequest(request)
@ -450,7 +457,8 @@ class Middleware(
contextId = command.context,
targetId = command.target,
position = position,
details = details
details = details,
templateId = command.template.orEmpty()
)
if (BuildConfig.DEBUG) logRequest(request)
@ -467,7 +475,7 @@ class Middleware(
}
@Throws(Exception::class)
fun createPage(command: Command.CreateNewDocument): String {
fun createPage(command: Command.CreateNewDocument): Id {
val details: MutableMap<String, Any> = mutableMapOf()
@ -475,7 +483,9 @@ class Middleware(
command.name.let { details[nameKey] = it }
command.type?.let { details[typeKey] = it }
val request = Rpc.Page.Create.Request(details = details.toMap())
val request = Rpc.Page.Create.Request(
details = details.toMap()
)
if (BuildConfig.DEBUG) logRequest(request)