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

DROID-920 Editor | Fix | Fix crash when dialog is not properly collapsed (#2888)

This commit is contained in:
Mikhail 2023-02-14 11:41:34 +03:00 committed by GitHub
parent 781dcea806
commit ce4d1818e6
Signed by: github
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 76 additions and 36 deletions

View file

@ -29,6 +29,7 @@ import com.anytypeio.anytype.core_utils.ext.subscribe
import com.anytypeio.anytype.core_utils.ext.visible
import com.anytypeio.anytype.core_utils.ext.withParent
import com.anytypeio.anytype.core_utils.ui.BaseBottomSheetTextInputFragment
import com.anytypeio.anytype.core_utils.ui.TextInputDialogBottomBehaviorApplier
import com.anytypeio.anytype.databinding.FragmentObjectSearchBinding
import com.anytypeio.anytype.di.common.componentManager
import com.anytypeio.anytype.presentation.moving.MoveToView
@ -96,18 +97,8 @@ class MoveToFragment : BaseBottomSheetTextInputFragment<FragmentObjectSearchBind
}
private fun closeKeyboardOnDismiss() {
sheet?.let {
BottomSheetBehavior.from(it).apply {
addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
override fun onSlide(bottomSheet: View, slideOffset: Float) {
if (slideOffset == -1F) {
vm.onDialogCancelled()
}
}
override fun onStateChanged(bottomSheet: View, newState: Int) {}
})
}
sheet?.let { sheet ->
TextInputDialogBottomBehaviorApplier(sheet, textInput, vm).apply()
}
}

View file

@ -21,12 +21,12 @@ import com.anytypeio.anytype.core_ui.extensions.drawable
import com.anytypeio.anytype.core_ui.features.navigation.DefaultObjectViewAdapter
import com.anytypeio.anytype.core_ui.features.search.ObjectSearchDividerItemDecoration
import com.anytypeio.anytype.core_ui.layout.SpacingItemDecoration
import com.anytypeio.anytype.core_utils.ext.hideSoftInput
import com.anytypeio.anytype.core_utils.ext.imm
import com.anytypeio.anytype.core_utils.ext.invisible
import com.anytypeio.anytype.core_utils.ext.syncFocusWithImeVisibility
import com.anytypeio.anytype.core_utils.ext.visible
import com.anytypeio.anytype.core_utils.insets.RootViewDeferringInsetsCallback
import com.anytypeio.anytype.core_utils.ui.TextInputDialogBottomBehaviorApplier
import com.anytypeio.anytype.databinding.FragmentObjectSearchBinding
import com.anytypeio.anytype.di.common.componentManager
import com.anytypeio.anytype.presentation.search.ObjectSearchView
@ -35,9 +35,8 @@ import com.anytypeio.anytype.presentation.search.ObjectSearchViewModelFactory
import com.anytypeio.anytype.ui.base.ViewStateFragment
import com.anytypeio.anytype.ui.moving.hideProgress
import com.anytypeio.anytype.ui.moving.showProgress
import com.google.android.material.bottomsheet.BottomSheetBehavior
import timber.log.Timber
import javax.inject.Inject
import timber.log.Timber
class ObjectSearchFragment :
ViewStateFragment<ObjectSearchView, FragmentObjectSearchBinding>(R.layout.fragment_object_search) {
@ -58,22 +57,6 @@ class ObjectSearchFragment :
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
BottomSheetBehavior.from(binding.sheet).apply {
skipCollapsed = true
state = BottomSheetBehavior.STATE_EXPANDED
isHideable = true
addBottomSheetCallback(
object : BottomSheetBehavior.BottomSheetCallback() {
override fun onSlide(bottomSheet: View, slideOffset: Float) {}
override fun onStateChanged(bottomSheet: View, newState: Int) {
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
hideSoftInput()
vm.onDialogCancelled()
}
}
}
)
}
vm.state.observe(viewLifecycleOwner, this)
vm.navigation.observe(viewLifecycleOwner, navObserver)
clearSearchText = binding.searchView.root.findViewById(R.id.clearSearchText)
@ -86,6 +69,7 @@ class ObjectSearchFragment :
}
true
}
TextInputDialogBottomBehaviorApplier(binding.sheet, filterInputField, vm).apply()
initialize()
}

View file

@ -0,0 +1,61 @@
package com.anytypeio.anytype.core_utils.ui
import android.view.View
import com.anytypeio.anytype.core_utils.ext.focusAndShowKeyboard
import com.anytypeio.anytype.core_utils.ext.hideKeyboard
import com.google.android.material.bottomsheet.BottomSheetBehavior
class TextInputDialogBottomBehaviorApplier(
private val attachView: View,
private val textInput: View,
private val dialogCancelListener: OnDialogCancelListener
) {
interface OnDialogCancelListener {
fun onDialogCancelled()
}
private var isOpenedKeyboard = true
fun apply() {
BottomSheetBehavior.from(attachView).apply {
skipCollapsed = true
state = BottomSheetBehavior.STATE_EXPANDED
isHideable = true
setupCallback()
}
}
private fun BottomSheetBehavior<View>.setupCallback() {
addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
override fun onSlide(bottomSheet: View, slideOffset: Float) {
onSlide(slideOffset)
}
override fun onStateChanged(bottomSheet: View, newState: Int) {
onStateChanged(newState)
}
})
}
private fun onSlide(slideOffset: Float) {
if (slideOffset <= MIDDLE_POSITION) {
if (isOpenedKeyboard) {
attachView.hideKeyboard()
isOpenedKeyboard = false
}
} else {
if (!isOpenedKeyboard) {
textInput.focusAndShowKeyboard()
isOpenedKeyboard = true
}
}
}
private fun onStateChanged(newState: Int) {
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
dialogCancelListener.onDialogCancelled()
}
}
}
private const val MIDDLE_POSITION = 0f

View file

@ -6,6 +6,7 @@ import com.anytypeio.anytype.analytics.base.Analytics
import com.anytypeio.anytype.core_models.Id
import com.anytypeio.anytype.core_models.ObjectWrapper
import com.anytypeio.anytype.core_models.SmartBlockType
import com.anytypeio.anytype.core_utils.ui.TextInputDialogBottomBehaviorApplier
import com.anytypeio.anytype.domain.base.Resultat
import com.anytypeio.anytype.domain.base.fold
import com.anytypeio.anytype.domain.base.getOrDefault
@ -38,7 +39,7 @@ class MoveToViewModel(
private val getObjectTypes: GetObjectTypes,
private val analytics: Analytics,
private val workspaceManager: WorkspaceManager
) : ViewModel() {
) : ViewModel(), TextInputDialogBottomBehaviorApplier.OnDialogCancelListener {
private val _viewState: MutableStateFlow<MoveToView> = MutableStateFlow(MoveToView.Init)
val viewState: StateFlow<MoveToView> get() = _viewState
@ -162,7 +163,7 @@ class MoveToViewModel(
sendSearchResultEvent(view.id)
}
fun onDialogCancelled() {
override fun onDialogCancelled() {
commands.value = Command.Exit
}

View file

@ -8,6 +8,7 @@ import com.anytypeio.anytype.core_models.ObjectType
import com.anytypeio.anytype.core_models.ObjectWrapper
import com.anytypeio.anytype.core_utils.common.EventWrapper
import com.anytypeio.anytype.core_utils.ext.cancel
import com.anytypeio.anytype.core_utils.ui.TextInputDialogBottomBehaviorApplier
import com.anytypeio.anytype.core_utils.ui.ViewStateViewModel
import com.anytypeio.anytype.domain.base.Resultat
import com.anytypeio.anytype.domain.base.fold
@ -43,7 +44,8 @@ open class ObjectSearchViewModel(
private val analytics: Analytics,
private val workspaceManager: WorkspaceManager
) : ViewStateViewModel<ObjectSearchView>(),
SupportNavigation<EventWrapper<AppNavigation.Command>> {
SupportNavigation<EventWrapper<AppNavigation.Command>>,
TextInputDialogBottomBehaviorApplier.OnDialogCancelListener {
private val jobs = mutableListOf<Job>()
@ -83,7 +85,8 @@ open class ObjectSearchViewModel(
stateData.postValue(ObjectSearchView.NoResults(userInput.value))
} else {
if (userInput.value.isEmpty()) {
val items = mutableListOf<DefaultSearchItem>(ObjectSearchSection.RecentlyOpened)
val items =
mutableListOf<DefaultSearchItem>(ObjectSearchSection.RecentlyOpened)
items.addAll(this)
stateData.postValue(ObjectSearchView.Success(items))
} else {
@ -191,7 +194,7 @@ open class ObjectSearchViewModel(
keys = ObjectSearchConstants.defaultKeys
)
open fun onDialogCancelled() {
override fun onDialogCancelled() {
navigateToDesktop()
}