diff options
author | Stefan Niedermann <info@niedermann.it> | 2020-05-05 15:26:54 +0300 |
---|---|---|
committer | Stefan Niedermann <info@niedermann.it> | 2020-05-05 15:26:54 +0300 |
commit | f0b0fe24a1cb68f0019d3400ec6a981165d65f36 (patch) | |
tree | ed44c868bbff0ac8b307802bc565a1657d2a5a48 /app/src/main/java | |
parent | eb022fc23cb6ce4935dcd9b60b3eccca727422ce (diff) |
Flatten due type filter list
Diffstat (limited to 'app/src/main/java')
10 files changed, 145 insertions, 146 deletions
diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterAssigneesFragment.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterAssigneesFragment.java index 6c7c1bd5c..1a4b37d0d 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterAssigneesFragment.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterAssigneesFragment.java @@ -8,13 +8,11 @@ import android.view.ViewGroup; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; -import androidx.lifecycle.LiveData; import androidx.lifecycle.ViewModelProvider; import it.niedermann.nextcloud.deck.R; import it.niedermann.nextcloud.deck.databinding.DialogFilterAssigneesBinding; import it.niedermann.nextcloud.deck.model.User; -import it.niedermann.nextcloud.deck.model.internal.FilterInformation; import it.niedermann.nextcloud.deck.persistence.sync.SyncManager; import it.niedermann.nextcloud.deck.ui.MainViewModel; @@ -24,26 +22,20 @@ import static java.util.Objects.requireNonNull; public class FilterAssigneesFragment extends Fragment implements SelectionListener<User> { - private LiveData<FilterInformation> filterInformationDraft; - private DialogFilterAssigneesBinding binding; - private MainViewModel mainViewModel; private FilterViewModel filterViewModel; - private UserFilterAdapter userAdapter; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - binding = DialogFilterAssigneesBinding.inflate(requireActivity().getLayoutInflater()); - mainViewModel = new ViewModelProvider(requireActivity()).get(MainViewModel.class); + final DialogFilterAssigneesBinding binding = DialogFilterAssigneesBinding.inflate(requireActivity().getLayoutInflater()); + final MainViewModel mainViewModel = new ViewModelProvider(requireActivity()).get(MainViewModel.class); + filterViewModel = new ViewModelProvider(requireActivity()).get(FilterViewModel.class); - final SyncManager syncManager = new SyncManager(requireActivity()); - this.filterInformationDraft = filterViewModel.getFilterInformationDraft(); - observeOnce(syncManager.findProposalsForUsersToAssign(mainViewModel.getCurrentAccount().getId(), mainViewModel.getCurrentBoardLocalId()), requireActivity(), (users) -> { - userAdapter = new UserFilterAdapter(dpToPx(requireContext(), R.dimen.avatar_size), mainViewModel.getCurrentAccount(), users, requireNonNull(this.filterInformationDraft.getValue()).getUsers(), this); + observeOnce(new SyncManager(requireContext()).findProposalsForUsersToAssign(mainViewModel.getCurrentAccount().getId(), mainViewModel.getCurrentBoardLocalId()), requireActivity(), (users) -> { binding.users.setNestedScrollingEnabled(false); - binding.users.setAdapter(userAdapter); + binding.users.setAdapter(new FilterUserAdapter(dpToPx(requireContext(), R.dimen.avatar_size), mainViewModel.getCurrentAccount(), users, requireNonNull(filterViewModel.getFilterInformationDraft().getValue()).getUsers(), this)); }); return binding.getRoot(); diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterDialogFragment.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterDialogFragment.java index 35b787dfa..b33b2a859 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterDialogFragment.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterDialogFragment.java @@ -122,7 +122,7 @@ public class FilterDialogFragment extends BrandedDialogFragment { case 1: return new FilterAssigneesFragment(); case 2: - return new FilterDuedateFragment(); + return new FilterDueTypeFragment(); default: throw new IllegalArgumentException("position must be between 0 and 2"); } diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterDueTypeAdapter.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterDueTypeAdapter.java new file mode 100644 index 000000000..dab5bb9f7 --- /dev/null +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterDueTypeAdapter.java @@ -0,0 +1,84 @@ +package it.niedermann.nextcloud.deck.ui.filter; + +import android.view.LayoutInflater; +import android.view.ViewGroup; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.recyclerview.widget.RecyclerView; + +import java.util.Arrays; + +import it.niedermann.nextcloud.deck.databinding.ItemFilterDuetypeBinding; +import it.niedermann.nextcloud.deck.model.enums.EDueType; + +public class FilterDueTypeAdapter extends RecyclerView.Adapter<FilterDueTypeAdapter.DueTypeViewHolder> { + @NonNull + private final EDueType[] dueTypes = EDueType.values(); + private int selectedDueTypePosition; + @Nullable + private final SelectionListener<EDueType> selectionListener; + + @SuppressWarnings("WeakerAccess") + public FilterDueTypeAdapter(@NonNull EDueType selectedDueType, @Nullable SelectionListener<EDueType> selectionListener) { + super(); + this.selectedDueTypePosition = Arrays.binarySearch(dueTypes, selectedDueType); + this.selectionListener = selectionListener; + setHasStableIds(true); + notifyDataSetChanged(); + } + + @NonNull + @Override + public DueTypeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + return new DueTypeViewHolder(ItemFilterDuetypeBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false)); + } + + @Override + public void onBindViewHolder(@NonNull DueTypeViewHolder viewHolder, int position) { + viewHolder.bind(dueTypes[position]); + } + + @Override + public long getItemId(int position) { + return position; + } + + @Override + public int getItemCount() { + return dueTypes.length; + } + + class DueTypeViewHolder extends RecyclerView.ViewHolder { + private ItemFilterDuetypeBinding binding; + + DueTypeViewHolder(@NonNull ItemFilterDuetypeBinding binding) { + super(binding.getRoot()); + this.binding = binding; + } + + void bind(final EDueType dueType) { + binding.dueType.setText(dueType.toString(binding.dueType.getContext())); + itemView.setSelected(dueTypes[selectedDueTypePosition].equals(dueType)); + + itemView.setOnClickListener(view -> { + final int oldSelection = selectedDueTypePosition; + if (dueTypes[selectedDueTypePosition].equals(dueType)) { + selectedDueTypePosition = Arrays.binarySearch(dueTypes, EDueType.NO_FILTER); + itemView.setSelected(false); + if (selectionListener != null) { + selectionListener.onItemSelected(EDueType.NO_FILTER); + } + notifyItemChanged(selectedDueTypePosition); + } else { + selectedDueTypePosition = Arrays.binarySearch(dueTypes, dueType); + itemView.setSelected(true); + if (selectionListener != null) { + selectionListener.onItemSelected(dueType); + } + } + notifyItemChanged(oldSelection); + }); + } + } +} diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterDueTypeFragment.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterDueTypeFragment.java new file mode 100644 index 000000000..01ba1a966 --- /dev/null +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterDueTypeFragment.java @@ -0,0 +1,40 @@ +package it.niedermann.nextcloud.deck.ui.filter; + +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; +import androidx.lifecycle.ViewModelProvider; + +import it.niedermann.nextcloud.deck.databinding.DialogFilterDuedateBinding; +import it.niedermann.nextcloud.deck.model.enums.EDueType; + +import static java.util.Objects.requireNonNull; + +public class FilterDueTypeFragment extends Fragment implements SelectionListener<EDueType> { + + private FilterViewModel filterViewModel; + + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + + final DialogFilterDuedateBinding binding = DialogFilterDuedateBinding.inflate(requireActivity().getLayoutInflater()); + + filterViewModel = new ViewModelProvider(requireActivity()).get(FilterViewModel.class); + + binding.dueType.setItemAnimator(null); + binding.dueType.setAdapter(new FilterDueTypeAdapter(requireNonNull(filterViewModel.getFilterInformationDraft().getValue()).getDueType(), this)); + + return binding.getRoot(); + } + + @Override + public void onItemSelected(EDueType item) { + filterViewModel.setFilterInformationDraftDueType(item); + } +} diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterDuedateFragment.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterDuedateFragment.java deleted file mode 100644 index 654e95744..000000000 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterDuedateFragment.java +++ /dev/null @@ -1,48 +0,0 @@ -package it.niedermann.nextcloud.deck.ui.filter; - -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.AdapterView; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.fragment.app.Fragment; -import androidx.lifecycle.LiveData; -import androidx.lifecycle.ViewModelProvider; - -import it.niedermann.nextcloud.deck.databinding.DialogFilterDuedateBinding; -import it.niedermann.nextcloud.deck.model.enums.EDueType; -import it.niedermann.nextcloud.deck.model.internal.FilterInformation; - -public class FilterDuedateFragment extends Fragment implements AdapterView.OnItemSelectedListener { - - private LiveData<FilterInformation> filterInformationDraft; - private DialogFilterDuedateBinding binding; - private FilterViewModel filterViewModel; - private OverdueFilterAdapter overdueAdapter; - - @Nullable - @Override - public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - binding = DialogFilterDuedateBinding.inflate(requireActivity().getLayoutInflater()); - filterViewModel = new ViewModelProvider(requireActivity()).get(FilterViewModel.class); - overdueAdapter = new OverdueFilterAdapter(requireContext()); - binding.overdue.setAdapter(overdueAdapter); - this.filterInformationDraft = filterViewModel.getFilterInformationDraft(); - binding.overdue.setSelection(overdueAdapter.getPosition(this.filterInformationDraft.getValue().getDueType())); - binding.overdue.setOnItemSelectedListener(this); - return binding.getRoot(); - } - - @Override - public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { - filterViewModel.setFilterInformationDraftDueType(overdueAdapter.getItem(position)); - } - - @Override - public void onNothingSelected(AdapterView<?> parent) { - filterViewModel.setFilterInformationDraftDueType(EDueType.NO_FILTER); - } -} diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/LabelFilterAdapter.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterLabelAdapter.java index e5c84b019..a7e379e27 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/LabelFilterAdapter.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterLabelAdapter.java @@ -17,7 +17,7 @@ import it.niedermann.nextcloud.deck.model.Label; import it.niedermann.nextcloud.deck.util.ColorUtil; @SuppressWarnings("WeakerAccess") -public class LabelFilterAdapter extends RecyclerView.Adapter<LabelFilterAdapter.LabelViewHolder> { +public class FilterLabelAdapter extends RecyclerView.Adapter<FilterLabelAdapter.LabelViewHolder> { @NonNull private final List<Label> labels = new ArrayList<>(); @NonNull @@ -25,7 +25,7 @@ public class LabelFilterAdapter extends RecyclerView.Adapter<LabelFilterAdapter. @Nullable private final SelectionListener<Label> selectionListener; - public LabelFilterAdapter(@NonNull List<Label> labels, @NonNull List<Label> selectedLabels, @Nullable SelectionListener<Label> selectionListener) { + public FilterLabelAdapter(@NonNull List<Label> labels, @NonNull List<Label> selectedLabels, @Nullable SelectionListener<Label> selectionListener) { super(); this.labels.addAll(labels); this.selectedLabels.addAll(selectedLabels); diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterLabelsFragment.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterLabelsFragment.java index 64384d89f..155cc53bb 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterLabelsFragment.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterLabelsFragment.java @@ -8,12 +8,10 @@ import android.view.ViewGroup; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; -import androidx.lifecycle.LiveData; import androidx.lifecycle.ViewModelProvider; import it.niedermann.nextcloud.deck.databinding.DialogFilterLabelsBinding; import it.niedermann.nextcloud.deck.model.Label; -import it.niedermann.nextcloud.deck.model.internal.FilterInformation; import it.niedermann.nextcloud.deck.persistence.sync.SyncManager; import it.niedermann.nextcloud.deck.ui.MainViewModel; @@ -22,26 +20,22 @@ import static java.util.Objects.requireNonNull; public class FilterLabelsFragment extends Fragment implements SelectionListener<Label> { - private LiveData<FilterInformation> filterInformationDraft; - private DialogFilterLabelsBinding binding; - private MainViewModel mainViewModel; private FilterViewModel filterViewModel; - private LabelFilterAdapter labelAdapter; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - binding = DialogFilterLabelsBinding.inflate(requireActivity().getLayoutInflater()); - mainViewModel = new ViewModelProvider(requireActivity()).get(MainViewModel.class); + + final DialogFilterLabelsBinding binding = DialogFilterLabelsBinding.inflate(requireActivity().getLayoutInflater()); + final MainViewModel mainViewModel = new ViewModelProvider(requireActivity()).get(MainViewModel.class); + filterViewModel = new ViewModelProvider(requireActivity()).get(FilterViewModel.class); - final SyncManager syncManager = new SyncManager(requireActivity()); - this.filterInformationDraft = filterViewModel.getFilterInformationDraft(); - observeOnce(syncManager.findProposalsForLabelsToAssign(mainViewModel.getCurrentAccount().getId(), mainViewModel.getCurrentBoardLocalId()), requireActivity(), (labels) -> { - labelAdapter = new LabelFilterAdapter(labels, requireNonNull(this.filterInformationDraft.getValue()).getLabels(), this); + observeOnce(new SyncManager(requireContext()).findProposalsForLabelsToAssign(mainViewModel.getCurrentAccount().getId(), mainViewModel.getCurrentBoardLocalId()), requireActivity(), (labels) -> { binding.labels.setNestedScrollingEnabled(false); - binding.labels.setAdapter(labelAdapter); + binding.labels.setAdapter(new FilterLabelAdapter(labels, requireNonNull(filterViewModel.getFilterInformationDraft().getValue()).getLabels(), this)); }); + return binding.getRoot(); } diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/UserFilterAdapter.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterUserAdapter.java index 4744f42ce..b4ae8f679 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/UserFilterAdapter.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterUserAdapter.java @@ -18,7 +18,7 @@ import it.niedermann.nextcloud.deck.model.User; import it.niedermann.nextcloud.deck.util.ViewUtil; @SuppressWarnings("WeakerAccess") -public class UserFilterAdapter extends RecyclerView.Adapter<UserFilterAdapter.UserViewHolder> { +public class FilterUserAdapter extends RecyclerView.Adapter<FilterUserAdapter.UserViewHolder> { @Px final int avatarSize; @NonNull @@ -30,7 +30,7 @@ public class UserFilterAdapter extends RecyclerView.Adapter<UserFilterAdapter.Us @Nullable private final SelectionListener<User> selectionListener; - public UserFilterAdapter(@Px int avatarSize, @NonNull Account account, @NonNull List<User> users, @NonNull List<User> selectedUsers, @Nullable SelectionListener selectionListener) { + public FilterUserAdapter(@Px int avatarSize, @NonNull Account account, @NonNull List<User> users, @NonNull List<User> selectedUsers, @Nullable SelectionListener selectionListener) { super(); this.avatarSize = avatarSize; this.account = account; diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/OverdueFilterAdapter.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/OverdueFilterAdapter.java deleted file mode 100644 index 5de6c50c8..000000000 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/OverdueFilterAdapter.java +++ /dev/null @@ -1,63 +0,0 @@ -package it.niedermann.nextcloud.deck.ui.filter; - -import android.content.Context; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ArrayAdapter; -import android.widget.TextView; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; - -import org.jetbrains.annotations.NotNull; - -import java.util.Objects; - -import it.niedermann.nextcloud.deck.model.enums.EDueType; - -public class OverdueFilterAdapter extends ArrayAdapter<EDueType> { - - @NonNull - private final LayoutInflater inflater; - - @SuppressWarnings("WeakerAccess") - public OverdueFilterAdapter(@NonNull Context context) { - super(context, android.R.layout.simple_list_item_1, android.R.id.text1, EDueType.values()); - inflater = LayoutInflater.from(context); - } - - @Override - public boolean hasStableIds() { - return true; - } - - @Override - public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { - return getView(position, convertView, parent); - } - - @NonNull - @Override - public EDueType getItem(int position) { - EDueType item = super.getItem(position); - if (item != null) { - return item; - } else { - return EDueType.NO_FILTER; - } - } - - @NotNull - @Override - public View getView(int position, View convertView, @NotNull ViewGroup parent) { - final View view; - if (convertView == null) { - view = inflater.inflate(android.R.layout.simple_list_item_1, parent, false); - } else { - view = convertView; - } - ((TextView) view.findViewById(android.R.id.text1)).setText(Objects.requireNonNull(getItem(position)).toString(view.getContext())); - return view; - } -} diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/SelectionListener.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/SelectionListener.java index 84e635743..d2635a860 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/SelectionListener.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/SelectionListener.java @@ -1,9 +1,9 @@ package it.niedermann.nextcloud.deck.ui.filter; -import it.niedermann.nextcloud.deck.model.interfaces.IRemoteEntity; - -public interface SelectionListener<T extends IRemoteEntity> { +public interface SelectionListener<T> { void onItemSelected(T item); - void onItemDeselected(T item); + default void onItemDeselected(T item) { + // Deselecting is optional + } } |