2019-01-14 08:59:47 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
import android.widget.ArrayAdapter;
|
|
|
|
|
import android.widget.ImageView;
|
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
2019-03-01 09:07:15 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
2019-01-14 08:59:47 +00:00
|
|
|
import androidx.annotation.NonNull;
|
2019-03-01 09:07:15 +00:00
|
|
|
import androidx.annotation.Nullable;
|
2019-01-14 08:59:47 +00:00
|
|
|
|
|
|
|
|
public class DrawerAdapter extends ArrayAdapter<DrawerItem> {
|
2019-03-01 09:07:15 +00:00
|
|
|
private List<DrawerItem> items = new ArrayList<>();
|
|
|
|
|
|
2019-01-14 08:59:47 +00:00
|
|
|
DrawerAdapter(@NonNull Context context) {
|
|
|
|
|
super(context, -1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
|
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
|
|
|
|
|
DrawerItem item = getItem(position);
|
|
|
|
|
View row = LayoutInflater.from(getContext()).inflate(item.getLayout(), null);
|
|
|
|
|
|
|
|
|
|
ImageView iv = row.findViewById(R.id.ivItem);
|
|
|
|
|
TextView tv = row.findViewById(R.id.tvItem);
|
|
|
|
|
|
|
|
|
|
if (iv != null) {
|
|
|
|
|
iv.setImageResource(item.getIcon());
|
|
|
|
|
if (item.getColor() != null)
|
|
|
|
|
iv.setColorFilter(item.getColor());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tv != null) {
|
2019-02-26 17:44:15 +00:00
|
|
|
tv.setText(item.getTitle(getContext()));
|
2019-01-14 08:59:47 +00:00
|
|
|
|
|
|
|
|
tv.setTextColor(Helper.resolveColor(getContext(),
|
|
|
|
|
item.getHighlight() ? R.attr.colorUnread : android.R.attr.textColorSecondary));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return row;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-01 09:07:15 +00:00
|
|
|
void set(List<DrawerItem> items) {
|
|
|
|
|
this.items = items;
|
|
|
|
|
notifyDataSetChanged();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int getCount() {
|
|
|
|
|
return items.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
|
@Override
|
|
|
|
|
public DrawerItem getItem(int position) {
|
|
|
|
|
if (position < items.size())
|
|
|
|
|
return items.get(position);
|
|
|
|
|
else
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-26 17:44:15 +00:00
|
|
|
@Override
|
|
|
|
|
public boolean hasStableIds() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public long getItemId(int position) {
|
|
|
|
|
DrawerItem item = getItem(position);
|
|
|
|
|
return (item == null ? 0 : item.getId());
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 08:59:47 +00:00
|
|
|
@Override
|
|
|
|
|
public boolean isEnabled(int position) {
|
|
|
|
|
DrawerItem item = getItem(position);
|
|
|
|
|
return (item != null && item.getId() != 0);
|
|
|
|
|
}
|
|
|
|
|
}
|