Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[path_provider] add getLibraryDirectory
  • Loading branch information
jerryzhoujw committed Aug 15, 2019
commit 791a223e367c8a39afed14e25d57c31e3352551c
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Pods/
ServiceDefinitions.json
xcuserdata/
*.xcworkspace
**/DerivedData/

local.properties
keystore.properties
Expand Down
7 changes: 7 additions & 0 deletions packages/path_provider/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`.
Expand Down
106 changes: 49 additions & 57 deletions packages/path_provider/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class MyHomePage extends StatefulWidget {
class _MyHomePageState extends State<MyHomePage> {
Future<Directory> _tempDirectory;
Future<Directory> _appSupportDirectory;
Future<Directory> _appLibraryDirectory;
Future<Directory> _appDocumentsDirectory;
Future<Directory> _externalDocumentsDirectory;

Expand Down Expand Up @@ -72,6 +73,12 @@ class _MyHomePageState extends State<MyHomePage> {
});
}

void _requestAppLibraryDirectory() {
setState(() {
_appLibraryDirectory = getLibraryDirectory();
});
}

void _requestExternalStorageDirectory() {
setState(() {
_externalDocumentsDirectory = getExternalStorageDirectory();
Expand All @@ -85,70 +92,55 @@ class _MyHomePageState extends State<MyHomePage> {
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
child: ListView(
children: <Widget>[
Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(16.0),
child: RaisedButton(
child: const Text('Get Temporary Directory'),
onPressed: _requestTempDirectory,
),
),
],
),
Expanded(
child: FutureBuilder<Directory>(
future: _tempDirectory, builder: _buildDirectory),
),
Column(
children: <Widget>[
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<Directory>(
future: _appDocumentsDirectory, builder: _buildDirectory),
FutureBuilder<Directory>(
future: _tempDirectory, builder: _buildDirectory),
Padding(
padding: const EdgeInsets.all(16.0),
child: RaisedButton(
child: const Text('Get Application Documents Directory'),
onPressed: _requestAppDocumentsDirectory,
),
),
Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(16.0),
child: RaisedButton(
child: const Text('Get Application Support Directory'),
onPressed: _requestAppSupportDirectory,
),
),
],
FutureBuilder<Directory>(
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<Directory>(
future: _appSupportDirectory, builder: _buildDirectory),
FutureBuilder<Directory>(
future: _appSupportDirectory, builder: _buildDirectory),
Padding(
padding: const EdgeInsets.all(16.0),
child: RaisedButton(
child: const Text('Get Application Library Directory'),
onPressed: _requestAppLibraryDirectory,
),
),
Column(children: <Widget>[
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<Directory>(
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<Directory>(
future: _externalDocumentsDirectory,
builder: _buildDirectory),
),
FutureBuilder<Directory>(
future: _externalDocumentsDirectory, builder: _buildDirectory)
],
),
),
Expand Down
15 changes: 15 additions & 0 deletions packages/path_provider/example/test_driver/path_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Directory> result = getLibraryDirectory();
expect(result, throwsA(isInstanceOf<UnsupportedError>()));
}
});

test('getExternalStorageDirectory', () async {
if (Platform.isIOS) {
final Future<Directory> result = getExternalStorageDirectory();
Expand Down
6 changes: 6 additions & 0 deletions packages/path_provider/ios/Classes/PathProviderPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
} else {
result(path);
}
} else if ([@"getLibraryDirectory" isEqualToString:call.method]) {
result([self getLibraryDirectory]);
} else {
result(FlutterMethodNotImplemented);
}
Expand All @@ -60,4 +62,8 @@ + (NSString*)getApplicationSupportDirectory {
return GetDirectoryOfType(NSApplicationSupportDirectory);
}

+ (NSString*)getLibraryDirectory {
return GetDirectoryOfType(NSLibraryDirectory);
}

@end
28 changes: 28 additions & 0 deletions packages/path_provider/lib/path_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,34 @@ Future<Directory> 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<Directory> getLibraryDirectory() async {
final String path =
await _channel.invokeMethod<String>('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.
///
Expand Down
2 changes: 1 addition & 1 deletion packages/path_provider/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
homepage: https://github.com/flutter/plugins/tree/master/packages/path_provider
version: 1.2.0
version: 1.2.1

flutter:
plugin:
Expand Down
36 changes: 36 additions & 0 deletions packages/path_provider/test/path_provider_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,28 @@ void main() {
expect(directory, isNull);
});

test('getApplicationSupportDirectory test', () async {
response = null;
final Directory directory = await getApplicationSupportDirectory();
expect(
log,
<Matcher>[
isMethodCall('getApplicationSupportDirectory', arguments: null)
],
);
expect(directory, isNull);
});

test('getLibraryDirectory test', () async {
response = null;
final Directory directory = await getLibraryDirectory();
expect(
log,
<Matcher>[isMethodCall('getLibraryDirectory', arguments: null)],
);
expect(directory, isNull);
});

test('TemporaryDirectory path test', () async {
final String fakePath = "/foo/bar/baz";
response = fakePath;
Expand All @@ -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));
});
}