Created
February 2, 2019 02:50
-
-
Save baggednismo/2840d0f777438d29673bc29096153970 to your computer and use it in GitHub Desktop.
This file contains 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
@Override | |
public void onKitchenOrdersReceived(KitchenOrdersResponse mKitchenOrdersResponse) | |
{ | |
AppLog.d(TAG, "onKitchenOrdersReceived"); | |
this.mKitchenOrdersResponse = mKitchenOrdersResponse; | |
mAdapter = new OrdersAdapter(mKitchenOrdersResponse); | |
recyclerView.setAdapter(mAdapter); | |
ItemTouchHelper.Callback callback = new ItemTouchHelperCallback(mAdapter); | |
ItemTouchHelper touchHelper = new ItemTouchHelper(callback); | |
touchHelper.attachToRecyclerView(recyclerView); | |
} |
This file contains 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
public class ItemTouchHelperCallback extends ItemTouchHelper.Callback | |
{ | |
private final ItemTouchHelperAdapter mAdapter; | |
public ItemTouchHelperCallback(OrdersAdapter adapter) | |
{ | |
mAdapter = adapter; | |
} | |
@Override | |
public boolean isLongPressDragEnabled() | |
{ | |
return true; | |
} | |
@Override | |
public boolean isItemViewSwipeEnabled() | |
{ | |
return true; | |
} | |
@Override | |
public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) | |
{ | |
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN; | |
int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END; | |
return makeMovementFlags(dragFlags, swipeFlags); | |
} | |
@Override | |
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder viewHolder1) | |
{ | |
mAdapter.onItemMove(viewHolder.getAdapterPosition(), viewHolder1.getAdapterPosition()); | |
return true; | |
} | |
@Override | |
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) | |
{ | |
mAdapter.onItemDismiss(viewHolder.getAdapterPosition()); | |
} | |
@Override | |
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) | |
{ | |
if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) | |
{ | |
ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder; | |
itemViewHolder.onItemSelected(); | |
} | |
super.onSelectedChanged(viewHolder, actionState); | |
} | |
@Override | |
public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) | |
{ | |
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); | |
} | |
@Override | |
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) | |
{ | |
super.clearView(recyclerView, viewHolder); | |
ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder; | |
itemViewHolder.onItemClear(); | |
} | |
} |
This file contains 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
public class KitchenOrdersResponse extends BaseResponse implements Parcelable | |
{ | |
/** | |
* { | |
* "status": true, | |
* "items": [ | |
* { | |
* "id": "LZWBJx0PmYkEwjGdr2vO", | |
* "item_id": "LZWBJx0PmYkEwjGdr2vO", | |
* "user_id": "nK8oWGj0zxqELP1A53ak", | |
* "completed": 0, | |
* "completed_at": "2019-01-28 20:33:31", | |
* "created_at": "2019-01-28 15:14:34", | |
* "updated_at": "2019-01-28 20:33:31", | |
* "products": [ | |
* { | |
* "id": "bZ6xBGP2mdPm3R8VX17L", | |
* "name": "Crispy Hot Dogs Wrapped in Bacon", | |
* "description": "Hot dogs on a bun with bacon lightly cooked.", | |
* "image": "http:\/\/testing.com\/kitchen-products\/images\/bZ6xBGP2mdPm3R8VX17L" | |
* } | |
* ], | |
* "user": { | |
* "id": "nK8oWGj0zxqELP1A53ak", | |
* "first_name": "John", | |
* "last_name": "Doe", | |
* "email": "[email protected]" | |
* } | |
* } | |
* ] | |
* } | |
*/ | |
public static final Parcelable.Creator<KitchenOrdersResponse> CREATOR = new Parcelable.Creator<KitchenOrdersResponse>() | |
{ | |
@Override | |
public KitchenOrdersResponse createFromParcel(Parcel source) | |
{ | |
return new KitchenOrdersResponse(source); | |
} | |
@Override | |
public KitchenOrdersResponse[] newArray(int size) | |
{ | |
return new KitchenOrdersResponse[size]; | |
} | |
}; | |
@SerializedName("items") | |
@Expose | |
private List<Item> items = new ArrayList<>(); | |
protected KitchenOrdersResponse(Parcel in) | |
{ | |
this.items = in.createTypedArrayList(Item.CREATOR); | |
} | |
public List<Item> getItems() | |
{ | |
return items; | |
} | |
public void setItems(List<Item> items) | |
{ | |
this.items = items; | |
} | |
@Override | |
public int describeContents() | |
{ | |
return 0; | |
} | |
@Override | |
public void writeToParcel(Parcel dest, int flags) | |
{ | |
dest.writeTypedList(this.items); | |
} | |
public static class Item implements Parcelable | |
{ | |
public static final Parcelable.Creator<Item> CREATOR = new Parcelable.Creator<Item>() | |
{ | |
@Override | |
public Item createFromParcel(Parcel source) | |
{ | |
return new Item(source); | |
} | |
@Override | |
public Item[] newArray(int size) | |
{ | |
return new Item[size]; | |
} | |
}; | |
@SerializedName("id") | |
@Expose | |
private String id; | |
@SerializedName("item_id") | |
@Expose | |
private String itemId; | |
@SerializedName("user_id") | |
@Expose | |
private String userId; | |
@SerializedName("completed") | |
@Expose | |
private Integer completed; | |
@SerializedName("completed_at") | |
@Expose | |
private String completedAt; | |
@SerializedName("created_at") | |
@Expose | |
private String createdAt; | |
@SerializedName("updated_at") | |
@Expose | |
private String updatedAt; | |
@SerializedName("notes") | |
@Expose | |
private String notes; | |
@SerializedName("products") | |
@Expose | |
private ArrayList<ProductsModel> products = new ArrayList<>(); | |
@SerializedName("user") | |
@Expose | |
private UserModel user; | |
public Item() { } | |
protected Item(Parcel in) | |
{ | |
this.id = in.readString(); | |
this.itemId = in.readString(); | |
this.userId = in.readString(); | |
this.completed = (Integer) in.readValue(Integer.class.getClassLoader()); | |
this.completedAt = in.readString(); | |
this.createdAt = in.readString(); | |
this.updatedAt = in.readString(); | |
} | |
public String getId() | |
{ | |
return id; | |
} | |
public void setId(String id) | |
{ | |
this.id = id; | |
} | |
public String getItemId() | |
{ | |
return itemId; | |
} | |
public void setItemId(String itemId) | |
{ | |
this.itemId = itemId; | |
} | |
public String getUserId() | |
{ | |
return userId; | |
} | |
public void setUserId(String userId) | |
{ | |
this.userId = userId; | |
} | |
public Integer getCompleted() | |
{ | |
return completed; | |
} | |
public void setCompleted(Integer completed) | |
{ | |
this.completed = completed; | |
} | |
public String getCompletedAt() | |
{ | |
return completedAt; | |
} | |
public void setCompletedAt(String completedAt) | |
{ | |
this.completedAt = completedAt; | |
} | |
public String getCreatedAt() | |
{ | |
return createdAt; | |
} | |
public void setCreatedAt(String createdAt) | |
{ | |
this.createdAt = createdAt; | |
} | |
public String getUpdatedAt() | |
{ | |
return updatedAt; | |
} | |
public void setUpdatedAt(String updatedAt) | |
{ | |
this.updatedAt = updatedAt; | |
} | |
public ArrayList<ProductsModel> getProducts() | |
{ | |
return products; | |
} | |
public void setProducts(ArrayList<ProductsModel> products) | |
{ | |
this.products = products; | |
} | |
public String getNotes() | |
{ | |
return notes; | |
} | |
public void setNotes(String notes) | |
{ | |
this.notes = notes; | |
} | |
public UserModel getUser() | |
{ | |
return user; | |
} | |
@Override | |
public int describeContents() | |
{ | |
return 0; | |
} | |
@Override | |
public void writeToParcel(Parcel dest, int flags) | |
{ | |
dest.writeString(this.id); | |
dest.writeString(this.itemId); | |
dest.writeString(this.userId); | |
dest.writeValue(this.completed); | |
dest.writeString(this.completedAt); | |
dest.writeString(this.createdAt); | |
dest.writeString(this.updatedAt); | |
} | |
} | |
} |
This file contains 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
public class OrdersAdapter extends RecyclerView.Adapter<OrdersAdapter.ViewHolder> implements ItemTouchHelperAdapter | |
{ | |
private static final String TAG = "OrdersAdapter"; | |
private KitchenOrdersResponse mKitchenOrdersResponse; | |
private List<KitchenOrdersResponse.Item> mItems = new ArrayList<>(); | |
public OrdersAdapter(KitchenOrdersResponse aKitchenOrdersResponse) | |
{ | |
AppLog.d(TAG, "OrdersAdapter()"); | |
mKitchenOrdersResponse = aKitchenOrdersResponse; | |
mItems.addAll(mKitchenOrdersResponse.getItems()); | |
setHasStableIds(true); | |
} | |
@Override | |
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) | |
{ | |
return new ViewHolder(LayoutInflater.from(viewGroup.getContext()) | |
.inflate(R.layout.layout_orders_item, viewGroup, false)); | |
} | |
@Override | |
public void onBindViewHolder(@NonNull final OrdersAdapter.ViewHolder mViewHolder, int i) | |
{ | |
mViewHolder.setIsRecyclable(false); | |
KitchenOrdersResponse.Item mItem = mKitchenOrdersResponse.getItems().get(i); | |
if (mItem.getCompleted() != 1) | |
{ | |
AppLog.d(TAG, mItem.getId()); | |
String orderDate = DateTimeUtils.getFormattedDateTime(mItem.getCreatedAt()); | |
mViewHolder.date.setText(orderDate); | |
mViewHolder.fullName.setText(mItem.getUser().getFullName()); | |
String products = ""; | |
for (int j = 0; j < mItem.getProducts().size(); j++) | |
{ | |
products += mItem.getProducts().get(j).getName() + "\n"; | |
} | |
mViewHolder.orderItems.setText(products); | |
mViewHolder.notes.setText(mItem.getNotes()); | |
} | |
} | |
@Override | |
public int getItemCount() | |
{ | |
return mItems.size(); | |
} | |
@Override | |
public void onItemMove(int fromPosition, int toPosition) { } | |
@Override | |
public void onItemDismiss(int position) | |
{ | |
AppLog.d(TAG, "onItemDismiss(): position " + position + " item ID: " + mItems.get(position).getId()); | |
mItems.remove(position); | |
notifyItemRemoved(position); | |
} | |
@Override | |
public long getItemId(int position) { | |
return position; | |
} | |
@Override | |
public int getItemViewType(int position) { | |
return position; | |
} | |
static class ViewHolder extends RecyclerView.ViewHolder implements ItemTouchHelperViewHolder | |
{ | |
@BindView(R.id.date) | |
AppCompatTextView date; | |
@BindView(R.id.fullName) | |
AppCompatTextView fullName; | |
@BindView(R.id.orderItems) | |
AppCompatTextView orderItems; | |
@BindView(R.id.tvNotes) | |
AppCompatTextView notes; | |
View itemView; | |
public ViewHolder(View itemView) | |
{ | |
super(itemView); | |
this.itemView = itemView; | |
ButterKnife.bind(this, itemView); | |
} | |
@Override | |
public void onItemSelected() { } | |
@Override | |
public void onItemClear() { } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment