Skip to content

Commit

Permalink
#107: added preserving categories filter state
Browse files Browse the repository at this point in the history
  • Loading branch information
bugy committed Feb 2, 2018
1 parent 184af6a commit e573a01
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public class CategoriesFilter extends FrameLayout {

private FactoryBasedAdapter<Category> categoriesAdapter;
private View popupAnchor;
private Context popupContext;
private final List<CategoriesFilter.Listener> listeners = new CopyOnWriteArrayList<>();
private PopupWindow popupWindow;

Expand Down Expand Up @@ -69,7 +68,6 @@ public CategoriesFilter(Context context, AttributeSet attrs, int defStyleAttr, i

private void init(final Context context) {
popupAnchor = this;
popupContext = context;

inflate(context, R.layout.categories_filter, this);

Expand Down Expand Up @@ -180,12 +178,21 @@ public void setCategories(Collection<Category> categories) {
categoriesAdapter.addAll(categories);
}

public void setPopupAnchor(View view) {
this.popupAnchor = view;
public void selectCategories(Collection<Category> categories) {
final List<Category> selectedItems = categoriesAdapter.getSelectedItems();
for (Category selectedItem : selectedItems) {
if (!categories.contains(selectedItem)) {
categoriesAdapter.deselectItem(selectedItem);
}
}

for (Category category : categories) {
categoriesAdapter.selectItem(category);
}
}

public void setPopupContext(Context popupContext) {
this.popupContext = popupContext;
public void setPopupAnchor(View view) {
this.popupAnchor = view;
}

public void addListener(CategoriesFilter.Listener listener) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import net.buggy.shoplist.units.EditProductUnit.ProductEditedEvent;
import net.buggy.shoplist.units.views.ViewRenderer;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
Expand All @@ -45,7 +46,7 @@ public class SelectShopItemsUnit extends Unit<ShopListActivity> {
private transient FactoryBasedAdapter<ShopItem> adapter;

private final Set<ShopItem> existingItems;
private List<Category> selectedCategories;
private final List<Category> selectedCategories = new ArrayList<>();

private final Map<Product, ShopItem> cachedItems = new ConcurrentHashMap<>();
private final Set<ShopItem> selectedItems = Collections.newSetFromMap(new ConcurrentHashMap<ShopItem, Boolean>());
Expand Down Expand Up @@ -263,15 +264,19 @@ public void renderTo(RelativeLayout parentView, ShopListActivity activity) {
R.id.unit_create_shopitems_toolbar_categories_filter);
categoriesFilter.setPopupAnchor(parentView);
categoriesFilter.setCategories(dao.getCategories());
categoriesFilter.selectCategories(selectedCategories);

categoriesFilter.addListener(new CategoriesFilter.Listener() {
@Override
public void categoriesSelected(List<Category> categories) {
selectedCategories = categories;
selectedCategories.clear();
selectedCategories.addAll(categories);

filterProducts(selectedCategories);
filterProducts(categories);
}
});

filterProducts(selectedCategories);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public class ShopItemListUnit extends Unit<ShopListActivity> {
private transient ImageButton cleanCheckedButton;
private Boolean showTipsOnEmpty;

private final List<Category> filterCategories = new ArrayList<>();

@Override
public void initialize() {
addRenderer(ShopListActivity.TOOLBAR_VIEW_ID, new ToolbarRenderer());
Expand Down Expand Up @@ -475,13 +477,19 @@ public void renderTo(final RelativeLayout parentView, final ShopListActivity act
R.id.unit_shop_item_list_toolbar_categories_filter);
categoriesFilter.setPopupAnchor(parentView);
categoriesFilter.setCategories(activity.getDao().getCategories());
categoriesFilter.selectCategories(filterCategories);

categoriesFilter.addListener(new CategoriesFilter.Listener() {
@Override
public void categoriesSelected(List<Category> categories) {
filterCategories.clear();
filterCategories.addAll(categories);

filterShopItems(categories);
}
});

filterShopItems(filterCategories);
}
}

Expand Down

0 comments on commit e573a01

Please sign in to comment.