MentionAutocompleteCallback.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Nextcloud Talk application
  3. *
  4. * @author Mario Danic
  5. * @author Andy Scherzinger
  6. * Copyright (C) 2022 Andy Scherzinger <info@andy-scherzinger.de>
  7. * Copyright (C) 2017-2018 Mario Danic <mario@lovelyhq.com>
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. package com.nextcloud.talk.callbacks;
  23. import android.content.Context;
  24. import android.text.Editable;
  25. import android.text.Spanned;
  26. import android.widget.EditText;
  27. import third.parties.fresco.BetterImageSpan;
  28. import com.nextcloud.talk.R;
  29. import com.nextcloud.talk.data.user.model.User;
  30. import com.nextcloud.talk.models.json.mention.Mention;
  31. import com.nextcloud.talk.ui.theme.ViewThemeUtils;
  32. import com.nextcloud.talk.utils.DisplayUtils;
  33. import com.nextcloud.talk.utils.MagicCharPolicy;
  34. import com.nextcloud.talk.utils.text.Spans;
  35. import com.otaliastudios.autocomplete.AutocompleteCallback;
  36. import com.vanniktech.emoji.EmojiRange;
  37. import com.vanniktech.emoji.Emojis;
  38. import kotlin.OptIn;
  39. public class MentionAutocompleteCallback implements AutocompleteCallback<Mention> {
  40. private final ViewThemeUtils viewThemeUtils;
  41. private Context context;
  42. private User conversationUser;
  43. private EditText editText;
  44. public MentionAutocompleteCallback(Context context,
  45. User conversationUser,
  46. EditText editText,
  47. ViewThemeUtils viewThemeUtils) {
  48. this.context = context;
  49. this.conversationUser = conversationUser;
  50. this.editText = editText;
  51. this.viewThemeUtils = viewThemeUtils;
  52. }
  53. @OptIn(markerClass = kotlin.ExperimentalStdlibApi.class)
  54. @Override
  55. public boolean onPopupItemClicked(Editable editable, Mention item) {
  56. MagicCharPolicy.TextSpan range = MagicCharPolicy.getQueryRange(editable);
  57. if (range == null) {
  58. return false;
  59. }
  60. String replacement = item.getLabel();
  61. StringBuilder replacementStringBuilder = new StringBuilder(item.getLabel());
  62. for (EmojiRange emojiRange : Emojis.emojis(replacement)) {
  63. replacementStringBuilder.delete(emojiRange.range.getStart(), emojiRange.range.getEndInclusive());
  64. }
  65. editable.replace(range.getStart(), range.getEnd(), replacementStringBuilder + " ");
  66. Spans.MentionChipSpan mentionChipSpan =
  67. new Spans.MentionChipSpan(DisplayUtils.getDrawableForMentionChipSpan(context,
  68. item.getId(),
  69. item.getLabel(),
  70. conversationUser,
  71. item.getSource(),
  72. R.xml.chip_you,
  73. editText,
  74. viewThemeUtils),
  75. BetterImageSpan.ALIGN_CENTER,
  76. item.getId(), item.getLabel());
  77. editable.setSpan(mentionChipSpan,
  78. range.getStart(),
  79. range.getStart() + replacementStringBuilder.length(),
  80. Spanned.SPAN_INCLUSIVE_INCLUSIVE);
  81. return true;
  82. }
  83. @Override
  84. public void onPopupVisibilityChanged(boolean shown) {
  85. }
  86. }