From 60c42091435c4f929632c04443dad8b7e1a0112f Mon Sep 17 00:00:00 2001 From: natsuk4ze Date: Mon, 4 Dec 2023 19:41:26 +0900 Subject: [PATCH 1/7] Add plugin_platform_interface dependency --- pubspec.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/pubspec.yaml b/pubspec.yaml index 02500c16..c9fc129e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -25,6 +25,7 @@ environment: dependencies: flutter: sdk: flutter + plugin_platform_interface: ^2.1.7 dev_dependencies: flutter_lints: ^3.0.1 From 686556496aa637c6c44dfdf403d2c0c8386ba0cb Mon Sep 17 00:00:00 2001 From: natsuk4ze Date: Mon, 4 Dec 2023 19:55:43 +0900 Subject: [PATCH 2/7] Remove GalPlatform class --- lib/src/gal_platform.dart | 51 --------------------------------------- 1 file changed, 51 deletions(-) delete mode 100644 lib/src/gal_platform.dart diff --git a/lib/src/gal_platform.dart b/lib/src/gal_platform.dart deleted file mode 100644 index a97c2cf9..00000000 --- a/lib/src/gal_platform.dart +++ /dev/null @@ -1,51 +0,0 @@ -import 'package:flutter/foundation.dart'; -import 'package:flutter/services.dart'; -import 'package:gal/gal.dart'; - -const _channel = MethodChannel('gal'); - -/// Plugin Communication -@immutable -final class GalPlatform { - const GalPlatform._(); - - static Future _invokeMethod( - String method, Map args) async { - try { - return await _channel.invokeMethod(method, args); - } on PlatformException catch (error, stackTrace) { - throw GalException.fromCode( - code: error.code, platformException: error, stackTrace: stackTrace); - } - } - - static 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 { - await requestAccess(toAlbum: album != null); - await _invokeMethod('putImage', {'path': path, 'album': album}); - } - - static 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', {}); - - static Future hasAccess({bool toAlbum = false}) async { - final hasAccess = - await _invokeMethod('hasAccess', {'toAlbum': toAlbum}); - return hasAccess ?? false; - } - - static Future requestAccess({bool toAlbum = false}) async { - final granted = - await _invokeMethod('requestAccess', {'toAlbum': toAlbum}); - return granted ?? false; - } -} From 1dfab32202a63e8bde1edf3bd815e0623fcaa5ef Mon Sep 17 00:00:00 2001 From: natsuk4ze Date: Mon, 4 Dec 2023 19:56:10 +0900 Subject: [PATCH 3/7] Update GalPlatform import and method calls --- lib/src/gal.dart | 14 ++++----- lib/src/gal_platform_interface.dart | 48 +++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 lib/src/gal_platform_interface.dart 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_interface.dart b/lib/src/gal_platform_interface.dart new file mode 100644 index 00000000..79aa9a7e --- /dev/null +++ b/lib/src/gal_platform_interface.dart @@ -0,0 +1,48 @@ +import 'dart:typed_data'; + +import 'package:plugin_platform_interface/plugin_platform_interface.dart'; + +import 'gal_method_channel.dart'; + +/// Plugin Platform Interface +abstract class GalPlatform extends PlatformInterface { + GalPlatform() : super(token: _token); + static final Object _token = Object(); + static GalPlatform _instance = MethodChannelGal(); + static GalPlatform get instance => _instance; + + static set instance(GalPlatform instance) { + PlatformInterface.verify(instance, _token); + _instance = instance; + } + + /// 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.'); +} \ No newline at end of file From 7dc4047bf0876f9cbfe9edadf1b56cbaa5e8cb94 Mon Sep 17 00:00:00 2001 From: natsuk4ze Date: Mon, 4 Dec 2023 19:56:55 +0900 Subject: [PATCH 4/7] Add MethodChannelGal --- lib/src/gal_method_channel.dart | 60 +++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 lib/src/gal_method_channel.dart diff --git a/lib/src/gal_method_channel.dart b/lib/src/gal_method_channel.dart new file mode 100644 index 00000000..832a1beb --- /dev/null +++ b/lib/src/gal_method_channel.dart @@ -0,0 +1,60 @@ +import 'package:flutter/foundation.dart'; +import 'package:flutter/services.dart'; +import 'package:gal/src/gal_exception.dart'; + +import 'gal_platform_interface.dart'; + +/// Plugin Communication +@immutable +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, + platformException: error, + stackTrace: stackTrace, + ); + } + } + + @override + Future putVideo(String path, {String? album}) async { + await requestAccess(toAlbum: album != null); + await _invokeMethod('putVideo', {'path': path, 'album': album}); + } + + @override + Future putImage(String path, {String? album}) async { + await requestAccess(toAlbum: album != null); + await _invokeMethod('putImage', {'path': path, 'album': album}); + } + + @override + Future putImageBytes(Uint8List bytes, {String? album}) async { + await requestAccess(toAlbum: album != null); + await _invokeMethod( + 'putImageBytes', {'bytes': bytes, 'album': album}); + } + + @override + Future open() async => _invokeMethod('open', {}); + + @override + Future hasAccess({bool toAlbum = false}) async { + final hasAccess = + await _invokeMethod('hasAccess', {'toAlbum': toAlbum}); + return hasAccess ?? false; + } + + @override + Future requestAccess({bool toAlbum = false}) async { + final granted = + await _invokeMethod('requestAccess', {'toAlbum': toAlbum}); + return granted ?? false; + } +} From d97dac8c2505fd15cd572a1dca80f0c711ba136a Mon Sep 17 00:00:00 2001 From: natsuk4ze Date: Mon, 4 Dec 2023 20:23:35 +0900 Subject: [PATCH 5/7] Add base keyword to GalPlatform --- lib/src/gal_platform_interface.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/gal_platform_interface.dart b/lib/src/gal_platform_interface.dart index 79aa9a7e..4e373b7f 100644 --- a/lib/src/gal_platform_interface.dart +++ b/lib/src/gal_platform_interface.dart @@ -5,7 +5,7 @@ import 'package:plugin_platform_interface/plugin_platform_interface.dart'; import 'gal_method_channel.dart'; /// Plugin Platform Interface -abstract class GalPlatform extends PlatformInterface { +base class GalPlatform extends PlatformInterface { GalPlatform() : super(token: _token); static final Object _token = Object(); static GalPlatform _instance = MethodChannelGal(); From 327e83490bbdf4e3a7faee43d7ad64463a9a4aec Mon Sep 17 00:00:00 2001 From: natsuk4ze Date: Mon, 4 Dec 2023 21:09:45 +0900 Subject: [PATCH 6/7] Remove plugin_platform_interface package The PlatformInterface class is used to enforce `extends`, which is replaced by Dart 3's `base`. See:https://github.com/flutter/flutter/issues/127396 --- lib/src/gal_platform_interface.dart | 17 ++++------------- pubspec.yaml | 1 - 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/lib/src/gal_platform_interface.dart b/lib/src/gal_platform_interface.dart index 4e373b7f..462a2738 100644 --- a/lib/src/gal_platform_interface.dart +++ b/lib/src/gal_platform_interface.dart @@ -1,20 +1,11 @@ import 'dart:typed_data'; -import 'package:plugin_platform_interface/plugin_platform_interface.dart'; - import 'gal_method_channel.dart'; /// Plugin Platform Interface -base class GalPlatform extends PlatformInterface { - GalPlatform() : super(token: _token); - static final Object _token = Object(); - static GalPlatform _instance = MethodChannelGal(); - static GalPlatform get instance => _instance; - - static set instance(GalPlatform instance) { - PlatformInterface.verify(instance, _token); - _instance = instance; - } +base class GalPlatform { + const GalPlatform(); + static GalPlatform instance = MethodChannelGal(); /// throw [UnimplementedError] when Plugin [MethodChannelGal] did not /// define [putVideo]. @@ -45,4 +36,4 @@ base class GalPlatform extends PlatformInterface { /// define [requestAccess]. Future requestAccess({bool toAlbum = false}) => throw UnimplementedError('requestAccess() has not been implemented.'); -} \ No newline at end of file +} diff --git a/pubspec.yaml b/pubspec.yaml index c9fc129e..02500c16 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -25,7 +25,6 @@ environment: dependencies: flutter: sdk: flutter - plugin_platform_interface: ^2.1.7 dev_dependencies: flutter_lints: ^3.0.1 From c62293863150e15002d49be0602a6115d0764708 Mon Sep 17 00:00:00 2001 From: natsuk4ze Date: Mon, 4 Dec 2023 21:19:51 +0900 Subject: [PATCH 7/7] Refactor method channel variable name --- lib/src/gal_method_channel.dart | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/src/gal_method_channel.dart b/lib/src/gal_method_channel.dart index 832a1beb..069a7b8d 100644 --- a/lib/src/gal_method_channel.dart +++ b/lib/src/gal_method_channel.dart @@ -7,12 +7,11 @@ import 'gal_platform_interface.dart'; /// Plugin Communication @immutable final class MethodChannelGal extends GalPlatform { - @visibleForTesting - final methodChannel = const MethodChannel('gal'); + final _methodChannel = const MethodChannel('gal'); Future _invokeMethod(String method, Map args) async { try { - return await methodChannel.invokeMethod(method, args); + return await _methodChannel.invokeMethod(method, args); } on PlatformException catch (error, stackTrace) { throw GalException.fromCode( code: error.code,