Skip to content

Commit ccd76de

Browse files
authored
Merge pull request #12544 from Stypox/download-options
Add option to delete a download without also deleting file
2 parents 9bc8139 + 7644066 commit ccd76de

File tree

5 files changed

+51
-14
lines changed

5 files changed

+51
-14
lines changed

app/src/main/java/us/shandian/giga/service/DownloadManager.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ public void pauseMission(DownloadMission mission) {
265265
}
266266
}
267267

268-
public void deleteMission(Mission mission) {
268+
public void deleteMission(Mission mission, boolean alsoDeleteFile) {
269269
synchronized (this) {
270270
if (mission instanceof DownloadMission) {
271271
mMissionsPending.remove(mission);
@@ -274,7 +274,9 @@ public void deleteMission(Mission mission) {
274274
mFinishedMissionStore.deleteMission(mission);
275275
}
276276

277-
mission.delete();
277+
if (alsoDeleteFile) {
278+
mission.delete();
279+
}
278280
}
279281
}
280282

app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ private void deleteFinishedDownloads() {
614614
while (i.hasNext()) {
615615
Mission mission = i.next();
616616
if (mission != null) {
617-
mDownloadManager.deleteMission(mission);
617+
mDownloadManager.deleteMission(mission, true);
618618
mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mission.storage.getUri()));
619619
}
620620
i.remove();
@@ -667,7 +667,14 @@ private boolean handlePopupItem(@NonNull ViewHolderItem h, @NonNull MenuItem opt
667667
shareFile(h.item.mission);
668668
return true;
669669
case R.id.delete:
670-
mDeleter.append(h.item.mission);
670+
// delete the entry and the file
671+
mDeleter.append(h.item.mission, true);
672+
applyChanges();
673+
checkMasterButtonsVisibility();
674+
return true;
675+
case R.id.delete_entry:
676+
// just delete the entry
677+
mDeleter.append(h.item.mission, false);
671678
applyChanges();
672679
checkMasterButtonsVisibility();
673680
return true;
@@ -676,7 +683,7 @@ private boolean handlePopupItem(@NonNull ViewHolderItem h, @NonNull MenuItem opt
676683
final StoredFileHelper storage = h.item.mission.storage;
677684
if (!storage.existsAsFile()) {
678685
Toast.makeText(mContext, R.string.missing_file, Toast.LENGTH_SHORT).show();
679-
mDeleter.append(h.item.mission);
686+
mDeleter.append(h.item.mission, true);
680687
applyChanges();
681688
return true;
682689
}

app/src/main/java/us/shandian/giga/ui/common/Deleter.java

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
import org.schabi.newpipe.R;
1414

1515
import java.util.ArrayList;
16+
import java.util.Optional;
1617

18+
import kotlin.Pair;
1719
import us.shandian.giga.get.FinishedMission;
1820
import us.shandian.giga.get.Mission;
1921
import 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
}

app/src/main/res/menu/mission.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727

2828
<item
2929
android:id="@+id/delete"
30-
android:title="@string/delete" />
30+
android:title="@string/delete_file" />
31+
32+
<item
33+
android:id="@+id/delete_entry"
34+
android:title="@string/delete_entry" />
3135

3236
<item
3337
android:id="@+id/error_message_view"

app/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,8 @@
333333
<string name="pause">Pause</string>
334334
<string name="create">Create</string>
335335
<string name="delete">Delete</string>
336+
<string name="delete_file">Delete file</string>
337+
<string name="delete_entry">Delete entry</string>
336338
<string name="checksum">Checksum</string>
337339
<string name="dismiss">Dismiss</string>
338340
<string name="rename">Rename</string>
@@ -872,4 +874,5 @@
872874
<string name="trending_podcasts">Trending podcasts</string>
873875
<string name="trending_movies">Trending movies and shows</string>
874876
<string name="trending_music">Trending music</string>
877+
<string name="entry_deleted">Entry deleted</string>
875878
</resources>

0 commit comments

Comments
 (0)