1313import org .schabi .newpipe .R ;
1414
1515import java .util .ArrayList ;
16+ import java .util .Optional ;
1617
18+ import kotlin .Pair ;
1719import us .shandian .giga .get .FinishedMission ;
1820import us .shandian .giga .get .Mission ;
1921import us .shandian .giga .service .DownloadManager ;
@@ -30,7 +32,8 @@ public class Deleter {
3032 private static final int DELAY_RESUME = 400 ;// ms
3133
3234 private Snackbar snackbar ;
33- private ArrayList <Mission > items ;
35+ // list of missions to be deleted, and whether to also delete the corresponding file
36+ private ArrayList <Pair <Mission , Boolean >> items ;
3437 private boolean running = true ;
3538
3639 private final Context mContext ;
@@ -51,7 +54,7 @@ public Deleter(View v, Context c, MissionAdapter a, DownloadManager d, MissionIt
5154 items = new ArrayList <>(2 );
5255 }
5356
54- public void append (Mission item ) {
57+ public void append (Mission item , boolean alsoDeleteFile ) {
5558 /* If a mission is removed from the list while the Snackbar for a previously
5659 * removed item is still showing, commit the action for the previous item
5760 * immediately. This prevents Snackbars from stacking up in reverse order.
@@ -60,13 +63,13 @@ public void append(Mission item) {
6063 commit ();
6164
6265 mIterator .hide (item );
63- items .add (0 , item );
66+ items .add (0 , new Pair <>( item , alsoDeleteFile ) );
6467
6568 show ();
6669 }
6770
6871 private void forget () {
69- mIterator .unHide (items .remove (0 ));
72+ mIterator .unHide (items .remove (0 ). getFirst () );
7073 mAdapter .applyChanges ();
7174
7275 show ();
@@ -84,7 +87,19 @@ private void show() {
8487 private void next () {
8588 if (items .size () < 1 ) return ;
8689
87- String msg = mContext .getString (R .string .file_deleted ).concat (":\n " ).concat (items .get (0 ).storage .getName ());
90+ final Optional <String > fileToBeDeleted = items .stream ()
91+ .filter (Pair ::getSecond )
92+ .map (p -> p .getFirst ().storage .getName ())
93+ .findFirst ();
94+
95+ String msg ;
96+ if (fileToBeDeleted .isPresent ()) {
97+ msg = mContext .getString (R .string .file_deleted )
98+ .concat (":\n " )
99+ .concat (fileToBeDeleted .get ());
100+ } else {
101+ msg = mContext .getString (R .string .entry_deleted );
102+ }
88103
89104 snackbar = Snackbar .make (mView , msg , Snackbar .LENGTH_INDEFINITE );
90105 snackbar .setAction (R .string .undo , s -> forget ());
@@ -98,11 +113,13 @@ private void commit() {
98113 if (items .size () < 1 ) return ;
99114
100115 while (items .size () > 0 ) {
101- Mission mission = items .remove (0 );
116+ Pair <Mission , Boolean > missionAndAlsoDeleteFile = items .remove (0 );
117+ Mission mission = missionAndAlsoDeleteFile .getFirst ();
118+ boolean alsoDeleteFile = missionAndAlsoDeleteFile .getSecond ();
102119 if (mission .deleted ) continue ;
103120
104121 mIterator .unHide (mission );
105- mDownloadManager .deleteMission (mission );
122+ mDownloadManager .deleteMission (mission , alsoDeleteFile );
106123
107124 if (mission instanceof FinishedMission ) {
108125 mContext .sendBroadcast (new Intent (Intent .ACTION_MEDIA_SCANNER_SCAN_FILE , mission .storage .getUri ()));
@@ -137,7 +154,11 @@ public void dispose() {
137154
138155 pause ();
139156
140- for (Mission mission : items ) mDownloadManager .deleteMission (mission );
157+ for (Pair <Mission , Boolean > missionAndAlsoDeleteFile : items ) {
158+ Mission mission = missionAndAlsoDeleteFile .getFirst ();
159+ boolean alsoDeleteFile = missionAndAlsoDeleteFile .getSecond ();
160+ mDownloadManager .deleteMission (mission , alsoDeleteFile );
161+ }
141162 items = null ;
142163 }
143164}
0 commit comments