From c4fe0bdb7f74695c501d7895c3db199ea892fac5 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Sun, 22 Dec 2019 07:08:28 +0100 Subject: [PATCH] Split into small methods --- .../home/room/detail/AutoCompleter.kt | 118 ++++++++++-------- 1 file changed, 68 insertions(+), 50 deletions(-) diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/detail/AutoCompleter.kt b/vector/src/main/java/im/vector/riotx/features/home/room/detail/AutoCompleter.kt index 5e8a87ba51..7edba64a05 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/room/detail/AutoCompleter.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/room/detail/AutoCompleter.kt @@ -17,6 +17,7 @@ package im.vector.riotx.features.home.room.detail import android.graphics.drawable.ColorDrawable +import android.graphics.drawable.Drawable import android.text.Editable import android.text.Spannable import android.widget.EditText @@ -68,12 +69,19 @@ class AutoCompleter @Inject constructor( fun setup(fragment: Fragment, editText: EditText, listener: AutoCompleterListener) { this.fragment = fragment - val elevation = 6f val backgroundDrawable = ColorDrawable(ThemeUtils.getColor(fragment.requireContext(), R.attr.riotx_background)) + + setupCommands(backgroundDrawable, editText) + setupUsers(backgroundDrawable, editText, listener) + setupRooms(backgroundDrawable, editText, listener) + setupGroups(backgroundDrawable, editText, listener) + } + + private fun setupCommands(backgroundDrawable: Drawable, editText: EditText) { Autocomplete.on(editText) .with(commandAutocompletePolicy) .with(autocompleteCommandPresenter) - .with(elevation) + .with(ELEVATION) .with(backgroundDrawable) .with(object : AutocompleteCallback { override fun onPopupItemClicked(editable: Editable, item: Command): Boolean { @@ -88,12 +96,62 @@ class AutoCompleter @Inject constructor( } }) .build() + } + private fun setupUsers(backgroundDrawable: ColorDrawable, editText: EditText, listener: AutocompleteUserPresenter.Callback) { + autocompleteUserPresenter.callback = listener + Autocomplete.on(editText) + .with(CharPolicy('@', true)) + .with(autocompleteUserPresenter) + .with(ELEVATION) + .with(backgroundDrawable) + .with(object : AutocompleteCallback { + override fun onPopupItemClicked(editable: Editable, item: User): Boolean { + // Detect last '@' and remove it + var startIndex = editable.lastIndexOf("@") + if (startIndex == -1) { + startIndex = 0 + } + + // Detect next word separator + var endIndex = editable.indexOf(" ", startIndex) + if (endIndex == -1) { + endIndex = editable.length + } + + // Replace the word by its completion + val matrixItem = item.toMatrixItem() + val displayName = matrixItem.getBestName() + + // with a trailing space + editable.replace(startIndex, endIndex, "$displayName ") + + // Add the span + val span = PillImageSpan( + glideRequests, + avatarRenderer, + fragment.requireContext(), + matrixItem + ) + span.bind(editText) + + editable.setSpan(span, startIndex, startIndex + displayName.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) + + return true + } + + override fun onPopupVisibilityChanged(shown: Boolean) { + } + }) + .build() + } + + private fun setupRooms(backgroundDrawable: ColorDrawable, editText: EditText, listener: AutocompleteRoomPresenter.Callback) { autocompleteRoomPresenter.callback = listener Autocomplete.on(editText) .with(CharPolicy('#', true)) .with(autocompleteRoomPresenter) - .with(elevation) + .with(ELEVATION) .with(backgroundDrawable) .with(object : AutocompleteCallback { override fun onPopupItemClicked(editable: Editable, item: RoomSummary): Boolean { @@ -134,12 +192,14 @@ class AutoCompleter @Inject constructor( } }) .build() + } + private fun setupGroups(backgroundDrawable: ColorDrawable, editText: EditText, listener: AutocompleteGroupPresenter.Callback) { autocompleteGroupPresenter.callback = listener Autocomplete.on(editText) .with(CharPolicy('+', true)) .with(autocompleteGroupPresenter) - .with(elevation) + .with(ELEVATION) .with(backgroundDrawable) .with(object : AutocompleteCallback { override fun onPopupItemClicked(editable: Editable, item: GroupSummary): Boolean { @@ -180,52 +240,6 @@ class AutoCompleter @Inject constructor( } }) .build() - - autocompleteUserPresenter.callback = listener - Autocomplete.on(editText) - .with(CharPolicy('@', true)) - .with(autocompleteUserPresenter) - .with(elevation) - .with(backgroundDrawable) - .with(object : AutocompleteCallback { - override fun onPopupItemClicked(editable: Editable, item: User): Boolean { - // Detect last '@' and remove it - var startIndex = editable.lastIndexOf("@") - if (startIndex == -1) { - startIndex = 0 - } - - // Detect next word separator - var endIndex = editable.indexOf(" ", startIndex) - if (endIndex == -1) { - endIndex = editable.length - } - - // Replace the word by its completion - val matrixItem = item.toMatrixItem() - val displayName = matrixItem.getBestName() - - // with a trailing space - editable.replace(startIndex, endIndex, "$displayName ") - - // Add the span - val span = PillImageSpan( - glideRequests, - avatarRenderer, - fragment.requireContext(), - matrixItem - ) - span.bind(editText) - - editable.setSpan(span, startIndex, startIndex + displayName.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) - - return true - } - - override fun onPopupVisibilityChanged(shown: Boolean) { - } - }) - .build() } fun render(state: TextComposerViewState) { @@ -238,4 +252,8 @@ class AutoCompleter @Inject constructor( AutocompleteUserPresenter.Callback, AutocompleteRoomPresenter.Callback, AutocompleteGroupPresenter.Callback + + companion object { + private const val ELEVATION = 6f + } }