Add Mentioning Support.

You can now mention people in a thread. Simply starting with an "@" and
a letter, you'll now get suggestions to mention people. This appeals to
issue #244.
This commit is contained in:
Jedi Burrell 2017-05-16 04:43:06 -04:00
parent 0cdfbea730
commit 5fc4d63a0e
4 changed files with 80 additions and 50 deletions

View File

@ -148,7 +148,6 @@ dependencies {
compile 'com.github.nightwhistler:HtmlSpanner:0.4'
compile 'net.sourceforge.htmlcleaner:htmlcleaner:2.2'
compile 'com.github.matthiasrobbers:shortbread:1.0.1'
compile 'com.linkedin.android.spyglass:spyglass:1.4.0'
provided "org.projectlombok:lombok:${lombokVersion}"
annotationProcessor 'io.requery:requery-processor:1.2.0'
annotationProcessor "org.projectlombok:lombok:${lombokVersion}"

View File

@ -6,10 +6,12 @@ import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.design.widget.Snackbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
@ -29,14 +31,16 @@ import com.fastaccess.helper.ViewHelper;
import com.fastaccess.provider.markdown.MarkDownProvider;
import com.fastaccess.ui.base.BaseActivity;
import com.fastaccess.ui.modules.editor.popup.EditorLinkImageDialogFragment;
import com.fastaccess.ui.widgets.FontEditText;
import com.fastaccess.ui.widgets.FontTextView;
import com.fastaccess.ui.widgets.ForegroundImageView;
import com.fastaccess.ui.widgets.dialog.MessageDialogView;
import com.linkedin.android.spyglass.tokenization.impl.WordTokenizerConfig;
import com.linkedin.android.spyglass.ui.MentionsEditText;
import java.util.ArrayList;
import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnItemClick;
import butterknife.OnTextChanged;
import es.dmoral.toasty.Toasty;
import icepick.State;
@ -52,20 +56,18 @@ public class EditorActivity extends BaseActivity<EditorMvp.View, EditorPresenter
private String sentFromFastHub;
private static final WordTokenizerConfig tokenizerConfig = new WordTokenizerConfig
.Builder()
.setThreshold(1)
.build();
private ArrayList<String> participants;
private int inMentionMode = -1;
private CharSequence savedText = "";
@BindView(R.id.replyQuote) LinearLayout replyQuote;
@BindView(R.id.replyQuoteText) FontTextView quote;
@BindView(R.id.view) ForegroundImageView viewCode;
@BindView(R.id.editText) MentionsEditText editText;
@BindView(R.id.editText) FontEditText editText;
@BindView(R.id.editorIconsHolder) View editorIconsHolder;
@BindView(R.id.sentVia) CheckBox sentVia;
@BindView(R.id.autocomplete)
ListView mention;
@BindView(R.id.list_divider) View listDivider;
@State @BundleConstant.ExtraTYpe String extraType;
@State String itemId;
@ -96,10 +98,55 @@ public class EditorActivity extends BaseActivity<EditorMvp.View, EditorPresenter
@OnTextChanged(value = R.id.editText, callback = OnTextChanged.Callback.TEXT_CHANGED) void onEdited(CharSequence charSequence) {
if (editText.isEnabled()) {
savedText = charSequence;
char lastChar = 0;
if(charSequence.length()>0) lastChar = charSequence.charAt(charSequence.length()-1);
if (lastChar!=0) {
if (lastChar == '@') {
inMentionMode = editText.getSelectionEnd();
mention.setVisibility(GONE);
listDivider.setVisibility(GONE);
return;
} else if (lastChar == ' ')
inMentionMode = -1;
else if (inMentionMode > -1)
updateMentionList(charSequence.toString().substring(inMentionMode, editText.getSelectionEnd()));
else {
String copy = editText.getText().toString().substring(0, editText.getSelectionEnd());
String[] list = copy.split("\\s+");
String last = list[list.length-1];
if(last.startsWith("@")) {
inMentionMode = copy.lastIndexOf("@") + 1;
updateMentionList(charSequence.toString().substring(inMentionMode, editText.getSelectionEnd()));
}
}
} else {
inMentionMode = -1;
}
if(inMentionMode>-1)
if(mention!=null) {
mention.setVisibility(inMentionMode > 0 ? View.VISIBLE : GONE);
listDivider.setVisibility(mention.getVisibility());
}
}
}
@OnItemClick(R.id.autocomplete) void onMentionSelection(int position){
String complete = mention.getAdapter().getItem(position).toString()+" ";
int end = editText.getSelectionEnd();
editText.getText().replace(inMentionMode, end, complete, 0, complete.length());
inMentionMode = -1;
mention.setVisibility(GONE);
listDivider.setVisibility(GONE);
}
@OnClick(R.id.view) void onViewMarkDown() {
if (editText.isEnabled() && !InputHelper.isEmpty(editText)) {
editText.setEnabled(false);
@ -172,6 +219,7 @@ public class EditorActivity extends BaseActivity<EditorMvp.View, EditorPresenter
else {
MarkDownProvider.setMdText(quote, bundle.getString("message", ""));
}
participants = bundle.getStringArrayList("participants");
}
}
if (!PrefGetter.isEditorHintShowed()) {
@ -278,4 +326,19 @@ public class EditorActivity extends BaseActivity<EditorMvp.View, EditorPresenter
MarkDownProvider.addPhoto(editText, InputHelper.toString(title), InputHelper.toString(link));
}
}
private void updateMentionList(@NonNull String mentioning) {
if(participants!=null){
ArrayList<String> mentions = new ArrayList<>();
for(String participant : participants)
if(participant.toLowerCase().startsWith(mentioning.replace("@", "").toLowerCase()))
mentions.add(participant);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, mentions.subList(0, Math.min(mentions.size(), 3)));
mention.setAdapter(adapter);
Log.d(getLoggingTag(), mentions.toString());
}
}
}

View File

@ -1,39 +0,0 @@
package com.fastaccess.ui.widgets;
import android.content.Context;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.inputmethod.EditorInfo;
import com.fastaccess.helper.TypeFaceHelper;
import com.linkedin.android.spyglass.ui.MentionsEditText;
/**
* Created by JediB on 5/15/2017.
*/
public class MentionsFontEditText extends MentionsEditText {
public MentionsFontEditText(@NonNull Context context) {
super(context);
init();
}
public MentionsFontEditText(@NonNull Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MentionsFontEditText(@NonNull Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
if (isInEditMode()) return;
setInputType(getInputType() | EditorInfo.IME_FLAG_NO_EXTRACT_UI | EditorInfo.IME_FLAG_NO_FULLSCREEN);
setImeOptions(getImeOptions() | EditorInfo.IME_FLAG_NO_FULLSCREEN);
TypeFaceHelper.applyTypeface(this);
}
}

View File

@ -39,7 +39,7 @@
android:background="?dividerColor"/>
</LinearLayout>
<com.linkedin.android.spyglass.ui.MentionsEditText
<com.fastaccess.ui.widgets.FontEditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="match_parent"
@ -56,11 +56,18 @@
</ScrollView>
<View
android:id="@+id/list_divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?dividerColor"
android:visibility="gone"/>
<ListView
android:id="@+id/autocomplete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:visibility="gone"/>
android:visibility="gone"/>
<View
android:layout_width="match_parent"