Using const for char triggers

This commit is contained in:
Maxime Naturel 2022-02-10 17:30:28 +01:00
parent d8e28d7be9
commit 38317e6033

View File

@ -107,7 +107,7 @@ class AutoCompleter @AssistedInject constructor(
Autocomplete.on<Command>(editText) Autocomplete.on<Command>(editText)
.with(commandAutocompletePolicy) .with(commandAutocompletePolicy)
.with(autocompleteCommandPresenter) .with(autocompleteCommandPresenter)
.with(ELEVATION) .with(ELEVATION_DP)
.with(backgroundDrawable) .with(backgroundDrawable)
.with(object : AutocompleteCallback<Command> { .with(object : AutocompleteCallback<Command> {
override fun onPopupItemClicked(editable: Editable, item: Command): Boolean { override fun onPopupItemClicked(editable: Editable, item: Command): Boolean {
@ -127,17 +127,17 @@ class AutoCompleter @AssistedInject constructor(
private fun setupMembers(backgroundDrawable: ColorDrawable, editText: EditText) { private fun setupMembers(backgroundDrawable: ColorDrawable, editText: EditText) {
autocompleteMemberPresenter = autocompleteMemberPresenterFactory.create(roomId) autocompleteMemberPresenter = autocompleteMemberPresenterFactory.create(roomId)
Autocomplete.on<AutocompleteMemberItem>(editText) Autocomplete.on<AutocompleteMemberItem>(editText)
.with(CharPolicy('@', true)) .with(CharPolicy(TRIGGER_AUTO_COMPLETE_MEMBERS, true))
.with(autocompleteMemberPresenter) .with(autocompleteMemberPresenter)
.with(ELEVATION) .with(ELEVATION_DP)
.with(backgroundDrawable) .with(backgroundDrawable)
.with(object : AutocompleteCallback<AutocompleteMemberItem> { .with(object : AutocompleteCallback<AutocompleteMemberItem> {
override fun onPopupItemClicked(editable: Editable, item: AutocompleteMemberItem): Boolean { override fun onPopupItemClicked(editable: Editable, item: AutocompleteMemberItem): Boolean {
when (item) { when (item) {
is AutocompleteMemberItem.RoomMember -> is AutocompleteMemberItem.RoomMember ->
insertMatrixItem(editText, editable, "@", item.roomMemberSummary.toMatrixItem()) insertMatrixItem(editText, editable, TRIGGER_AUTO_COMPLETE_MEMBERS, item.roomMemberSummary.toMatrixItem())
is AutocompleteMemberItem.Everyone -> is AutocompleteMemberItem.Everyone ->
insertMatrixItem(editText, editable, "@", item.roomSummary.toEveryoneInRoomMatrixItem()) insertMatrixItem(editText, editable, TRIGGER_AUTO_COMPLETE_MEMBERS, item.roomSummary.toEveryoneInRoomMatrixItem())
} }
return true return true
} }
@ -150,13 +150,13 @@ class AutoCompleter @AssistedInject constructor(
private fun setupRooms(backgroundDrawable: ColorDrawable, editText: EditText) { private fun setupRooms(backgroundDrawable: ColorDrawable, editText: EditText) {
Autocomplete.on<RoomSummary>(editText) Autocomplete.on<RoomSummary>(editText)
.with(CharPolicy('#', true)) .with(CharPolicy(TRIGGER_AUTO_COMPLETE_ROOMS, true))
.with(autocompleteRoomPresenter) .with(autocompleteRoomPresenter)
.with(ELEVATION) .with(ELEVATION_DP)
.with(backgroundDrawable) .with(backgroundDrawable)
.with(object : AutocompleteCallback<RoomSummary> { .with(object : AutocompleteCallback<RoomSummary> {
override fun onPopupItemClicked(editable: Editable, item: RoomSummary): Boolean { override fun onPopupItemClicked(editable: Editable, item: RoomSummary): Boolean {
insertMatrixItem(editText, editable, "#", item.toRoomAliasMatrixItem()) insertMatrixItem(editText, editable, TRIGGER_AUTO_COMPLETE_ROOMS, item.toRoomAliasMatrixItem())
return true return true
} }
@ -168,13 +168,13 @@ class AutoCompleter @AssistedInject constructor(
private fun setupGroups(backgroundDrawable: ColorDrawable, editText: EditText) { private fun setupGroups(backgroundDrawable: ColorDrawable, editText: EditText) {
Autocomplete.on<GroupSummary>(editText) Autocomplete.on<GroupSummary>(editText)
.with(CharPolicy('+', true)) .with(CharPolicy(TRIGGER_AUTO_COMPLETE_GROUPS, true))
.with(autocompleteGroupPresenter) .with(autocompleteGroupPresenter)
.with(ELEVATION) .with(ELEVATION_DP)
.with(backgroundDrawable) .with(backgroundDrawable)
.with(object : AutocompleteCallback<GroupSummary> { .with(object : AutocompleteCallback<GroupSummary> {
override fun onPopupItemClicked(editable: Editable, item: GroupSummary): Boolean { override fun onPopupItemClicked(editable: Editable, item: GroupSummary): Boolean {
insertMatrixItem(editText, editable, "+", item.toMatrixItem()) insertMatrixItem(editText, editable, TRIGGER_AUTO_COMPLETE_GROUPS, item.toMatrixItem())
return true return true
} }
@ -186,9 +186,9 @@ class AutoCompleter @AssistedInject constructor(
private fun setupEmojis(backgroundDrawable: Drawable, editText: EditText) { private fun setupEmojis(backgroundDrawable: Drawable, editText: EditText) {
Autocomplete.on<String>(editText) Autocomplete.on<String>(editText)
.with(CharPolicy(':', false)) .with(CharPolicy(TRIGGER_AUTO_COMPLETE_EMOJIS, false))
.with(autocompleteEmojiPresenter) .with(autocompleteEmojiPresenter)
.with(ELEVATION) .with(ELEVATION_DP)
.with(backgroundDrawable) .with(backgroundDrawable)
.with(object : AutocompleteCallback<String> { .with(object : AutocompleteCallback<String> {
override fun onPopupItemClicked(editable: Editable, item: String): Boolean { override fun onPopupItemClicked(editable: Editable, item: String): Boolean {
@ -216,7 +216,7 @@ class AutoCompleter @AssistedInject constructor(
.build() .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 // Detect last firstChar and remove it
var startIndex = editable.lastIndexOf(firstChar) var startIndex = editable.lastIndexOf(firstChar)
if (startIndex == -1) { if (startIndex == -1) {
@ -234,7 +234,7 @@ class AutoCompleter @AssistedInject constructor(
// Adding trailing space " " or ": " if the user started mention someone // Adding trailing space " " or ": " if the user started mention someone
val displayNameSuffix = val displayNameSuffix =
if (firstChar == "@" && startIndex == 0) { if (firstChar == TRIGGER_AUTO_COMPLETE_MEMBERS && startIndex == 0) {
": " ": "
} else { } else {
" " " "
@ -255,7 +255,10 @@ class AutoCompleter @AssistedInject constructor(
} }
companion object { companion object {
// TODO add consts for string trigger for autocomplete private const val ELEVATION_DP = 6f
private const val ELEVATION = 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 = ':'
} }
} }