Skip to content

Commit 1a3b9e1

Browse files
committed
Auto upload
1 parent 3d93b3b commit 1a3b9e1

12 files changed

Lines changed: 269 additions & 49 deletions

File tree

l10n/app_en.arb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,25 @@
875875
"settings_autoUploadDestination": "Destination",
876876
"settings_autoUploadDestinationInvalid": "Invalid destination album",
877877
"settings_autoUploadDestinationInfo": "Please select the album or sub-album into which photos and videos will be auto-uploaded.",
878+
"settings_autoUploadPeriod": "Every",
879+
"settings_autoUploadPeriodMinutes": "{count, plural, =1{1 minute} other{{count} minutes}}",
880+
"@settings_autoUploadPeriodMinutes" : {
881+
"placeholders": {
882+
"count": {}
883+
}
884+
},
885+
"settings_autoUploadPeriodHours": "{count, plural, =1{1 hour} other{{count} hours}}",
886+
"@settings_autoUploadPeriodHours" : {
887+
"placeholders": {
888+
"count": {}
889+
}
890+
},
891+
"settings_autoUploadPeriodDays": "{count, plural, =1{1 day} other{{count} days}}",
892+
"@settings_autoUploadPeriodDays" : {
893+
"placeholders": {
894+
"count": {}
895+
}
896+
},
878897
"autoUploadError_Disabled": "The Auto-Upload option is disabled in the app settings.",
879898
"autoUploadError_Failed": "Several transfers failed and the upload queue is on hold. Please check in the app.",
880899

lib/api/images.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ Future<List<XFile>?> downloadImages(
210210
}) async {
211211
String? dirPath = (await getTemporaryDirectory()).path;
212212
if (!cached) {
213-
dirPath = await Preferences.getDownloadDestination;
213+
dirPath = Preferences.getDownloadDestination ?? await pickDirectoryPath();
214214
}
215215

216216
if (dirPath == null) return null;

lib/app.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import 'package:piwigo_ng/views/image/edit_image_page.dart';
1313
import 'package:piwigo_ng/views/image/image_favorites_page.dart';
1414
import 'package:piwigo_ng/views/image/image_search_view_page.dart';
1515
import 'package:piwigo_ng/views/image/image_view_page.dart';
16+
import 'package:piwigo_ng/views/settings/auto_upload_page.dart';
1617
import 'package:piwigo_ng/views/settings/privacy_policy_view_page.dart';
1718
import 'package:piwigo_ng/views/settings/select_language_view_page.dart';
1819
import 'package:piwigo_ng/views/settings/settings_view_page.dart';
@@ -141,6 +142,11 @@ Route<dynamic> generateRoute(RouteSettings settings) {
141142
builder: (_) => UploadStatusPage(),
142143
settings: settings,
143144
);
145+
case AutoUploadPage.routeName:
146+
return MaterialPageRoute(
147+
builder: (_) => AutoUploadPage(),
148+
settings: settings,
149+
);
144150
case ImageViewPage.routeName:
145151
return MaterialPageRoute(
146152
builder: (_) => ImageViewPage(

lib/components/modals/move_or_copy_modal.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import 'package:piwigo_ng/utils/localizations.dart';
77
class MoveOrCopyModal extends StatefulWidget {
88
const MoveOrCopyModal({
99
Key? key,
10-
required this.album,
10+
this.album,
1111
this.isImage = false,
1212
this.onSelected,
1313
this.title,
1414
this.subtitle,
1515
}) : super(key: key);
1616

17-
final AlbumModel album;
17+
final AlbumModel? album;
1818
final bool isImage;
1919
final String? title;
2020
final String? subtitle;
@@ -34,9 +34,9 @@ class _MoveOrCopyModalState extends State<MoveOrCopyModal> {
3434
@override
3535
void initState() {
3636
_albumFuture = getAlbumTree();
37-
List<String> parentAlbums = widget.album.upperCategories.split(',');
37+
List<String> parentAlbums = widget.album?.upperCategories.split(',') ?? [];
3838
_disabledAlbums = [
39-
widget.album.id,
39+
if (widget.album != null) widget.album!.id,
4040
if (parentAlbums.length == 1 || widget.isImage) 0,
4141
if (!widget.isImage && parentAlbums.length > 1) int.parse(parentAlbums[parentAlbums.length - 2]),
4242
];

lib/components/sections/settings_section.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,14 @@ class SettingsSectionItemButton extends StatelessWidget {
159159
this.title,
160160
this.onPressed,
161161
this.disabled = false,
162+
this.expandedChild = false,
162163
this.icon,
163164
}) : super(key: key);
164165

165166
final String? title;
166167
final String? text;
167168
final bool disabled;
169+
final bool expandedChild;
168170
final Function()? onPressed;
169171
final Widget? icon;
170172

@@ -175,12 +177,13 @@ class SettingsSectionItemButton extends StatelessWidget {
175177
child: SettingsSectionItem(
176178
title: title,
177179
disabled: disabled,
180+
expandedChild: expandedChild,
178181
child: Row(
179182
mainAxisAlignment: MainAxisAlignment.end,
180183
children: [
181184
Text(
182-
text ?? "",
183-
textAlign: TextAlign.end,
185+
text ?? '',
186+
overflow: TextOverflow.ellipsis,
184187
style: Theme.of(context).textTheme.bodySmall,
185188
),
186189
Padding(

lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import 'package:flutter/services.dart';
55
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
66
import 'package:piwigo_ng/api/api_client.dart';
77
import 'package:piwigo_ng/app.dart';
8+
import 'package:piwigo_ng/services/auto_upload_manager.dart';
89
import 'package:piwigo_ng/services/notification_service.dart';
910
import 'package:piwigo_ng/services/preferences_service.dart';
10-
import 'package:piwigo_ng/services/work_manager.dart';
1111
import 'package:shared_preferences/shared_preferences.dart';
1212

1313
void main() async {
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import 'dart:io';
2+
3+
import 'package:flutter/material.dart';
4+
import 'package:path_provider/path_provider.dart';
5+
import 'package:piwigo_ng/services/preferences_service.dart';
6+
import 'package:shared_preferences/shared_preferences.dart';
7+
import 'package:workmanager/workmanager.dart';
8+
9+
class AutoUploadManager {
10+
final String taskKey = 'com.piwigo.piwigo_ng.auto_upload';
11+
final String tag = '<auto_upload>';
12+
late Workmanager _manager;
13+
14+
AutoUploadManager() {
15+
_manager = Workmanager();
16+
}
17+
18+
Future<void> endAutoUpload() async {
19+
appPreferences.setBool(
20+
Preferences.autoUploadKey,
21+
false,
22+
);
23+
await _manager.cancelByUniqueName(taskKey);
24+
}
25+
26+
Future<void> startAutoUpload() async {
27+
appPreferences.setBool(
28+
Preferences.autoUploadKey,
29+
true,
30+
);
31+
await _manager.registerPeriodicTask(
32+
taskKey,
33+
taskKey,
34+
frequency: const Duration(minutes: 15),
35+
);
36+
}
37+
38+
Future<Directory?> getUploadDirectory() async {
39+
return await getTemporaryDirectory();
40+
}
41+
}
42+
43+
@pragma('vm:entry-point')
44+
void callbackDispatcher() {
45+
Workmanager().executeTask((task, inputData) async {
46+
final SharedPreferences prefs = await SharedPreferences.getInstance();
47+
debugPrint("Background $task");
48+
debugPrint(prefs.getString('UPLOAD_AUTHOR_NAME') ?? '');
49+
final Directory? appDocDir = await AutoUploadManager().getUploadDirectory();
50+
if (appDocDir == null) return false;
51+
debugPrint(appDocDir.listSync().toString());
52+
// final List<File> files = appDocDir.listSync().whereType<File>().toList();
53+
// List<XFile> uploadFiles = files.map<XFile>((file) => XFile(file.path)).toList();
54+
// final result = await uploadPhotos(uploadFiles, 92);
55+
// debugPrint(result.toString());
56+
return Future.value(true);
57+
});
58+
}
59+
60+
void initializeWorkManager() {
61+
Workmanager().initialize(
62+
callbackDispatcher, // The top level function, aka callbackDispatcher
63+
isInDebugMode: true, // If enabled it will post a notification whenever the task is running. Handy for debugging tasks
64+
);
65+
}

lib/services/preferences_service.dart

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
2-
import 'package:piwigo_ng/api/images.dart';
32
import 'package:piwigo_ng/models/status_model.dart';
43
import 'package:piwigo_ng/utils/settings.dart';
54
import 'package:shared_preferences/shared_preferences.dart';
@@ -103,9 +102,24 @@ class Preferences {
103102
return appPreferences.getDouble(uploadQualityKey) ?? Settings.defaultUploadQuality;
104103
}
105104

105+
static const String autoUploadKey = 'AUTO_UPLOAD';
106+
static bool get getAutoUpload {
107+
return appPreferences.getBool(autoUploadKey) ?? false;
108+
}
109+
110+
static const String autoUploadSourceKey = 'AUTO_UPLOAD_SOURCE';
111+
static String? get getAutoUploadSource {
112+
return appPreferences.getString(autoUploadSourceKey);
113+
}
114+
115+
static const String autoUploadDestinationKey = 'AUTO_UPLOAD_DESTINATION';
116+
static int? get getAutoUploadDestination {
117+
return appPreferences.getInt(autoUploadDestinationKey);
118+
}
119+
106120
static const String downloadDestinationKey = 'DOWNLOAD_DESTINATION';
107-
static Future<String?> get getDownloadDestination async {
108-
return appPreferences.getString(downloadDestinationKey) ?? await pickDirectoryPath();
121+
static String? get getDownloadDestination {
122+
return appPreferences.getString(downloadDestinationKey);
109123
}
110124

111125
static const String downloadNotificationKey = 'DOWNLOAD_NOTIFICATION';

lib/services/work_manager.dart

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

0 commit comments

Comments
 (0)