diff options
author | stefan-niedermann <info@niedermann.it> | 2020-03-06 22:22:25 +0300 |
---|---|---|
committer | stefan-niedermann <info@niedermann.it> | 2020-03-06 22:22:25 +0300 |
commit | 8555d4d0d53ce1acf64d815b82095efb4a6c7252 (patch) | |
tree | f1bc55a22561d637ed25aaa829aa280538b250ae /app/src/main/java/it/niedermann/nextcloud/deck/ui/card/LabelAutoCompleteAdapter.java | |
parent | ad793eddbb89ccc0fa2c5aec20de65748c33b6fe (diff) |
Generify AutoCompleteAdapter
Diffstat (limited to 'app/src/main/java/it/niedermann/nextcloud/deck/ui/card/LabelAutoCompleteAdapter.java')
-rw-r--r-- | app/src/main/java/it/niedermann/nextcloud/deck/ui/card/LabelAutoCompleteAdapter.java | 100 |
1 files changed, 23 insertions, 77 deletions
diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/LabelAutoCompleteAdapter.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/LabelAutoCompleteAdapter.java index 266b3699b..685214ea9 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/LabelAutoCompleteAdapter.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/LabelAutoCompleteAdapter.java @@ -1,97 +1,62 @@ package it.niedermann.nextcloud.deck.ui.card; -import android.app.Activity; -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 androidx.activity.ComponentActivity; import androidx.annotation.NonNull; -import androidx.fragment.app.Fragment; -import androidx.lifecycle.LifecycleOwner; +import androidx.annotation.Nullable; import androidx.lifecycle.LiveData; import java.util.ArrayList; import java.util.List; -import java.util.Objects; import it.niedermann.nextcloud.deck.R; import it.niedermann.nextcloud.deck.databinding.ItemAutocompleteDropdownBinding; import it.niedermann.nextcloud.deck.model.Label; -import it.niedermann.nextcloud.deck.persistence.sync.SyncManager; +import it.niedermann.nextcloud.deck.util.AutoCompleteAdapter; import it.niedermann.nextcloud.deck.util.ViewUtil; -public class LabelAutoCompleteAdapter extends BaseAdapter implements Filterable { - public static final long CREATE_ID = Long.MIN_VALUE; - private Context context; - private List<Label> labelList = new ArrayList<>(); - private SyncManager syncManager; - private long accountId; - private long boardId; - private long cardId; - private LifecycleOwner owner; +public class LabelAutoCompleteAdapter extends AutoCompleteAdapter<Label> { + @Nullable private Label createLabel; private String lastFilterText; private boolean canManage = false; - LabelAutoCompleteAdapter(@NonNull LifecycleOwner owner, Activity activity, long accountId, long boardId, long cardId) { - this.owner = owner; - this.context = activity; - this.accountId = accountId; - this.boardId = boardId; - this.cardId = cardId; - syncManager = new SyncManager(activity); - syncManager.getFullBoardById(accountId, boardId).observe(owner, (fullBoard) -> { + LabelAutoCompleteAdapter(@NonNull ComponentActivity activity, long accountId, long boardId, long cardId) { + super(activity, accountId, boardId, cardId); + syncManager.getFullBoardById(accountId, boardId).observe(activity, (fullBoard) -> { if (fullBoard.getBoard().isPermissionManage()) { + canManage = true; createLabel = new Label(); - createLabel.setLocalId(CREATE_ID); + createLabel.setLocalId(ITEM_CREATE); createLabel.setBoardId(boardId); createLabel.setAccountId(accountId); - createLabel.setColor(Integer.toHexString(context.getResources().getColor(R.color.grey600))); - canManage = true; + createLabel.setColor(Integer.toHexString(this.activity.getResources().getColor(R.color.grey600))); } }); } @Override - public int getCount() { - return labelList.size(); - } - - @Override - public Label getItem(int position) { - return labelList.get(position); - } - - @Override - public long getItemId(int position) { - return position; - } - - @Override public View getView(int position, View convertView, ViewGroup parent) { - ViewHolder holder; + ViewHolder<ItemAutocompleteDropdownBinding> holder; if (convertView != null) { holder = (ViewHolder) convertView.getTag(); } else { - LayoutInflater inflater = (LayoutInflater) context - .getSystemService(Context.LAYOUT_INFLATER_SERVICE); ItemAutocompleteDropdownBinding binding = ItemAutocompleteDropdownBinding.inflate(inflater, parent, false); - holder = new ViewHolder(binding); + holder = new ViewHolder<>(binding); convertView = binding.getRoot(); convertView.setTag(holder); } int iconResource = R.drawable.ic_label_grey600_24dp; - if((lastFilterText != null && lastFilterText.length() > 0) && (position == labelList.size() - (canManage ? 1 : 0))) { + if ((lastFilterText != null && lastFilterText.length() > 0) && (position == itemList.size() - (canManage ? 1 : 0))) { iconResource = R.drawable.ic_plus; } holder.binding.icon.setImageDrawable( - ViewUtil.getTintedImageView(context, iconResource,"#" + getItem(position).getColor() + ViewUtil.getTintedImageView(activity, iconResource, "#" + getItem(position).getColor() )); holder.binding.label.setText(getItem(position).getTitle()); return convertView; @@ -99,21 +64,22 @@ public class LabelAutoCompleteAdapter extends BaseAdapter implements Filterable @Override public Filter getFilter() { - return new Filter() { - final FilterResults filterResults = new FilterResults(); - + return new AutoCompleteFilter() { @Override protected FilterResults performFiltering(CharSequence constraint) { if (constraint != null) { lastFilterText = constraint.toString(); - Objects.requireNonNull(((Fragment) owner).getActivity()).runOnUiThread(() -> { + activity.runOnUiThread(() -> { LiveData<List<Label>> liveData = constraint.toString().trim().length() > 0 ? syncManager.searchNotYetAssignedLabelsByTitle(accountId, boardId, cardId, constraint.toString()) - : syncManager.findProposalsForLabelsToAssign(accountId, boardId, cardId, context.getResources().getInteger(R.integer.max_labels_suggested)); - liveData.observe(owner, (labels -> { + : syncManager.findProposalsForLabelsToAssign(accountId, boardId, cardId, activity.getResources().getInteger(R.integer.max_labels_suggested)); + liveData.observe(activity, (labels -> { final boolean constraintLengthGreaterZero = constraint.toString().trim().length() > 0; if (canManage && constraintLengthGreaterZero) { - createLabel.setTitle(String.format(context.getString(R.string.label_add), constraint)); + if (createLabel == null) { + throw new IllegalStateException("Owner has right to edit card, but createLable is null"); + } + createLabel.setTitle(String.format(activity.getString(R.string.label_add), constraint)); } if (labels != null) { if (canManage && constraintLengthGreaterZero) { @@ -135,30 +101,10 @@ public class LabelAutoCompleteAdapter extends BaseAdapter implements Filterable } return filterResults; } - - @Override - protected void publishResults(CharSequence constraint, FilterResults results) { - if (results != null && results.count > 0) { - if (!labelList.equals(results.values)) { - labelList = (List<Label>) results.values; - } - notifyDataSetChanged(); - } else { - notifyDataSetInvalidated(); - } - } }; } String getLastFilterText() { return this.lastFilterText; } - - static class ViewHolder { - private ItemAutocompleteDropdownBinding binding; - - ViewHolder(ItemAutocompleteDropdownBinding binding) { - this.binding = binding; - } - } } |