Merge pull request #2988 from ByteHamster/deleteMedia

Only delete from queue if deletion of media succeeds
This commit is contained in:
H. Lehmann 2019-01-15 20:52:06 +01:00 committed by GitHub
commit 1c9b4be17a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 87 additions and 83 deletions

View File

@ -7,6 +7,7 @@ import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import io.reactivex.annotations.NonNull;
import org.shredzone.flattr4j.model.Flattr;
import java.io.File;
@ -74,11 +75,27 @@ public class DBWriter {
private DBWriter() {
}
private static Future<?> doDeleteFeedMediaOfItem(final Context context,
/**
* Deletes a downloaded FeedMedia file from the storage device.
*
* @param context A context that is used for opening a database connection.
* @param mediaId ID of the FeedMedia object whose downloaded file should be deleted.
*/
public static Future<?> deleteFeedMediaOfItem(@NonNull final Context context,
final long mediaId) {
return dbExec.submit(() -> {
final FeedMedia media = DBReader.getFeedMedia(mediaId);
if (media != null) {
boolean result = deleteFeedMediaSynchronous(context, media);
if (result && UserPreferences.shouldDeleteRemoveFromQueue()) {
DBWriter.removeQueueItemSynchronous(context, media.getItem(), false);
}
}
});
}
private static boolean deleteFeedMediaSynchronous(@NonNull Context context, @NonNull FeedMedia media) {
Log.i(TAG, String.format("Requested to delete FeedMedia [id=%d, title=%s, downloaded=%s",
media.getId(), media.getEpisodeTitle(), String.valueOf(media.isDownloaded())));
if (media.isDownloaded()) {
@ -87,7 +104,7 @@ public class DBWriter {
if (mediaFile.exists() && !mediaFile.delete()) {
MessageEvent evt = new MessageEvent(context.getString(R.string.delete_failed));
EventBus.getDefault().post(evt);
return;
return false;
}
media.setDownloaded(false);
media.setFile_url(null);
@ -126,23 +143,8 @@ public class DBWriter {
}
EventBus.getDefault().post(FeedItemEvent.deletedMedia(Collections.singletonList(media.getItem())));
EventDistributor.getInstance().sendUnreadItemsUpdateBroadcast();
}
});
}
/**
* Deletes a downloaded FeedMedia file from the storage device.
*
* @param context A context that is used for opening a database connection.
* @param mediaId ID of the FeedMedia object whose downloaded file should be deleted.
*/
public static Future<?> deleteFeedMediaOfItem(final Context context,
final long mediaId) {
Future res = doDeleteFeedMediaOfItem(context, mediaId);
if (UserPreferences.shouldDeleteRemoveFromQueue()) {
DBWriter.removeQueueItem(context, DBReader.getFeedMedia(mediaId).getItem(), false);
}
return res;
return true;
}
/**
@ -428,7 +430,11 @@ public class DBWriter {
*/
public static Future<?> removeQueueItem(final Context context,
final FeedItem item, final boolean performAutoDownload) {
return dbExec.submit(() -> {
return dbExec.submit(() -> removeQueueItemSynchronous(context, item, performAutoDownload));
}
private static void removeQueueItemSynchronous(final Context context,
final FeedItem item, final boolean performAutoDownload) {
final PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
final List<FeedItem> queue = DBReader.getQueue(adapter);
@ -451,8 +457,6 @@ public class DBWriter {
if (performAutoDownload) {
DBTasks.autodownloadUndownloadedItems(context);
}
});
}
public static Future<?> addFavoriteItem(final FeedItem item) {