This commit is contained in:
Benoit Marty 2019-12-22 07:14:26 +01:00
parent c4fe0bdb7f
commit d88e5d8af8

View file

@ -28,6 +28,7 @@ import com.otaliastudios.autocomplete.CharPolicy
import im.vector.matrix.android.api.session.group.model.GroupSummary import im.vector.matrix.android.api.session.group.model.GroupSummary
import im.vector.matrix.android.api.session.room.model.RoomSummary import im.vector.matrix.android.api.session.room.model.RoomSummary
import im.vector.matrix.android.api.session.user.model.User import im.vector.matrix.android.api.session.user.model.User
import im.vector.matrix.android.api.util.MatrixItem
import im.vector.matrix.android.api.util.toMatrixItem import im.vector.matrix.android.api.util.toMatrixItem
import im.vector.matrix.android.api.util.toRoomAliasMatrixItem import im.vector.matrix.android.api.util.toRoomAliasMatrixItem
import im.vector.riotx.R import im.vector.riotx.R
@ -77,6 +78,12 @@ class AutoCompleter @Inject constructor(
setupGroups(backgroundDrawable, editText, listener) setupGroups(backgroundDrawable, editText, listener)
} }
fun render(state: TextComposerViewState) {
autocompleteUserPresenter.render(state.asyncUsers)
autocompleteRoomPresenter.render(state.asyncRooms)
autocompleteGroupPresenter.render(state.asyncGroups)
}
private fun setupCommands(backgroundDrawable: Drawable, editText: EditText) { private fun setupCommands(backgroundDrawable: Drawable, editText: EditText) {
Autocomplete.on<Command>(editText) Autocomplete.on<Command>(editText)
.with(commandAutocompletePolicy) .with(commandAutocompletePolicy)
@ -107,36 +114,7 @@ class AutoCompleter @Inject constructor(
.with(backgroundDrawable) .with(backgroundDrawable)
.with(object : AutocompleteCallback<User> { .with(object : AutocompleteCallback<User> {
override fun onPopupItemClicked(editable: Editable, item: User): Boolean { override fun onPopupItemClicked(editable: Editable, item: User): Boolean {
// Detect last '@' and remove it insertMatrixItem(editText, editable, "@", item.toMatrixItem())
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 return true
} }
@ -155,36 +133,7 @@ class AutoCompleter @Inject constructor(
.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 {
// Detect last '#' and remove it insertMatrixItem(editText, editable, "#", item.toRoomAliasMatrixItem())
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.toRoomAliasMatrixItem()
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 return true
} }
@ -203,8 +152,19 @@ class AutoCompleter @Inject constructor(
.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 {
// Detect last '+' and remove it insertMatrixItem(editText, editable, "+", item.toMatrixItem())
var startIndex = editable.lastIndexOf("+") return true
}
override fun onPopupVisibilityChanged(shown: Boolean) {
}
})
.build()
}
private fun insertMatrixItem(editText: EditText, editable: Editable, firstChar: String, matrixItem: MatrixItem) {
// Detect last firstChar and remove it
var startIndex = editable.lastIndexOf(firstChar)
if (startIndex == -1) { if (startIndex == -1) {
startIndex = 0 startIndex = 0
} }
@ -216,7 +176,6 @@ class AutoCompleter @Inject constructor(
} }
// Replace the word by its completion // Replace the word by its completion
val matrixItem = item.toMatrixItem()
val displayName = matrixItem.getBestName() val displayName = matrixItem.getBestName()
// with a trailing space // with a trailing space
@ -232,20 +191,6 @@ class AutoCompleter @Inject constructor(
span.bind(editText) span.bind(editText)
editable.setSpan(span, startIndex, startIndex + displayName.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) editable.setSpan(span, startIndex, startIndex + displayName.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
return true
}
override fun onPopupVisibilityChanged(shown: Boolean) {
}
})
.build()
}
fun render(state: TextComposerViewState) {
autocompleteUserPresenter.render(state.asyncUsers)
autocompleteRoomPresenter.render(state.asyncRooms)
autocompleteGroupPresenter.render(state.asyncGroups)
} }
interface AutoCompleterListener : interface AutoCompleterListener :