diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadLabelScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadLabelScene.java index 95e1a1204..e71ba8f03 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadLabelScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadLabelScene.java @@ -19,7 +19,6 @@ package com.hippo.ehviewer.ui.scene; import android.content.DialogInterface; import android.graphics.drawable.NinePatchDrawable; import android.os.Bundle; -import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v7.app.AlertDialog; import android.support.v7.widget.LinearLayoutManager; @@ -53,26 +52,26 @@ import com.hippo.ehviewer.dao.DownloadLabel; import com.hippo.view.ViewTransition; import com.hippo.yorozuya.ViewUtils; -import java.util.HashMap; import java.util.List; -import java.util.Map; public class DownloadLabelScene extends ToolbarScene { + /*--------------- + Whole life cycle + ---------------*/ + @Nullable + public List mList = null; + + /*--------------- + View life cycle + ---------------*/ @Nullable private EasyRecyclerView mRecyclerView; @Nullable private ViewTransition mViewTransition; - @Nullable private RecyclerView.Adapter mAdapter; - @Nullable - public List mList = null; - - @NonNull - public final Map mPinState = new HashMap<>(); - @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -92,42 +91,37 @@ public class DownloadLabelScene extends ToolbarScene { @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.scene_download_label, container, false); - mRecyclerView = (EasyRecyclerView) view.findViewById(R.id.recycler_view); - View tip = view.findViewById(R.id.tip); + mRecyclerView = (EasyRecyclerView) ViewUtils.$$(view, R.id.recycler_view); + View tip = ViewUtils.$$(view, R.id.tip); + TextView tipText = (TextView) ViewUtils.$$(tip, R.id.tip_text); mViewTransition = new ViewTransition(mRecyclerView, tip); - if (mRecyclerView != null) { - // touch guard manager (this class is required to suppress scrolling while swipe-dismiss animation is running) - RecyclerViewTouchActionGuardManager guardManager = new RecyclerViewTouchActionGuardManager(); - guardManager.setInterceptVerticalScrollingWhileAnimationRunning(true); - guardManager.setEnabled(true); + tipText.setText(R.string.no_download_label); - // drag & drop manager - RecyclerViewDragDropManager dragDropManager = new RecyclerViewDragDropManager(); - dragDropManager.setDraggingItemShadowDrawable( - (NinePatchDrawable) getResources().getDrawable(R.drawable.shadow_8dp)); - - // swipe manager - RecyclerViewSwipeManager swipeManager = new RecyclerViewSwipeManager(); - - RecyclerView.Adapter adapter = new LabelAdapter(); - adapter.setHasStableIds(true); - adapter = dragDropManager.createWrappedAdapter(adapter); // wrap for dragging - adapter = swipeManager.createWrappedAdapter(adapter); // wrap for swiping - mAdapter = adapter; - - final GeneralItemAnimator animator = new SwipeDismissItemAnimator(); - animator.setSupportsChangeAnimations(false); - - mRecyclerView.hasFixedSize(); - mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext())); - mRecyclerView.setAdapter(adapter); - mRecyclerView.setItemAnimator(animator); - - guardManager.attachRecyclerView(mRecyclerView); - swipeManager.attachRecyclerView(mRecyclerView); - dragDropManager.attachRecyclerView(mRecyclerView); - } + // touch guard manager (this class is required to suppress scrolling while swipe-dismiss animation is running) + RecyclerViewTouchActionGuardManager guardManager = new RecyclerViewTouchActionGuardManager(); + guardManager.setInterceptVerticalScrollingWhileAnimationRunning(true); + guardManager.setEnabled(true); + // drag & drop manager + RecyclerViewDragDropManager dragDropManager = new RecyclerViewDragDropManager(); + dragDropManager.setDraggingItemShadowDrawable( + (NinePatchDrawable) getResources().getDrawable(R.drawable.shadow_8dp)); + // swipe manager + RecyclerViewSwipeManager swipeManager = new RecyclerViewSwipeManager(); + RecyclerView.Adapter adapter = new LabelAdapter(); + adapter.setHasStableIds(true); + adapter = dragDropManager.createWrappedAdapter(adapter); // wrap for dragging + adapter = swipeManager.createWrappedAdapter(adapter); // wrap for swiping + mAdapter = adapter; + final GeneralItemAnimator animator = new SwipeDismissItemAnimator(); + animator.setSupportsChangeAnimations(false); + mRecyclerView.hasFixedSize(); + mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext())); + mRecyclerView.setAdapter(adapter); + mRecyclerView.setItemAnimator(animator); + guardManager.attachRecyclerView(mRecyclerView); + swipeManager.attachRecyclerView(mRecyclerView); + dragDropManager.attachRecyclerView(mRecyclerView); updateView(); @@ -141,6 +135,15 @@ public class DownloadLabelScene extends ToolbarScene { setNavigationIcon(R.drawable.v_arrow_left_dark_x24); } + @Override + public void onDestroyView() { + super.onDestroyView(); + + mRecyclerView = null; + mViewTransition = null; + mAdapter = null; + } + @Override public void onNavigationClick() { onBackPressed(); @@ -176,55 +179,6 @@ public class DownloadLabelScene extends ToolbarScene { } } - private boolean isPinned(int position) { - if (mList != null) { - Boolean pinned = mPinState.get(mList.get(position)); - if (pinned != null) { - return pinned; - } - } - return false; - } - - private void setPinned(int position, boolean pinned) { - if (mList != null) { - mPinState.put(mList.get(position), pinned); - } - } - - private class DeleteLabelDialogHelper implements DialogInterface.OnClickListener { - - private final int mIndex; - - public DeleteLabelDialogHelper(int index) { - mIndex = index; - } - - @Override - public void onClick(DialogInterface dialog, int which) { - if (which != DialogInterface.BUTTON_POSITIVE) { - return; - } - if (mList == null) { - return; - } - - DownloadLabel raw = mList.get(mIndex); - EhApplication.getDownloadManager(getContext()).deleteLabel(raw.getLabel()); - mPinState.remove(raw); - if (mAdapter != null) { - mAdapter.notifyItemRemoved(mIndex); - } - if (mViewTransition != null) { - if (mList != null && mList.size() > 0) { - mViewTransition.showView(0); - } else { - mViewTransition.showView(1); - } - } - } - } - private class NewLabelDialogHelper implements View.OnClickListener { private final EditTextDialogBuilder mBuilder; @@ -308,7 +262,6 @@ public class DownloadLabelScene extends ToolbarScene { private class LabelHolder extends AbstractDraggableSwipeableItemViewHolder implements View.OnClickListener { - public final View delete; public final View swipeHandler; public final TextView label; public final View dragHandler; @@ -316,12 +269,10 @@ public class DownloadLabelScene extends ToolbarScene { public LabelHolder(View itemView) { super(itemView); - delete = itemView.findViewById(R.id.delete); - swipeHandler = itemView.findViewById(R.id.swipe_handler); - label = (TextView) itemView.findViewById(R.id.label); - dragHandler = itemView.findViewById(R.id.drag_handler); + swipeHandler = ViewUtils.$$(itemView, R.id.swipe_handler); + label = (TextView) ViewUtils.$$(itemView, R.id.label); + dragHandler = ViewUtils.$$(itemView, R.id.drag_handler); - delete.setOnClickListener(this); label.setOnClickListener(this); } @@ -336,14 +287,7 @@ public class DownloadLabelScene extends ToolbarScene { return; } - if (delete == v) { - DeleteLabelDialogHelper helper = new DeleteLabelDialogHelper(index); - new AlertDialog.Builder(getContext()) - .setTitle(R.string.delete_label_title) - .setMessage(getString(R.string.delete_label_message, mList.get(index).getLabel())) - .setPositiveButton(android.R.string.ok, helper) - .show(); - } else if (label == v) { + if (label == v) { DownloadLabel raw = mList.get(index); EditTextDialogBuilder builder = new EditTextDialogBuilder( getContext(), raw.getLabel(), getString(R.string.label)); @@ -367,7 +311,7 @@ public class DownloadLabelScene extends ToolbarScene { @Override public LabelHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new LabelHolder(getActivity().getLayoutInflater() - .inflate(R.layout.item_download_label, parent, false)); + .inflate(R.layout.item_label_list, parent, false)); } @Override @@ -375,11 +319,7 @@ public class DownloadLabelScene extends ToolbarScene { if (mList != null) { holder.label.setText(mList.get(position).getLabel()); } - - // set swiping properties - holder.setMaxLeftSwipeAmount(-0.5f); - holder.setMaxRightSwipeAmount(0); - holder.setSwipeItemHorizontalSlideAmount(isPinned(position) ? -0.5f : 0); + holder.setSwipeItemHorizontalSlideAmount(0); } @Override @@ -438,11 +378,7 @@ public class DownloadLabelScene extends ToolbarScene { case SwipeableItemConstants.RESULT_SWIPED_RIGHT: case SwipeableItemConstants.RESULT_CANCELED: default: - if (position >= 0) { - return new UnpinResultAction(position); - } else { - return null; - } + return new SwipeResultActionDefault(); } } } @@ -459,35 +395,30 @@ public class DownloadLabelScene extends ToolbarScene { protected void onPerformAction() { super.onPerformAction(); - if (!isPinned(mPosition)) { - setPinned(mPosition, true); - - if (mAdapter != null) { - mAdapter.notifyItemChanged(mPosition); - } + final List list = mList; + if (list == null || mPosition < 0 || mPosition >= list.size()) { + return; } - } - } + final DownloadLabel label = list.get(mPosition); - private class UnpinResultAction extends SwipeResultActionDefault { - - private final int mPosition; - - public UnpinResultAction(int position) { - mPosition = position; - } - - @Override - protected void onPerformAction() { - super.onPerformAction(); - - if (isPinned(mPosition)) { - setPinned(mPosition, false); - - if (mAdapter != null) { - mAdapter.notifyItemChanged(mPosition); - } - } + new AlertDialog.Builder(getContext()) + .setTitle(R.string.delete_label_title) + .setMessage(getString(R.string.delete_label_message, label.getLabel())) + .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + EhApplication.getDownloadManager(getContext()).deleteLabel(label.getLabel()); + } + }) + .setOnDismissListener(new DialogInterface.OnDismissListener() { + @Override + public void onDismiss(DialogInterface dialog) { + if (null != mAdapter) { + mAdapter.notifyDataSetChanged(); + } + updateView(); + } + }).show(); } } } diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/QuickSearchScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/QuickSearchScene.java index c62f5b1c0..7f0116841 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/QuickSearchScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/QuickSearchScene.java @@ -151,8 +151,7 @@ public final class QuickSearchScene extends ToolbarScene { } } - private class QuickSearchHolder extends AbstractDraggableSwipeableItemViewHolder - implements View.OnClickListener { + private class QuickSearchHolder extends AbstractDraggableSwipeableItemViewHolder { public final View swipeHandler; public final TextView label; @@ -170,11 +169,6 @@ public final class QuickSearchScene extends ToolbarScene { public View getSwipeableContainerView() { return swipeHandler; } - - @Override - public void onClick(View v) { - - } } @@ -185,7 +179,7 @@ public final class QuickSearchScene extends ToolbarScene { @Override public QuickSearchHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new QuickSearchHolder(getActivity().getLayoutInflater() - .inflate(R.layout.item_quick_search, parent, false)); + .inflate(R.layout.item_label_list, parent, false)); } @Override diff --git a/app/src/main/res/layout/item_download_label.xml b/app/src/main/res/layout/item_download_label.xml deleted file mode 100644 index 7a7f6f0f2..000000000 --- a/app/src/main/res/layout/item_download_label.xml +++ /dev/null @@ -1,82 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/app/src/main/res/layout/item_quick_search.xml b/app/src/main/res/layout/item_label_list.xml similarity index 100% rename from app/src/main/res/layout/item_quick_search.xml rename to app/src/main/res/layout/item_label_list.xml diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index d0a769c79..bd82a5264 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -245,7 +245,7 @@ 默认下载标签 - 无标签 + 这里是下载标签 添加 新标签 标签文本为空 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index de063ffa2..95de99b6f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -285,7 +285,7 @@ Default download label - No labels + Download labels will be shown here Add New label Label text is empty