mirror of
https://github.com/anyproto/anytype-kotlin.git
synced 2025-06-08 13:57:10 +09:00
Editor | Feature | Update set layout use case(#1865)
This commit is contained in:
parent
418c75b7b3
commit
8a4f0e05eb
14 changed files with 74 additions and 16 deletions
|
@ -37,7 +37,7 @@ class ObjectLayoutFragment : BaseBottomSheetFragment() {
|
|||
|
||||
private val adapterLayouts by lazy {
|
||||
ObjectLayoutAdapter(
|
||||
onItemClick = { code -> vm.onLayoutClicked(ctx, code) }
|
||||
onItemClick = { view -> vm.onLayoutClicked(ctx, view) }
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ import com.anytypeio.anytype.core_ui.features.objects.holders.ObjectLayoutHolder
|
|||
import com.anytypeio.anytype.presentation.objects.ObjectLayoutView
|
||||
|
||||
class ObjectLayoutAdapter(
|
||||
private val onItemClick: (Int) -> Unit
|
||||
private val onItemClick: (ObjectLayoutView) -> Unit
|
||||
) : AbstractAdapter<ObjectLayoutView>(emptyList()) {
|
||||
|
||||
override fun onCreateViewHolder(
|
||||
|
@ -20,7 +20,7 @@ class ObjectLayoutAdapter(
|
|||
itemView.setOnClickListener {
|
||||
val position = bindingAdapterPosition
|
||||
if (position != RecyclerView.NO_POSITION) {
|
||||
onItemClick(items[bindingAdapterPosition].id)
|
||||
onItemClick(items[bindingAdapterPosition])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -472,4 +472,7 @@ class BlockDataRepository(
|
|||
)
|
||||
|
||||
override fun deleteObjects(targets: List<Id>) = factory.remote.deleteObjects(targets = targets)
|
||||
|
||||
override fun setObjectLayout(ctx: Id, layout: ObjectType.Layout): Payload =
|
||||
factory.remote.setObjectLayout(ctx, layout)
|
||||
}
|
|
@ -175,4 +175,6 @@ interface BlockDataStore {
|
|||
|
||||
fun setObjectListIsArchived(targets: List<Id>, isArchived: Boolean)
|
||||
fun deleteObjects(targets: List<Id>)
|
||||
|
||||
fun setObjectLayout(ctx: Id, layout: ObjectType.Layout) : Payload
|
||||
}
|
|
@ -175,4 +175,6 @@ interface BlockRemote {
|
|||
|
||||
fun setObjectListIsArchived(targets: List<Id>, isArchived: Boolean)
|
||||
fun deleteObjects(targets: List<Id>)
|
||||
|
||||
fun setObjectLayout(ctx: Id, layout: ObjectType.Layout) : Payload
|
||||
}
|
|
@ -406,4 +406,7 @@ class BlockRemoteDataStore(private val remote: BlockRemote) : BlockDataStore {
|
|||
)
|
||||
|
||||
override fun deleteObjects(targets: List<Id>) = remote.deleteObjects(targets = targets)
|
||||
|
||||
override fun setObjectLayout(ctx: Id, layout: ObjectType.Layout): Payload =
|
||||
remote.setObjectLayout(ctx, layout)
|
||||
}
|
|
@ -229,4 +229,6 @@ interface BlockRepository {
|
|||
fun setObjectListIsArchived(targets: List<Id>, isArchived: Boolean)
|
||||
|
||||
fun deleteObjects(targets: List<Id>)
|
||||
|
||||
fun setObjectLayout(ctx: Id, layout: ObjectType.Layout) : Payload
|
||||
}
|
|
@ -5,20 +5,18 @@ import com.anytypeio.anytype.core_models.ObjectType
|
|||
import com.anytypeio.anytype.core_models.Payload
|
||||
import com.anytypeio.anytype.domain.base.BaseUseCase
|
||||
import com.anytypeio.anytype.domain.block.repo.BlockRepository
|
||||
import com.anytypeio.anytype.core_models.Relations
|
||||
|
||||
class SetObjectLayout(private val repo: BlockRepository): BaseUseCase<Payload, SetObjectLayout.Params>() {
|
||||
|
||||
override suspend fun run(params: Params) = safe {
|
||||
repo.updateDetail(
|
||||
repo.setObjectLayout(
|
||||
ctx = params.ctx,
|
||||
key = Relations.LAYOUT,
|
||||
value = params.code.toDouble()
|
||||
layout = params.layout
|
||||
)
|
||||
}
|
||||
|
||||
data class Params(
|
||||
val ctx: Id,
|
||||
val code: Int
|
||||
val layout: ObjectType.Layout
|
||||
)
|
||||
}
|
|
@ -435,4 +435,7 @@ class BlockMiddleware(
|
|||
targets = targets,
|
||||
isArchived = isArchived
|
||||
)
|
||||
|
||||
override fun setObjectLayout(ctx: Id, layout: ObjectType.Layout) : Payload =
|
||||
middleware.setObjectLayout(ctx, layout)
|
||||
}
|
|
@ -1462,4 +1462,15 @@ class Middleware(
|
|||
val response = service.objectListDelete(request)
|
||||
if (BuildConfig.DEBUG) logResponse(response)
|
||||
}
|
||||
|
||||
fun setObjectLayout(ctx: Id, layout: ObjectType.Layout) : Payload {
|
||||
val request = Rpc.Object.SetLayout.Request(
|
||||
contextId = ctx,
|
||||
layout = layout.toMiddlewareModel()
|
||||
)
|
||||
if (BuildConfig.DEBUG) logRequest(request)
|
||||
val response = service.objectSetLayout(request)
|
||||
if (BuildConfig.DEBUG) logResponse(response)
|
||||
return response.event.toPayload()
|
||||
}
|
||||
}
|
|
@ -203,4 +203,7 @@ interface MiddlewareService {
|
|||
|
||||
@Throws(Exception::class)
|
||||
fun objectListDelete(request: ObjectList.Delete.Request): ObjectList.Delete.Response
|
||||
|
||||
@Throws(Exception::class)
|
||||
fun objectSetLayout(request: Object.SetLayout.Request): Object.SetLayout.Response
|
||||
}
|
|
@ -808,4 +808,17 @@ class MiddlewareServiceImplementation : MiddlewareService {
|
|||
return response
|
||||
}
|
||||
}
|
||||
|
||||
override fun objectSetLayout(request: Object.SetLayout.Request): Object.SetLayout.Response {
|
||||
val encoded = Service.objectSetLayout(
|
||||
Object.SetLayout.Request.ADAPTER.encode(request)
|
||||
)
|
||||
val response = Object.SetLayout.Response.ADAPTER.decode(encoded)
|
||||
val error = response.error
|
||||
if (error != null && error.code != Object.SetLayout.Response.Error.Code.NULL) {
|
||||
throw Exception(error.description)
|
||||
} else {
|
||||
return response
|
||||
}
|
||||
}
|
||||
}
|
|
@ -10,6 +10,7 @@ import com.anytypeio.anytype.domain.layout.GetSupportedObjectLayouts
|
|||
import com.anytypeio.anytype.domain.layout.SetObjectLayout
|
||||
import com.anytypeio.anytype.presentation.common.BaseViewModel
|
||||
import com.anytypeio.anytype.presentation.editor.Editor
|
||||
import com.anytypeio.anytype.presentation.mapper.toObjectLayout
|
||||
import com.anytypeio.anytype.presentation.mapper.toView
|
||||
import com.anytypeio.anytype.presentation.objects.ObjectLayoutView
|
||||
import com.anytypeio.anytype.presentation.util.Dispatcher
|
||||
|
@ -72,16 +73,18 @@ class ObjectLayoutViewModel(
|
|||
|
||||
fun onLayoutClicked(
|
||||
ctx: Id,
|
||||
code: Int
|
||||
layoutView: ObjectLayoutView
|
||||
) {
|
||||
val params = SetObjectLayout.Params(
|
||||
ctx = ctx,
|
||||
layout = layoutView.toObjectLayout()
|
||||
)
|
||||
viewModelScope.launch {
|
||||
setObjectLayout(
|
||||
SetObjectLayout.Params(
|
||||
ctx = ctx,
|
||||
code = code
|
||||
)
|
||||
).process(
|
||||
failure = { Timber.e(it, ERROR_MESSAGE).also { _toasts.emit(ERROR_MESSAGE) } },
|
||||
setObjectLayout(params).process(
|
||||
failure = {
|
||||
Timber.e(it, ERROR_MESSAGE)
|
||||
_toasts.emit(ERROR_MESSAGE)
|
||||
},
|
||||
success = { dispatcher.send(it) }
|
||||
)
|
||||
}
|
||||
|
|
|
@ -723,4 +723,19 @@ fun List<ObjectType.Layout>.toView(): List<ObjectLayoutView> = map { layout ->
|
|||
ObjectType.Layout.DATABASE -> ObjectLayoutView.Database(id = layout.code, isSelected = false)
|
||||
ObjectType.Layout.SPACE -> ObjectLayoutView.Space(id = layout.code, isSelected = false)
|
||||
}
|
||||
}
|
||||
|
||||
fun ObjectLayoutView.toObjectLayout() = when (this) {
|
||||
is ObjectLayoutView.Basic -> ObjectType.Layout.BASIC
|
||||
is ObjectLayoutView.Dashboard -> ObjectType.Layout.DASHBOARD
|
||||
is ObjectLayoutView.Database -> ObjectType.Layout.DATABASE
|
||||
is ObjectLayoutView.File -> ObjectType.Layout.FILE
|
||||
is ObjectLayoutView.Image -> ObjectType.Layout.IMAGE
|
||||
is ObjectLayoutView.Note -> ObjectType.Layout.NOTE
|
||||
is ObjectLayoutView.ObjectType -> ObjectType.Layout.OBJECT_TYPE
|
||||
is ObjectLayoutView.Profile -> ObjectType.Layout.PROFILE
|
||||
is ObjectLayoutView.Relation -> ObjectType.Layout.RELATION
|
||||
is ObjectLayoutView.Set -> ObjectType.Layout.SET
|
||||
is ObjectLayoutView.Space -> ObjectType.Layout.SPACE
|
||||
is ObjectLayoutView.Todo -> ObjectType.Layout.TODO
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue