Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 27 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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`.
Expand Down Expand Up @@ -266,6 +267,30 @@ Future<Directory?> externalCache() => DocMan.dir.externalCache();
Future<Directory?> 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<void> getAllDirs() async {
final Map<String, String> 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"
};
```

<a name="plugin-cache-cleaner"></a>

#### ♻️ **Plugin Cache cleaner**
Expand Down Expand Up @@ -723,15 +748,14 @@ and can be performed in the background (with isolates or WorkManager).
```dart
Future<DocumentThumbnail?> 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.

> [!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`.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class AppDirsAction(
when (action) {
"path" -> getPath()
"clear" -> clear()
"all" -> getAll()
else -> notImplementedAction(action)
}
}
Expand All @@ -66,6 +67,18 @@ class AppDirsAction(
success(true)
}

private fun getAll() {
val dirs = mutableMapOf<String, String>(
"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,
Expand Down
31 changes: 26 additions & 5 deletions example/lib/src/utils/app_dir.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,20 @@ class AppDir {
///
/// Returns a Future that completes with the [AppDir] instance.
Future<AppDir> 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([
Expand All @@ -61,6 +70,18 @@ class AppDir {
return _instance;
}

void _setDirectoryFromEntry(MapEntry<String, String> 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.
Expand Down
3 changes: 2 additions & 1 deletion lib/src/docman.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions lib/src/methods/app_dirs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ enum AppDir {
Future<bool> clear() async =>
await _onMethodResult<bool>(_args('clear')) ?? false;

/// Get all application directories (paths) at once.
static Future<Map<String, String>?> all() async {
final result = await ActionChannel.instance
.call<Map>('appdirs', {'dir': 'all', 'action': 'all'});
return result?.cast<String, String>();
}

/// Get [Directory] by path
Future<Directory?> asDir() async {
final path = await getPath();
Expand Down
17 changes: 17 additions & 0 deletions lib/src/utils/doc_man_app_dirs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,21 @@ class DocManAppDirs {
///
/// Returns `true` if the directories were cleared successfully, otherwise `false`.
Future<bool> 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<Map<String, String>?> all() => AppDir.all();
}
Loading