First step towards mention chips

Signed-off-by: Mario Danic <mario@lovelyhq.com>
This commit is contained in:
Mario Danic 2019-04-09 21:18:02 +02:00
parent 19131ad853
commit f4f96bb7d9
16 changed files with 262 additions and 37 deletions

View file

@ -17,8 +17,8 @@ android {
targetSdkVersion 28
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
versionCode 90
versionName "6.0.0beta2"
versionCode 91
versionName "6.0.0beta3"
flavorDimensions "default"
renderscriptTargetApi 19
@ -100,7 +100,6 @@ android {
}
ext {
supportLibraryVersion = '28.0.0'
workVersion = "1.0.0"
}
@ -115,17 +114,17 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha4'
implementation 'com.github.vanniktech:Emoji:746caa4623'
implementation 'org.michaelevans.colorart:library:0.0.3'
implementation "android.arch.work:work-runtime:${workVersion}"
implementation "android.arch.work:work-rxjava2:${workVersion}"
androidTestImplementation "android.arch.work:work-testing:${workVersion}"
implementation 'androidx.biometric:biometric:1.0.0-alpha03'
implementation 'androidx.biometric:biometric:1.0.0-alpha04'
implementation "androidx.lifecycle:lifecycle-extensions:2.0.0"
implementation 'androidx.multidex:multidex:2.0.0'
implementation 'androidx.multidex:multidex:2.0.1'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation "io.reactivex.rxjava2:rxjava:2.2.7"

View file

@ -145,18 +145,22 @@ public class MagicIncomingTextMessageViewHolder
Map<String, String> individualHashMap = message.getMessageParameters().get(key);
if (individualHashMap != null) {
if (individualHashMap.get("type").equals("user") || individualHashMap.get("type").equals("guest") || individualHashMap.get("type").equals("call")) {
int color;
if (individualHashMap.get("id").equals(message.getActiveUserId())) {
color = NextcloudTalkApplication.getSharedApplication().getResources().getColor(R.color
.nc_incoming_text_mention_you);
messageString =
DisplayUtils.searchAndReplaceWithMentionSpan(messageText.getContext(),
messageString,
individualHashMap.get("id"),
individualHashMap.get("name"),
R.xml.chip_simple_background);
} else {
color = NextcloudTalkApplication.getSharedApplication().getResources().getColor(R.color
.nc_incoming_text_mention_others);
messageString =
DisplayUtils.searchAndReplaceWithMentionSpan(messageText.getContext(),
messageString,
individualHashMap.get("id"),
individualHashMap.get("name"),
R.xml.chip_accent_background);
}
messageString = DisplayUtils.searchAndColor(messageString,
"@" + individualHashMap.get("name"), color);
} else if (individualHashMap.get("type").equals("file")) {
itemView.setOnClickListener(v -> {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(individualHashMap.get("link")));

View file

@ -58,6 +58,9 @@ public class MagicOutcomingTextMessageViewHolder extends MessageHolders.Outcomin
@Inject
UserUtils userUtils;
@Inject
Context context;
private View itemView;
public MagicOutcomingTextMessageViewHolder(View itemView) {
@ -76,7 +79,6 @@ public class MagicOutcomingTextMessageViewHolder extends MessageHolders.Outcomin
Spannable messageString = new SpannableString(message.getText());
Context context = NextcloudTalkApplication.getSharedApplication().getApplicationContext();
itemView.setSelected(false);
messageTimeView.setTextColor(context.getResources().getColor(R.color.white60));
@ -92,11 +94,19 @@ public class MagicOutcomingTextMessageViewHolder extends MessageHolders.Outcomin
if (individualHashMap.get("type").equals("user") || individualHashMap.get("type").equals("guest") || individualHashMap.get("type").equals("call")) {
if (!individualHashMap.get("id").equals(message.getActiveUserId())) {
messageString =
DisplayUtils.searchAndColor(messageString,
"@" + individualHashMap.get("name"), NextcloudTalkApplication
.getSharedApplication().getResources().getColor(R.color.nc_outcoming_text_default));
DisplayUtils.searchAndReplaceWithMentionSpan(messageText.getContext(),
messageString,
individualHashMap.get("id"),
individualHashMap.get("name"),
R.xml.chip_simple_background);
} else {
messageString =
DisplayUtils.searchAndReplaceWithMentionSpan(messageText.getContext(),
messageString,
individualHashMap.get("id"),
individualHashMap.get("name"),
R.xml.chip_outgoing_own_mention);
}
} else if (individualHashMap.get("type").equals("file")) {
itemView.setOnClickListener(v -> {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(individualHashMap.get("link")));

View file

@ -20,15 +20,24 @@
package com.nextcloud.talk.callbacks;
import android.graphics.Typeface;
import android.content.Context;
import android.text.Editable;
import android.text.Spanned;
import android.text.style.DynamicDrawableSpan;
import com.nextcloud.talk.R;
import com.nextcloud.talk.models.json.mention.Mention;
import com.nextcloud.talk.utils.DisplayUtils;
import com.nextcloud.talk.utils.MagicCharPolicy;
import com.nextcloud.talk.utils.text.Spans;
import com.otaliastudios.autocomplete.AutocompleteCallback;
public class MentionAutocompleteCallback implements AutocompleteCallback<Mention> {
private Context context;
public MentionAutocompleteCallback(Context context) {
this.context = context;
}
@Override
public boolean onPopupItemClicked(Editable editable, Mention item) {
int[] range = MagicCharPolicy.getQueryRange(editable);
@ -37,8 +46,12 @@ public class MentionAutocompleteCallback implements AutocompleteCallback<Mention
int end = range[1];
String replacement = item.getLabel();
editable.replace(start, end, replacement + " ");
Spans.MentionSpan mentionSpan = new Spans.MentionSpan(Typeface.BOLD, item.getId(), item.getLabel());
editable.setSpan(mentionSpan, start, start + item.getLabel().length() , Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
Spans.MentionChipSpan mentionChipSpan =
new Spans.MentionChipSpan(DisplayUtils.getDrawableForMentionChipSpan(context,
item.getLabel(), R.xml.chip_accent_background), DynamicDrawableSpan.ALIGN_BASELINE,
item.getId(), item.getLabel());
editable.setSpan(mentionChipSpan, start, start + item.getLabel().length() ,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
return true;
}

View file

@ -118,6 +118,8 @@ public class ChatController extends BaseController implements MessagesListAdapte
UserUtils userUtils;
@Inject
AppPreferences appPreferences;
@Inject
Context context;
@BindView(R.id.messagesListView)
MessagesList messagesListView;
@BindView(R.id.messageInputView)
@ -385,8 +387,9 @@ public class ChatController extends BaseController implements MessagesListAdapte
}
Editable editable = messageInput.getEditableText();
Spans.MentionSpan[] mentionSpans = editable.getSpans(0, messageInput.length(), Spans.MentionSpan.class);
Spans.MentionSpan mentionSpan;
Spans.MentionChipSpan[] mentionSpans = editable.getSpans(0, messageInput.length(),
Spans.MentionChipSpan.class);
Spans.MentionChipSpan mentionSpan;
for (int i = 0; i < mentionSpans.length; i++) {
mentionSpan = mentionSpans[i];
if (start >= editable.getSpanStart(mentionSpan) && start < editable.getSpanEnd(mentionSpan)) {
@ -482,7 +485,7 @@ public class ChatController extends BaseController implements MessagesListAdapte
float elevation = 6f;
Drawable backgroundDrawable = new ColorDrawable(Color.WHITE);
AutocompletePresenter<Mention> presenter = new MentionAutocompletePresenter(getApplicationContext(), roomToken);
AutocompleteCallback<Mention> callback = new MentionAutocompleteCallback();
AutocompleteCallback<Mention> callback = new MentionAutocompleteCallback(getActivity());
if (mentionAutocomplete == null && messageInput != null) {
mentionAutocomplete = Autocomplete.<Mention>on(messageInput)
@ -728,8 +731,9 @@ 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;
Spans.MentionChipSpan mentionSpans[] = editable.getSpans(0, editable.length(),
Spans.MentionChipSpan.class);
Spans.MentionChipSpan mentionSpan;
for (int i = 0; i < mentionSpans.length; i++) {
mentionSpan = mentionSpans[i];
editable.replace(editable.getSpanStart(mentionSpan), editable.getSpanEnd(mentionSpan), "@" + mentionSpan.getId());

View file

@ -32,6 +32,7 @@ import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.biometric.BiometricPrompt;
import androidx.fragment.app.FragmentActivity;
import autodagger.AutoInjector;
import butterknife.OnClick;
import com.nextcloud.talk.R;
@ -88,7 +89,7 @@ public class LockedController extends BaseController {
Executor executor = Executors.newSingleThreadExecutor();
final BiometricPrompt biometricPrompt = new BiometricPrompt((MainActivity) context, executor,
final BiometricPrompt biometricPrompt = new BiometricPrompt((FragmentActivity) context, executor,
new BiometricPrompt.AuthenticationCallback() {
@Override
public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {

View file

@ -37,10 +37,7 @@ import android.net.Uri;
import android.os.Build;
import android.text.*;
import android.text.method.LinkMovementMethod;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.text.style.*;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
@ -57,8 +54,10 @@ import com.facebook.imagepipeline.image.ImageInfo;
import com.facebook.imagepipeline.postprocessors.RoundAsCirclePostprocessor;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.google.android.material.chip.ChipDrawable;
import com.nextcloud.talk.R;
import com.nextcloud.talk.application.NextcloudTalkApplication;
import com.nextcloud.talk.utils.text.Spans;
import com.vanniktech.emoji.EmojiTextView;
import java.lang.reflect.Constructor;
@ -209,6 +208,38 @@ public class DisplayUtils {
}
public static Drawable getDrawableForMentionChipSpan(Context context, String label, @XmlRes int chipResource) {
ChipDrawable chip = ChipDrawable.createFromResource(context, chipResource);
chip.setText(label);
chip.setBounds(0, 0, chip.getIntrinsicWidth(), chip.getIntrinsicHeight());
return chip;
}
public static Spannable searchAndReplaceWithMentionSpan(Context context, Spannable text,
String id, String label,
@XmlRes int chipXmlRes) {
Spannable spannableString = new SpannableString(text);
String stringText = text.toString();
Matcher m = Pattern.compile("@" + label,
Pattern.CASE_INSENSITIVE | Pattern.LITERAL | Pattern.MULTILINE)
.matcher(spannableString);
int lastStartIndex = -1;
Spans.MentionChipSpan mentionChipSpan;
while (m.find()) {
int start = stringText.indexOf(m.group(), lastStartIndex);
int end = start + m.group().length();
lastStartIndex = end;
mentionChipSpan = new Spans.MentionChipSpan(DisplayUtils.getDrawableForMentionChipSpan(context,
label, chipXmlRes), DynamicDrawableSpan.ALIGN_BASELINE, id, label);
spannableString.setSpan(mentionChipSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return spannableString;
}
public static Spannable searchAndColor(Spannable text, String searchText, @ColorInt int color) {
Spannable spannableString = new SpannableString(text);

View file

@ -20,20 +20,23 @@
package com.nextcloud.talk.utils.text;
import android.graphics.drawable.Drawable;
import android.text.style.StyleSpan;
import androidx.annotation.NonNull;
import lombok.Data;
public class Spans {
@Data
public static class MentionSpan extends StyleSpan {
public static class MentionChipSpan extends TextAlignedImageSpan {
String id;
String label;
public MentionSpan(int style, String id, String label) {
super(style);
public MentionChipSpan(@NonNull Drawable drawable, int verticalAlignment, String id, String label) {
super(drawable, verticalAlignment);
this.id = id;
this.label = label;
}
}
}

View file

@ -0,0 +1,71 @@
/*
* 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/>.
*
* Taken and adapter from
*/
package com.nextcloud.talk.utils.text;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.style.ImageSpan;
import androidx.annotation.NonNull;
public class TextAlignedImageSpan extends ImageSpan {
public TextAlignedImageSpan(@NonNull Drawable drawable, int verticalAlignment) {
super(drawable, verticalAlignment);
}
public int getSize(@NonNull Paint paint, CharSequence text, int start, int end,
Paint.FontMetricsInt fontMetricsInt) {
Drawable drawable = getDrawable();
Rect drawableBounds = drawable.getBounds();
if (fontMetricsInt != null) {
Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt();
int fontHeight = fmPaint.bottom - fmPaint.top;
int drHeight = drawableBounds.bottom - drawableBounds.top;
int top = drHeight / 2 - fontHeight / 4;
int bottom = drHeight / 2 + fontHeight / 4;
fontMetricsInt.ascent = -bottom;
fontMetricsInt.top = -bottom;
fontMetricsInt.bottom = top;
fontMetricsInt.descent = top;
}
return drawableBounds.right;
}
@Override
public void draw(@NonNull Canvas canvas, CharSequence text, int start, int end,
float x, int top, int y, int bottom, @NonNull Paint paint) {
Drawable drawable = getDrawable();
canvas.save();
int transY;
transY = ((bottom - top) - drawable.getBounds().bottom) / 2 + top;
canvas.translate(x, transY);
drawable.draw(canvas);
canvas.restore();
}
}

View file

@ -52,6 +52,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/colorPrimary"
android:layout_marginBottom="4dp"
android:textSize="12sp" />
<com.vanniktech.emoji.EmojiTextView
@ -59,11 +60,11 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="true"
android:lineSpacingMultiplier="1.2"
app:layout_alignSelf="flex_start"
app:layout_flexGrow="1"
app:layout_wrapBefore="true" />
<TextView
android:id="@id/messageTime"
android:layout_width="wrap_content"

View file

@ -44,6 +44,7 @@
android:layout_height="wrap_content"
android:layout_alignWithParentIfMissing="true"
android:textColorHighlight="@color/nc_grey"
android:lineSpacingMultiplier="1.2"
android:textIsSelectable="true" />
<TextView

View file

@ -40,6 +40,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:lineSpacingMultiplier="1.2"
android:imeOptions="actionDone"
android:layout_toStartOf="@id/sendButtonSpace"
android:inputType="textAutoCorrect|textMultiLine|textCapSentences"/>

View file

@ -15,4 +15,12 @@
<item name="android:textSize">12sp</item>
</style>
<style name="ChipTextAppearance" parent="TextAppearance.MaterialComponents.Chip">
<item name="android:textColor">@android:color/white</item>
</style>
<style name="ChipAccentTextAppearance" parent="TextAppearance.MaterialComponents.Chip">
<item name="android:textColor">@color/colorAccent</item>
</style>
</resources>

View file

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ 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/>.
-->
<chip xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:textAppearance="@style/ChipTextAppearance"
app:chipBackgroundColor="@color/colorAccent"
app:closeIconEnabled="false"/>

View file

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ 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/>.
-->
<chip xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:textAppearance="@style/ChipTextAppearance"
app:chipBackgroundColor="@color/white_five"
app:closeIconEnabled="false" />

View file

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ 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/>.
-->
<chip xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:textAppearance="@style/ChipAccentTextAppearance"
app:chipBackgroundColor="@color/white"
app:closeIconEnabled="false" />