mirror of
https://github.com/element-hq/element-android
synced 2024-11-28 13:38:49 +03:00
DRY
This commit is contained in:
parent
c4fe0bdb7f
commit
d88e5d8af8
1 changed files with 39 additions and 94 deletions
|
@ -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,36 +152,7 @@ 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("+")
|
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -242,10 +162,35 @@ class AutoCompleter @Inject constructor(
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun render(state: TextComposerViewState) {
|
private fun insertMatrixItem(editText: EditText, editable: Editable, firstChar: String, matrixItem: MatrixItem) {
|
||||||
autocompleteUserPresenter.render(state.asyncUsers)
|
// Detect last firstChar and remove it
|
||||||
autocompleteRoomPresenter.render(state.asyncRooms)
|
var startIndex = editable.lastIndexOf(firstChar)
|
||||||
autocompleteGroupPresenter.render(state.asyncGroups)
|
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 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
interface AutoCompleterListener :
|
interface AutoCompleterListener :
|
||||||
|
|
Loading…
Reference in a new issue