Skip to content

Commit a71f65e

Browse files
franciscojma86Egor
authored and
Egor
committed
[path_provider] Use platform interface (flutter#2557)
* Add dart file * Rename file * add interface, rename files, add methods to class * Add to method channel, not done * Add dart file * Rename file * Adds method channel test * Remove prints * Fix imports and version * Format and commentas * Space * Rename apis to path and provide only the string from the platform * Add test and remove null * Format * Add export * Add imports * Completed apis * Fix and works * Improve tests * Remove asyncs * Add imports * Completed apis * Fix and works * Update version * Finish test * Make results per test * Fix * Add dependency * Fix typo nad enum * Change interface version * Address comments * Remove mock call * Typo and import * Remove file
1 parent 4bb7b17 commit a71f65e

File tree

5 files changed

+102
-317
lines changed

5 files changed

+102
-317
lines changed

packages/path_provider/path_provider/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.6.3
2+
3+
* Use `path_provider_platform_interface` in core plugin.
4+
15
## 1.6.2
26

37
* Move package contents into `path_provider` for platform federation.

packages/path_provider/path_provider/example/macos/Runner.xcworkspace/contents.xcworkspacedata

Lines changed: 0 additions & 7 deletions
This file was deleted.

packages/path_provider/path_provider/lib/path_provider.dart

Lines changed: 13 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,12 @@
55
import 'dart:async';
66
import 'dart:io' show Directory;
77

8-
import 'package:flutter/services.dart';
9-
import 'package:meta/meta.dart';
10-
import 'package:platform/platform.dart';
8+
import 'package:path_provider_platform_interface/path_provider_platform_interface.dart';
119

12-
const MethodChannel _channel =
13-
MethodChannel('plugins.flutter.io/path_provider');
10+
export 'package:path_provider_platform_interface/path_provider_platform_interface.dart'
11+
show StorageDirectory;
1412

15-
Platform _platform = const LocalPlatform();
16-
17-
/// This API is only exposed for the unit tests. It should not be used by
18-
/// any code outside of the plugin itself.
19-
@visibleForTesting
20-
void setMockPathProviderPlatform(Platform platform) {
21-
_platform = platform;
22-
}
13+
PathProviderPlatform get _platform => PathProviderPlatform.instance;
2314

2415
/// Path to the temporary directory on the device that is not backed up and is
2516
/// suitable for storing caches of downloaded files.
@@ -33,8 +24,7 @@ void setMockPathProviderPlatform(Platform platform) {
3324
///
3425
/// On Android, this uses the `getCacheDir` API on the context.
3526
Future<Directory> getTemporaryDirectory() async {
36-
final String path =
37-
await _channel.invokeMethod<String>('getTemporaryDirectory');
27+
final String path = await _platform.getTemporaryPath();
3828
if (path == null) {
3929
return null;
4030
}
@@ -52,8 +42,7 @@ Future<Directory> getTemporaryDirectory() async {
5242
///
5343
/// On Android, this function uses the `getFilesDir` API on the context.
5444
Future<Directory> getApplicationSupportDirectory() async {
55-
final String path =
56-
await _channel.invokeMethod<String>('getApplicationSupportDirectory');
45+
final String path = await _platform.getApplicationSupportPath();
5746
if (path == null) {
5847
return null;
5948
}
@@ -67,11 +56,7 @@ Future<Directory> getApplicationSupportDirectory() async {
6756
/// On Android, this function throws an [UnsupportedError] as no equivalent
6857
/// path exists.
6958
Future<Directory> getLibraryDirectory() async {
70-
if (_platform.isAndroid) {
71-
throw UnsupportedError('Functionality not available on Android');
72-
}
73-
final String path =
74-
await _channel.invokeMethod<String>('getLibraryDirectory');
59+
final String path = await _platform.getLibraryPath();
7560
if (path == null) {
7661
return null;
7762
}
@@ -88,8 +73,7 @@ Future<Directory> getLibraryDirectory() async {
8873
/// using [getExternalStorageDirectory] instead if data is intended to be visible
8974
/// to the user.
9075
Future<Directory> getApplicationDocumentsDirectory() async {
91-
final String path =
92-
await _channel.invokeMethod<String>('getApplicationDocumentsDirectory');
76+
final String path = await _platform.getApplicationDocumentsPath();
9377
if (path == null) {
9478
return null;
9579
}
@@ -105,11 +89,7 @@ Future<Directory> getApplicationDocumentsDirectory() async {
10589
///
10690
/// On Android this uses the `getExternalFilesDir(null)`.
10791
Future<Directory> getExternalStorageDirectory() async {
108-
if (_platform.isIOS) {
109-
throw UnsupportedError('Functionality not available on iOS');
110-
}
111-
final String path =
112-
await _channel.invokeMethod<String>('getStorageDirectory');
92+
final String path = await _platform.getExternalStoragePath();
11393
if (path == null) {
11494
return null;
11595
}
@@ -130,65 +110,11 @@ Future<Directory> getExternalStorageDirectory() async {
130110
/// On Android this returns Context.getExternalCacheDirs() or
131111
/// Context.getExternalCacheDir() on API levels below 19.
132112
Future<List<Directory>> getExternalCacheDirectories() async {
133-
if (_platform.isIOS) {
134-
throw UnsupportedError('Functionality not available on iOS');
135-
}
136-
final List<String> paths =
137-
await _channel.invokeListMethod<String>('getExternalCacheDirectories');
113+
final List<String> paths = await _platform.getExternalCachePaths();
138114

139115
return paths.map((String path) => Directory(path)).toList();
140116
}
141117

142-
/// Corresponds to constants defined in Androids `android.os.Environment` class.
143-
///
144-
/// https://developer.android.com/reference/android/os/Environment.html#fields_1
145-
enum StorageDirectory {
146-
/// Contains audio files that should be treated as music.
147-
///
148-
/// See https://developer.android.com/reference/android/os/Environment.html#DIRECTORY_MUSIC.
149-
music,
150-
151-
/// Contains audio files that should be treated as podcasts.
152-
///
153-
/// See https://developer.android.com/reference/android/os/Environment.html#DIRECTORY_PODCASTS.
154-
podcasts,
155-
156-
/// Contains audio files that should be treated as ringtones.
157-
///
158-
/// See https://developer.android.com/reference/android/os/Environment.html#DIRECTORY_RINGTONES.
159-
ringtones,
160-
161-
/// Contains audio files that should be treated as alarm sounds.
162-
///
163-
/// See https://developer.android.com/reference/android/os/Environment.html#DIRECTORY_ALARMS.
164-
alarms,
165-
166-
/// Contains audio files that should be treated as notification sounds.
167-
///
168-
/// See https://developer.android.com/reference/android/os/Environment.html#DIRECTORY_NOTIFICATIONS.
169-
notifications,
170-
171-
/// Contains images. See https://developer.android.com/reference/android/os/Environment.html#DIRECTORY_PICTURES.
172-
pictures,
173-
174-
/// Contains movies. See https://developer.android.com/reference/android/os/Environment.html#DIRECTORY_MOVIES.
175-
movies,
176-
177-
/// Contains files of any type that have been downloaded by the user.
178-
///
179-
/// See https://developer.android.com/reference/android/os/Environment.html#DIRECTORY_DOWNLOADS.
180-
downloads,
181-
182-
/// Used to hold both pictures and videos when the device filesystem is
183-
/// treated like a camera's.
184-
///
185-
/// See https://developer.android.com/reference/android/os/Environment.html#DIRECTORY_DCIM.
186-
dcim,
187-
188-
/// Holds user-created documents. See https://developer.android.com/reference/android/os/Environment.html#DIRECTORY_DOCUMENTS.
189-
documents,
190-
}
191-
192118
/// Paths to directories where application specific data can be stored.
193119
/// These paths typically reside on external storage like separate partitions
194120
/// or SD cards. Phones may have multiple storage directories available.
@@ -206,13 +132,8 @@ Future<List<Directory>> getExternalStorageDirectories({
206132
/// how this type translates to Android storage directories.
207133
StorageDirectory type,
208134
}) async {
209-
if (_platform.isIOS) {
210-
throw UnsupportedError('Functionality not available on iOS');
211-
}
212-
final List<String> paths = await _channel.invokeListMethod<String>(
213-
'getExternalStorageDirectories',
214-
<String, dynamic>{'type': type?.index},
215-
);
135+
final List<String> paths =
136+
await _platform.getExternalStoragePaths(type: type);
216137

217138
return paths.map((String path) => Directory(path)).toList();
218139
}
@@ -223,14 +144,7 @@ Future<List<Directory>> getExternalStorageDirectories({
223144
/// On Android and on iOS, this function throws an [UnsupportedError] as no equivalent
224145
/// path exists.
225146
Future<Directory> getDownloadsDirectory() async {
226-
if (_platform.isAndroid) {
227-
throw UnsupportedError('Functionality not available on Android');
228-
}
229-
if (_platform.isIOS) {
230-
throw UnsupportedError('Functionality not available on iOS');
231-
}
232-
final String path =
233-
await _channel.invokeMethod<String>('getDownloadsDirectory');
147+
final String path = await _platform.getDownloadsPath();
234148
if (path == null) {
235149
return null;
236150
}

packages/path_provider/path_provider/pubspec.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: path_provider
22
description: Flutter plugin for getting commonly used locations on the Android &
33
iOS file systems, such as the temp and app data directories.
44
homepage: https://github.com/flutter/plugins/tree/master/packages/path_provider/path_provider
5-
version: 1.6.2
5+
version: 1.6.3
66

77
flutter:
88
plugin:
@@ -16,8 +16,7 @@ flutter:
1616
dependencies:
1717
flutter:
1818
sdk: flutter
19-
platform: ^2.0.0
20-
meta: ^1.0.5
19+
path_provider_platform_interface: ^1.0.1
2120

2221
dev_dependencies:
2322
e2e: ^0.2.1
@@ -28,6 +27,8 @@ dev_dependencies:
2827
test: any
2928
uuid: "^1.0.0"
3029
pedantic: ^1.8.0
30+
mockito: ^4.1.1
31+
plugin_platform_interface: ^1.0.0
3132

3233
environment:
3334
sdk: ">=2.0.0-dev.28.0 <3.0.0"

0 commit comments

Comments
 (0)