Skip to content

Commit

Permalink
Merge pull request #82 from natsuk4ze/refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
natsuk4ze authored Aug 13, 2023
2 parents 80c5bcf + 60c2e12 commit ee8c166
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 52 deletions.
18 changes: 11 additions & 7 deletions example/integration_test/test_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ class App extends StatelessWidget {
label: 'Toggle toAlbum',
),
buildButton(
onPressed: () async => Gal.hasAccess(),
onPressed: () async => Gal.hasAccess(toAlbum: toAlbum),
label: 'hasAccess(toAlbum: $toAlbum)',
),
buildButton(
onPressed: () async => Gal.requestAccess(),
onPressed: () async => Gal.requestAccess(toAlbum: toAlbum),
label: 'requestAccess(toAlbum: $toAlbum)',
),
buildButton(
Expand All @@ -42,11 +42,8 @@ class App extends StatelessWidget {
),
buildButton(
onPressed: () async {
final byteData = await rootBundle.load('assets/done.jpg');
final uint8List = byteData.buffer.asUint8List(
byteData.offsetInBytes, byteData.lengthInBytes);
await Gal.putImageBytes(Uint8List.fromList(uint8List),
album: album);
final bytes = await getBytesData('assets/done.jpg');
await Gal.putImageBytes(bytes, album: album);
},
label: 'putImageBytes(toAlbum: $toAlbum)',
),
Expand Down Expand Up @@ -101,6 +98,13 @@ class App extends StatelessWidget {
.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));
return file.path;
}

Future<Uint8List> getBytesData(String path) async {
final byteData = await rootBundle.load(path);
final uint8List = byteData.buffer
.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes);
return Uint8List.fromList(uint8List);
}
}

class Logger {
Expand Down
13 changes: 9 additions & 4 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,8 @@ class _AppState extends State<App> {
),
FilledButton(
onPressed: () async {
final byteData = await rootBundle.load('assets/done.jpg');
final uint8List = byteData.buffer.asUint8List(
byteData.offsetInBytes, byteData.lengthInBytes);
await Gal.putImageBytes(Uint8List.fromList(uint8List));
final bytes = await getBytesData('assets/done.jpg');
await Gal.putImageBytes(bytes, album: album);
if (!context.mounted) return;
ScaffoldMessenger.of(context).showSnackBar(snackBar);
},
Expand Down Expand Up @@ -171,4 +169,11 @@ class _AppState extends State<App> {
.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));
return file.path;
}

Future<Uint8List> getBytesData(String path) async {
final byteData = await rootBundle.load(path);
final uint8List = byteData.buffer
.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes);
return Uint8List.fromList(uint8List);
}
}
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);
}
}
}
2 changes: 1 addition & 1 deletion lib/src/gal_exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class GalException implements Exception {
return GalException(type: type, error: error, stackTrace: stackTrace);
}
@override
String toString() => type.message;
String toString() => "GalException ${type.name}: ${type.message}";
}

enum GalExceptionType {
Expand Down
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 ee8c166

Please sign in to comment.