diff --git a/app/src/main/java/com/nextcloud/talk/callbacks/MentionAutocompleteCallback.java b/app/src/main/java/com/nextcloud/talk/callbacks/MentionAutocompleteCallback.java index c8e89c780..b40898dbb 100644 --- a/app/src/main/java/com/nextcloud/talk/callbacks/MentionAutocompleteCallback.java +++ b/app/src/main/java/com/nextcloud/talk/callbacks/MentionAutocompleteCallback.java @@ -20,9 +20,15 @@ package com.nextcloud.talk.callbacks; +import android.graphics.Typeface; import android.text.Editable; +import android.text.Spanned; +import android.text.style.StyleSpan; +import android.text.style.TypefaceSpan; +import com.nextcloud.talk.R; import com.nextcloud.talk.models.json.mention.Mention; import com.nextcloud.talk.utils.MagicCharPolicy; +import com.nextcloud.talk.utils.text.Spans; import com.otaliastudios.autocomplete.AutocompleteCallback; public class MentionAutocompleteCallback implements AutocompleteCallback { @@ -30,10 +36,12 @@ public class MentionAutocompleteCallback implements AutocompleteCallback { - sendMessage(input); - return true; - }); - + messageInputView.getButton().setOnClickListener(v -> submitMessage()); messageInputView.getButton().setContentDescription(getResources() .getString(R.string.nc_description_send_message_button)); @@ -665,6 +662,21 @@ public class ChatController extends BaseController implements MessagesListAdapte } } + private void submitMessage() { + final Editable editable = messageInput.getEditableText(); + Spans.MentionSpan mentionSpans[] = editable.getSpans(0, editable.length(), Spans.MentionSpan.class); + Spans.MentionSpan mentionSpan; + mentionAutocomplete.setEnabled(false); + for (int i = 0; i < mentionSpans.length; i++) { + mentionSpan = mentionSpans[i]; + editable.replace(editable.getSpanStart(mentionSpan), editable.getSpanEnd(mentionSpan), "@" + mentionSpan.getId()); + } + + mentionAutocomplete.setEnabled(true); + messageInput.setText(""); + sendMessage(editable); + } + private void sendMessage(CharSequence message) { ncApi.sendChatMessage(credentials, ApiUtils.getUrlForChat(conversationUser.getBaseUrl(), roomToken), diff --git a/app/src/main/java/com/nextcloud/talk/utils/text/Spans.java b/app/src/main/java/com/nextcloud/talk/utils/text/Spans.java new file mode 100644 index 000000000..1bcd93e54 --- /dev/null +++ b/app/src/main/java/com/nextcloud/talk/utils/text/Spans.java @@ -0,0 +1,39 @@ +/* + * Nextcloud Talk application + * + * @author Mario Danic + * Copyright (C) 2017-2018 Mario Danic + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.nextcloud.talk.utils.text; + +import android.text.style.StyleSpan; +import lombok.Data; + +public class Spans { + @Data + public static class MentionSpan extends StyleSpan { + String id; + String label; + + public MentionSpan(int style, String id, String label) { + super(style); + this.id = id; + this.label = label; + } + + } +}