fedilab-Android-App/app/src/main/java/app/fedilab/android/drawers/AccountsSearchAdapter.java

182 lines
6.3 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.drawers;
2017-05-05 16:36:04 +02:00
/* Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2017-05-05 16:36:04 +02:00
*
* 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.
*
2019-05-18 11:10:30 +02:00
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
2017-05-05 16:36:04 +02:00
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
2019-05-18 11:10:30 +02:00
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
2017-05-05 16:36:04 +02:00
* see <http://www.gnu.org/licenses>. */
import android.content.Context;
2017-10-04 15:57:53 +02:00
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
2019-06-11 19:38:26 +02:00
import androidx.annotation.NonNull;
2017-05-05 16:36:04 +02:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
2017-08-02 17:44:36 +02:00
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
2017-05-05 16:36:04 +02:00
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
2017-12-02 11:02:25 +01:00
import com.bumptech.glide.Glide;
2017-08-02 17:44:36 +02:00
import java.util.ArrayList;
2017-05-05 16:36:04 +02:00
import java.util.List;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.Entities.Account;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.sqlite.AccountDAO;
import app.fedilab.android.sqlite.Sqlite;
import app.fedilab.android.R;
2017-05-05 16:36:04 +02:00
/**
* Created by Thomas on 05/05/2017.
* Adapter for accounts when searching
*/
2017-08-02 17:44:36 +02:00
public class AccountsSearchAdapter extends ArrayAdapter<Account> implements Filterable {
2017-05-05 16:36:04 +02:00
2017-08-02 17:44:36 +02:00
private List<Account> accounts, tempAccounts, suggestions ;
2017-05-05 16:36:04 +02:00
private LayoutInflater layoutInflater;
2017-10-04 13:44:32 +02:00
private boolean owner;
2017-10-04 15:57:53 +02:00
private Context context;
2017-05-05 16:36:04 +02:00
public AccountsSearchAdapter(Context context, List<Account> accounts){
2017-08-02 17:44:36 +02:00
super(context, android.R.layout.simple_list_item_1, accounts);
2017-05-05 16:36:04 +02:00
this.accounts = accounts;
2017-10-04 15:57:53 +02:00
this.context = context;
2017-08-02 17:44:36 +02:00
this.tempAccounts = new ArrayList<>(accounts);
this.suggestions = new ArrayList<>(accounts);
2017-05-05 16:36:04 +02:00
layoutInflater = LayoutInflater.from(context);
2017-10-04 13:44:32 +02:00
this.owner = false;
}
public AccountsSearchAdapter(Context context, List<Account> accounts, boolean owner){
super(context, android.R.layout.simple_list_item_1, accounts);
this.accounts = accounts;
2017-10-04 15:57:53 +02:00
this.context = context;
2017-10-04 13:44:32 +02:00
this.tempAccounts = new ArrayList<>(accounts);
this.suggestions = new ArrayList<>(accounts);
layoutInflater = LayoutInflater.from(context);
this.owner = owner;
2017-05-05 16:36:04 +02:00
}
2017-10-25 07:53:31 +02:00
2017-05-05 16:36:04 +02:00
@Override
public int getCount() {
return accounts.size();
}
@Override
2017-08-02 17:44:36 +02:00
public Account getItem(int position) {
2017-05-05 16:36:04 +02:00
return accounts.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
2017-08-02 17:44:36 +02:00
@NonNull
2017-05-05 16:36:04 +02:00
@Override
2017-08-02 17:44:36 +02:00
public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
2017-05-05 16:36:04 +02:00
final Account account = accounts.get(position);
final ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.drawer_account_search, parent, false);
holder = new ViewHolder();
2017-12-17 13:56:38 +01:00
holder.account_pp = convertView.findViewById(R.id.account_pp);
holder.account_dn = convertView.findViewById(R.id.account_dn);
holder.account_un = convertView.findViewById(R.id.account_un);
2017-05-05 16:36:04 +02:00
2017-12-17 13:56:38 +01:00
holder.account_container = convertView.findViewById(R.id.account_container);
2017-05-05 16:36:04 +02:00
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
2017-10-04 13:44:32 +02:00
if( owner) {
2017-10-04 15:57:53 +02:00
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
2019-06-05 14:35:42 +02:00
String instance = sharedpreferences.getString(Helper.PREF_INSTANCE, null);
Account currentAccount = new AccountDAO(context, db).getUniqAccount(userId, instance);
instance = (account.getInstance() !=null)?account.getInstance():currentAccount.getInstance();
2017-10-04 15:57:53 +02:00
holder.account_un.setText(String.format("@%s", account.getUsername() + "@" + instance));
2017-10-04 13:44:32 +02:00
holder.account_dn.setVisibility(View.GONE);
}else {
2017-10-15 18:20:51 +02:00
holder.account_un.setText(String.format("@%s", account.getAcct()));
2017-10-04 13:44:32 +02:00
holder.account_dn.setText(Helper.shortnameToUnicode(account.getDisplay_name(), true));
holder.account_dn.setVisibility(View.VISIBLE);
}
2017-05-05 16:36:04 +02:00
//Profile picture
2017-12-02 15:05:54 +01:00
Glide.with(holder.account_pp.getContext())
2017-12-02 11:02:25 +01:00
.load(account.getAvatar())
.into(holder.account_pp);
2017-05-05 16:36:04 +02:00
return convertView;
}
2017-08-02 17:44:36 +02:00
@NonNull
@Override
public Filter getFilter() {
return accountFilter;
}
private Filter accountFilter = new Filter() {
@Override
public CharSequence convertResultToString(Object resultValue) {
Account account = (Account) resultValue;
return "@" + account.getAcct();
2017-08-02 17:44:36 +02:00
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
if (constraint != null) {
suggestions.clear();
2018-01-13 10:41:56 +01:00
suggestions.addAll(tempAccounts);
2017-08-02 17:44:36 +02:00
FilterResults filterResults = new FilterResults();
filterResults.values = suggestions;
filterResults.count = suggestions.size();
return filterResults;
} else {
return new FilterResults();
}
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
ArrayList<Account> c = (ArrayList<Account>) results.values;
if (results.count > 0) {
clear();
2018-01-13 10:41:56 +01:00
addAll(c);
notifyDataSetChanged();
} else{
2017-08-02 17:44:36 +02:00
clear();
notifyDataSetChanged();
}
}
};
2017-05-05 16:36:04 +02:00
private class ViewHolder {
ImageView account_pp;
TextView account_dn;
TextView account_un;
LinearLayout account_container;
}
}