Line data Source code
1 : import 'package:flutter/foundation.dart';
2 :
3 : import '../worker.dart';
4 :
5 : /// Target shared storage location for [MoveToSharedStorageWorker].
6 : enum SharedStorageType {
7 : /// Public Downloads folder.
8 : ///
9 : /// Android API 29+: `MediaStore.Downloads`.
10 : /// Android API 28−: `Environment.DIRECTORY_DOWNLOADS`.
11 : /// iOS: app's `Documents` directory (accessible via Files app).
12 : downloads,
13 :
14 : /// Device photo library / camera roll.
15 : ///
16 : /// Android: `MediaStore.Images` (API 29+) or `DIRECTORY_PICTURES` (API 28−).
17 : /// iOS: `PHPhotoLibrary` — requires `NSPhotoLibraryAddUsageDescription` in Info.plist.
18 : photos,
19 :
20 : /// Public Music folder / audio library.
21 : ///
22 : /// Android: `MediaStore.Audio` (API 29+) or `DIRECTORY_MUSIC` (API 28−).
23 : /// iOS: Falls back to `Documents` directory (MPMediaLibrary write is not supported).
24 : music,
25 :
26 : /// Public Video folder / video library.
27 : ///
28 : /// Android: `MediaStore.Video` (API 29+) or `DIRECTORY_MOVIES` (API 28−).
29 : /// iOS: `PHPhotoLibrary` (supports both image and video files).
30 : video,
31 : }
32 :
33 : /// Move a file from app-private storage to a shared / public location.
34 : ///
35 : /// On Android this uses `MediaStore` (API 29+) or the legacy
36 : /// `Environment.getExternalStoragePublicDirectory` (API 28−).
37 : ///
38 : /// On iOS the `downloads` and `music` types copy the file to the app's
39 : /// `Documents` directory (accessible via the Files app). The `photos` and
40 : /// `video` types save to `PHPhotoLibrary`.
41 : ///
42 : /// Example:
43 : /// ```dart
44 : /// await NativeWorkManager.enqueue(
45 : /// taskId: 'save-photo',
46 : /// trigger: const TaskTrigger.oneTime(),
47 : /// worker: NativeWorker.moveToSharedStorage(
48 : /// sourcePath: '/path/to/download/photo.jpg',
49 : /// storageType: SharedStorageType.photos,
50 : /// ),
51 : /// constraints: const Constraints(),
52 : /// );
53 : /// ```
54 : @immutable
55 : final class MoveToSharedStorageWorker extends Worker {
56 3 : const MoveToSharedStorageWorker({
57 : required this.sourcePath,
58 : required this.storageType,
59 : this.fileName,
60 : this.mimeType,
61 : this.subDir,
62 : });
63 :
64 : /// Absolute path to the source file inside the app sandbox.
65 : final String sourcePath;
66 :
67 : /// Target shared storage bucket.
68 : final SharedStorageType storageType;
69 :
70 : /// Filename to use in the shared storage location.
71 : /// Defaults to the basename of [sourcePath].
72 : final String? fileName;
73 :
74 : /// MIME type hint for MediaStore insertion.
75 : /// Auto-detected from file extension when `null`.
76 : final String? mimeType;
77 :
78 : /// Optional subdirectory within the shared storage location.
79 : /// On Android (API 29+) this sets `MediaStore.MediaColumns.RELATIVE_PATH`.
80 : /// Example: `'MyApp/Camera'` → `Downloads/MyApp/Camera/`.
81 : final String? subDir;
82 :
83 2 : @override
84 : String get workerClassName => 'MoveToSharedStorageWorker';
85 :
86 3 : @override
87 3 : Map<String, dynamic> toMap() => {
88 3 : 'workerType': 'moveToSharedStorage',
89 6 : 'sourcePath': sourcePath,
90 9 : 'storageType': storageType.name,
91 9 : if (fileName != null) 'fileName': fileName,
92 9 : if (mimeType != null) 'mimeType': mimeType,
93 9 : if (subDir != null) 'subDir': subDir,
94 : };
95 : }
|