diff --git a/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java b/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java index eea3f2795..11cdf5a3d 100644 --- a/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java +++ b/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java @@ -5,6 +5,7 @@ import android.app.Activity; import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; +import android.graphics.Color; import android.net.Uri; import android.os.AsyncTask; import android.os.Build; @@ -35,6 +36,8 @@ import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView.Adapter; import androidx.recyclerview.widget.RecyclerView.ViewHolder; +import com.google.android.material.snackbar.Snackbar; + import org.schabi.newpipe.BuildConfig; import org.schabi.newpipe.R; import org.schabi.newpipe.extractor.NewPipe; @@ -46,6 +49,7 @@ import java.io.File; import java.lang.ref.WeakReference; import java.net.URI; import java.util.ArrayList; +import java.util.Iterator; import us.shandian.giga.get.DownloadMission; import us.shandian.giga.get.FinishedMission; @@ -84,6 +88,7 @@ public class MissionAdapter extends Adapter implements Handler.Callb private static final String UNDEFINED_PROGRESS = "--.-%"; private static final String DEFAULT_MIME_TYPE = "*/*"; private static final String UNDEFINED_ETA = "--:--"; + private static final int TIMEOUT = 5000;// ms static { @@ -104,8 +109,12 @@ public class MissionAdapter extends Adapter implements Handler.Callb private MenuItem mPauseButton; private View mEmptyMessage; private RecoverHelper mRecover; + private View mView; + private ArrayList mHidden; + private Snackbar mSnackbar; private final Runnable rUpdater = this::updater; + private final Runnable rDelete = this::deleteFinishedDownloads; public MissionAdapter(Context context, @NonNull DownloadManager downloadManager, View emptyMessage, View root) { mContext = context; @@ -122,6 +131,10 @@ public class MissionAdapter extends Adapter implements Handler.Callb mDeleter = new Deleter(root, mContext, this, mDownloadManager, mIterator, mHandler); + mView = root; + + mHidden = new ArrayList<>(); + checkEmptyMessageVisibility(); onResume(); } @@ -558,18 +571,49 @@ public class MissionAdapter extends Adapter implements Handler.Callb } public void clearFinishedDownloads(boolean delete) { - if (delete && mIterator.hasFinishedMissions()) { + if (delete && mIterator.hasFinishedMissions() && mHidden.isEmpty()) { for (int i = 0; i < mIterator.getOldListSize(); i++) { FinishedMission mission = mIterator.getItem(i).mission instanceof FinishedMission ? (FinishedMission) mIterator.getItem(i).mission : null; if (mission != null) { - mDownloadManager.deleteMission(mission); - mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mission.storage.getUri())); + mIterator.hide(mission); + mHidden.add(mission); } } + applyChanges(); + + String msg = String.format(mContext.getString(R.string.deleted_downloads), String.valueOf(mHidden.size())); + mSnackbar = Snackbar.make(mView, msg, Snackbar.LENGTH_INDEFINITE); + mSnackbar.setAction(R.string.undo, s -> { + Iterator i = mHidden.iterator(); + while (i.hasNext()) { + mIterator.unHide(i.next()); + i.remove(); + } + applyChanges(); + mHandler.removeCallbacks(rDelete); + }); + mSnackbar.setActionTextColor(Color.YELLOW); + mSnackbar.show(); + + mHandler.postDelayed(rDelete, TIMEOUT); } else if (!delete) { mDownloadManager.forgetFinishedDownloads(); + applyChanges(); + } + } + + private void deleteFinishedDownloads() { + if(mSnackbar != null) mSnackbar.dismiss(); + + Iterator i = mHidden.iterator(); + while (i.hasNext()) { + Mission mission = i.next(); + if (mission != null) { + mDownloadManager.deleteMission(mission); + mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mission.storage.getUri())); + } + i.remove(); } - applyChanges(); } private boolean handlePopupItem(@NonNull ViewHolderItem h, @NonNull MenuItem option) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index bc3389062..9e3f89357 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -588,4 +588,5 @@ The Storage Access Framework allows downloads to an external SD card.\nNote: some devices are not compatible Choose an instance Delete downloaded files + Deleted %s downloads