diff --git a/lib/src/gal.dart b/lib/src/gal.dart index af7c7298..2045ccee 100644 --- a/lib/src/gal.dart +++ b/lib/src/gal.dart @@ -1,6 +1,6 @@ import 'package:flutter/foundation.dart'; import 'package:gal/src/gal_exception.dart'; -import 'package:gal/src/gal_platform.dart'; +import 'package:gal/src/gal_platform_interface.dart'; /// Main class of gal. /// @@ -20,7 +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 => - GalPlatform.putVideo(path, album: album); + GalPlatform.instance.putVideo(path, album: album); /// Save a image to the gallery from file [path]. /// @@ -33,7 +33,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 => - GalPlatform.putImage(path, album: album); + GalPlatform.instance.putImage(path, album: album); /// Save a image to the gallery from [Uint8List]. /// @@ -42,12 +42,12 @@ 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 => - GalPlatform.putImageBytes(bytes, album: album); + GalPlatform.instance.putImageBytes(bytes, album: album); /// Open gallery app. /// /// If there are multiple gallery apps, App selection sheet may be displayed. - static Future open() async => GalPlatform.open(); + static Future open() async => GalPlatform.instance.open(); /// Check if the app has access permissions. /// @@ -55,7 +55,7 @@ final class Gal { /// 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 => - GalPlatform.hasAccess(toAlbum: toAlbum); + GalPlatform.instance.hasAccess(toAlbum: toAlbum); /// Request access permissions. /// @@ -65,5 +65,5 @@ final class Gal { /// 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 requestAccess({bool toAlbum = false}) async => - GalPlatform.requestAccess(toAlbum: toAlbum); + GalPlatform.instance.requestAccess(toAlbum: toAlbum); } diff --git a/lib/src/gal_platform.dart b/lib/src/gal_method_channel.dart similarity index 50% rename from lib/src/gal_platform.dart rename to lib/src/gal_method_channel.dart index a97c2cf9..069a7b8d 100644 --- a/lib/src/gal_platform.dart +++ b/lib/src/gal_method_channel.dart @@ -1,49 +1,57 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; -import 'package:gal/gal.dart'; +import 'package:gal/src/gal_exception.dart'; -const _channel = MethodChannel('gal'); +import 'gal_platform_interface.dart'; /// Plugin Communication @immutable -final class GalPlatform { - const GalPlatform._(); +final class MethodChannelGal extends GalPlatform { + final _methodChannel = const MethodChannel('gal'); - static Future _invokeMethod( - String method, Map args) async { + Future _invokeMethod(String method, Map args) async { try { - return await _channel.invokeMethod(method, args); + return await _methodChannel.invokeMethod(method, args); } on PlatformException catch (error, stackTrace) { throw GalException.fromCode( - code: error.code, platformException: error, stackTrace: stackTrace); + code: error.code, + platformException: error, + stackTrace: stackTrace, + ); } } - static Future putVideo(String path, {String? album}) async { + @override + Future putVideo(String path, {String? album}) async { await requestAccess(toAlbum: album != null); await _invokeMethod('putVideo', {'path': path, 'album': album}); } - static Future putImage(String path, {String? album}) async { + @override + Future putImage(String path, {String? album}) async { await requestAccess(toAlbum: album != null); await _invokeMethod('putImage', {'path': path, 'album': album}); } - static Future putImageBytes(Uint8List bytes, {String? album}) async { + @override + Future putImageBytes(Uint8List bytes, {String? album}) async { await requestAccess(toAlbum: album != null); await _invokeMethod( 'putImageBytes', {'bytes': bytes, 'album': album}); } - static Future open() async => _invokeMethod('open', {}); + @override + Future open() async => _invokeMethod('open', {}); - static Future hasAccess({bool toAlbum = false}) async { + @override + Future hasAccess({bool toAlbum = false}) async { final hasAccess = await _invokeMethod('hasAccess', {'toAlbum': toAlbum}); return hasAccess ?? false; } - static Future requestAccess({bool toAlbum = false}) async { + @override + Future requestAccess({bool toAlbum = false}) async { final granted = await _invokeMethod('requestAccess', {'toAlbum': toAlbum}); return granted ?? false; diff --git a/lib/src/gal_platform_interface.dart b/lib/src/gal_platform_interface.dart new file mode 100644 index 00000000..462a2738 --- /dev/null +++ b/lib/src/gal_platform_interface.dart @@ -0,0 +1,39 @@ +import 'dart:typed_data'; + +import 'gal_method_channel.dart'; + +/// Plugin Platform Interface +base class GalPlatform { + const GalPlatform(); + static GalPlatform instance = MethodChannelGal(); + + /// throw [UnimplementedError] when Plugin [MethodChannelGal] did not + /// define [putVideo]. + Future putVideo(String path, {String? album}) => + throw UnimplementedError('putVideo() has not been implemented.'); + + /// throw [UnimplementedError] when Plugin [MethodChannelGal] did not + /// define [putImage]. + Future putImage(String path, {String? album}) => + throw UnimplementedError('putImage() has not been implemented.'); + + /// throw [UnimplementedError] when Plugin [MethodChannelGal] did not + /// define [putImageBytes]. + Future putImageBytes(Uint8List bytes, {String? album}) => + throw UnimplementedError('putImageBytes() has not been implemented.'); + + /// throw [UnimplementedError] when Plugin [MethodChannelGal] did not + /// define [open]. + Future open() => + throw UnimplementedError('open() has not been implemented.'); + + /// throw [UnimplementedError] when Plugin [MethodChannelGal] did not + /// define [hasAccess]. + Future hasAccess({bool toAlbum = false}) => + throw UnimplementedError('hasAccess() has not been implemented.'); + + /// throw [UnimplementedError] when Plugin [MethodChannelGal] did not + /// define [requestAccess]. + Future requestAccess({bool toAlbum = false}) => + throw UnimplementedError('requestAccess() has not been implemented.'); +}