diff --git a/.gitignore b/.gitignore index ccb0eeb34605..734169649fd3 100644 --- a/.gitignore +++ b/.gitignore @@ -22,6 +22,7 @@ Pods/ ServiceDefinitions.json xcuserdata/ *.xcworkspace +**/DerivedData/ local.properties keystore.properties diff --git a/packages/path_provider/CHANGELOG.md b/packages/path_provider/CHANGELOG.md index 498127adefe6..6ded47745ddc 100644 --- a/packages/path_provider/CHANGELOG.md +++ b/packages/path_provider/CHANGELOG.md @@ -1,3 +1,10 @@ +## 1.3.0 + +* Added iOS-only support for `getLibraryDirectory`. +* Update integration tests and example test. +* Update example app UI to use a `ListView` show the list of content. +* Update .gitignore to include Xcode build output folder `**/DerivedData/` + ## 1.2.2 * Correct the integration test for Android's `getApplicationSupportDirectory` call. diff --git a/packages/path_provider/example/lib/main.dart b/packages/path_provider/example/lib/main.dart index bea99bc4b671..2e9e9513f787 100644 --- a/packages/path_provider/example/lib/main.dart +++ b/packages/path_provider/example/lib/main.dart @@ -36,6 +36,7 @@ class MyHomePage extends StatefulWidget { class _MyHomePageState extends State { Future _tempDirectory; Future _appSupportDirectory; + Future _appLibraryDirectory; Future _appDocumentsDirectory; Future _externalDocumentsDirectory; @@ -72,6 +73,12 @@ class _MyHomePageState extends State { }); } + void _requestAppLibraryDirectory() { + setState(() { + _appLibraryDirectory = getLibraryDirectory(); + }); + } + void _requestExternalStorageDirectory() { setState(() { _externalDocumentsDirectory = getExternalStorageDirectory(); @@ -85,70 +92,55 @@ class _MyHomePageState extends State { title: Text(widget.title), ), body: Center( - child: Column( - mainAxisAlignment: MainAxisAlignment.spaceEvenly, + child: ListView( children: [ - Column( - children: [ - Padding( - padding: const EdgeInsets.all(16.0), - child: RaisedButton( - child: const Text('Get Temporary Directory'), - onPressed: _requestTempDirectory, - ), - ), - ], - ), - Expanded( - child: FutureBuilder( - future: _tempDirectory, builder: _buildDirectory), - ), - Column( - children: [ - Padding( - padding: const EdgeInsets.all(16.0), - child: RaisedButton( - child: const Text('Get Application Documents Directory'), - onPressed: _requestAppDocumentsDirectory, - ), - ), - ], + Padding( + padding: const EdgeInsets.all(16.0), + child: RaisedButton( + child: const Text('Get Temporary Directory'), + onPressed: _requestTempDirectory, + ), ), - Expanded( - child: FutureBuilder( - future: _appDocumentsDirectory, builder: _buildDirectory), + FutureBuilder( + future: _tempDirectory, builder: _buildDirectory), + Padding( + padding: const EdgeInsets.all(16.0), + child: RaisedButton( + child: const Text('Get Application Documents Directory'), + onPressed: _requestAppDocumentsDirectory, + ), ), - Column( - children: [ - Padding( - padding: const EdgeInsets.all(16.0), - child: RaisedButton( - child: const Text('Get Application Support Directory'), - onPressed: _requestAppSupportDirectory, - ), - ), - ], + FutureBuilder( + future: _appDocumentsDirectory, builder: _buildDirectory), + Padding( + padding: const EdgeInsets.all(16.0), + child: RaisedButton( + child: const Text('Get Application Support Directory'), + onPressed: _requestAppSupportDirectory, + ), ), - Expanded( - child: FutureBuilder( - future: _appSupportDirectory, builder: _buildDirectory), + FutureBuilder( + future: _appSupportDirectory, builder: _buildDirectory), + Padding( + padding: const EdgeInsets.all(16.0), + child: RaisedButton( + child: const Text('Get Application Library Directory'), + onPressed: _requestAppLibraryDirectory, + ), ), - Column(children: [ - Padding( - padding: const EdgeInsets.all(16.0), - child: RaisedButton( - child: Text( - '${Platform.isIOS ? "External directories are unavailable " "on iOS" : "Get External Storage Directory"}'), - onPressed: - Platform.isIOS ? null : _requestExternalStorageDirectory, - ), + FutureBuilder( + future: _appLibraryDirectory, builder: _buildDirectory), + Padding( + padding: const EdgeInsets.all(16.0), + child: RaisedButton( + child: Text( + '${Platform.isIOS ? "External directories are unavailable " "on iOS" : "Get External Storage Directory"}'), + onPressed: + Platform.isIOS ? null : _requestExternalStorageDirectory, ), - ]), - Expanded( - child: FutureBuilder( - future: _externalDocumentsDirectory, - builder: _buildDirectory), ), + FutureBuilder( + future: _externalDocumentsDirectory, builder: _buildDirectory) ], ), ), diff --git a/packages/path_provider/example/test_driver/path_provider.dart b/packages/path_provider/example/test_driver/path_provider.dart index ca9ae8cf642a..8438965f9646 100644 --- a/packages/path_provider/example/test_driver/path_provider.dart +++ b/packages/path_provider/example/test_driver/path_provider.dart @@ -45,6 +45,21 @@ void main() { file.deleteSync(); }); + test('getLibraryDirectory', () async { + if (Platform.isIOS) { + final Directory result = await getLibraryDirectory(); + final String uuid = Uuid().v1(); + final File file = File('${result.path}/$uuid.txt'); + file.writeAsStringSync('Hello world!'); + expect(file.readAsStringSync(), 'Hello world!'); + expect(result.listSync(), isNotEmpty); + file.deleteSync(); + } else if (Platform.isAndroid) { + final Future result = getLibraryDirectory(); + expect(result, throwsA(isInstanceOf())); + } + }); + test('getExternalStorageDirectory', () async { if (Platform.isIOS) { final Future result = getExternalStorageDirectory(); diff --git a/packages/path_provider/ios/Classes/PathProviderPlugin.m b/packages/path_provider/ios/Classes/PathProviderPlugin.m index a8dc4a7b5bfe..1bf0e7af1ff8 100644 --- a/packages/path_provider/ios/Classes/PathProviderPlugin.m +++ b/packages/path_provider/ios/Classes/PathProviderPlugin.m @@ -42,6 +42,8 @@ + (void)registerWithRegistrar:(NSObject*)registrar { } else { result(path); } + } else if ([@"getLibraryDirectory" isEqualToString:call.method]) { + result([self getLibraryDirectory]); } else { result(FlutterMethodNotImplemented); } @@ -60,4 +62,8 @@ + (NSString*)getApplicationSupportDirectory { return GetDirectoryOfType(NSApplicationSupportDirectory); } ++ (NSString*)getLibraryDirectory { + return GetDirectoryOfType(NSLibraryDirectory); +} + @end diff --git a/packages/path_provider/lib/path_provider.dart b/packages/path_provider/lib/path_provider.dart index 069973a5aca7..f0587225c498 100644 --- a/packages/path_provider/lib/path_provider.dart +++ b/packages/path_provider/lib/path_provider.dart @@ -59,6 +59,17 @@ Future getApplicationSupportDirectory() async { return Directory(path); } +/// Path to the directory where application can store files that are persistent, +/// backed up, and not visible to the user, such as sqlite.db. +Future getLibraryDirectory() async { + final String path = + await _channel.invokeMethod('getLibraryDirectory'); + if (path == null) { + return null; + } + return Directory(path); +} + /// Path to a directory where the application may place data that is /// user-generated, or that cannot otherwise be recreated by your application. /// diff --git a/packages/path_provider/pubspec.yaml b/packages/path_provider/pubspec.yaml index e7e7555d93cc..80c6bde28379 100644 --- a/packages/path_provider/pubspec.yaml +++ b/packages/path_provider/pubspec.yaml @@ -3,7 +3,7 @@ description: Flutter plugin for getting commonly used locations on the Android & iOS file systems, such as the temp and app data directories. author: Flutter Team homepage: https://github.com/flutter/plugins/tree/master/packages/path_provider -version: 1.2.2 +version: 1.3.0 flutter: plugin: diff --git a/packages/path_provider/test/path_provider_test.dart b/packages/path_provider/test/path_provider_test.dart index 1e8afecab1cd..24368c04cb8c 100644 --- a/packages/path_provider/test/path_provider_test.dart +++ b/packages/path_provider/test/path_provider_test.dart @@ -64,6 +64,18 @@ void main() { expect(directory, isNull); }); + test('getApplicationSupportDirectory test', () async { + response = null; + final Directory directory = await getApplicationSupportDirectory(); + expect( + log, + [ + isMethodCall('getApplicationSupportDirectory', arguments: null) + ], + ); + expect(directory, isNull); + }); + test('getExternalStorageDirectory test', () async { response = null; final Directory directory = await getExternalStorageDirectory(); @@ -74,6 +86,16 @@ void main() { expect(directory, isNull); }); + test('getLibraryDirectory test', () async { + response = null; + final Directory directory = await getLibraryDirectory(); + expect( + log, + [isMethodCall('getLibraryDirectory', arguments: null)], + ); + expect(directory, isNull); + }); + test('getExternalStorageDirectory iOS test', () async { setMockPathProviderPlatform(FakePlatform(operatingSystem: 'ios')); @@ -107,6 +129,20 @@ void main() { expect(directory.path, equals(fakePath)); }); + test('ApplicationSupportDirectory path test', () async { + final String fakePath = "/foo/bar/baz"; + response = fakePath; + final Directory directory = await getApplicationSupportDirectory(); + expect(directory.path, equals(fakePath)); + }); + + test('ApplicationLibraryDirectory path test', () async { + final String fakePath = "/foo/bar/baz"; + response = fakePath; + final Directory directory = await getLibraryDirectory(); + expect(directory.path, equals(fakePath)); + }); + test('ExternalStorageDirectory path test', () async { final String fakePath = "/foo/bar/baz"; response = fakePath;