Skip to content

Commit

Permalink
Move _voidOrThrow to methodChannel
Browse files Browse the repository at this point in the history
  • Loading branch information
natsuk4ze committed Aug 13, 2023
1 parent 1de41a6 commit cceacd9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 40 deletions.
22 changes: 4 additions & 18 deletions lib/src/gal.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -21,8 +20,7 @@ final class Gal {
/// if an error occurs during saving.
/// See: [Formats](https://github.com/natsuk4ze/gal/wiki/Formats)
static Future<void> 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].
///
Expand All @@ -31,8 +29,7 @@ final class Gal {
/// if an error occurs during saving.
/// See: [Formats](https://github.com/natsuk4ze/gal/wiki/Formats)
static Future<void> 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].
///
Expand All @@ -41,8 +38,7 @@ final class Gal {
/// if an error occurs during saving.
/// See: [Formats](https://github.com/natsuk4ze/gal/wiki/Formats)
static Future<void> putImageBytes(Uint8List bytes, {String? album}) async =>
_voidOrThrow(
() async => GalPlatform.instance.putImageBytes(bytes, album: album));
GalPlatform.instance.putImageBytes(bytes, album: album);

/// Open gallery app.
///
Expand All @@ -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<bool> hasAccess({bool toAlbum = false}) async =>
Expand All @@ -67,14 +63,4 @@ final class Gal {
/// See: [Permissions](https://github.com/natsuk4ze/gal/wiki/Permissions)
static Future<bool> requestAccess({bool toAlbum = false}) async =>
GalPlatform.instance.requestAccess(toAlbum: toAlbum);

/// Throw [GalException] when [PlatformException] was throwed by native api.
static Future<void> _voidOrThrow(Future<void> Function() cb) async {
try {
return await cb();
} on PlatformException catch (error, stackTrace) {
throw GalException.fromCode(
code: error.code, error: error, stackTrace: stackTrace);
}
}
}
43 changes: 21 additions & 22 deletions lib/src/gal_method_channel.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -9,43 +10,41 @@ final class MethodChannelGal extends GalPlatform {
@visibleForTesting
final methodChannel = const MethodChannel('gal');

Future<T?> _invokeMethod<T>(String method, Map<String, dynamic> args) async {
try {
return await methodChannel.invokeMethod<T>(method, args);
} on PlatformException catch (error, stackTrace) {
throw GalException.fromCode(
code: error.code, error: error, stackTrace: stackTrace);
}
}

@override
Future<void> putVideo(String path, {String? album}) async =>
methodChannel.invokeMethod<void>('putVideo', {
'path': path,
'album': album,
});
Future<void> putVideo(String path, {String? album}) =>
_invokeMethod<void>('putVideo', {'path': path, 'album': album});

@override
Future<void> putImage(String path, {String? album}) async =>
methodChannel.invokeMethod<void>('putImage', {
'path': path,
'album': album,
});
Future<void> putImage(String path, {String? album}) =>
_invokeMethod<void>('putImage', {'path': path, 'album': album});

@override
Future<void> putImageBytes(Uint8List bytes, {String? album}) async =>
methodChannel.invokeMethod<void>('putImageBytes', {
'bytes': bytes,
'album': album,
});
Future<void> putImageBytes(Uint8List bytes, {String? album}) =>
_invokeMethod<void>('putImageBytes', {'bytes': bytes, 'album': album});

@override
Future<void> open() async => methodChannel.invokeMethod<void>('open');
Future<void> open() async => _invokeMethod<void>('open', {});

@override
Future<bool> hasAccess({bool toAlbum = false}) async {
final hasAccess = await methodChannel.invokeMethod<bool>('hasAccess', {
'toAlbum': toAlbum,
});
final hasAccess =
await _invokeMethod<bool>('hasAccess', {'toAlbum': toAlbum});
return hasAccess ?? false;
}

@override
Future<bool> requestAccess({bool toAlbum = false}) async {
final granted = await methodChannel.invokeMethod<bool>('requestAccess', {
'toAlbum': toAlbum,
});
final granted =
await _invokeMethod<bool>('requestAccess', {'toAlbum': toAlbum});
return granted ?? false;
}
}

0 comments on commit cceacd9

Please sign in to comment.