From cceacd9ea1f64e4df226d92546d41c2d9efaf0a1 Mon Sep 17 00:00:00 2001 From: Midori Date: Sun, 13 Aug 2023 11:27:02 +0900 Subject: [PATCH] Move _voidOrThrow to methodChannel --- lib/src/gal.dart | 22 +++-------------- lib/src/gal_method_channel.dart | 43 ++++++++++++++++----------------- 2 files changed, 25 insertions(+), 40 deletions(-) diff --git a/lib/src/gal.dart b/lib/src/gal.dart index 2d6455e3..3a73a6f7 100644 --- a/lib/src/gal.dart +++ b/lib/src/gal.dart @@ -1,5 +1,4 @@ import 'package:flutter/foundation.dart'; -import 'package:flutter/services.dart'; import 'package:gal/src/gal_exception.dart'; import 'gal_platform_interface.dart'; @@ -21,8 +20,7 @@ final class Gal { /// if an error occurs during saving. /// See: [Formats](https://github.com/natsuk4ze/gal/wiki/Formats) static Future putVideo(String path, {String? album}) async => - _voidOrThrow( - () async => GalPlatform.instance.putVideo(path, album: album)); + GalPlatform.instance.putVideo(path, album: album); /// Save a image to the gallery from file [path]. /// @@ -31,8 +29,7 @@ final class Gal { /// if an error occurs during saving. /// See: [Formats](https://github.com/natsuk4ze/gal/wiki/Formats) static Future putImage(String path, {String? album}) async => - _voidOrThrow( - () async => GalPlatform.instance.putImage(path, album: album)); + GalPlatform.instance.putImage(path, album: album); /// Save a image to the gallery from [Uint8List]. /// @@ -41,8 +38,7 @@ final class Gal { /// if an error occurs during saving. /// See: [Formats](https://github.com/natsuk4ze/gal/wiki/Formats) static Future putImageBytes(Uint8List bytes, {String? album}) async => - _voidOrThrow( - () async => GalPlatform.instance.putImageBytes(bytes, album: album)); + GalPlatform.instance.putImageBytes(bytes, album: album); /// Open gallery app. /// @@ -52,7 +48,7 @@ final class Gal { /// Check if the app has access permissions. /// /// On iOS, use the [toAlbum] option, which requires additional permissions - /// to save to an album. on android, it is ignored. If you want to save to + /// to save to an album. on android, it is ignored. If you want to save to /// an album other than the one created by your app /// See: [Permissions](https://github.com/natsuk4ze/gal/wiki/Permissions) static Future hasAccess({bool toAlbum = false}) async => @@ -67,14 +63,4 @@ final class Gal { /// See: [Permissions](https://github.com/natsuk4ze/gal/wiki/Permissions) static Future requestAccess({bool toAlbum = false}) async => GalPlatform.instance.requestAccess(toAlbum: toAlbum); - - /// Throw [GalException] when [PlatformException] was throwed by native api. - static Future _voidOrThrow(Future Function() cb) async { - try { - return await cb(); - } on PlatformException catch (error, stackTrace) { - throw GalException.fromCode( - code: error.code, error: error, stackTrace: stackTrace); - } - } } diff --git a/lib/src/gal_method_channel.dart b/lib/src/gal_method_channel.dart index 39fbe3ec..1e2af96c 100644 --- a/lib/src/gal_method_channel.dart +++ b/lib/src/gal_method_channel.dart @@ -1,5 +1,6 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; +import 'package:gal/src/gal_exception.dart'; import 'gal_platform_interface.dart'; @@ -9,43 +10,41 @@ final class MethodChannelGal extends GalPlatform { @visibleForTesting final methodChannel = const MethodChannel('gal'); + Future _invokeMethod(String method, Map args) async { + try { + return await methodChannel.invokeMethod(method, args); + } on PlatformException catch (error, stackTrace) { + throw GalException.fromCode( + code: error.code, error: error, stackTrace: stackTrace); + } + } + @override - Future putVideo(String path, {String? album}) async => - methodChannel.invokeMethod('putVideo', { - 'path': path, - 'album': album, - }); + Future putVideo(String path, {String? album}) => + _invokeMethod('putVideo', {'path': path, 'album': album}); @override - Future putImage(String path, {String? album}) async => - methodChannel.invokeMethod('putImage', { - 'path': path, - 'album': album, - }); + Future putImage(String path, {String? album}) => + _invokeMethod('putImage', {'path': path, 'album': album}); @override - Future putImageBytes(Uint8List bytes, {String? album}) async => - methodChannel.invokeMethod('putImageBytes', { - 'bytes': bytes, - 'album': album, - }); + Future putImageBytes(Uint8List bytes, {String? album}) => + _invokeMethod('putImageBytes', {'bytes': bytes, 'album': album}); @override - Future open() async => methodChannel.invokeMethod('open'); + Future open() async => _invokeMethod('open', {}); @override Future hasAccess({bool toAlbum = false}) async { - final hasAccess = await methodChannel.invokeMethod('hasAccess', { - 'toAlbum': toAlbum, - }); + final hasAccess = + await _invokeMethod('hasAccess', {'toAlbum': toAlbum}); return hasAccess ?? false; } @override Future requestAccess({bool toAlbum = false}) async { - final granted = await methodChannel.invokeMethod('requestAccess', { - 'toAlbum': toAlbum, - }); + final granted = + await _invokeMethod('requestAccess', {'toAlbum': toAlbum}); return granted ?? false; } }