-
Notifications
You must be signed in to change notification settings - Fork 23
Add Categories #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tobiasKaminsky
wants to merge
4
commits into
woefe:master
Choose a base branch
from
tobiasKaminsky:addCategories
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add Categories #65
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,14 +15,16 @@ | |
import android.widget.ImageView; | ||
import android.widget.TextView; | ||
|
||
import com.afollestad.sectionedrecyclerview.SectionedRecyclerViewAdapter; | ||
import com.afollestad.sectionedrecyclerview.SectionedViewHolder; | ||
import com.woefe.shoppinglist.R; | ||
import com.woefe.shoppinglist.shoppinglist.ListItem; | ||
import com.woefe.shoppinglist.shoppinglist.ShoppingList; | ||
|
||
/** | ||
* @author Wolfgang Popp | ||
*/ | ||
public class RecyclerListAdapter extends RecyclerView.Adapter<RecyclerListAdapter.ViewHolder> { | ||
public class RecyclerListAdapter extends SectionedRecyclerViewAdapter<SectionedViewHolder> { | ||
private final int colorChecked; | ||
private final int colorDefault; | ||
private final int colorBackground; | ||
|
@@ -40,9 +42,6 @@ public void onShoppingListUpdate(ShoppingList list, ShoppingList.Event e) { | |
case ShoppingList.Event.ITEM_INSERTED: | ||
notifyItemInserted(e.getIndex()); | ||
break; | ||
case ShoppingList.Event.ITEM_MOVED: | ||
notifyItemMoved(e.getOldIndex(), e.getNewIndex()); | ||
break; | ||
case ShoppingList.Event.ITEM_REMOVED: | ||
notifyItemRemoved(e.getIndex()); | ||
break; | ||
|
@@ -72,8 +71,8 @@ public void disconnectShoppingList() { | |
} | ||
} | ||
|
||
public void move(int fromPos, int toPos) { | ||
shoppingList.move(fromPos, toPos); | ||
public void move(ListItem from, ListItem to) { | ||
shoppingList.move(from, to); | ||
} | ||
|
||
public void remove(int pos) { | ||
|
@@ -90,76 +89,119 @@ public void setOnItemLongClickListener(ItemLongClickListener listener) { | |
|
||
@NonNull | ||
@Override | ||
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { | ||
View v = LayoutInflater.from(parent.getContext()) | ||
.inflate(R.layout.list_item, parent, false); | ||
return new ViewHolder(v); | ||
public SectionedViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { | ||
if (viewType == VIEW_TYPE_HEADER) { | ||
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_category, parent, false); | ||
return new CategoryViewHolder(v); | ||
} else { | ||
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false); | ||
return new ItemViewHolder(v); | ||
} | ||
} | ||
|
||
@Override | ||
public int getSectionCount() { | ||
return shoppingList.getCategories().size(); | ||
} | ||
|
||
@Override | ||
public int getItemCount(int section) { | ||
String category = shoppingList.getCategories().get(section); | ||
int count = 0; | ||
|
||
for (int i = 0; i < shoppingList.size(); i++) { | ||
if (shoppingList.get(i).getCategory().equals(category)) { | ||
count++; | ||
} | ||
} | ||
|
||
return count; | ||
} | ||
|
||
@Override | ||
public void onBindHeaderViewHolder(SectionedViewHolder sectionedViewHolder, int section, boolean expanded) { | ||
CategoryViewHolder categoryViewHolder = (CategoryViewHolder) sectionedViewHolder; | ||
categoryViewHolder.category.setText(shoppingList.getCategories().get(section)); | ||
} | ||
|
||
@Override | ||
public void onBindFooterViewHolder(SectionedViewHolder sectionedViewHolder, int i) { | ||
// not needed | ||
} | ||
|
||
@Override | ||
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) { | ||
ListItem listItem = shoppingList.get(position); | ||
holder.description.setText(listItem.getDescription()); | ||
holder.quantity.setText(listItem.getQuantity()); | ||
public void onBindViewHolder(SectionedViewHolder sectionedViewHolder, | ||
int section, | ||
int relativePosition, | ||
final int absolutePosition) { | ||
|
||
final ItemViewHolder itemViewHolder = (ItemViewHolder) sectionedViewHolder; | ||
String category = shoppingList.getCategories().get(section); | ||
final ListItem listItem = shoppingList.getListItemByCategory(category).get(relativePosition); | ||
|
||
itemViewHolder.description.setText(listItem.getDescription()); | ||
itemViewHolder.quantity.setText(listItem.getQuantity()); | ||
itemViewHolder.listItem = listItem; | ||
|
||
if (listItem.isChecked()) { | ||
holder.description.setTextColor(colorChecked); | ||
holder.quantity.setTextColor(colorChecked); | ||
itemViewHolder.description.setTextColor(colorChecked); | ||
itemViewHolder.quantity.setTextColor(colorChecked); | ||
} else { | ||
holder.description.setTextColor(colorDefault); | ||
holder.quantity.setTextColor(colorDefault); | ||
itemViewHolder.description.setTextColor(colorDefault); | ||
itemViewHolder.quantity.setTextColor(colorDefault); | ||
} | ||
|
||
holder.itemView.setBackgroundColor(colorBackground); | ||
itemViewHolder.itemView.setBackgroundColor(colorBackground); | ||
|
||
holder.view.setOnClickListener(new View.OnClickListener() { | ||
itemViewHolder.view.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
shoppingList.toggleChecked(holder.getAdapterPosition()); | ||
shoppingList.toggleChecked(listItem, absolutePosition); | ||
} | ||
}); | ||
|
||
|
||
holder.view.setOnLongClickListener(new View.OnLongClickListener() { | ||
itemViewHolder.view.setOnLongClickListener(new View.OnLongClickListener() { | ||
@Override | ||
public boolean onLongClick(View v) { | ||
return longClickListener != null | ||
&& longClickListener.onLongClick(holder.getAdapterPosition()); | ||
&& longClickListener.onLongClick(listItem); | ||
} | ||
}); | ||
|
||
holder.dragHandler.setOnTouchListener(new View.OnTouchListener() { | ||
itemViewHolder.dragHandler.setOnTouchListener(new View.OnTouchListener() { | ||
@Override | ||
public boolean onTouch(View v, MotionEvent event) { | ||
if (event.getAction() == MotionEvent.ACTION_DOWN) { | ||
touchHelper.startDrag(holder); | ||
touchHelper.startDrag(itemViewHolder); | ||
return true; | ||
} | ||
return false; | ||
} | ||
}); | ||
|
||
} | ||
|
||
@Override | ||
public int getItemCount() { | ||
if (shoppingList != null) { | ||
return shoppingList.size(); | ||
} | ||
return 0; | ||
public interface ItemLongClickListener { | ||
boolean onLongClick(ListItem item); | ||
} | ||
|
||
public interface ItemLongClickListener { | ||
boolean onLongClick(int position); | ||
static class CategoryViewHolder extends SectionedViewHolder { | ||
TextView category; | ||
|
||
CategoryViewHolder(View itemView) { | ||
super(itemView); | ||
|
||
category = itemView.findViewById(R.id.category); | ||
} | ||
} | ||
|
||
public static class ViewHolder extends RecyclerView.ViewHolder { | ||
static class ItemViewHolder extends SectionedViewHolder { | ||
TextView description; | ||
TextView quantity; | ||
ImageView dragHandler; | ||
View view; | ||
ListItem listItem; | ||
|
||
public ViewHolder(View itemView) { | ||
public ItemViewHolder(View itemView) { | ||
super(itemView); | ||
view = itemView; | ||
description = itemView.findViewById(R.id.text_description); | ||
|
@@ -190,19 +232,28 @@ public boolean isLongPressDragEnabled() { | |
} | ||
|
||
@Override | ||
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { | ||
public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) { | ||
final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN; | ||
final int swipeFlags = ItemTouchHelper.START; | ||
return makeMovementFlags(dragFlags, swipeFlags); | ||
} | ||
|
||
@Override | ||
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { | ||
public boolean onMove(@NonNull RecyclerView recyclerView, | ||
RecyclerView.ViewHolder viewHolder, | ||
RecyclerView.ViewHolder target) { | ||
if (viewHolder.getItemViewType() != target.getItemViewType()) { | ||
return false; | ||
} | ||
|
||
RecyclerListAdapter.this.move(viewHolder.getAdapterPosition(), target.getAdapterPosition()); | ||
if (!(viewHolder instanceof ItemViewHolder) || !(target instanceof ItemViewHolder)) { | ||
return false; | ||
} | ||
|
||
ItemViewHolder sourceViewHolder = (ItemViewHolder) viewHolder; | ||
ItemViewHolder targetViewHolder = (ItemViewHolder) target; | ||
|
||
RecyclerListAdapter.this.move(sourceViewHolder.listItem, targetViewHolder.listItem); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not entirely sure where it was introduced, but when moving items, it only swaps with the neighbor item. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will have a look 👍 |
||
return true; | ||
} | ||
|
||
|
@@ -212,7 +263,13 @@ public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { | |
} | ||
|
||
@Override | ||
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { | ||
public void onChildDraw(@NonNull Canvas c, | ||
@NonNull RecyclerView recyclerView, | ||
@NonNull RecyclerView.ViewHolder viewHolder, | ||
float dX, | ||
float dY, | ||
int actionState, | ||
boolean isCurrentlyActive) { | ||
|
||
if (actionState != ItemTouchHelper.ACTION_STATE_SWIPE) { | ||
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not too happy about this dependency, since it seems to be pretty much dead. The author even removed it from his GitHub profile
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know, this is totally outdated, but also used in NC Files App…
If you find a proper replace, I am more than happy to change it.
(also the other way around, if I find one for NC Files App).
But for now it "just works" (for >500k users…)