From 791a223e367c8a39afed14e25d57c31e3352551c Mon Sep 17 00:00:00 2001 From: jerryzhoujw Date: Tue, 6 Aug 2019 21:49:01 +0700 Subject: [PATCH 1/3] [path_provider] add getLibraryDirectory --- .gitignore | 1 + packages/path_provider/CHANGELOG.md | 7 ++ packages/path_provider/example/lib/main.dart | 106 ++++++++---------- .../example/test_driver/path_provider.dart | 15 +++ .../ios/Classes/PathProviderPlugin.m | 6 + packages/path_provider/lib/path_provider.dart | 28 +++++ packages/path_provider/pubspec.yaml | 2 +- .../test/path_provider_test.dart | 36 ++++++ 8 files changed, 143 insertions(+), 58 deletions(-) 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 2e7f5ad33464..42ac1bba11b4 100644 --- a/packages/path_provider/CHANGELOG.md +++ b/packages/path_provider/CHANGELOG.md @@ -1,3 +1,10 @@ +## 1.2.1 + +* On iOS, Added `getLibraryDirectory`. +* Update integration tests and example test. +* Update demo UI to use ListView show the long list of content. +* Update .gitignore to include Xcode build output folder `**/DerivedData/` + ## 1.2.0 * On Android, `getApplicationSupportDirectory` is now supported using `getFilesDir`. 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 219d6660df7e..c4f4dbba6857 100644 --- a/packages/path_provider/example/test_driver/path_provider.dart +++ b/packages/path_provider/example/test_driver/path_provider.dart @@ -50,6 +50,21 @@ void main() { } }); + 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 c53468ae05ca..6dca30265074 100644 --- a/packages/path_provider/lib/path_provider.dart +++ b/packages/path_provider/lib/path_provider.dart @@ -50,6 +50,34 @@ Future getApplicationSupportDirectory() async { return Directory(path); } +/// Path to the directory where application can store +/// `persistent, backed up, not visible to the user` files, such as sqlite.db. +/// And others resource which want to keep and hide to user. +/// +/// From link of [https://stackoverflow.com/questions/7268299/path-directory-usable-in-ios]: +/// 1. `NSDocumentDirectory` is `Documents/` +/// (persistent, backed up, may be visible in iTunes) +/// 2. `NSLibraryDirectory` is `Library/` +/// (persistent, backed up, not visible to the user) +/// 3. `NSCachesDirectory` is `Library/Caches/` +/// (not backed up, may be cleared by system) +/// +/// From iOS Frameworks: +/// NSLibraryDirectory, // various documentation, support, +/// and configuration files, resources (Library) +/// NSApplicationSupportDirectory = 14, // location of application support +/// files (plug-ins, etc) (Library/Application Support) +/// +/// +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 2eed0ae7bd9d..634ba1ce834b 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.0 +version: 1.2.1 flutter: plugin: diff --git a/packages/path_provider/test/path_provider_test.dart b/packages/path_provider/test/path_provider_test.dart index 9da5a2568a9a..45f0f05f6081 100644 --- a/packages/path_provider/test/path_provider_test.dart +++ b/packages/path_provider/test/path_provider_test.dart @@ -45,6 +45,28 @@ 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('getLibraryDirectory test', () async { + response = null; + final Directory directory = await getLibraryDirectory(); + expect( + log, + [isMethodCall('getLibraryDirectory', arguments: null)], + ); + expect(directory, isNull); + }); + test('TemporaryDirectory path test', () async { final String fakePath = "/foo/bar/baz"; response = fakePath; @@ -58,4 +80,18 @@ void main() { final Directory directory = await getApplicationDocumentsDirectory(); 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)); + }); } From 3b59eb443719d7070ef617e14b8cc09a09ae53c8 Mon Sep 17 00:00:00 2001 From: Collin Jackson Date: Mon, 26 Aug 2019 14:24:22 -0700 Subject: [PATCH 2/3] Update comment --- packages/path_provider/lib/path_provider.dart | 21 ++----------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/packages/path_provider/lib/path_provider.dart b/packages/path_provider/lib/path_provider.dart index 6dca30265074..04665fb12f74 100644 --- a/packages/path_provider/lib/path_provider.dart +++ b/packages/path_provider/lib/path_provider.dart @@ -50,25 +50,8 @@ Future getApplicationSupportDirectory() async { return Directory(path); } -/// Path to the directory where application can store -/// `persistent, backed up, not visible to the user` files, such as sqlite.db. -/// And others resource which want to keep and hide to user. -/// -/// From link of [https://stackoverflow.com/questions/7268299/path-directory-usable-in-ios]: -/// 1. `NSDocumentDirectory` is `Documents/` -/// (persistent, backed up, may be visible in iTunes) -/// 2. `NSLibraryDirectory` is `Library/` -/// (persistent, backed up, not visible to the user) -/// 3. `NSCachesDirectory` is `Library/Caches/` -/// (not backed up, may be cleared by system) -/// -/// From iOS Frameworks: -/// NSLibraryDirectory, // various documentation, support, -/// and configuration files, resources (Library) -/// NSApplicationSupportDirectory = 14, // location of application support -/// files (plug-ins, etc) (Library/Application Support) -/// -/// +/// 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'); From bb06dfc8ab055e86dbecba89e931bb1cc8cfa1aa Mon Sep 17 00:00:00 2001 From: Collin Jackson Date: Mon, 26 Aug 2019 14:24:30 -0700 Subject: [PATCH 3/3] Update CHANGELOG --- packages/path_provider/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/path_provider/CHANGELOG.md b/packages/path_provider/CHANGELOG.md index 24db5e95a718..8dce4b0bdee8 100644 --- a/packages/path_provider/CHANGELOG.md +++ b/packages/path_provider/CHANGELOG.md @@ -1,6 +1,6 @@ ## 1.3.0 -* On iOS, Added `getLibraryDirectory`. +* 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/`