From 38317e6033966d4ad6a248ae69d9fe3f4091f7d5 Mon Sep 17 00:00:00 2001 From: Maxime Naturel Date: Thu, 10 Feb 2022 17:30:28 +0100 Subject: [PATCH] Using const for char triggers --- .../home/room/detail/AutoCompleter.kt | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/AutoCompleter.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/AutoCompleter.kt index 4bd38095a9..550f89dd70 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/AutoCompleter.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/AutoCompleter.kt @@ -107,7 +107,7 @@ class AutoCompleter @AssistedInject constructor( Autocomplete.on(editText) .with(commandAutocompletePolicy) .with(autocompleteCommandPresenter) - .with(ELEVATION) + .with(ELEVATION_DP) .with(backgroundDrawable) .with(object : AutocompleteCallback { override fun onPopupItemClicked(editable: Editable, item: Command): Boolean { @@ -127,17 +127,17 @@ class AutoCompleter @AssistedInject constructor( private fun setupMembers(backgroundDrawable: ColorDrawable, editText: EditText) { autocompleteMemberPresenter = autocompleteMemberPresenterFactory.create(roomId) Autocomplete.on(editText) - .with(CharPolicy('@', true)) + .with(CharPolicy(TRIGGER_AUTO_COMPLETE_MEMBERS, true)) .with(autocompleteMemberPresenter) - .with(ELEVATION) + .with(ELEVATION_DP) .with(backgroundDrawable) .with(object : AutocompleteCallback { override fun onPopupItemClicked(editable: Editable, item: AutocompleteMemberItem): Boolean { when (item) { is AutocompleteMemberItem.RoomMember -> - insertMatrixItem(editText, editable, "@", item.roomMemberSummary.toMatrixItem()) + insertMatrixItem(editText, editable, TRIGGER_AUTO_COMPLETE_MEMBERS, item.roomMemberSummary.toMatrixItem()) is AutocompleteMemberItem.Everyone -> - insertMatrixItem(editText, editable, "@", item.roomSummary.toEveryoneInRoomMatrixItem()) + insertMatrixItem(editText, editable, TRIGGER_AUTO_COMPLETE_MEMBERS, item.roomSummary.toEveryoneInRoomMatrixItem()) } return true } @@ -150,13 +150,13 @@ class AutoCompleter @AssistedInject constructor( private fun setupRooms(backgroundDrawable: ColorDrawable, editText: EditText) { Autocomplete.on(editText) - .with(CharPolicy('#', true)) + .with(CharPolicy(TRIGGER_AUTO_COMPLETE_ROOMS, true)) .with(autocompleteRoomPresenter) - .with(ELEVATION) + .with(ELEVATION_DP) .with(backgroundDrawable) .with(object : AutocompleteCallback { override fun onPopupItemClicked(editable: Editable, item: RoomSummary): Boolean { - insertMatrixItem(editText, editable, "#", item.toRoomAliasMatrixItem()) + insertMatrixItem(editText, editable, TRIGGER_AUTO_COMPLETE_ROOMS, item.toRoomAliasMatrixItem()) return true } @@ -168,13 +168,13 @@ class AutoCompleter @AssistedInject constructor( private fun setupGroups(backgroundDrawable: ColorDrawable, editText: EditText) { Autocomplete.on(editText) - .with(CharPolicy('+', true)) + .with(CharPolicy(TRIGGER_AUTO_COMPLETE_GROUPS, true)) .with(autocompleteGroupPresenter) - .with(ELEVATION) + .with(ELEVATION_DP) .with(backgroundDrawable) .with(object : AutocompleteCallback { override fun onPopupItemClicked(editable: Editable, item: GroupSummary): Boolean { - insertMatrixItem(editText, editable, "+", item.toMatrixItem()) + insertMatrixItem(editText, editable, TRIGGER_AUTO_COMPLETE_GROUPS, item.toMatrixItem()) return true } @@ -186,9 +186,9 @@ class AutoCompleter @AssistedInject constructor( private fun setupEmojis(backgroundDrawable: Drawable, editText: EditText) { Autocomplete.on(editText) - .with(CharPolicy(':', false)) + .with(CharPolicy(TRIGGER_AUTO_COMPLETE_EMOJIS, false)) .with(autocompleteEmojiPresenter) - .with(ELEVATION) + .with(ELEVATION_DP) .with(backgroundDrawable) .with(object : AutocompleteCallback { override fun onPopupItemClicked(editable: Editable, item: String): Boolean { @@ -216,7 +216,7 @@ class AutoCompleter @AssistedInject constructor( .build() } - private fun insertMatrixItem(editText: EditText, editable: Editable, firstChar: String, matrixItem: MatrixItem) { + private fun insertMatrixItem(editText: EditText, editable: Editable, firstChar: Char, matrixItem: MatrixItem) { // Detect last firstChar and remove it var startIndex = editable.lastIndexOf(firstChar) if (startIndex == -1) { @@ -234,7 +234,7 @@ class AutoCompleter @AssistedInject constructor( // Adding trailing space " " or ": " if the user started mention someone val displayNameSuffix = - if (firstChar == "@" && startIndex == 0) { + if (firstChar == TRIGGER_AUTO_COMPLETE_MEMBERS && startIndex == 0) { ": " } else { " " @@ -255,7 +255,10 @@ class AutoCompleter @AssistedInject constructor( } companion object { - // TODO add consts for string trigger for autocomplete - private const val ELEVATION = 6f + private const val ELEVATION_DP = 6f + private const val TRIGGER_AUTO_COMPLETE_MEMBERS = '@' + private const val TRIGGER_AUTO_COMPLETE_ROOMS = '#' + private const val TRIGGER_AUTO_COMPLETE_GROUPS = '+' + private const val TRIGGER_AUTO_COMPLETE_EMOJIS = ':' } }