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

Set random emoji when creating a new page (#605)

This commit is contained in:
Evgenii Kozlov 2020-07-28 17:42:37 +02:00 committed by GitHub
parent ab417e6182
commit 670af5cdec
Signed by: github
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 187 additions and 22 deletions

View file

@ -4,6 +4,8 @@
### New features 🚀
* Setting random emoji icon when creating a new page (#603)
### Design & UX 🔳
* Redesigned block toolbar (#590)

View file

@ -11,6 +11,7 @@ import com.agileburo.anytype.domain.dashboard.interactor.CloseDashboard
import com.agileburo.anytype.domain.dashboard.interactor.OpenDashboard
import com.agileburo.anytype.domain.event.interactor.EventChannel
import com.agileburo.anytype.domain.event.interactor.InterceptEvents
import com.agileburo.anytype.domain.icon.DocumentEmojiIconProvider
import com.agileburo.anytype.domain.misc.UrlBuilder
import com.agileburo.anytype.domain.page.CreatePage
import com.agileburo.anytype.presentation.desktop.HomeDashboardEventConverter
@ -93,9 +94,11 @@ class HomeDashboardModule {
@Provides
@PerScreen
fun provideCreatePageUseCase(
repo: BlockRepository
repo: BlockRepository,
documentEmojiIconProvider: DocumentEmojiIconProvider
): CreatePage = CreatePage(
repo = repo
repo = repo,
documentEmojiIconProvider = documentEmojiIconProvider
)
@Provides

View file

@ -12,6 +12,7 @@ import com.agileburo.anytype.domain.download.DownloadFile
import com.agileburo.anytype.domain.download.Downloader
import com.agileburo.anytype.domain.event.interactor.EventChannel
import com.agileburo.anytype.domain.event.interactor.InterceptEvents
import com.agileburo.anytype.domain.icon.DocumentEmojiIconProvider
import com.agileburo.anytype.domain.misc.UrlBuilder
import com.agileburo.anytype.domain.page.*
import com.agileburo.anytype.domain.page.bookmark.SetupBookmark
@ -199,9 +200,11 @@ class PageModule {
@Provides
@PerScreen
fun provideCreatePageUseCase(
repo: BlockRepository
repo: BlockRepository,
documentEmojiIconProvider: DocumentEmojiIconProvider
): CreatePage = CreatePage(
repo = repo
repo = repo,
documentEmojiIconProvider = documentEmojiIconProvider
)
@Provides
@ -257,9 +260,11 @@ class PageModule {
@Provides
@PerScreen
fun provideCreateDocumentUseCase(
repo: BlockRepository
repo: BlockRepository,
documentEmojiIconProvider: DocumentEmojiIconProvider
): CreateDocument = CreateDocument(
repo = repo
repo = repo,
documentEmojiProvider = documentEmojiIconProvider
)
@Provides

View file

@ -2,6 +2,8 @@ package com.agileburo.anytype.di.main
import android.content.Context
import com.agileburo.anytype.domain.icon.DocumentEmojiIconProvider
import com.agileburo.anytype.emojifier.data.DefaultDocumentEmojiIconProvider
import com.agileburo.anytype.emojifier.suggest.EmojiSuggester
import com.agileburo.anytype.emojifier.suggest.data.DefaultEmojiSuggestStorage
import com.agileburo.anytype.emojifier.suggest.data.DefaultEmojiSuggester
@ -38,4 +40,10 @@ class EmojiModule {
fun provideEmojiSuggestStorage(context: Context): EmojiSuggestStorage {
return DefaultEmojiSuggestStorage(context, Gson())
}
@Provides
@Singleton
fun provideDocumentEmojiIconProvider(): DocumentEmojiIconProvider {
return DefaultDocumentEmojiIconProvider()
}
}

View file

@ -396,7 +396,8 @@ fun Command.CreateDocument.toEntity() = CommandEntity.CreateDocument(
context = context,
target = target,
prototype = prototype.toEntity(),
position = position.toEntity()
position = position.toEntity(),
emoji = emoji
)
fun Command.Replace.toEntity() = CommandEntity.Replace(

View file

@ -75,7 +75,8 @@ class CommandEntity {
val context: String,
val target: String,
val position: PositionEntity,
val prototype: BlockEntity.Prototype.Page
val prototype: BlockEntity.Prototype.Page,
val emoji: String?
)
class Move(

View file

@ -34,7 +34,8 @@ class BlockDataRepository(
command: Command.UpdateAlignment
) : Payload = factory.remote.updateAlignment(command.toEntity()).toDomain()
override suspend fun createPage(parentId: String) = factory.remote.createPage(parentId)
override suspend fun createPage(parentId: String, emoji: String?) =
factory.remote.createPage(parentId, emoji)
override suspend fun closePage(id: String) {
factory.remote.closePage(id)

View file

@ -26,7 +26,7 @@ interface BlockDataStore {
suspend fun move(command: CommandEntity.Move): PayloadEntity
suspend fun unlink(command: CommandEntity.Unlink): PayloadEntity
suspend fun getConfig(): ConfigEntity
suspend fun createPage(parentId: String): String
suspend fun createPage(parentId: String, emoji: String?): String
suspend fun openPage(id: String): PayloadEntity
suspend fun openProfile(id: String): PayloadEntity
suspend fun closePage(id: String)

View file

@ -24,7 +24,7 @@ interface BlockRemote {
suspend fun updateCheckbox(command: CommandEntity.UpdateCheckbox)
suspend fun move(command: CommandEntity.Move): PayloadEntity
suspend fun getConfig(): ConfigEntity
suspend fun createPage(parentId: String): String
suspend fun createPage(parentId: String, emoji: String?): String
suspend fun openPage(id: String): PayloadEntity
suspend fun openProfile(id: String): PayloadEntity
suspend fun closePage(id: String)

View file

@ -16,7 +16,8 @@ class BlockRemoteDataStore(private val remote: BlockRemote) : BlockDataStore {
remote.closeDashboard(id = id)
}
override suspend fun createPage(parentId: String): String = remote.createPage(parentId)
override suspend fun createPage(parentId: String, emoji: String?): String =
remote.createPage(parentId, emoji)
override suspend fun openPage(id: String): PayloadEntity = remote.openPage(id)
override suspend fun openProfile(id: String): PayloadEntity = remote.openProfile(id)

View file

@ -134,12 +134,14 @@ sealed class Command {
* @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 prototype a prototype of the block we would like to create
* @property emoji random emoji for new page
*/
class CreateDocument(
val context: Id,
val target: Id,
val position: Position,
val prototype: Block.Prototype.Page
val prototype: Block.Prototype.Page,
val emoji: String?
)
class Move(

View file

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

View file

@ -0,0 +1,5 @@
package com.agileburo.anytype.domain.icon
interface DocumentEmojiIconProvider {
fun random(): String
}

View file

@ -8,13 +8,15 @@ import com.agileburo.anytype.domain.block.model.Position
import com.agileburo.anytype.domain.block.repo.BlockRepository
import com.agileburo.anytype.domain.common.Id
import com.agileburo.anytype.domain.event.model.Payload
import com.agileburo.anytype.domain.icon.DocumentEmojiIconProvider
/**
* Use-case for creating a new document.
* Should return a pair of ids, where the first one is block id, the second one is target id.
*/
class CreateDocument(
private val repo: BlockRepository
private val repo: BlockRepository,
private val documentEmojiProvider: DocumentEmojiIconProvider
) : BaseUseCase<CreateDocument.Result, CreateDocument.Params>() {
override suspend fun run(params: Params) = try {
@ -23,7 +25,8 @@ class CreateDocument(
context = params.context,
target = params.target,
prototype = params.prototype,
position = params.position
position = params.position,
emoji = documentEmojiProvider.random()
)
).let { (id, target, payload) ->
Either.Right(

View file

@ -5,24 +5,32 @@ import com.agileburo.anytype.domain.base.Either
import com.agileburo.anytype.domain.block.repo.BlockRepository
import com.agileburo.anytype.domain.common.Id
import com.agileburo.anytype.domain.config.MainConfig
import com.agileburo.anytype.domain.icon.DocumentEmojiIconProvider
/**
* A use-case for creating a new page.
* Currently used for creating a new page inside a dashboard.
*/
class CreatePage(
private val repo: BlockRepository
private val repo: BlockRepository,
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(config.home).let {
repo.createPage(
parentId = config.home,
emoji = documentEmojiIconProvider.random()
).let {
Either.Right(it)
}
}
} else {
repo.createPage(params.id).let {
repo.createPage(
parentId = params.id,
emoji = documentEmojiIconProvider.random()
).let {
Either.Right(it)
}
}

View file

@ -0,0 +1,24 @@
package com.agileburo.anytype.emojifier.data
import com.agileburo.anytype.domain.icon.DocumentEmojiIconProvider
class DefaultDocumentEmojiIconProvider : DocumentEmojiIconProvider {
companion object {
val DOCUMENT_SET = listOf(
"🌳", "", "🧬", "🎈", "🎓",
"💡", "🎒", "🚀", "🤖", "📚",
"🍎", "🏡", "🤝", "😍", "",
"🔥", "💥", "", "", "📌",
"🚩", "🦉", "📮", "📄", "🖌",
"🗳", "", "🔑", "🎉", "🗃",
"🔖", "🧠", "👁", "🎗", "🎲",
"🧩", "🚲", "", "🔶", "🌍",
"🏕", "🧳", "🌵", "🚗", "🚂",
"🖼", "", "🥁", "🚠", "🛫",
"🏔", "🏗", "🛠", "🔍", "🕹"
)
}
override fun random(): String = DOCUMENT_SET.random()
}

View file

@ -21,7 +21,8 @@ class BlockMiddleware(
middleware.closeDashboard(id)
}
override suspend fun createPage(parentId: String): String = middleware.createPage(parentId)
override suspend fun createPage(parentId: String, emoji: String?): String =
middleware.createPage(parentId, emoji)
override suspend fun openPage(id: String): PayloadEntity = middleware.openBlock(id)
override suspend fun openProfile(id: String): PayloadEntity = middleware.openBlock(id)

View file

@ -11,6 +11,7 @@ import com.agileburo.anytype.middleware.model.CreateAccountResponse;
import com.agileburo.anytype.middleware.model.CreateWalletResponse;
import com.agileburo.anytype.middleware.model.SelectAccountResponse;
import com.agileburo.anytype.middleware.service.MiddlewareService;
import com.google.protobuf.Struct;
import com.google.protobuf.Value;
import java.util.List;
@ -229,10 +230,29 @@ public class Middleware {
return mapper.toPayload(response.getEvent());
}
public String createPage(String parentId) throws Exception {
public String createPage(String parentId, String emoji) throws Exception {
Value emojiValue = null;
if (emoji != null) {
emojiValue = Value.newBuilder().setStringValue(emoji).build();
}
Struct details;
if (emojiValue != null) {
details = Struct
.newBuilder()
.putFields(iconEmojiKey, emojiValue)
.build();
} else {
details = Struct.getDefaultInstance();
}
Block.CreatePage.Request request = Block.CreatePage.Request
.newBuilder()
.setContextId(parentId)
.setDetails(details)
.setPosition(Models.Block.Position.Inner)
.build();
@ -534,6 +554,23 @@ public class Middleware {
public Triple<String, String, PayloadEntity> createDocument(CommandEntity.CreateDocument command) throws Exception {
Value emojiValue = null;
if (command.getEmoji() != null) {
emojiValue = Value.newBuilder().setStringValue(command.getEmoji()).build();
}
Struct details;
if (emojiValue != null) {
details = Struct
.newBuilder()
.putFields(iconEmojiKey, emojiValue)
.build();
} else {
details = Struct.getDefaultInstance();
}
Models.Block.Position position = mapper.toMiddleware(command.getPosition());
Block.CreatePage.Request request = Block.CreatePage.Request
@ -541,6 +578,7 @@ public class Middleware {
.setContextId(command.getContext())
.setTargetId(command.getTarget())
.setPosition(position)
.setDetails(details)
.build();
if (BuildConfig.DEBUG) {

View file

@ -11,6 +11,7 @@ import com.agileburo.anytype.middleware.interactor.Middleware
import com.agileburo.anytype.middleware.interactor.MiddlewareFactory
import com.agileburo.anytype.middleware.interactor.MiddlewareMapper
import com.agileburo.anytype.middleware.service.MiddlewareService
import com.google.protobuf.Struct
import com.google.protobuf.Value
import com.nhaarman.mockitokotlin2.*
import org.junit.Before
@ -66,7 +67,8 @@ class MiddlewareTest {
prototype = BlockEntity.Prototype.Page(
style = BlockEntity.Content.Page.Style.EMPTY
),
position = PositionEntity.INNER
position = PositionEntity.INNER,
emoji = null
)
val response = Block.CreatePage.Response
@ -80,6 +82,66 @@ class MiddlewareTest {
.setContextId(command.context)
.setTargetId(command.target)
.setPosition(Models.Block.Position.Inner)
.setDetails(Struct.getDefaultInstance())
.build()
service.stub {
on { blockCreatePage(any()) } doReturn response
}
// TESTING
val (block, target) = middleware.createDocument(command)
verify(service, times(1)).blockCreatePage(request)
assertEquals(
expected = response.blockId,
actual = block
)
assertEquals(
expected = response.targetId,
actual = target
)
}
@Test
fun `should create request to create new document with emoji`() {
// SETUP
val emoji = "🎒"
val command = CommandEntity.CreateDocument(
context = MockDataFactory.randomUuid(),
target = MockDataFactory.randomUuid(),
prototype = BlockEntity.Prototype.Page(
style = BlockEntity.Content.Page.Style.EMPTY
),
position = PositionEntity.INNER,
emoji = emoji
)
val response = Block.CreatePage.Response
.newBuilder()
.setBlockId(MockDataFactory.randomUuid())
.setTargetId(MockDataFactory.randomUuid())
.build()
val request = Block.CreatePage.Request
.newBuilder()
.setContextId(command.context)
.setTargetId(command.target)
.setPosition(Models.Block.Position.Inner)
.setDetails(
Struct.newBuilder()
.putFields(
"iconEmoji",
Value.newBuilder().setStringValue(emoji).build()
)
.build()
)
.build()
service.stub {