prep to delete multiple entries
This commit is contained in:
parent
1175cff8fe
commit
a69a7ea436
3 changed files with 37 additions and 22 deletions
|
@ -7,6 +7,7 @@ import android.os.Looper;
|
|||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import deckers.thibault.aves.model.ImageEntry;
|
||||
|
@ -95,30 +96,44 @@ public class ImageFileHandler implements MethodChannel.MethodCallHandler {
|
|||
}
|
||||
|
||||
private void delete(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
|
||||
Map entryMap = call.argument("entry");
|
||||
if (entryMap == null) {
|
||||
List<Map> entryMapList = call.argument("entries");
|
||||
if (entryMapList == null) {
|
||||
result.error("delete-args", "failed because of missing arguments", null);
|
||||
return;
|
||||
}
|
||||
Uri uri = Uri.parse((String) entryMap.get("uri"));
|
||||
String path = (String) entryMap.get("path");
|
||||
|
||||
ImageProvider provider = ImageProviderFactory.getProvider(uri);
|
||||
if (provider == null) {
|
||||
result.error("delete-provider", "failed to find provider for uri=" + uri, null);
|
||||
if (entryMapList.size() == 0) {
|
||||
result.success(0);
|
||||
return;
|
||||
}
|
||||
provider.delete(activity, path, uri, new ImageProvider.ImageOpCallback() {
|
||||
@Override
|
||||
public void onSuccess(Map<String, Object> newFields) {
|
||||
new Handler(Looper.getMainLooper()).post(() -> result.success(newFields));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure() {
|
||||
new Handler(Looper.getMainLooper()).post(() -> result.error("delete-failure", "failed to delete", null));
|
||||
}
|
||||
});
|
||||
// assume same provider for all entries
|
||||
Map firstEntry = entryMapList.get(0);
|
||||
Uri firstUri = Uri.parse((String) firstEntry.get("uri"));
|
||||
ImageProvider provider = ImageProviderFactory.getProvider(firstUri);
|
||||
if (provider == null) {
|
||||
result.error("delete-provider", "failed to find provider for uri=" + firstUri, null);
|
||||
return;
|
||||
}
|
||||
|
||||
for (Map entryMap : entryMapList) {
|
||||
Uri uri = Uri.parse((String) entryMap.get("uri"));
|
||||
String path = (String) entryMap.get("path");
|
||||
provider.delete(activity, path, uri, new ImageProvider.ImageOpCallback() {
|
||||
|
||||
// TODO TLAD this will fail for more than 1 entry. stream results back instead
|
||||
|
||||
@Override
|
||||
public void onSuccess(Map<String, Object> newFields) {
|
||||
new Handler(Looper.getMainLooper()).post(() -> result.success(1));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure() {
|
||||
new Handler(Looper.getMainLooper()).post(() -> result.error("delete-failure", "failed to delete", null));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void rename(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
|
||||
|
|
|
@ -295,7 +295,7 @@ class ImageEntry {
|
|||
return true;
|
||||
}
|
||||
|
||||
Future<bool> delete() => ImageFileService.delete(this);
|
||||
Future<bool> delete() async => (await ImageFileService.delete([this])) == 1;
|
||||
|
||||
void toggleFavourite() {
|
||||
if (isFavourite) {
|
||||
|
|
|
@ -77,16 +77,16 @@ class ImageFileService {
|
|||
);
|
||||
}
|
||||
|
||||
static Future<bool> delete(ImageEntry entry) async {
|
||||
static Future<int> delete(List<ImageEntry> entries) async {
|
||||
try {
|
||||
await platform.invokeMethod('delete', <String, dynamic>{
|
||||
'entry': entry.toMap(),
|
||||
'entries': entries.map((e) => e.toMap()).toList(),
|
||||
});
|
||||
return true;
|
||||
return 1;
|
||||
} on PlatformException catch (e) {
|
||||
debugPrint('delete failed with code=${e.code}, exception=${e.message}, details=${e.details}');
|
||||
}
|
||||
return false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static Future<Map> rename(ImageEntry entry, String newName) async {
|
||||
|
|
Loading…
Reference in a new issue