mirror of
https://github.com/anyproto/anytype-kotlin.git
synced 2025-06-10 18:10:44 +09:00
Feature/move to (#840)
This commit is contained in:
parent
479d12c09b
commit
080a310540
16 changed files with 713 additions and 10 deletions
|
@ -0,0 +1,113 @@
|
|||
package com.agileburo.anytype.presentation.moving
|
||||
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.agileburo.anytype.core_utils.common.EventWrapper
|
||||
import com.agileburo.anytype.core_utils.ext.timber
|
||||
import com.agileburo.anytype.core_utils.ui.ViewState
|
||||
import com.agileburo.anytype.core_utils.ui.ViewStateViewModel
|
||||
import com.agileburo.anytype.domain.block.interactor.Move
|
||||
import com.agileburo.anytype.domain.block.model.Position
|
||||
import com.agileburo.anytype.domain.common.Id
|
||||
import com.agileburo.anytype.domain.config.GetConfig
|
||||
import com.agileburo.anytype.domain.misc.UrlBuilder
|
||||
import com.agileburo.anytype.domain.page.navigation.GetPageInfoWithLinks
|
||||
import com.agileburo.anytype.presentation.mapper.toEmojiView
|
||||
import com.agileburo.anytype.presentation.mapper.toImageView
|
||||
import com.agileburo.anytype.presentation.mapper.toView
|
||||
import com.agileburo.anytype.presentation.navigation.AppNavigation
|
||||
import com.agileburo.anytype.presentation.navigation.PageNavigationView
|
||||
import com.agileburo.anytype.presentation.navigation.SupportNavigation
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.launch
|
||||
import timber.log.Timber
|
||||
|
||||
class MoveToViewModel(
|
||||
private val urlBuilder: UrlBuilder,
|
||||
private val getPageInfoWithLinks: GetPageInfoWithLinks,
|
||||
private val getConfig: GetConfig,
|
||||
private val move: Move
|
||||
) : ViewStateViewModel<ViewState<PageNavigationView>>(),
|
||||
SupportNavigation<EventWrapper<AppNavigation.Command>> {
|
||||
|
||||
private var pageId: String = ""
|
||||
private var home: String = ""
|
||||
|
||||
val isMovingDisabled: MutableStateFlow<Boolean> = MutableStateFlow(true)
|
||||
|
||||
override val navigation: MutableLiveData<EventWrapper<AppNavigation.Command>> =
|
||||
MutableLiveData()
|
||||
|
||||
fun onViewCreated() {
|
||||
stateData.postValue(ViewState.Init)
|
||||
}
|
||||
|
||||
fun onStart(initialTarget: Id) {
|
||||
viewModelScope.launch {
|
||||
getConfig(Unit).proceed(
|
||||
failure = { Timber.e(it, "Error while getting config") },
|
||||
success = { config ->
|
||||
home = config.home
|
||||
proceedWithGettingDocumentLinks(initialTarget)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun proceedWithGettingDocumentLinks(target: String) {
|
||||
stateData.postValue(ViewState.Loading)
|
||||
viewModelScope.launch {
|
||||
getPageInfoWithLinks.invoke(GetPageInfoWithLinks.Params(pageId = target)).proceed(
|
||||
failure = { error ->
|
||||
error.timber()
|
||||
stateData.postValue(ViewState.Error(error.message ?: "Unknown error"))
|
||||
},
|
||||
success = { response ->
|
||||
with(response.pageInfoWithLinks) {
|
||||
pageId = this.id
|
||||
stateData.postValue(
|
||||
ViewState.Success(
|
||||
PageNavigationView(
|
||||
title = pageInfo.fields.name.orEmpty(),
|
||||
subtitle = pageInfo.snippet.orEmpty(),
|
||||
image = pageInfo.fields.toImageView(urlBuilder),
|
||||
emoji = pageInfo.fields.toEmojiView(),
|
||||
inbound = links.inbound.map { it.toView(urlBuilder) },
|
||||
outbound = links.outbound.map { it.toView(urlBuilder) }
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun onLinkClicked(
|
||||
target: Id,
|
||||
context: Id
|
||||
) {
|
||||
isMovingDisabled.value = (target == context || target == home)
|
||||
proceedWithGettingDocumentLinks(target)
|
||||
}
|
||||
|
||||
fun onMoveToClicked(
|
||||
context: Id,
|
||||
targets: List<Id>
|
||||
) {
|
||||
viewModelScope.launch {
|
||||
move(
|
||||
Move.Params(
|
||||
context = context,
|
||||
blockIds = targets,
|
||||
position = Position.INNER,
|
||||
targetId = pageId,
|
||||
targetContext = pageId
|
||||
)
|
||||
).proceed(
|
||||
failure = { Timber.e(it, "Error while moving blocks") },
|
||||
success = { navigate(EventWrapper(AppNavigation.Command.Exit)) }
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.agileburo.anytype.presentation.moving
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import com.agileburo.anytype.domain.block.interactor.Move
|
||||
import com.agileburo.anytype.domain.config.GetConfig
|
||||
import com.agileburo.anytype.domain.misc.UrlBuilder
|
||||
import com.agileburo.anytype.domain.page.navigation.GetPageInfoWithLinks
|
||||
|
||||
class MoveToViewModelFactory(
|
||||
private val urlBuilder: UrlBuilder,
|
||||
private val getPageInfoWithLinks: GetPageInfoWithLinks,
|
||||
private val getConfig: GetConfig,
|
||||
private val move: Move
|
||||
) : ViewModelProvider.Factory {
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
|
||||
return MoveToViewModel(
|
||||
urlBuilder = urlBuilder,
|
||||
getPageInfoWithLinks = getPageInfoWithLinks,
|
||||
getConfig = getConfig,
|
||||
move = move
|
||||
) as T
|
||||
}
|
||||
}
|
|
@ -33,6 +33,7 @@ interface AppNavigation {
|
|||
fun openDebugSettings()
|
||||
fun openPageNavigation(target: String)
|
||||
fun openLinkTo(target: String, context: String, replace: Boolean)
|
||||
fun openMoveTo(targets: List<String>, context: String)
|
||||
fun openPageSearch()
|
||||
fun exitToDesktopAndOpenPage(pageId: String)
|
||||
fun exitToInvitationCodeScreen()
|
||||
|
@ -71,12 +72,18 @@ interface AppNavigation {
|
|||
object OpenDebugSettingsScreen: Command()
|
||||
|
||||
data class OpenPageNavigationScreen(val target: String) : Command()
|
||||
|
||||
data class OpenLinkToScreen(
|
||||
val context: String,
|
||||
val target: String,
|
||||
val replace: Boolean
|
||||
) : Command()
|
||||
|
||||
data class OpenMoveToScreen(
|
||||
val context: String,
|
||||
val targets: List<String>
|
||||
) : Command()
|
||||
|
||||
data class ExitToDesktopAndOpenPage(val pageId: String) : Command()
|
||||
object OpenPageSearch: Command()
|
||||
}
|
||||
|
|
|
@ -1057,7 +1057,16 @@ class PageViewModel(
|
|||
_error.value = "Rename not implemented"
|
||||
}
|
||||
ActionItemType.MoveTo -> {
|
||||
_error.value = "Move To not implemented"
|
||||
onExitActionMode()
|
||||
dispatch(Command.PopBackStack)
|
||||
navigate(
|
||||
EventWrapper(
|
||||
AppNavigation.Command.OpenMoveToScreen(
|
||||
context = context,
|
||||
targets = listOf(id)
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
ActionItemType.Style -> {
|
||||
val textSelection = orchestrator.stores.textSelection.current()
|
||||
|
@ -1414,9 +1423,7 @@ class PageViewModel(
|
|||
private fun onExitActionMode() {
|
||||
mode = EditorMode.EDITING
|
||||
controlPanelInteractor.onEvent(ControlPanelMachine.Event.ReadMode.OnExit)
|
||||
viewModelScope.launch {
|
||||
refresh()
|
||||
}
|
||||
viewModelScope.launch { refresh() }
|
||||
}
|
||||
|
||||
fun onMultiSelectModeDeleteClicked() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue