Commit 0e664955 authored by Geoffrey Métais's avatar Geoffrey Métais
Browse files

Warn user when file deletion failed

parent 499066f5
......@@ -569,4 +569,5 @@
<string name="recommendations_desc">Recommendations cards on launcher</string>
<string name="browser_quick_access">Quick Access</string>
<string name="browser_storages">Storages</string>
<string name="msg_delete_failed">Failed to delete media %1$s</string>
</resources>
......@@ -342,20 +342,21 @@ public class PlaylistActivity extends AudioPlayerContainerActivity implements IE
return true;
} else if (id == R.id.audio_list_browser_delete) {
mAdapter.remove(media);
final Runnable cancel = new Runnable() {
@Override
public void run() {
mAdapter.addItem(position, media);
}
};
UiTools.snackerWithCancel(mBinding.getRoot(), getString(R.string.file_deleted), new Runnable() {
@Override
public void run() {
if (mIsPlaylist)
((Playlist) mPlaylist).remove(media.getId());
else
deleteMedia(media);
}
}, new Runnable() {
@Override
public void run() {
mAdapter.addItem(position, media);
deleteMedia(media, cancel);
}
});
}, cancel);
return true;
} else if (id == R.id.audio_view_info) {
showInfoDialog(media);
......@@ -389,7 +390,7 @@ public class PlaylistActivity extends AudioPlayerContainerActivity implements IE
return info != null && handleContextItemSelected(menu, info.position);
}
protected void deleteMedia(final MediaLibraryItem mw) {
protected void deleteMedia(final MediaLibraryItem mw, final Runnable cancel) {
VLCApplication.runBackground(new Runnable() {
@Override
public void run() {
......@@ -397,10 +398,11 @@ public class PlaylistActivity extends AudioPlayerContainerActivity implements IE
final LinkedList<String> mediaPaths = new LinkedList<>();
for (MediaWrapper media : mw.getTracks()) {
String path = media.getUri().getPath();
mediaPaths.add(media.getLocation());
String parentPath = FileUtils.getParent(path);
if (FileUtils.deleteFile(path) && media.getId() > 0L && !foldersToReload.contains(parentPath))
if (FileUtils.deleteFile(path) && media.getId() > 0L && !foldersToReload.contains(parentPath)) {
foldersToReload.add(parentPath);
mediaPaths.add(media.getLocation());
} else UiTools.snacker(mBinding.getRoot(), getString(R.string.msg_delete_failed, media.getTitle()));
}
for (String folder : foldersToReload)
mMediaLibrary.reload(folder);
......@@ -408,8 +410,11 @@ public class PlaylistActivity extends AudioPlayerContainerActivity implements IE
VLCApplication.runOnMainThread(new Runnable() {
@Override
public void run() {
for (String path : mediaPaths)
mService.removeLocation(path);
if (mediaPaths.isEmpty()) cancel.run();
else {
for (String path : mediaPaths)
mService.removeLocation(path);
}
}
});
}
......
......@@ -224,17 +224,18 @@ public class AudioAlbumsSongsFragment extends BaseAudioBrowser implements SwipeR
adapter.remove(mediaItem);
UiTools.snackerWithCancel(mViewPager, getString(R.string.file_deleted), new Runnable() {
final Runnable cancel = new Runnable() {
@Override
public void run() {
deleteMedia(mediaItem, true);
adapter.addItem(position, mediaItem);
}
}, new Runnable() {
};
UiTools.snackerWithCancel(mViewPager, getString(R.string.file_deleted), new Runnable() {
@Override
public void run() {
adapter.addItem(position, mediaItem);
deleteMedia(mediaItem, true, cancel);
}
});
}, cancel);
return true;
}
......
......@@ -261,22 +261,23 @@ public class AudioBrowserFragment extends BaseAudioBrowser implements SwipeRefre
};
} else if (mode == MODE_SONG) {
message = getString(R.string.file_deleted);
final Runnable cancel = new Runnable() {
@Override
public void run() {
if (separator != null)
adapter.addItem(position-1, separator);
adapter.addItem(position, mediaLibraryItem);
}
};
action = new Runnable() {
@Override
public void run() {
deleteMedia(mediaLibraryItem, true);
deleteMedia(mediaLibraryItem, true, cancel);
}
};
} else
return false;
UiTools.snackerWithCancel(getView(), message, action, new Runnable() {
@Override
public void run() {
if (separator != null)
adapter.addItem(position-1, separator);
adapter.addItem(position, mediaLibraryItem);
}
});
UiTools.snackerWithCancel(getView(), message, action);
return true;
}
......
......@@ -553,17 +553,18 @@ public abstract class BaseBrowserFragment extends SortableFragment<BaseBrowserAd
}
case R.id.directory_view_delete:
mAdapter.removeItem(position);
UiTools.snackerWithCancel(getView(), getString(R.string.file_deleted), new Runnable() {
final Runnable cancel = new Runnable() {
@Override
public void run() {
deleteMedia(mw, false);
mAdapter.addItem(mw, true, position);
}
}, new Runnable() {
};
UiTools.snackerWithCancel(getView(), getString(R.string.file_deleted), new Runnable() {
@Override
public void run() {
mAdapter.addItem(mw, true, position);
deleteMedia(mw, false, cancel);
}
});
}, cancel);
return true;
case R.id.directory_view_info:
showMediaInfo(mw);
......@@ -822,10 +823,6 @@ public abstract class BaseBrowserFragment extends SortableFragment<BaseBrowserAd
case R.id.action_mode_file_append:
mService.append(list);
break;
case R.id.action_mode_file_delete:
for (MediaWrapper media : list)
deleteMedia(media, true);
break;
case R.id.action_mode_file_add_playlist:
UiTools.addToPlaylist(getActivity(), list);
break;
......
......@@ -47,6 +47,7 @@ import org.videolan.vlc.R;
import org.videolan.vlc.VLCApplication;
import org.videolan.vlc.gui.InfoActivity;
import org.videolan.vlc.gui.PlaybackServiceFragment;
import org.videolan.vlc.gui.helpers.UiTools;
import org.videolan.vlc.gui.view.ContextMenuRecyclerView;
import org.videolan.vlc.gui.view.SwipeRefreshLayout;
import org.videolan.vlc.util.Constants;
......@@ -172,30 +173,33 @@ public abstract class MediaBrowserFragment extends PlaybackServiceFragment imple
return info != null && handleContextItemSelected(menu, info.position);
}
protected void deleteMedia(final MediaLibraryItem mw, final boolean refresh) {
protected void deleteMedia(final MediaLibraryItem mw, final boolean refresh, final Runnable failCB) {
VLCApplication.runBackground(new Runnable() {
@Override
public void run() {
final LinkedList<String> foldersToReload = new LinkedList<>();
final LinkedList<String> mediaPaths = new LinkedList<>();
for (MediaWrapper media : mw.getTracks()) {
String path = media.getUri().getPath();
mediaPaths.add(media.getLocation());
String parentPath = FileUtils.getParent(path);
if (FileUtils.deleteFile(path) && media.getId() > 0L && !foldersToReload.contains(parentPath))
final String path = media.getUri().getPath();
final String parentPath = FileUtils.getParent(path);
if (FileUtils.deleteFile(path) && media.getId() > 0L && !foldersToReload.contains(parentPath)) {
foldersToReload.add(parentPath);
mediaPaths.add(media.getLocation());
} else onDeleteFailed(media);
}
for (String folder : foldersToReload)
mMediaLibrary.reload(folder);
for (String folder : foldersToReload) mMediaLibrary.reload(folder);
if (mService != null && getActivity() != null) {
VLCApplication.runOnMainThread(new Runnable() {
@Override
public void run() {
if (mediaPaths.isEmpty()) {
if (failCB != null) failCB.run();
return;
}
if (mService != null)
for (String path : mediaPaths)
mService.removeLocation(path);
if (refresh)
onRefresh();
if (refresh) onRefresh();
}
});
}
......@@ -203,6 +207,10 @@ public abstract class MediaBrowserFragment extends PlaybackServiceFragment imple
});
}
private void onDeleteFailed(MediaWrapper media) {
UiTools.snacker(getView(), getString(R.string.msg_delete_failed, media.getTitle()));
}
protected void showInfoDialog(MediaLibraryItem item) {
Intent i = new Intent(getActivity(), InfoActivity.class);
i.putExtra(InfoActivity.TAG_ITEM, item);
......
......@@ -289,18 +289,20 @@ public class VideoGridFragment extends SortableFragment<VideoListAdapter> implem
private void removeVideo(final MediaWrapper media) {
final int position = mAdapter.remove(media);
final View view = getView();
if (position != -1 && view != null)
UiTools.snackerWithCancel(view, getString(R.string.file_deleted), new Runnable() {
if (position != -1 && view != null) {
final Runnable revert = new Runnable() {
@Override
public void run() {
deleteMedia(media, false);
mAdapter.add(media, position);
}
}, new Runnable() {
};
UiTools.snackerWithCancel(view, getString(R.string.file_deleted), new Runnable() {
@Override
public void run() {
mAdapter.add(media, position);
deleteMedia(media, false, revert);
}
});
}, revert);
}
}
@Override
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment