Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public void pauseMission(DownloadMission mission) {
}
}

public void deleteMission(Mission mission) {
public void deleteMission(Mission mission, boolean alsoDeleteFile) {
synchronized (this) {
if (mission instanceof DownloadMission) {
mMissionsPending.remove(mission);
Expand All @@ -274,7 +274,9 @@ public void deleteMission(Mission mission) {
mFinishedMissionStore.deleteMission(mission);
}

mission.delete();
if (alsoDeleteFile) {
mission.delete();
}
}
}

Expand Down
13 changes: 10 additions & 3 deletions app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ private void deleteFinishedDownloads() {
while (i.hasNext()) {
Mission mission = i.next();
if (mission != null) {
mDownloadManager.deleteMission(mission);
mDownloadManager.deleteMission(mission, true);
mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mission.storage.getUri()));
}
i.remove();
Expand Down Expand Up @@ -667,7 +667,14 @@ private boolean handlePopupItem(@NonNull ViewHolderItem h, @NonNull MenuItem opt
shareFile(h.item.mission);
return true;
case R.id.delete:
mDeleter.append(h.item.mission);
// delete the entry and the file
mDeleter.append(h.item.mission, true);
applyChanges();
checkMasterButtonsVisibility();
return true;
case R.id.delete_entry:
// just delete the entry
mDeleter.append(h.item.mission, false);
applyChanges();
checkMasterButtonsVisibility();
return true;
Expand All @@ -676,7 +683,7 @@ private boolean handlePopupItem(@NonNull ViewHolderItem h, @NonNull MenuItem opt
final StoredFileHelper storage = h.item.mission.storage;
if (!storage.existsAsFile()) {
Toast.makeText(mContext, R.string.missing_file, Toast.LENGTH_SHORT).show();
mDeleter.append(h.item.mission);
mDeleter.append(h.item.mission, true);
applyChanges();
return true;
}
Expand Down
37 changes: 29 additions & 8 deletions app/src/main/java/us/shandian/giga/ui/common/Deleter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import org.schabi.newpipe.R;

import java.util.ArrayList;
import java.util.Optional;

import kotlin.Pair;
import us.shandian.giga.get.FinishedMission;
import us.shandian.giga.get.Mission;
import us.shandian.giga.service.DownloadManager;
Expand All @@ -30,7 +32,8 @@ public class Deleter {
private static final int DELAY_RESUME = 400;// ms

private Snackbar snackbar;
private ArrayList<Mission> items;
// list of missions to be deleted, and whether to also delete the corresponding file
private ArrayList<Pair<Mission, Boolean>> items;
private boolean running = true;

private final Context mContext;
Expand All @@ -51,7 +54,7 @@ public Deleter(View v, Context c, MissionAdapter a, DownloadManager d, MissionIt
items = new ArrayList<>(2);
}

public void append(Mission item) {
public void append(Mission item, boolean alsoDeleteFile) {
/* If a mission is removed from the list while the Snackbar for a previously
* removed item is still showing, commit the action for the previous item
* immediately. This prevents Snackbars from stacking up in reverse order.
Expand All @@ -60,13 +63,13 @@ public void append(Mission item) {
commit();

mIterator.hide(item);
items.add(0, item);
items.add(0, new Pair<>(item, alsoDeleteFile));

show();
}

private void forget() {
mIterator.unHide(items.remove(0));
mIterator.unHide(items.remove(0).getFirst());
mAdapter.applyChanges();

show();
Expand All @@ -84,7 +87,19 @@ private void show() {
private void next() {
if (items.size() < 1) return;

String msg = mContext.getString(R.string.file_deleted).concat(":\n").concat(items.get(0).storage.getName());
final Optional<String> fileToBeDeleted = items.stream()
.filter(Pair::getSecond)
.map(p -> p.getFirst().storage.getName())
.findFirst();

String msg;
if (fileToBeDeleted.isPresent()) {
msg = mContext.getString(R.string.file_deleted)
.concat(":\n")
.concat(fileToBeDeleted.get());
} else {
msg = mContext.getString(R.string.entry_deleted);
}

snackbar = Snackbar.make(mView, msg, Snackbar.LENGTH_INDEFINITE);
snackbar.setAction(R.string.undo, s -> forget());
Expand All @@ -98,11 +113,13 @@ private void commit() {
if (items.size() < 1) return;

while (items.size() > 0) {
Mission mission = items.remove(0);
Pair<Mission, Boolean> missionAndAlsoDeleteFile = items.remove(0);
Mission mission = missionAndAlsoDeleteFile.getFirst();
boolean alsoDeleteFile = missionAndAlsoDeleteFile.getSecond();
if (mission.deleted) continue;

mIterator.unHide(mission);
mDownloadManager.deleteMission(mission);
mDownloadManager.deleteMission(mission, alsoDeleteFile);

if (mission instanceof FinishedMission) {
mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mission.storage.getUri()));
Expand Down Expand Up @@ -137,7 +154,11 @@ public void dispose() {

pause();

for (Mission mission : items) mDownloadManager.deleteMission(mission);
for (Pair<Mission, Boolean> missionAndAlsoDeleteFile : items) {
Mission mission = missionAndAlsoDeleteFile.getFirst();
boolean alsoDeleteFile = missionAndAlsoDeleteFile.getSecond();
mDownloadManager.deleteMission(mission, alsoDeleteFile);
}
items = null;
}
}
6 changes: 5 additions & 1 deletion app/src/main/res/menu/mission.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@

<item
android:id="@+id/delete"
android:title="@string/delete" />
android:title="@string/delete_file" />

<item
android:id="@+id/delete_entry"
android:title="@string/delete_entry" />

<item
android:id="@+id/error_message_view"
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@
<string name="pause">Pause</string>
<string name="create">Create</string>
<string name="delete">Delete</string>
<string name="delete_file">Delete file</string>
<string name="delete_entry">Delete entry</string>
<string name="checksum">Checksum</string>
<string name="dismiss">Dismiss</string>
<string name="rename">Rename</string>
Expand Down Expand Up @@ -872,4 +874,5 @@
<string name="trending_podcasts">Trending podcasts</string>
<string name="trending_movies">Trending movies and shows</string>
<string name="trending_music">Trending music</string>
<string name="entry_deleted">Entry deleted</string>
</resources>