First batch of mention changes

Signed-off-by: Mario Danic <mario@lovelyhq.com>
This commit is contained in:
Mario Danic 2019-03-05 14:13:57 +01:00
parent 119e684f8d
commit d6a7a2ff94
3 changed files with 67 additions and 8 deletions

View File

@ -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<Mention> {
@ -30,10 +36,12 @@ public class MentionAutocompleteCallback implements AutocompleteCallback<Mention
public boolean onPopupItemClicked(Editable editable, Mention item) {
int[] range = MagicCharPolicy.getQueryRange(editable);
if (range == null) return false;
int start = range[0] + 1;
int start = range[0];
int end = range[1];
String replacement = item.getId() + " ";
editable.replace(start, end, replacement);
String replacement = item.getLabel();
editable.replace(start, end, replacement + " ");
Spans.MentionSpan mentionSpan = new Spans.MentionSpan(Typeface.BOLD, item.getId(), item.getLabel());
editable.setSpan(mentionSpan, 0, replacement.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
return true;
}

View File

@ -75,6 +75,7 @@ import com.nextcloud.talk.utils.bundle.BundleKeys;
import com.nextcloud.talk.utils.database.user.UserUtils;
import com.nextcloud.talk.utils.preferences.AppPreferences;
import com.nextcloud.talk.utils.singletons.ApplicationWideCurrentRoomHolder;
import com.nextcloud.talk.utils.text.Spans;
import com.otaliastudios.autocomplete.Autocomplete;
import com.otaliastudios.autocomplete.AutocompleteCallback;
import com.otaliastudios.autocomplete.AutocompletePresenter;
@ -381,11 +382,7 @@ public class ChatController extends BaseController implements MessagesListAdapte
}
});
messageInputView.setInputListener(input -> {
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),

View File

@ -0,0 +1,39 @@
/*
* Nextcloud Talk application
*
* @author Mario Danic
* Copyright (C) 2017-2018 Mario Danic <mario@lovelyhq.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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;
}
}
}