Skip to content

Commit

Permalink
Refactor (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
natsuk4ze authored Jul 29, 2023
1 parent be594f1 commit 4591ae5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 63 deletions.
1 change: 0 additions & 1 deletion example/integration_test/integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:flutter/material.dart' show Key;
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';

// ignore: avoid_relative_lib_imports
import 'test_app.dart' as app;

/// All tests are done in integration tests,
Expand Down
8 changes: 4 additions & 4 deletions example/integration_test/test_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ class App extends StatefulWidget {
}

class _AppState extends State<App> {
bool isLoading = false;
bool isTesting = false;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: isLoading
child: isTesting
? const CircularProgressIndicator()
: SingleChildScrollView(
child: Column(
Expand Down Expand Up @@ -80,14 +80,14 @@ class _AppState extends State<App> {
key: Key(label),
onPressed: () async {
logger = Logger();
setState(() => isLoading = true);
setState(() => isTesting = true);
try {
await onPressed();
} catch (e, st) {
logger.error = e;
logger.stackTrace = st;
} finally {
setState(() => isLoading = false);
setState(() => isTesting = false);
}
},
child: Text(label),
Expand Down
98 changes: 40 additions & 58 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ class App extends StatelessWidget {

@override
Widget build(BuildContext context) {
final snackBar = SnackBar(
content: const Text('Saved! ✅'),
action: SnackBarAction(
label: 'Gallery ->',
onPressed: () async => Gal.open(),
),
);

return MaterialApp(
theme: ThemeData(
useMaterial3: true,
Expand All @@ -33,13 +25,16 @@ class App extends StatelessWidget {
child: SingleChildScrollView(
child: Column(
children: [
_Button(
FilledButton(
onPressed: () async {
final requestGranted = await Gal.requestAccess();
if (requestGranted) {
final path = await getFilePath('assets/done.jpg');
try {
await Gal.putImage(path);

if (!context.mounted) return;
ScaffoldMessenger.of(context).showSnackBar(snackBar);
} on GalException catch (e) {
log(e.toString());
}
Expand All @@ -61,44 +56,31 @@ class App extends StatelessWidget {
),
);
},
label: 'Best Practice',
child: const Text('Best Practice'),
),
_Button(
FilledButton(
onPressed: () async => Gal.open(),
label: 'Open Gallery',
child: const Text('Open Gallery'),
),
_Button(
FilledButton(
onPressed: () async {
final path = await getFilePath('assets/done.mp4');
await Gal.putVideo(path);
if (!context.mounted) return;
ScaffoldMessenger.of(context).showSnackBar(snackBar);
},
label: 'Save Video from local',
),
_Button(
onPressed: () async {
final path = '${Directory.systemTemp.path}/done.mp4';
await Dio().download(
'https://github.com/natsuk4ze/gal/raw/main/example/assets/done.mp4',
path,
);
await Gal.putVideo(path);
if (!context.mounted) return;
ScaffoldMessenger.of(context).showSnackBar(snackBar);
},
label: 'Download Video',
child: const Text('Save Video from file path'),
),
_Button(
FilledButton(
onPressed: () async {
final path = await getFilePath('assets/done.jpg');
await Gal.putImage(path);
if (!context.mounted) return;
ScaffoldMessenger.of(context).showSnackBar(snackBar);
},
label: 'Save Image from local',
child: const Text('Save Image from file path'),
),
_Button(
FilledButton(
onPressed: () async {
final byteData = await rootBundle.load('assets/done.jpg');
final uint8List = byteData.buffer.asUint8List(
Expand All @@ -107,9 +89,9 @@ class App extends StatelessWidget {
if (!context.mounted) return;
ScaffoldMessenger.of(context).showSnackBar(snackBar);
},
label: 'Save Image from bytes',
child: const Text('Save Image from bytes'),
),
_Button(
FilledButton(
onPressed: () async {
final path = '${Directory.systemTemp.path}/done.jpg';
await Dio().download(
Expand All @@ -120,21 +102,34 @@ class App extends StatelessWidget {
if (!context.mounted) return;
ScaffoldMessenger.of(context).showSnackBar(snackBar);
},
label: 'Download Image',
child: const Text('Download Image'),
),
FilledButton(
onPressed: () async {
final path = '${Directory.systemTemp.path}/done.mp4';
await Dio().download(
'https://github.com/natsuk4ze/gal/raw/main/example/assets/done.mp4',
path,
);
await Gal.putVideo(path);
if (!context.mounted) return;
ScaffoldMessenger.of(context).showSnackBar(snackBar);
},
child: const Text('Download Video'),
),
_Button(
FilledButton(
onPressed: () async {
final hasAccess = await Gal.hasAccess();
log('Has Access:${hasAccess.toString()}');
},
label: 'Has Access',
child: const Text('Has Access'),
),
_Button(
FilledButton(
onPressed: () async {
final requestGranted = await Gal.requestAccess();
log('Request Granted:${requestGranted.toString()}');
},
label: 'Request Access',
child: const Text('Request Access'),
),
],
),
Expand All @@ -145,6 +140,14 @@ class App extends StatelessWidget {
);
}

SnackBar get snackBar => SnackBar(
content: const Text('Saved! ✅'),
action: SnackBarAction(
label: 'Gallery ->',
onPressed: () async => Gal.open(),
),
);

Future<String> getFilePath(String path) async {
final byteData = await rootBundle.load(path);
final file = await File(
Expand All @@ -155,24 +158,3 @@ class App extends StatelessWidget {
return file.path;
}
}

class _Button extends StatelessWidget {
const _Button({
required this.label,
required this.onPressed,
});

final String label;
final void Function() onPressed;

@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(12.0),
child: FloatingActionButton.extended(
onPressed: onPressed,
label: Text(label),
),
);
}
}

0 comments on commit 4591ae5

Please sign in to comment.