MentionAutocompleteCallback.java 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Nextcloud Talk application
  3. *
  4. * @author Mario Danic
  5. * Copyright (C) 2017-2018 Mario Danic <mario@lovelyhq.com>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package com.nextcloud.talk.callbacks;
  21. import android.content.Context;
  22. import android.text.Editable;
  23. import android.text.Spanned;
  24. import android.text.style.DynamicDrawableSpan;
  25. import com.nextcloud.talk.R;
  26. import com.nextcloud.talk.models.database.UserEntity;
  27. import com.nextcloud.talk.models.json.mention.Mention;
  28. import com.nextcloud.talk.utils.DisplayUtils;
  29. import com.nextcloud.talk.utils.MagicCharPolicy;
  30. import com.nextcloud.talk.utils.text.Spans;
  31. import com.otaliastudios.autocomplete.AutocompleteCallback;
  32. import com.vanniktech.emoji.EmojiEditText;
  33. public class MentionAutocompleteCallback implements AutocompleteCallback<Mention> {
  34. private Context context;
  35. private UserEntity conversationUser;
  36. private EmojiEditText emojiEditText;
  37. public MentionAutocompleteCallback(Context context, UserEntity conversationUser,
  38. EmojiEditText emojiEditText) {
  39. this.context = context;
  40. this.conversationUser = conversationUser;
  41. this.emojiEditText = emojiEditText;
  42. }
  43. @Override
  44. public boolean onPopupItemClicked(Editable editable, Mention item) {
  45. int[] range = MagicCharPolicy.getQueryRange(editable);
  46. if (range == null) return false;
  47. int start = range[0];
  48. int end = range[1];
  49. String replacement = item.getLabel();
  50. editable.replace(start, end, replacement + " ");
  51. Spans.MentionChipSpan mentionChipSpan =
  52. new Spans.MentionChipSpan(DisplayUtils.getDrawableForMentionChipSpan(context,
  53. item.getId(), item.getLabel(), conversationUser, item.getSource(),
  54. R.xml.chip_accent_background, emojiEditText),
  55. DynamicDrawableSpan.ALIGN_BASELINE,
  56. item.getId(), item.getLabel());
  57. editable.setSpan(mentionChipSpan, start, start + item.getLabel().length(),
  58. Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
  59. return true;
  60. }
  61. @Override
  62. public void onPopupVisibilityChanged(boolean shown) {
  63. }
  64. }