Skip to content

Commit

Permalink
Update formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirollos Morkos committed Sep 5, 2018
1 parent b39ce4f commit 44756ee
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 91 deletions.
81 changes: 29 additions & 52 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@ import 'package:flutter/material.dart';
import 'package:flutter_photokit/flutter_photokit.dart';
import 'package:path_provider/path_provider.dart';

void main() => runApp(const MyApp());
enum _SaveStatus { NONE, SAVING, SAVED }

enum _SaveStatus {
NONE,
SAVING,
SAVED
}
void main() => runApp(const MyApp());

class MyApp extends StatefulWidget {
const MyApp({ Key key }) : super(key: key);
const MyApp({Key key}) : super(key: key);

@override
_MyAppState createState() => new _MyAppState();
Expand All @@ -28,7 +24,8 @@ class _MyAppState extends State<MyApp> {
TextEditingController _albumTextController;

static HttpClient _httpClient = HttpClient();
static String _sampleImageUrl = 'https://cdn-images-1.medium.com/max/1200/1*5-aoK8IBmXve5whBQM90GA.png';
static String _sampleImageUrl =
'https://cdn-images-1.medium.com/max/1200/1*5-aoK8IBmXve5whBQM90GA.png';
// Use this link to test with a video.
// static String _sampleVideoUrl = 'https://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_1mb.mp4';

Expand Down Expand Up @@ -58,11 +55,7 @@ class _MyAppState extends State<MyApp> {
setState(() => _cameraRollStatus = _SaveStatus.SAVING);

File file = await _downloadFile(_sampleImageUrl);
print('Downloaded file');
await FlutterPhotokit.saveToCameraRoll(
filePath: file.path
);
print('Saved file to camera roll');
await FlutterPhotokit.saveToCameraRoll(filePath: file.path);

setState(() => _cameraRollStatus = _SaveStatus.SAVED);
}
Expand All @@ -71,12 +64,8 @@ class _MyAppState extends State<MyApp> {
setState(() => _albumStatus = _SaveStatus.SAVING);

File file = await _downloadFile(_sampleImageUrl);
print('Downloaded file...');
await FlutterPhotokit.saveToAlbum(
filePath: file.path,
albumName: _albumTextController.value.text
);
print('Saved file to album');
filePath: file.path, albumName: _albumTextController.value.text);

setState(() => _albumStatus = _SaveStatus.SAVED);
}
Expand All @@ -85,48 +74,41 @@ class _MyAppState extends State<MyApp> {
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: const Text('Flutter PhotoKit Example'),
),
body: _appBody()
),
appBar: new AppBar(
title: const Text('Flutter PhotoKit Example'),
),
body: _appBody()),
);
}

Widget _appBody() {
if (Platform.isIOS) {
return SingleChildScrollView(
child: new Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Image.network(_sampleImageUrl),
SizedBox(height: 16.0),
_saveToCameraRollRow(),
SizedBox(height: 16.0),
_saveToAlbumRow()
],
)
);
child: new Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Image.network(_sampleImageUrl),
SizedBox(height: 16.0),
_saveToCameraRollRow(),
SizedBox(height: 16.0),
_saveToAlbumRow()
],
));
} else {
return Center(
child: Text('The PhotoKit plugin is only available for iOS!')
);
child: Text('The PhotoKit plugin is only available for iOS!'));
}
}

Widget _saveToCameraRollRow() {
switch (_cameraRollStatus) {
case _SaveStatus.NONE:
return RaisedButton(
onPressed: _saveToCameraRoll,
child: Text('Save to Camera Roll')
);
onPressed: _saveToCameraRoll, child: Text('Save to Camera Roll'));
case _SaveStatus.SAVING:
return CircularProgressIndicator();
case _SaveStatus.SAVED:
return Icon(
Icons.check
);
return Icon(Icons.check);
}
}

Expand All @@ -137,26 +119,21 @@ class _MyAppState extends State<MyApp> {
children: <Widget>[
Expanded(
child: TextField(
controller: _albumTextController,
decoration: InputDecoration(
labelText: 'Album name'
)
),
controller: _albumTextController,
decoration: InputDecoration(labelText: 'Album name')),
),
RaisedButton(
onPressed: _albumTextController.value.text.trim().isEmpty
? null
: _saveToAlbum,
? null
: _saveToAlbum,
child: Text('Save to album'),
)
],
);
case _SaveStatus.SAVING:
return CircularProgressIndicator();
case _SaveStatus.SAVED:
return Icon(
Icons.check
);
return Icon(Icons.check);
}
}
}
42 changes: 14 additions & 28 deletions lib/flutter_photokit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,35 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

class FlutterPhotokit {
static const MethodChannel _channel =
const MethodChannel('flutter_photokit');
static const MethodChannel _channel = const MethodChannel('flutter_photokit');

FlutterPhotokit._();

/// Saves the file at [filePath] to [albumName].
///
///
/// [filePath] must contain either an image or video file.
///
///
/// If an album named [albumName] doesn't exist, it will be created.
static Future<bool> saveToAlbum({
@required String filePath,
@required String albumName
}) async {
static Future<bool> saveToAlbum(
{@required String filePath, @required String albumName}) async {
_checkPlatform();

final bool success = await _channel.invokeMethod(
'saveToAlbum',
<String, dynamic>{
'filePath': filePath,
'albumName': albumName
}
);
final bool success = await _channel.invokeMethod('saveToAlbum',
<String, dynamic>{'filePath': filePath, 'albumName': albumName});

return success;
}

/// Saves the file at [filePath] to the device's camera roll.
///
///
/// [filePath] must contain either an image or video file.
static Future<bool> saveToCameraRoll({
@required String filePath
}) async {
static Future<bool> saveToCameraRoll({@required String filePath}) async {
_checkPlatform();

final bool success = await _channel.invokeMethod(
'saveToCameraRoll',
<String, dynamic>{
'filePath': filePath,
}
);
final bool success =
await _channel.invokeMethod('saveToCameraRoll', <String, dynamic>{
'filePath': filePath,
});

return success;
}
Expand All @@ -54,9 +42,7 @@ class FlutterPhotokit {
/// If not, throws an [UnimplementedError]
static void _checkPlatform() async {
if (!Platform.isIOS) {
throw UnimplementedError(
'This plugin is only available for iOS devices'
);
throw UnimplementedError('This plugin is only available for iOS devices');
}
}
}
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: flutter_photokit
description: Flutter plugin for interacting with PhotoKit on iOS.
description: Flutter plugin for interacting with PhotoKit on iOS. Contains methods for saving photos/videos to user's camera roll or custom albums.
author: Crater Dev Team <[email protected]>
homepage: https://github.com/kmorkos/flutter_photokit
version: 0.1.0
version: 0.1.1

environment:
sdk: ">=2.0.0-dev.68.0 <3.0.0"
Expand Down
15 changes: 6 additions & 9 deletions test/flutter_photokit_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
@OnPlatform(const {
'!ios': const Skip('Plugin only available for iOS')
})
@OnPlatform(const {'!ios': const Skip('Plugin only available for iOS')})

import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
Expand All @@ -11,8 +9,7 @@ void main() {
final String mockAlbumName = 'MySampleAlbum';

group('$FlutterPhotokit', () {
const MethodChannel channel =
MethodChannel('flutter_photokit');
const MethodChannel channel = MethodChannel('flutter_photokit');

final List<MethodCall> log = <MethodCall>[];

Expand All @@ -27,7 +24,8 @@ void main() {

group('#saveToAlbum', () {
test('passes the arguments correctly', () async {
await FlutterPhotokit.saveToAlbum(filePath: mockFilePath, albumName: mockAlbumName);
await FlutterPhotokit.saveToAlbum(
filePath: mockFilePath, albumName: mockAlbumName);

expect(
log,
Expand All @@ -48,9 +46,8 @@ void main() {
expect(
log,
<Matcher>[
isMethodCall('saveToCameraRoll', arguments: <String, dynamic>{
'filePath': mockFilePath
}),
isMethodCall('saveToCameraRoll',
arguments: <String, dynamic>{'filePath': mockFilePath}),
],
);
});
Expand Down

0 comments on commit 44756ee

Please sign in to comment.