Skip to content

Commit 43c898e

Browse files
authored
- Feature: method all for directories (#10)
- Updated README.md Signed-off-by: Alex Yackers <7115964+yackers@users.noreply.github.com>
1 parent 4152f07 commit 43c898e

6 files changed

Lines changed: 92 additions & 9 deletions

File tree

README.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ All public classes and methods are well-documented.
8383
- [Pick visualMedia](#pick-visualmedia)
8484
3. 📂 [**App Directories**](#-app-directories) (🖼️ [*see examples*](#app-directories-examples))
8585
- [Supported app directories](#supported-app-directories)
86+
- [Get all directories at once](#get-all-directories-at-once)
8687
- ♻️ [Plugin Cache cleaner](#plugin-cache-cleaner)
8788
4. 🛡️ [**Persisted permissions**](#persisted-permissions) (🖼️ [*see examples*](#persisted-permissions-examples))
8889
- [PersistedPermission data class](#persistedpermission-class)
@@ -114,7 +115,7 @@ Add the following dependency to your `pubspec.yaml` file:
114115

115116
```yaml
116117
dependencies:
117-
docman: ^1.0.0
118+
docman: ^1.2.0
118119
```
119120
120121
Then run ➡️ `flutter pub get`.
@@ -266,6 +267,30 @@ Future<Directory?> externalCache() => DocMan.dir.externalCache();
266267
Future<Directory?> filesExt() => DocMan.dir.filesExt();
267268
```
268269

270+
#### **Get all directories at once:**
271+
272+
It is possible to get all directories at once.
273+
The method returns a map with the **directory name** as the `key` and **directory path** as `value`.
274+
Only `cacheExt` & `filesExt` can be empty strings if external storage is not available.
275+
276+
```dart
277+
///Get all directories at once via helper method
278+
Future<void> getAllDirs() async {
279+
final Map<String, String> dirs = await DocMan.dir.all();
280+
281+
print(dirs);
282+
}
283+
284+
/// Result Example:
285+
final dirs = {
286+
"cache": "/data/user/0/devdf.plugins.docman_example/cache",
287+
"files": "/data/user/0/devdf.plugins.docman_example/files",
288+
"data": "/data/user/0/devdf.plugins.docman_example/app_flutter",
289+
"cacheExt": "/storage/emulated/0/Android/data/devdf.plugins.docman_example/cache",
290+
"filesExt": "/storage/emulated/0/Android/data/devdf.plugins.docman_example/files"
291+
};
292+
```
293+
269294
<a name="plugin-cache-cleaner"></a>
270295

271296
#### ♻️ **Plugin Cache cleaner**
@@ -723,15 +748,14 @@ and can be performed in the background (with isolates or WorkManager).
723748
```dart
724749
Future<DocumentThumbnail?> thumbnail(DocumentFile file) => file.thumbnail(width: 256, height: 256, quality: 70);
725750
```
726-
751+
727752
> [!NOTE]
728753
> ⚠️ Sometimes due to different document providers, thumbnail can have bigger dimensions, than requested.
729754
> Some document providers may not support thumbnail generation.
730755
731756
> [!TIP]
732757
> ⚠️ If file is local image, only `jpg`, `png`, `webp`, `gif`
733758
> types are currently supported for thumbnail generation, in all other cases support depends on the document provider.
734-
735759
736760
- `thumbnailFile` `📄` Get the thumbnail of the file as a `File`.
737761

android/src/main/kotlin/devdf/plugins/docman/methods/AppDirsAction.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class AppDirsAction(
4242
when (action) {
4343
"path" -> getPath()
4444
"clear" -> clear()
45+
"all" -> getAll()
4546
else -> notImplementedAction(action)
4647
}
4748
}
@@ -66,6 +67,18 @@ class AppDirsAction(
6667
success(true)
6768
}
6869

70+
private fun getAll() {
71+
val dirs = mutableMapOf<String, String>(
72+
"cache" to plugin.context.cacheDir.path,
73+
"files" to plugin.context.filesDir.path,
74+
"data" to PathUtils.getDataDirectory(plugin.context),
75+
"cacheExt" to (plugin.context.externalCacheDir?.path ?: ""),
76+
"filesExt" to (plugin.context.getExternalFilesDir(null)?.path ?: "")
77+
)
78+
79+
success(dirs)
80+
}
81+
6982
private fun dirPathError() {
7083
plugin.queue.finishWithError(
7184
requestCode,

example/lib/src/utils/app_dir.dart

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,20 @@ class AppDir {
4343
///
4444
/// Returns a Future that completes with the [AppDir] instance.
4545
Future<AppDir> init() async {
46-
cache = (await DocMan.dir.cache())!;
47-
files = (await DocMan.dir.files())!;
48-
data = (await DocMan.dir.data())!;
49-
cacheExt = await DocMan.dir.cacheExt();
50-
filesExt = await DocMan.dir.filesExt();
46+
//1. You can get each directory separately, like this:
47+
// cache = (await DocMan.dir.cache())!;
48+
// files = (await DocMan.dir.files())!;
49+
// data = (await DocMan.dir.data())!;
50+
// cacheExt = await DocMan.dir.cacheExt();
51+
// filesExt = await DocMan.dir.filesExt();
52+
53+
//2. Or you can get all directories at once, like this:
54+
final dirs = await DocMan.dir.all();
55+
//2.1. Set the directories from the map
56+
for (var entry in dirs!.entries) {
57+
_setDirectoryFromEntry(entry);
58+
}
59+
5160
// Initialize the provider directory for future use in the app
5261
// By this path, you can add files & dirs to the `Documents Provider`
5362
provider = Directory([
@@ -61,6 +70,18 @@ class AppDir {
6170
return _instance;
6271
}
6372

73+
void _setDirectoryFromEntry(MapEntry<String, String> entry) =>
74+
switch (entry.key) {
75+
'cache' => cache = Directory(entry.value),
76+
'files' => files = Directory(entry.value),
77+
'data' => data = Directory(entry.value),
78+
'cacheExt' => cacheExt =
79+
entry.value.isEmpty ? null : Directory(entry.value),
80+
'filesExt' => filesExt =
81+
entry.value.isEmpty ? null : Directory(entry.value),
82+
_ => throw AppDirPathException('Invalid directory entry: ${entry.key}'),
83+
};
84+
6485
/// Returns a string representation of the [AppDir] instance.
6586
///
6687
/// This method prints all the application directories.

lib/src/docman.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ class DocMan {
1818
/// [DocManPicker] for the picker methods, like `directory`, `documents`, and `files`.
1919
static final pick = DocManPicker();
2020

21-
/// [DocManAppDirs] for the application directories, like `cache`, `files`, `data`, `cacheExt`, and `filesExt`.
21+
/// [DocManAppDirs] for the application directories, like `cache`, `files`, `data`, `cacheExt`, and `filesExt`,
22+
/// or get all directories at once with `all` method.
2223
static final dir = DocManAppDirs();
2324

2425
/// [DocManPermissionManager] for the persisted permission manager.

lib/src/methods/app_dirs.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ enum AppDir {
5050
Future<bool> clear() async =>
5151
await _onMethodResult<bool>(_args('clear')) ?? false;
5252

53+
/// Get all application directories (paths) at once.
54+
static Future<Map<String, String>?> all() async {
55+
final result = await ActionChannel.instance
56+
.call<Map>('appdirs', {'dir': 'all', 'action': 'all'});
57+
return result?.cast<String, String>();
58+
}
59+
5360
/// Get [Directory] by path
5461
Future<Directory?> asDir() async {
5562
final path = await getPath();

lib/src/utils/doc_man_app_dirs.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,21 @@ class DocManAppDirs {
4848
///
4949
/// Returns `true` if the directories were cleared successfully, otherwise `false`.
5050
Future<bool> clearCache() => AppDir.cache.clear();
51+
52+
/// Get all application directories (paths) at once.
53+
///
54+
/// Returns a map of all the app directories.
55+
/// Only the values of `cacheExt` & `filesExt` can be empty Strings.
56+
///
57+
/// Result Example:
58+
/// ```dart
59+
/// {
60+
/// "cache": "/data/user/0/devdf.plugins.docman_example/cache",
61+
/// "files": "/data/user/0/devdf.plugins.docman_example/files",
62+
/// "data": "/data/user/0/devdf.plugins.docman_example/app_flutter",
63+
/// "cacheExt": "/storage/emulated/0/Android/data/devdf.plugins.docman_example/cache",
64+
/// "filesExt": "/storage/emulated/0/Android/data/devdf.plugins.docman_example/files"
65+
/// }
66+
/// ```
67+
Future<Map<String, String>?> all() => AppDir.all();
5168
}

0 commit comments

Comments
 (0)