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

Refact | New page creation flow (#1727)

This commit is contained in:
Evgenii Kozlov 2021-08-11 12:25:43 +03:00 committed by GitHub
parent a8eb513091
commit 6dd02faf63
Signed by: github
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 28 additions and 45 deletions

View file

@ -42,8 +42,10 @@ class BlockDataRepository(
command: Command.UpdateAlignment
): Payload = factory.remote.updateAlignment(command)
override suspend fun createPage(parentId: String, emoji: String?) =
factory.remote.createPage(parentId, emoji)
override suspend fun createPage(ctx: Id?, emoji: String?) = factory.remote.createPage(
ctx = ctx,
emoji = emoji
)
override suspend fun closePage(id: String) {
factory.remote.closePage(id)

View file

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

View file

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

View file

@ -15,8 +15,10 @@ class BlockRemoteDataStore(private val remote: BlockRemote) : BlockDataStore {
remote.closeDashboard(id = id)
}
override suspend fun createPage(parentId: String, emoji: String?): String =
remote.createPage(parentId, emoji)
override suspend fun createPage(
ctx: Id?,
emoji: String?
): Id = remote.createPage(ctx = ctx, emoji = emoji)
override suspend fun openPage(id: String): Payload = remote.openPage(id)
override suspend fun openProfile(id: String): Payload = remote.openProfile(id)

View file

@ -71,8 +71,7 @@ interface BlockRepository {
suspend fun getConfig(): Config
@Deprecated("Should be replaced by createDocument() command")
suspend fun createPage(parentId: String, emoji: String? = null): Id
suspend fun createPage(ctx: Id?, emoji: String?): Id
suspend fun openPage(id: String): Result<Payload>

View file

@ -1,10 +1,8 @@
package com.anytypeio.anytype.domain.page
import com.anytypeio.anytype.domain.base.BaseUseCase
import com.anytypeio.anytype.domain.base.Either
import com.anytypeio.anytype.domain.block.repo.BlockRepository
import com.anytypeio.anytype.core_models.Id
import com.anytypeio.anytype.domain.config.MainConfig
import com.anytypeio.anytype.domain.base.BaseUseCase
import com.anytypeio.anytype.domain.block.repo.BlockRepository
import com.anytypeio.anytype.domain.icon.DocumentEmojiIconProvider
/**
@ -16,34 +14,15 @@ class CreatePage(
private val documentEmojiIconProvider: DocumentEmojiIconProvider
) : BaseUseCase<Id, CreatePage.Params>() {
override suspend fun run(params: Params) = try {
if (params.id == MainConfig.HOME_DASHBOARD_ID) {
repo.getConfig().let { config ->
repo.createPage(
parentId = config.home,
emoji = documentEmojiIconProvider.random()
).let {
Either.Right(it)
}
}
} else {
repo.createPage(
parentId = params.id,
emoji = documentEmojiIconProvider.random()
).let {
Either.Right(it)
}
}
} catch (t: Throwable) {
Either.Left(t)
override suspend fun run(params: Params) = safe {
repo.createPage(
ctx = params.ctx,
emoji = documentEmojiIconProvider.random()
)
}
/**
* @property id parent id for a new page
* @property [ctx] context (parent) for this new page.
*/
data class Params(val id: String) {
companion object {
fun insideDashboard() = Params(MainConfig.HOME_DASHBOARD_ID)
}
}
data class Params(val ctx: Id?)
}

View file

@ -22,8 +22,9 @@ class BlockMiddleware(
middleware.closeDashboard(id)
}
override suspend fun createPage(parentId: String, emoji: String?): String =
middleware.createPage(parentId, emoji)
override suspend fun createPage(
ctx: Id?, emoji: String?
): String = middleware.createPage(ctx = ctx, emoji = emoji)
override suspend fun createPage(command: Command.CreateNewDocument): String =
middleware.createPage(command)

View file

@ -182,7 +182,7 @@ class Middleware(
}
@Throws(Exception::class)
fun createPage(parentId: String, emoji: String?): String {
fun createPage(ctx: Id?, emoji: String?): Id {
val details = if (emoji != null)
mapOf(iconEmojiKey to emoji)
@ -190,7 +190,7 @@ class Middleware(
emptyMap()
val request = Rpc.Block.CreatePage.Request(
contextId = parentId,
contextId = ctx.orEmpty(),
details = details,
position = Block.Position.Inner
)

View file

@ -177,7 +177,7 @@ class HomeDashboardViewModel(
fun onAddNewDocumentClicked() {
val startTime = System.currentTimeMillis()
createPage.invoke(viewModelScope, CreatePage.Params.insideDashboard()) { result ->
createPage.invoke(viewModelScope, CreatePage.Params(ctx = null)) { result ->
result.either(
fnL = { e -> Timber.e(e, "Error while creating a new page") },
fnR = { id ->

View file

@ -3653,7 +3653,7 @@ class EditorViewModel(
val startTime = System.currentTimeMillis()
createPage(
scope = viewModelScope,
params = CreatePage.Params.insideDashboard()
params = CreatePage.Params(null)
) { result ->
result.either(
fnL = { Timber.e(it, "Error while creating a new page on home dashboard") },

View file

@ -2453,7 +2453,7 @@ open class EditorViewModelTest {
verify(createPage, times(1)).invoke(
scope = any(),
params = eq(CreatePage.Params.insideDashboard()),
params = eq(CreatePage.Params(null)),
onResult = any()
)
}