2019-01-28 11:04:05 +01:00
|
|
|
/* Copyright 2017 Andrew Dawson
|
|
|
|
*
|
|
|
|
* This file is a part of Tusky.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Tusky 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 Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
package com.keylesspalace.tusky.adapter;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.BaseAdapter;
|
|
|
|
import android.widget.Filter;
|
|
|
|
import android.widget.Filterable;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import com.keylesspalace.tusky.R;
|
|
|
|
import com.keylesspalace.tusky.entity.Account;
|
2019-03-04 19:28:08 +01:00
|
|
|
import com.keylesspalace.tusky.entity.Emoji;
|
2019-01-28 11:04:05 +01:00
|
|
|
import com.keylesspalace.tusky.util.CustomEmojiHelper;
|
|
|
|
import com.squareup.picasso.Picasso;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by charlag on 12/11/17.
|
|
|
|
*/
|
|
|
|
|
2019-03-04 19:28:08 +01:00
|
|
|
public class ComposeAutoCompleteAdapter extends BaseAdapter
|
2019-01-28 11:04:05 +01:00
|
|
|
implements Filterable {
|
2019-03-04 19:28:08 +01:00
|
|
|
private static final int ACCOUNT_VIEW_TYPE = 1;
|
|
|
|
private static final int HASHTAG_VIEW_TYPE = 2;
|
|
|
|
private static final int EMOJI_VIEW_TYPE = 3;
|
|
|
|
private static final int SEPARATOR_VIEW_TYPE = 0;
|
2019-01-28 11:04:05 +01:00
|
|
|
|
|
|
|
private final ArrayList<AutocompleteResult> resultList;
|
|
|
|
private final AutocompletionProvider autocompletionProvider;
|
|
|
|
|
2019-03-04 19:28:08 +01:00
|
|
|
public ComposeAutoCompleteAdapter(AutocompletionProvider autocompletionProvider) {
|
2019-01-28 11:04:05 +01:00
|
|
|
super();
|
|
|
|
resultList = new ArrayList<>();
|
|
|
|
this.autocompletionProvider = autocompletionProvider;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getCount() {
|
|
|
|
return resultList.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public AutocompleteResult getItem(int index) {
|
|
|
|
return resultList.get(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getItemId(int position) {
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@NonNull
|
|
|
|
public Filter getFilter() {
|
|
|
|
return new Filter() {
|
|
|
|
@Override
|
|
|
|
public CharSequence convertResultToString(Object resultValue) {
|
|
|
|
if (resultValue instanceof AccountResult) {
|
2019-01-30 22:10:05 +01:00
|
|
|
return formatUsername(((AccountResult) resultValue));
|
2019-03-04 19:28:08 +01:00
|
|
|
} else if (resultValue instanceof HashtagResult) {
|
2019-01-28 11:04:05 +01:00
|
|
|
return formatHashtag((HashtagResult) resultValue);
|
2019-03-04 19:28:08 +01:00
|
|
|
} else if (resultValue instanceof EmojiResult) {
|
|
|
|
return formatEmoji((EmojiResult) resultValue);
|
|
|
|
} else {
|
|
|
|
return "";
|
2019-01-28 11:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This method is invoked in a worker thread.
|
|
|
|
@Override
|
|
|
|
protected FilterResults performFiltering(CharSequence constraint) {
|
|
|
|
FilterResults filterResults = new FilterResults();
|
|
|
|
if (constraint != null) {
|
|
|
|
List<AutocompleteResult> results =
|
|
|
|
autocompletionProvider.search(constraint.toString());
|
|
|
|
filterResults.values = results;
|
|
|
|
filterResults.count = results.size();
|
|
|
|
}
|
|
|
|
return filterResults;
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
@Override
|
|
|
|
protected void publishResults(CharSequence constraint, FilterResults results) {
|
|
|
|
if (results != null && results.count > 0) {
|
|
|
|
resultList.clear();
|
|
|
|
resultList.addAll((List<AutocompleteResult>) results.values);
|
|
|
|
notifyDataSetChanged();
|
|
|
|
} else {
|
|
|
|
notifyDataSetInvalidated();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@NonNull
|
|
|
|
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
|
|
|
|
View view = convertView;
|
|
|
|
final Context context = parent.getContext();
|
|
|
|
|
|
|
|
switch (getItemViewType(position)) {
|
|
|
|
case ACCOUNT_VIEW_TYPE:
|
2019-03-04 19:28:08 +01:00
|
|
|
AccountViewHolder accountViewHolder;
|
2019-01-28 11:04:05 +01:00
|
|
|
if (convertView == null) {
|
|
|
|
view = ((LayoutInflater) context
|
|
|
|
.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
|
|
|
|
.inflate(R.layout.item_autocomplete_account, parent, false);
|
|
|
|
}
|
|
|
|
if (view.getTag() == null) {
|
|
|
|
view.setTag(new AccountViewHolder(view));
|
|
|
|
}
|
2019-03-04 19:28:08 +01:00
|
|
|
accountViewHolder = (AccountViewHolder) view.getTag();
|
2019-01-28 11:04:05 +01:00
|
|
|
|
|
|
|
AccountResult accountResult = ((AccountResult) getItem(position));
|
|
|
|
if (accountResult != null) {
|
|
|
|
Account account = accountResult.account;
|
2019-03-04 19:28:08 +01:00
|
|
|
String formattedUsername = context.getString(
|
|
|
|
R.string.status_username_format,
|
|
|
|
account.getUsername()
|
|
|
|
);
|
|
|
|
accountViewHolder.username.setText(formattedUsername);
|
2019-01-28 11:04:05 +01:00
|
|
|
CharSequence emojifiedName = CustomEmojiHelper.emojifyString(account.getName(),
|
2019-03-04 19:28:08 +01:00
|
|
|
account.getEmojis(), accountViewHolder.displayName);
|
|
|
|
accountViewHolder.displayName.setText(emojifiedName);
|
2019-01-28 11:04:05 +01:00
|
|
|
if (!account.getAvatar().isEmpty()) {
|
|
|
|
Picasso.with(context)
|
|
|
|
.load(account.getAvatar())
|
|
|
|
.placeholder(R.drawable.avatar_default)
|
2019-03-04 19:28:08 +01:00
|
|
|
.into(accountViewHolder.avatar);
|
2019-01-28 11:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HASHTAG_VIEW_TYPE:
|
|
|
|
if (convertView == null) {
|
|
|
|
view = ((LayoutInflater) context
|
|
|
|
.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
|
2019-03-04 19:28:08 +01:00
|
|
|
.inflate(R.layout.item_autocomplete_hashtag, parent, false);
|
2019-01-28 11:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
HashtagResult result = (HashtagResult) getItem(position);
|
|
|
|
if (result != null) {
|
|
|
|
((TextView) view).setText(formatHashtag(result));
|
|
|
|
}
|
|
|
|
break;
|
2019-03-04 19:28:08 +01:00
|
|
|
|
|
|
|
case EMOJI_VIEW_TYPE:
|
|
|
|
EmojiViewHolder emojiViewHolder;
|
|
|
|
if (convertView == null) {
|
|
|
|
view = ((LayoutInflater) context
|
|
|
|
.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
|
|
|
|
.inflate(R.layout.item_autocomplete_emoji, parent, false);
|
|
|
|
}
|
|
|
|
if (view.getTag() == null) {
|
|
|
|
view.setTag(new EmojiViewHolder(view));
|
|
|
|
}
|
|
|
|
emojiViewHolder = (EmojiViewHolder) view.getTag();
|
|
|
|
|
|
|
|
EmojiResult emojiResult = ((EmojiResult) getItem(position));
|
|
|
|
if (emojiResult != null) {
|
|
|
|
Emoji emoji = emojiResult.emoji;
|
|
|
|
String formattedShortcode = context.getString(
|
|
|
|
R.string.emoji_shortcode_format,
|
|
|
|
emoji.getShortcode()
|
|
|
|
);
|
|
|
|
emojiViewHolder.shortcode.setText(formattedShortcode);
|
|
|
|
Picasso.with(context)
|
|
|
|
.load(emoji.getUrl())
|
|
|
|
.into(emojiViewHolder.preview);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SEPARATOR_VIEW_TYPE:
|
|
|
|
if (convertView == null) {
|
|
|
|
view = ((LayoutInflater) context
|
|
|
|
.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
|
|
|
|
.inflate(R.layout.item_autocomplete_divider, parent, false);
|
|
|
|
}
|
|
|
|
break;
|
2019-01-28 11:04:05 +01:00
|
|
|
default:
|
|
|
|
throw new AssertionError("unknown view type");
|
|
|
|
}
|
|
|
|
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2019-01-30 22:10:05 +01:00
|
|
|
private String formatUsername(AccountResult result) {
|
|
|
|
return String.format("@%s", result.account.getUsername());
|
|
|
|
}
|
|
|
|
|
2019-01-28 11:04:05 +01:00
|
|
|
private String formatHashtag(HashtagResult result) {
|
|
|
|
return String.format("#%s", result.hashtag);
|
|
|
|
}
|
|
|
|
|
2019-03-04 19:28:08 +01:00
|
|
|
private String formatEmoji(EmojiResult result) {
|
|
|
|
return String.format(":%s:", result.emoji.getShortcode());
|
|
|
|
}
|
|
|
|
|
2019-01-28 11:04:05 +01:00
|
|
|
@Override
|
|
|
|
public int getViewTypeCount() {
|
2019-03-04 19:28:08 +01:00
|
|
|
return 4;
|
2019-01-28 11:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemViewType(int position) {
|
2019-03-04 19:28:08 +01:00
|
|
|
AutocompleteResult item = getItem(position);
|
|
|
|
|
|
|
|
if (item instanceof AccountResult) {
|
2019-01-28 11:04:05 +01:00
|
|
|
return ACCOUNT_VIEW_TYPE;
|
2019-03-04 19:28:08 +01:00
|
|
|
} else if (item instanceof HashtagResult) {
|
2019-01-28 11:04:05 +01:00
|
|
|
return HASHTAG_VIEW_TYPE;
|
2019-03-04 19:28:08 +01:00
|
|
|
} else if (item instanceof EmojiResult) {
|
|
|
|
return EMOJI_VIEW_TYPE;
|
|
|
|
} else {
|
|
|
|
return SEPARATOR_VIEW_TYPE;
|
2019-01-28 11:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 19:28:08 +01:00
|
|
|
@Override
|
|
|
|
public boolean areAllItemsEnabled() {
|
|
|
|
// there may be separators
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isEnabled(int position) {
|
|
|
|
return !(getItem(position) instanceof ResultSeparator);
|
|
|
|
}
|
|
|
|
|
2019-01-28 11:04:05 +01:00
|
|
|
public abstract static class AutocompleteResult {
|
|
|
|
AutocompleteResult() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public final static class AccountResult extends AutocompleteResult {
|
|
|
|
private final Account account;
|
|
|
|
|
|
|
|
public AccountResult(Account account) {
|
|
|
|
this.account = account;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public final static class HashtagResult extends AutocompleteResult {
|
|
|
|
private final String hashtag;
|
|
|
|
|
|
|
|
public HashtagResult(String hashtag) {
|
|
|
|
this.hashtag = hashtag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 19:28:08 +01:00
|
|
|
public final static class EmojiResult extends AutocompleteResult {
|
|
|
|
private final Emoji emoji;
|
|
|
|
|
|
|
|
public EmojiResult(Emoji emoji) {
|
|
|
|
this.emoji = emoji;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public final static class ResultSeparator extends AutocompleteResult {}
|
|
|
|
|
2019-01-28 11:04:05 +01:00
|
|
|
public interface AutocompletionProvider {
|
|
|
|
List<AutocompleteResult> search(String mention);
|
|
|
|
}
|
|
|
|
|
|
|
|
private class AccountViewHolder {
|
|
|
|
final TextView username;
|
|
|
|
final TextView displayName;
|
|
|
|
final ImageView avatar;
|
|
|
|
|
|
|
|
private AccountViewHolder(View view) {
|
|
|
|
username = view.findViewById(R.id.username);
|
|
|
|
displayName = view.findViewById(R.id.display_name);
|
|
|
|
avatar = view.findViewById(R.id.avatar);
|
|
|
|
}
|
|
|
|
}
|
2019-03-04 19:28:08 +01:00
|
|
|
|
|
|
|
private class EmojiViewHolder {
|
|
|
|
final TextView shortcode;
|
|
|
|
final ImageView preview;
|
|
|
|
|
|
|
|
private EmojiViewHolder(View view) {
|
|
|
|
shortcode = view.findViewById(R.id.shortcode);
|
|
|
|
preview = view.findViewById(R.id.preview);
|
|
|
|
}
|
|
|
|
}
|
2019-01-28 11:04:05 +01:00
|
|
|
}
|