diff --git a/README.md b/README.md index 797a84e..204ef45 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,7 @@ All public classes and methods are well-documented. - [Pick visualMedia](#pick-visualmedia) 3. 📂 [**App Directories**](#-app-directories) (🖼️ [*see examples*](#app-directories-examples)) - [Supported app directories](#supported-app-directories) + - [Get all directories at once](#get-all-directories-at-once) - ♻️ [Plugin Cache cleaner](#plugin-cache-cleaner) 4. 🛡️ [**Persisted permissions**](#persisted-permissions) (🖼️ [*see examples*](#persisted-permissions-examples)) - [PersistedPermission data class](#persistedpermission-class) @@ -114,7 +115,7 @@ Add the following dependency to your `pubspec.yaml` file: ```yaml dependencies: - docman: ^1.0.0 + docman: ^1.2.0 ``` Then run ➡️ `flutter pub get`. @@ -266,6 +267,30 @@ Future externalCache() => DocMan.dir.externalCache(); Future filesExt() => DocMan.dir.filesExt(); ``` +#### **Get all directories at once:** + +It is possible to get all directories at once. +The method returns a map with the **directory name** as the `key` and **directory path** as `value`. +Only `cacheExt` & `filesExt` can be empty strings if external storage is not available. + +```dart +///Get all directories at once via helper method +Future getAllDirs() async { + final Map dirs = await DocMan.dir.all(); + + print(dirs); +} + +/// Result Example: +final dirs = { + "cache": "/data/user/0/devdf.plugins.docman_example/cache", + "files": "/data/user/0/devdf.plugins.docman_example/files", + "data": "/data/user/0/devdf.plugins.docman_example/app_flutter", + "cacheExt": "/storage/emulated/0/Android/data/devdf.plugins.docman_example/cache", + "filesExt": "/storage/emulated/0/Android/data/devdf.plugins.docman_example/files" +}; +``` + #### ♻️ **Plugin Cache cleaner** @@ -723,7 +748,7 @@ and can be performed in the background (with isolates or WorkManager). ```dart Future thumbnail(DocumentFile file) => file.thumbnail(width: 256, height: 256, quality: 70); ``` - + > [!NOTE] > ⚠️ Sometimes due to different document providers, thumbnail can have bigger dimensions, than requested. > Some document providers may not support thumbnail generation. @@ -731,7 +756,6 @@ and can be performed in the background (with isolates or WorkManager). > [!TIP] > ⚠️ If file is local image, only `jpg`, `png`, `webp`, `gif` > types are currently supported for thumbnail generation, in all other cases support depends on the document provider. - - `thumbnailFile` `📄` Get the thumbnail of the file as a `File`. diff --git a/android/src/main/kotlin/devdf/plugins/docman/methods/AppDirsAction.kt b/android/src/main/kotlin/devdf/plugins/docman/methods/AppDirsAction.kt index 91eb2d7..9e38058 100644 --- a/android/src/main/kotlin/devdf/plugins/docman/methods/AppDirsAction.kt +++ b/android/src/main/kotlin/devdf/plugins/docman/methods/AppDirsAction.kt @@ -42,6 +42,7 @@ class AppDirsAction( when (action) { "path" -> getPath() "clear" -> clear() + "all" -> getAll() else -> notImplementedAction(action) } } @@ -66,6 +67,18 @@ class AppDirsAction( success(true) } + private fun getAll() { + val dirs = mutableMapOf( + "cache" to plugin.context.cacheDir.path, + "files" to plugin.context.filesDir.path, + "data" to PathUtils.getDataDirectory(plugin.context), + "cacheExt" to (plugin.context.externalCacheDir?.path ?: ""), + "filesExt" to (plugin.context.getExternalFilesDir(null)?.path ?: "") + ) + + success(dirs) + } + private fun dirPathError() { plugin.queue.finishWithError( requestCode, diff --git a/example/lib/src/utils/app_dir.dart b/example/lib/src/utils/app_dir.dart index e44dc12..ed2cc59 100644 --- a/example/lib/src/utils/app_dir.dart +++ b/example/lib/src/utils/app_dir.dart @@ -43,11 +43,20 @@ class AppDir { /// /// Returns a Future that completes with the [AppDir] instance. Future init() async { - cache = (await DocMan.dir.cache())!; - files = (await DocMan.dir.files())!; - data = (await DocMan.dir.data())!; - cacheExt = await DocMan.dir.cacheExt(); - filesExt = await DocMan.dir.filesExt(); + //1. You can get each directory separately, like this: + // cache = (await DocMan.dir.cache())!; + // files = (await DocMan.dir.files())!; + // data = (await DocMan.dir.data())!; + // cacheExt = await DocMan.dir.cacheExt(); + // filesExt = await DocMan.dir.filesExt(); + + //2. Or you can get all directories at once, like this: + final dirs = await DocMan.dir.all(); + //2.1. Set the directories from the map + for (var entry in dirs!.entries) { + _setDirectoryFromEntry(entry); + } + // Initialize the provider directory for future use in the app // By this path, you can add files & dirs to the `Documents Provider` provider = Directory([ @@ -61,6 +70,18 @@ class AppDir { return _instance; } + void _setDirectoryFromEntry(MapEntry entry) => + switch (entry.key) { + 'cache' => cache = Directory(entry.value), + 'files' => files = Directory(entry.value), + 'data' => data = Directory(entry.value), + 'cacheExt' => cacheExt = + entry.value.isEmpty ? null : Directory(entry.value), + 'filesExt' => filesExt = + entry.value.isEmpty ? null : Directory(entry.value), + _ => throw AppDirPathException('Invalid directory entry: ${entry.key}'), + }; + /// Returns a string representation of the [AppDir] instance. /// /// This method prints all the application directories. diff --git a/lib/src/docman.dart b/lib/src/docman.dart index 2240616..8f5d060 100644 --- a/lib/src/docman.dart +++ b/lib/src/docman.dart @@ -18,7 +18,8 @@ class DocMan { /// [DocManPicker] for the picker methods, like `directory`, `documents`, and `files`. static final pick = DocManPicker(); - /// [DocManAppDirs] for the application directories, like `cache`, `files`, `data`, `cacheExt`, and `filesExt`. + /// [DocManAppDirs] for the application directories, like `cache`, `files`, `data`, `cacheExt`, and `filesExt`, + /// or get all directories at once with `all` method. static final dir = DocManAppDirs(); /// [DocManPermissionManager] for the persisted permission manager. diff --git a/lib/src/methods/app_dirs.dart b/lib/src/methods/app_dirs.dart index fd9526f..a2fc3d2 100644 --- a/lib/src/methods/app_dirs.dart +++ b/lib/src/methods/app_dirs.dart @@ -50,6 +50,13 @@ enum AppDir { Future clear() async => await _onMethodResult(_args('clear')) ?? false; + /// Get all application directories (paths) at once. + static Future?> all() async { + final result = await ActionChannel.instance + .call('appdirs', {'dir': 'all', 'action': 'all'}); + return result?.cast(); + } + /// Get [Directory] by path Future asDir() async { final path = await getPath(); diff --git a/lib/src/utils/doc_man_app_dirs.dart b/lib/src/utils/doc_man_app_dirs.dart index aa85682..8325ba4 100644 --- a/lib/src/utils/doc_man_app_dirs.dart +++ b/lib/src/utils/doc_man_app_dirs.dart @@ -48,4 +48,21 @@ class DocManAppDirs { /// /// Returns `true` if the directories were cleared successfully, otherwise `false`. Future clearCache() => AppDir.cache.clear(); + + /// Get all application directories (paths) at once. + /// + /// Returns a map of all the app directories. + /// Only the values of `cacheExt` & `filesExt` can be empty Strings. + /// + /// Result Example: + /// ```dart + /// { + /// "cache": "/data/user/0/devdf.plugins.docman_example/cache", + /// "files": "/data/user/0/devdf.plugins.docman_example/files", + /// "data": "/data/user/0/devdf.plugins.docman_example/app_flutter", + /// "cacheExt": "/storage/emulated/0/Android/data/devdf.plugins.docman_example/cache", + /// "filesExt": "/storage/emulated/0/Android/data/devdf.plugins.docman_example/files" + /// } + /// ``` + Future?> all() => AppDir.all(); }