diff --git a/app/src/main/java/com/hippo/ehviewer/service/DownloadManager.java b/app/src/main/java/com/hippo/ehviewer/service/DownloadManager.java index c60ea4999..f953905c9 100644 --- a/app/src/main/java/com/hippo/ehviewer/service/DownloadManager.java +++ b/app/src/main/java/com/hippo/ehviewer/service/DownloadManager.java @@ -19,6 +19,7 @@ package com.hippo.ehviewer.service; import android.os.SystemClock; import android.support.annotation.Nullable; import android.text.TextUtils; +import android.util.Log; import com.hippo.ehviewer.Constants; import com.hippo.ehviewer.client.data.DownloadInfo; @@ -38,11 +39,14 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Set; +// TODO use SortedList public class DownloadManager { private static final String TAG = DownloadManager.class.getSimpleName(); @@ -71,13 +75,9 @@ public class DownloadManager { @Override public int compare(DownloadInfo lhs, DownloadInfo rhs) { return lhs.time > rhs.time ? 1 : lhs.time < rhs.time ? -1 : 0; - - - //return (int) (lhs.time - rhs.time); } }; - private List mDownloadInfos; private Map> mDownloadInfoMap; private List mDefaultDownloadInfos; @@ -462,6 +462,63 @@ public class DownloadManager { releaseCurrent(); } + public static boolean isDefaultLabel(String label) { + return TextUtils.isEmpty(label); + } + + public static boolean labelEquals(String label1, String label2) { + if (TextUtils.isEmpty(label1)) { + return TextUtils.isEmpty(label2); + } else { + return label1.equals(label2); + } + } + + /** + * Change label + */ + public void moveDownloadInfo(List list, String label) { + // Get the download list to add to + List targetList; + if (isDefaultLabel(label)) { + targetList = mDefaultDownloadInfos; + } else { + targetList = getDownloadListInternal(label, true); + } + + String lastLabel = null; + List lastList = null; + Set> modifiedInfoList = new HashSet<>(1); + + for (DownloadInfo info : list) { + List infoList = null; + if (labelEquals(info.label, lastLabel)) { + infoList = lastList; + } + if (infoList == null) { + infoList = getDownloadListInternal(info.label, false); + } + if (infoList == null) { + Log.e("TAG", "Can't get download info list, label is " + info.label); + continue; + } + + info.label = label; + DBUtils.updateDownloadInfo(info); + infoList.remove(info); + targetList.add(info); + + lastLabel = info.label; + lastList = infoList; + modifiedInfoList.add(lastList); + } + + // Sort modified info list + for (List infoList : modifiedInfoList) { + Collections.sort(infoList, TIME_ASC_COMPARATOR); + } + } + public boolean hasWaitingDownloadInfo() { return !mWaitList.isEmpty(); } diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadScene.java index bffb13c8d..ff42f1bb4 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadScene.java @@ -23,7 +23,6 @@ import android.content.Intent; import android.content.res.Resources; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; -import android.util.Log; import android.util.SparseBooleanArray; import android.view.View; import android.view.ViewGroup; @@ -57,6 +56,7 @@ import com.hippo.yorozuya.AssertUtils; import com.hippo.yorozuya.FileUtils; import com.hippo.yorozuya.Messenger; +import java.util.ArrayList; import java.util.List; public class DownloadScene extends AppbarScene implements DrawerProvider, @@ -499,14 +499,47 @@ public class DownloadScene extends AppbarScene implements DrawerProvider, } } + private class MoveDownloadInfoListener implements SimpleDialog.OnClickListener { + private List mLabels; + private boolean mHasDefaultLabel; + public MoveDownloadInfoListener(List labels, boolean hasDefaultLabel) { + mLabels = labels; + mHasDefaultLabel = hasDefaultLabel; + } + @Override + public boolean onClick(SimpleDialog dialog, int which) { + // Target label + String label; + if (mHasDefaultLabel && which == 0) { + label = null; + } else { + label = mLabels.get(which); + } + // Target download infos + List infos = new ArrayList<>(); + SparseBooleanArray checkedState = mRecyclerView.getCheckedItemPositions(); + for (int i = 0, n = checkedState.size(); i < n; i++) { + if (checkedState.valueAt(i)) { + DownloadInfo info = mDownloadInfos.get(checkedState.keyAt(i)); + if (!DownloadManager.labelEquals(info.label, label)) { + infos.add(info); + } + } + } + // Must out of choice mode here + mRecyclerView.outOfCustomChoiceMode(); - - - + if (!infos.isEmpty()) { + DownloadManager.getInstance().moveDownloadInfo(infos, label); + mAdapter.notifyDataSetChanged(); + } + return true; + } + } @Override public void onClick(View v) { @@ -524,6 +557,8 @@ public class DownloadScene extends AppbarScene implements DrawerProvider, getStageActivity().startService(intent); } } + // Out of choice mode + mRecyclerView.outOfCustomChoiceMode(); } else if (mStop == v) { DownloadManager manager = DownloadManager.getInstance(); SparseBooleanArray checkedState = mRecyclerView.getCheckedItemPositions(); @@ -533,15 +568,19 @@ public class DownloadScene extends AppbarScene implements DrawerProvider, manager.stopDownload(mDownloadInfos.get(position).galleryBase.gid); } } + // Out of choice mode + mRecyclerView.outOfCustomChoiceMode(); } else if (mDelete == v) { // TODO } else if (mMove == v) { // Get available target label + boolean hasDefaultLabel = false; final List labels = DBUtils.getAllDownloadLabel(); if (mActivatedLabelPosition > INDEX_DEFAULT) { labels.remove(mActivatedLabelPosition - 2); } if (mActivatedLabelPosition != INDEX_DEFAULT) { + hasDefaultLabel = true; labels.add(getResources().getString(R.string.download_tag_default)); } @@ -549,15 +588,8 @@ public class DownloadScene extends AppbarScene implements DrawerProvider, Toast.makeText(getContext(), "No available label", Toast.LENGTH_SHORT).show(); // TODO hardcode } else { new SimpleDialog.Builder(getContext()).setTitle("Move to") // TODO hardcode - .setItems(labels.toArray(new String[labels.size()]), new SimpleDialog.OnClickListener() { - @Override - public boolean onClick(SimpleDialog dialog, int which) { - String label = labels.get(which); - Log.d("TAG", "label = " + label); - // TODO - return true; - } - }).show(this); + .setItems(labels.toArray(new String[labels.size()]), + new MoveDownloadInfoListener(labels, hasDefaultLabel)).show(this); } } else if (mCheckAll == v) { SparseBooleanArray checkedState = mRecyclerView.getCheckedItemPositions();