diff --git a/.idea/.name b/.idea/.name index 8225020..645af55 100644 --- a/.idea/.name +++ b/.idea/.name @@ -1 +1 @@ -BackPack \ No newline at end of file +packwithme \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 508b3d9..8d2df47 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -5,18 +5,13 @@ - diff --git a/.idea/misc.xml b/.idea/misc.xml index 477b86a..5d19981 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -37,37 +37,10 @@ - + - - - - - 1.8 - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml index ea59753..53118bf 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,8 +2,8 @@ - + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 94a25f7..6564d52 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/app/src/main/java/hikapro/com/backpack/MainActivity.java b/app/src/main/java/hikapro/com/backpack/MainActivity.java index 9bde2ca..b053626 100644 --- a/app/src/main/java/hikapro/com/backpack/MainActivity.java +++ b/app/src/main/java/hikapro/com/backpack/MainActivity.java @@ -16,6 +16,7 @@ import hikapro.com.backpack.model.entities.Set; import hikapro.com.backpack.presenter.ItemDetailPresenter; import hikapro.com.backpack.presenter.ItemListPresenter; import hikapro.com.backpack.presenter.SetListPresenter; +import hikapro.com.backpack.presenter.adapters.helper.OnStartDragListener; import hikapro.com.backpack.view.View; import hikapro.com.backpack.view.fragments.ItemDetailFragment; import hikapro.com.backpack.view.fragments.ItemListFragment; diff --git a/app/src/main/java/hikapro/com/backpack/model/Model.java b/app/src/main/java/hikapro/com/backpack/model/Model.java index 6e06da9..6347622 100644 --- a/app/src/main/java/hikapro/com/backpack/model/Model.java +++ b/app/src/main/java/hikapro/com/backpack/model/Model.java @@ -28,6 +28,9 @@ public interface Model { int getSetsCount(); void setPresenter(Presenter.SetList presenter); Presenter.SetList getPresenter(); + + //GLM + List getSet(); } interface Item extends Base { diff --git a/app/src/main/java/hikapro/com/backpack/model/SetModel.java b/app/src/main/java/hikapro/com/backpack/model/SetModel.java index aebc326..1390631 100644 --- a/app/src/main/java/hikapro/com/backpack/model/SetModel.java +++ b/app/src/main/java/hikapro/com/backpack/model/SetModel.java @@ -121,9 +121,11 @@ public class SetModel implements Model.Set { //endregion + //GLM - - - - + @Override + public List getSet() + { + return iList; + } } diff --git a/app/src/main/java/hikapro/com/backpack/presenter/Presenter.java b/app/src/main/java/hikapro/com/backpack/presenter/Presenter.java index f7d927a..277232e 100644 --- a/app/src/main/java/hikapro/com/backpack/presenter/Presenter.java +++ b/app/src/main/java/hikapro/com/backpack/presenter/Presenter.java @@ -2,7 +2,10 @@ package hikapro.com.backpack.presenter; import android.content.Context; import android.os.Bundle; +import android.support.v7.widget.RecyclerView; +import android.view.DragEvent; import android.view.LayoutInflater; +import android.view.View; import android.view.ViewGroup; import hikapro.com.backpack.model.Model; @@ -34,6 +37,11 @@ public interface Presenter { void setModel(Model.Set model); void notifyDataSetChanged(); void showMessage(String message); + + //GLM_add_resources_SetList + void onItemDismiss(int position); + boolean onItemMove(int fromPosition, int toPosition); + void onStartDrag(RecyclerView.ViewHolder viewHolder); } interface ItemList extends Base { diff --git a/app/src/main/java/hikapro/com/backpack/presenter/SetListPresenter.java b/app/src/main/java/hikapro/com/backpack/presenter/SetListPresenter.java index 11cd528..3b61acd 100644 --- a/app/src/main/java/hikapro/com/backpack/presenter/SetListPresenter.java +++ b/app/src/main/java/hikapro/com/backpack/presenter/SetListPresenter.java @@ -2,21 +2,29 @@ package hikapro.com.backpack.presenter; import android.content.Context; import android.os.Bundle; +import android.support.v4.view.MotionEventCompat; import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; +import android.support.v7.widget.helper.ItemTouchHelper; +import android.view.DragEvent; import android.view.LayoutInflater; +import android.view.MotionEvent; import android.view.ViewGroup; import android.widget.Toast; import java.lang.ref.WeakReference; +import java.util.Collections; import hikapro.com.backpack.R; import hikapro.com.backpack.model.SetModel; import hikapro.com.backpack.model.Model; import hikapro.com.backpack.model.entities.Set; import hikapro.com.backpack.presenter.adapters.SetListAdapter; +import hikapro.com.backpack.presenter.adapters.helper.OnStartDragListener; +import hikapro.com.backpack.presenter.adapters.helper.SimpleItemTouchHelperCallback; import hikapro.com.backpack.view.View; +import hikapro.com.backpack.view.fragments.SetListFragment; import hikapro.com.backpack.view.recycler.SetViewHolder; /** @@ -28,12 +36,33 @@ public class SetListPresenter implements Presenter.SetList { private Model.Set model; private SetListAdapter adapter; + + //GLM_add_resources_SetList + private OnStartDragListener mDragStartListener; + private ItemTouchHelper mItemTouchHelper; + public SetListPresenter() { this.adapter = new SetListAdapter(this); } // life cycle --> + + //GLM_add_resources_SetList + @Override + public void onItemDismiss(int position) { + model.getSet().remove(position); + adapter.notifyItemRemoved(position); + } + + //GLM_add_resources_SetList + @Override + public boolean onItemMove(int fromPosition, int toPosition) { + Collections.swap(model.getSet(), fromPosition, toPosition); + adapter.notifyItemMoved(fromPosition, toPosition); + return true; + } + @Override public void onDestroy(boolean isChangingConfiguration) { view = null; @@ -52,6 +81,12 @@ public class SetListPresenter implements Presenter.SetList { setRecycler.setAdapter(adapter); setRecycler.setItemAnimator(new DefaultItemAnimator()); model.executeQuery(); + + //GLM_add_resources_SetList + ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(adapter); + mItemTouchHelper = new ItemTouchHelper(callback); + mItemTouchHelper.attachToRecyclerView(setRecycler); + return view; } @@ -81,6 +116,17 @@ public class SetListPresenter implements Presenter.SetList { showMessage("There is no view in presenter"); } }); + + + //GLM_add_resources_SetList + holder.cardView.setOnLongClickListener(new android.view.View.OnLongClickListener() { + @Override + public boolean onLongClick(android.view.View v) { + + mDragStartListener.onStartDrag(holder); + return false; + } + }); } @Override public int getSetsCount() { @@ -128,6 +174,7 @@ public class SetListPresenter implements Presenter.SetList { @Override public void setView(View.SetList view) { this.view = new WeakReference<>(view); + this.mDragStartListener = getView().getOnStartDragListener(); } @Override public void setModel(Model.Set model) { @@ -147,5 +194,9 @@ public class SetListPresenter implements Presenter.SetList { throw new NullPointerException("View is unavailable"); } - + //GLM_add_resources_SetList + @Override + public void onStartDrag(RecyclerView.ViewHolder viewHolder) { + mItemTouchHelper.startDrag(viewHolder); + } } diff --git a/app/src/main/java/hikapro/com/backpack/presenter/adapters/SetListAdapter.java b/app/src/main/java/hikapro/com/backpack/presenter/adapters/SetListAdapter.java index 15034fb..42fe656 100644 --- a/app/src/main/java/hikapro/com/backpack/presenter/adapters/SetListAdapter.java +++ b/app/src/main/java/hikapro/com/backpack/presenter/adapters/SetListAdapter.java @@ -4,12 +4,13 @@ import android.support.v7.widget.RecyclerView; import android.view.ViewGroup; import hikapro.com.backpack.presenter.SetListPresenter; +import hikapro.com.backpack.presenter.adapters.helper.ItemTouchHelperAdapter; import hikapro.com.backpack.view.recycler.SetViewHolder; /** * Created by tariel on 20/04/16. */ -public class SetListAdapter extends RecyclerView.Adapter { +public class SetListAdapter extends RecyclerView.Adapter implements ItemTouchHelperAdapter{ private SetListPresenter presenter; @@ -31,4 +32,16 @@ public class SetListAdapter extends RecyclerView.Adapter { public int getItemCount() { return presenter.getSetsCount(); } + + //GLM_add_resources_SetList + @Override + public void onItemDismiss(int position) { + presenter.onItemDismiss(position); + } + + //GLM_add_resources_SetList + @Override + public boolean onItemMove(int fromPosition, int toPosition) { + return presenter.onItemMove(fromPosition, toPosition); + } } diff --git a/app/src/main/java/hikapro/com/backpack/presenter/adapters/helper/ItemTouchHelperAdapter.java b/app/src/main/java/hikapro/com/backpack/presenter/adapters/helper/ItemTouchHelperAdapter.java new file mode 100644 index 0000000..bce3ead --- /dev/null +++ b/app/src/main/java/hikapro/com/backpack/presenter/adapters/helper/ItemTouchHelperAdapter.java @@ -0,0 +1,12 @@ +package hikapro.com.backpack.presenter.adapters.helper; + +/** + * Created by N551 on 25.04.2016. + */ +//GLM_add_resources_SetList +public interface ItemTouchHelperAdapter { + + boolean onItemMove(int fromPosition, int toPosition); + + void onItemDismiss(int position); +} diff --git a/app/src/main/java/hikapro/com/backpack/presenter/adapters/helper/ItemTouchHelperViewHolder.java b/app/src/main/java/hikapro/com/backpack/presenter/adapters/helper/ItemTouchHelperViewHolder.java new file mode 100644 index 0000000..0060002 --- /dev/null +++ b/app/src/main/java/hikapro/com/backpack/presenter/adapters/helper/ItemTouchHelperViewHolder.java @@ -0,0 +1,12 @@ +package hikapro.com.backpack.presenter.adapters.helper; + +/** + * Created by N551 on 25.04.2016. + */ +//GLM_add_resources_SetList +public interface ItemTouchHelperViewHolder { + + void onItemSelected(); + + void onItemClear(); +} diff --git a/app/src/main/java/hikapro/com/backpack/presenter/adapters/helper/OnStartDragListener.java b/app/src/main/java/hikapro/com/backpack/presenter/adapters/helper/OnStartDragListener.java new file mode 100644 index 0000000..aca2fbe --- /dev/null +++ b/app/src/main/java/hikapro/com/backpack/presenter/adapters/helper/OnStartDragListener.java @@ -0,0 +1,12 @@ +package hikapro.com.backpack.presenter.adapters.helper; + +import android.support.v7.widget.RecyclerView; + +/** + * Created by N551 on 25.04.2016. + */ +//GLM_add_resources_SetList +public interface OnStartDragListener { + + void onStartDrag(RecyclerView.ViewHolder viewHolder); +} diff --git a/app/src/main/java/hikapro/com/backpack/presenter/adapters/helper/SimpleItemTouchHelperCallback.java b/app/src/main/java/hikapro/com/backpack/presenter/adapters/helper/SimpleItemTouchHelperCallback.java new file mode 100644 index 0000000..c8774b3 --- /dev/null +++ b/app/src/main/java/hikapro/com/backpack/presenter/adapters/helper/SimpleItemTouchHelperCallback.java @@ -0,0 +1,93 @@ +package hikapro.com.backpack.presenter.adapters.helper; + +import android.graphics.Canvas; +import android.support.v7.widget.RecyclerView; +import android.support.v7.widget.helper.ItemTouchHelper; + +/** + * Created by N551 on 25.04.2016. + */ +//GLM_add_resources_SetList +public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback { + public static final float ALPHA_FULL = 1.0f; + + private final ItemTouchHelperAdapter mAdapter; + + public SimpleItemTouchHelperCallback(ItemTouchHelperAdapter adapter) { + mAdapter = adapter; + } + + @Override + public boolean isLongPressDragEnabled() { + return true; + } + + @Override + public boolean isItemViewSwipeEnabled() { + return false; + } + + @Override + public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { + + final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN; + final int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END; + return makeMovementFlags(dragFlags, swipeFlags); + } + + @Override + public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder source, RecyclerView.ViewHolder target) { + if (source.getItemViewType() != target.getItemViewType()) { + return false; + } + + // Notify the adapter of the move + mAdapter.onItemMove(source.getAdapterPosition(), target.getAdapterPosition()); + return true; + } + + @Override + public void onSwiped(RecyclerView.ViewHolder viewHolder, int i) { + // Notify the adapter of the dismissal + mAdapter.onItemDismiss(viewHolder.getAdapterPosition()); + } + + @Override + public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { + if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) { + // Fade out the view as it is swiped out of the parent's bounds + final float alpha = ALPHA_FULL - Math.abs(dX) / (float) viewHolder.itemView.getWidth(); + viewHolder.itemView.setAlpha(alpha); + viewHolder.itemView.setTranslationX(dX); + } else { + super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); + } + } + + @Override + public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) { + // We only want the active item to change + if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) { + if (viewHolder instanceof ItemTouchHelperViewHolder) { + // Let the view holder know that this item is being moved or dragged + ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder; + itemViewHolder.onItemSelected(); + } + } + + super.onSelectedChanged(viewHolder, actionState); + } + + @Override + public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { + super.clearView(recyclerView, viewHolder); + + viewHolder.itemView.setAlpha(ALPHA_FULL); + + if (viewHolder instanceof ItemTouchHelperViewHolder) { + // Tell the view holder it's time to restore the idle state + ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder; + itemViewHolder.onItemClear(); + } + } +} diff --git a/app/src/main/java/hikapro/com/backpack/view/View.java b/app/src/main/java/hikapro/com/backpack/view/View.java index 547ce9f..9e02aba 100644 --- a/app/src/main/java/hikapro/com/backpack/view/View.java +++ b/app/src/main/java/hikapro/com/backpack/view/View.java @@ -6,6 +6,7 @@ import android.widget.Toast; import hikapro.com.backpack.model.entities.Item; import hikapro.com.backpack.model.entities.Set; import hikapro.com.backpack.presenter.Presenter; +import hikapro.com.backpack.presenter.adapters.helper.OnStartDragListener; /** * Created by tariel on 19/04/16. @@ -19,6 +20,9 @@ public interface View { interface SetList extends Base { void showItemList(Set set); void setPresenter(Presenter.SetList presenter); + + //GLM_add_resources_SetList + OnStartDragListener getOnStartDragListener(); } interface ItemList extends Base { diff --git a/app/src/main/java/hikapro/com/backpack/view/fragments/SetListFragment.java b/app/src/main/java/hikapro/com/backpack/view/fragments/SetListFragment.java index 79c88b6..d2b4985 100644 --- a/app/src/main/java/hikapro/com/backpack/view/fragments/SetListFragment.java +++ b/app/src/main/java/hikapro/com/backpack/view/fragments/SetListFragment.java @@ -5,18 +5,18 @@ import android.app.Activity; import android.app.Fragment; import android.content.Context; import android.os.Bundle; +import android.support.v7.widget.RecyclerView; +import android.support.v7.widget.helper.ItemTouchHelper; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.Toast; -import hikapro.com.backpack.R; import hikapro.com.backpack.model.entities.Set; import hikapro.com.backpack.presenter.Presenter; -import hikapro.com.backpack.presenter.SetListPresenter; +import hikapro.com.backpack.presenter.adapters.helper.OnStartDragListener; -public class SetListFragment extends Fragment implements hikapro.com.backpack.view.View.SetList { +public class SetListFragment extends Fragment implements hikapro.com.backpack.view.View.SetList, OnStartDragListener{ private Presenter.SetList presenter; private hikapro.com.backpack.view.View.ActivityCallback activityCallback; @@ -130,4 +130,17 @@ public class SetListFragment extends Fragment implements hikapro.com.backpack.vi public void setPresenter(Presenter.SetList presenter) { this.presenter = presenter; } + + + //GLM_add_resources_SetList + @Override + public void onStartDrag(RecyclerView.ViewHolder viewHolder) { + presenter.onStartDrag(viewHolder); + } + + //GLM_add_resources_SetList + @Override + public OnStartDragListener getOnStartDragListener() { + return this; + } } diff --git a/app/src/main/java/hikapro/com/backpack/view/recycler/SetViewHolder.java b/app/src/main/java/hikapro/com/backpack/view/recycler/SetViewHolder.java index c51778c..e57463f 100644 --- a/app/src/main/java/hikapro/com/backpack/view/recycler/SetViewHolder.java +++ b/app/src/main/java/hikapro/com/backpack/view/recycler/SetViewHolder.java @@ -1,27 +1,53 @@ package hikapro.com.backpack.view.recycler; +import android.graphics.Color; +import android.graphics.drawable.Drawable; import android.support.v7.widget.CardView; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.TextView; import hikapro.com.backpack.R; +import hikapro.com.backpack.presenter.adapters.helper.ItemTouchHelperViewHolder; /** * Created by tariel on 20/04/16. */ -public class SetViewHolder extends RecyclerView.ViewHolder { +public class SetViewHolder extends RecyclerView.ViewHolder implements ItemTouchHelperViewHolder{ public CardView cardView; public TextView textView; + //GLM_add_resources_SetList + private View view; + private Drawable drawable; + public SetViewHolder(View v) { super(v); + // + this.view = v; + + // setupViews(v); } private void setupViews(View view) { cardView = (CardView) view.findViewById(R.id.card_view_set); textView = (TextView) view.findViewById(R.id.set_text); + + this.view = view; + drawable = view.getBackground(); + } + + //GLM_add_resources_SetList + @Override + public void onItemClear() { + view.setBackground(drawable); + } + + //GLM_add_resources_SetList + @Override + public void onItemSelected() { + view.setBackgroundColor(Color.GRAY); } }