Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[mobile][photos] simplify device trash error handling

Priyanshu Dangarecab37d07e458
diff --git a/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/devicetrash/DeviceTrashService.kt b/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/devicetrash/DeviceTrashService.kt
index e512408a33..ebf86b1397 100644
--- a/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/devicetrash/DeviceTrashService.kt
+++ b/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/devicetrash/DeviceTrashService.kt
@@ -2,7 +2,6 @@ package io.ente.photos.platform.devicetrash
import android.content.ContentResolver
import android.content.Context
-import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
@@ -22,16 +21,20 @@ internal class DeviceTrashService(context: Context) {
@Volatile
private var isClosed = false
- fun getFiles(callback: (Result<List<DeviceTrashFile>>) -> Unit) {
+ fun getFiles(
+ onSuccess: (List<DeviceTrashFile>) -> Unit,
+ onFailure: (Exception) -> Unit,
+ ) {
executor.execute {
- val result =
- try {
- Result.success(queryFiles())
- } catch (error: Exception) {
- Result.failure(error)
+ try {
+ val files = queryFiles()
+ mainHandler.post {
+ if (!isClosed) onSuccess(files)
+ }
+ } catch (error: Exception) {
+ mainHandler.post {
+ if (!isClosed) onFailure(error)
}
- mainHandler.post {
- if (!isClosed) callback(result)
}
}
}
@@ -43,10 +46,6 @@ internal class DeviceTrashService(context: Context) {
}
private fun queryFiles(): List<DeviceTrashFile> {
- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
- throw UnsupportedOperationException("Device trash requires Android 11 or newer")
- }
-
val projection =
arrayOf(
MediaStore.Files.FileColumns._ID,
diff --git a/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/flutter/DeviceTrashChannelAdapter.kt b/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/flutter/DeviceTrashChannelAdapter.kt
index b54ce9061d..2f103dda0f 100644
--- a/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/flutter/DeviceTrashChannelAdapter.kt
+++ b/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/flutter/DeviceTrashChannelAdapter.kt
@@ -1,5 +1,6 @@
package io.ente.photos.platform.flutter
+import android.os.Build
import io.ente.photos.platform.devicetrash.DeviceTrashFile
import io.ente.photos.platform.devicetrash.DeviceTrashService
import io.flutter.embedding.engine.plugins.FlutterPlugin
@@ -15,23 +16,17 @@ internal class DeviceTrashChannelAdapter : MethodChannel.MethodCallHandler {
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) =
when (call.method) {
- "deviceTrash.getFiles" -> service.getFiles { filesResult ->
- filesResult.fold(
- onSuccess = { files -> result.success(files.map { it.toChannelMap() }) },
- onFailure = { error ->
- result.error(
- when (error) {
- is UnsupportedOperationException ->
- "device_trash_unsupported"
-
- else -> "device_trash_query_failed"
- },
- error.message,
- null,
- )
- },
- )
- }
+ "deviceTrash.getFiles" ->
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
+ result.error("device_trash_unsupported", null, null)
+ } else {
+ service.getFiles(
+ onSuccess = { files -> result.success(files.map { it.toChannelMap() }) },
+ onFailure = { error ->
+ result.error("device_trash_query_failed", error.message, null)
+ },
+ )
+ }
else -> result.notImplemented()
}
diff --git a/mobile/apps/photos/plugins/ente_photos_platform/lib/src/device_trash.dart b/mobile/apps/photos/plugins/ente_photos_platform/lib/src/device_trash.dart
index 1bad10e915..a5c92a8b90 100644
--- a/mobile/apps/photos/plugins/ente_photos_platform/lib/src/device_trash.dart
+++ b/mobile/apps/photos/plugins/ente_photos_platform/lib/src/device_trash.dart
@@ -23,27 +23,23 @@ class DeviceTrashFile {
final String deviceFolder;
}
-abstract interface class DeviceTrashSource {
- Future<List<DeviceTrashFile>> getFiles();
-}
-
-class DeviceTrashClient implements DeviceTrashSource {
- DeviceTrashClient({MethodChannel? methodChannel})
- : _methodChannel = methodChannel ?? const MethodChannel(_methodChannelName);
-
+class DeviceTrashClient {
static final instance = DeviceTrashClient();
- static const _methodChannelName = 'io.ente.photos.platform';
+ static const _methodChannel = MethodChannel('io.ente.photos.platform');
- final MethodChannel _methodChannel;
-
- @override
Future<List<DeviceTrashFile>> getFiles() async {
if (!Platform.isAndroid) {
throw UnsupportedError('Device trash is only supported on Android');
}
- final result = await _methodChannel.invokeListMethod<Map<dynamic, dynamic>>(
- 'deviceTrash.getFiles',
- );
- return List.unmodifiable(result!.map(DeviceTrashFile.fromMap));
+ try {
+ final result = await _methodChannel
+ .invokeListMethod<Map<dynamic, dynamic>>('deviceTrash.getFiles');
+ return List.unmodifiable(result!.map(DeviceTrashFile.fromMap));
+ } on PlatformException catch (error) {
+ if (error.code == 'device_trash_unsupported') {
+ throw UnsupportedError('Device trash requires Android 11 or newer');
+ }
+ rethrow;
+ }
}
}