Skip to content

Commit

Permalink
Show mention picker immediately after @ entered.
Browse files Browse the repository at this point in the history
  • Loading branch information
cody-signal committed Aug 7, 2020
1 parent d563de4 commit 1634d7d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected void onSelectionChanged(int selectionStart, int selectionEnd) {
if (selectionStart == selectionEnd) {
doAfterCursorChange(getText());
} else {
updateQuery("");
updateQuery(null);
}
}

Expand Down Expand Up @@ -284,18 +284,18 @@ private void doAfterCursorChange(@NonNull Editable text) {
if (enoughToFilter(text)) {
performFiltering(text);
} else {
updateQuery("");
updateQuery(null);
}
}

private void performFiltering(@NonNull Editable text) {
int end = getSelectionEnd();
int start = findQueryStart(text, end);
CharSequence query = text.subSequence(start, end);
updateQuery(query);
updateQuery(query.toString());
}

private void updateQuery(@NonNull CharSequence query) {
private void updateQuery(@Nullable String query) {
if (mentionQueryChangedListener != null) {
mentionQueryChangedListener.onQueryChanged(query);
}
Expand All @@ -306,7 +306,7 @@ private boolean enoughToFilter(@NonNull Editable text) {
if (end < 0) {
return false;
}
return end - findQueryStart(text, end) >= 1;
return findQueryStart(text, end) != -1;
}

public void replaceTextWithMention(@NonNull String displayName, @NonNull RecipientId recipientId) {
Expand Down Expand Up @@ -340,7 +340,7 @@ public void replaceTextWithMention(@NonNull String displayName, @NonNull Recipie

private int findQueryStart(@NonNull CharSequence text, int inputCursorPosition) {
if (inputCursorPosition == 0) {
return inputCursorPosition;
return -1;
}

int delimiterSearchIndex = inputCursorPosition - 1;
Expand All @@ -351,7 +351,7 @@ private int findQueryStart(@NonNull CharSequence text, int inputCursorPosition)
if (delimiterSearchIndex >= 0 && text.charAt(delimiterSearchIndex) == MENTION_STARTER) {
return delimiterSearchIndex + 1;
}
return inputCursorPosition;
return -1;
}

private static class CommitContentListener implements InputConnectionCompat.OnCommitContentListener {
Expand Down Expand Up @@ -391,6 +391,6 @@ public interface CursorPositionChangedListener {
}

public interface MentionQueryChangedListener {
void onQueryChanged(CharSequence query);
void onQueryChanged(@Nullable String query);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread;

import com.annimon.stream.Stream;
Expand All @@ -26,8 +27,8 @@ final class MentionsPickerRepository {
}

@WorkerThread
@NonNull List<Recipient> search(MentionQuery mentionQuery) {
if (TextUtils.isEmpty(mentionQuery.query)) {
@NonNull List<Recipient> search(@NonNull MentionQuery mentionQuery) {
if (mentionQuery.query == null) {
return Collections.emptyList();
}

Expand All @@ -40,10 +41,10 @@ final class MentionsPickerRepository {
}

static class MentionQuery {
private final String query;
private final List<GroupMemberEntry.FullMember> members;
@Nullable private final String query;
@NonNull private final List<GroupMemberEntry.FullMember> members;

MentionQuery(@NonNull String query, @NonNull List<GroupMemberEntry.FullMember> members) {
MentionQuery(@Nullable String query, @NonNull List<GroupMemberEntry.FullMember> members) {
this.query = query;
this.members = members;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.thoughtcrime.securesms.conversation.ui.mentions;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Transformations;
Expand All @@ -20,22 +21,23 @@
import org.thoughtcrime.securesms.util.livedata.LiveDataUtil;

import java.util.List;
import java.util.Objects;

public class MentionsPickerViewModel extends ViewModel {

private final SingleLiveEvent<Recipient> selectedRecipient;
private final LiveData<List<MappingModel<?>>> mentionList;
private final MutableLiveData<LiveGroup> group;
private final MutableLiveData<String> liveQuery;
private final MutableLiveData<Query> liveQuery;

MentionsPickerViewModel(@NonNull MentionsPickerRepository mentionsPickerRepository) {
group = new MutableLiveData<>();
liveQuery = new MutableLiveData<>();
liveQuery = new MutableLiveData<>(Query.NONE);
selectedRecipient = new SingleLiveEvent<>();

LiveData<List<FullMember>> fullMembers = Transformations.distinctUntilChanged(Transformations.switchMap(group, LiveGroup::getFullMembers));
LiveData<String> query = Transformations.distinctUntilChanged(liveQuery);
LiveData<MentionQuery> mentionQuery = LiveDataUtil.combineLatest(query, fullMembers, MentionQuery::new);
LiveData<Query> query = Transformations.distinctUntilChanged(liveQuery);
LiveData<MentionQuery> mentionQuery = LiveDataUtil.combineLatest(query, fullMembers, (q, m) -> new MentionQuery(q.query, m));

mentionList = LiveDataUtil.mapAsync(mentionQuery, q -> Stream.of(mentionsPickerRepository.search(q)).<MappingModel<?>>map(MentionViewState::new).toList());
}
Expand All @@ -52,8 +54,8 @@ void onSelectionChange(@NonNull Recipient recipient) {
return selectedRecipient;
}

public void onQueryChange(@NonNull CharSequence query) {
liveQuery.setValue(query.toString());
public void onQueryChange(@Nullable String query) {
liveQuery.setValue(query == null ? Query.NONE : new Query(query));
}

public void onRecipientChange(@NonNull Recipient recipient) {
Expand All @@ -64,6 +66,39 @@ public void onRecipientChange(@NonNull Recipient recipient) {
}
}

/**
* Wraps a nullable query string so it can be properly propagated through
* {@link LiveDataUtil#combineLatest(LiveData, LiveData, LiveDataUtil.Combine)}.
*/
private static class Query {
static final Query NONE = new Query(null);

@Nullable private final String query;

Query(@Nullable String query) {
this.query = query;
}

@Override
public boolean equals(Object object) {
if (this == object) {
return true;
}

if (object == null || getClass() != object.getClass()) {
return false;
}

Query other = (Query) object;
return Objects.equals(query, other.query);
}

@Override
public int hashCode() {
return Objects.hash(query);
}
}

public static final class Factory implements ViewModelProvider.Factory {
@Override
public @NonNull <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1938,10 +1938,6 @@ public void updateSystemContactColors(@NonNull ColorUpdater updater) {
}

public @NonNull List<Recipient> queryRecipientsForMentions(@NonNull String query, @Nullable List<RecipientId> recipientIds) {
if (TextUtils.isEmpty(query)) {
return Collections.emptyList();
}

query = buildCaseInsensitiveGlobPattern(query);

String ids = null;
Expand Down

0 comments on commit 1634d7d

Please sign in to comment.