2
2
import 'dart:convert' ;
3
3
4
4
import 'package:background_downloader/background_downloader.dart' ;
5
+ import 'package:collection/collection.dart' ;
5
6
import 'package:flutter/cupertino.dart' ;
6
7
import 'package:hooks_riverpod/hooks_riverpod.dart' ;
7
- import 'package:immich_mobile/constants/constants.dart' ;
8
+
8
9
import 'package:immich_mobile/domain/utils/background_sync.dart' ;
9
10
import 'package:immich_mobile/providers/background_sync.provider.dart' ;
10
-
11
11
import 'package:immich_mobile/services/exp_backup.service.dart' ;
12
12
import 'package:immich_mobile/services/upload.service.dart' ;
13
13
14
+ class ExpUploadStatus {
15
+ final String taskId;
16
+ final String filename;
17
+ final double progress;
18
+ ExpUploadStatus ({
19
+ required this .taskId,
20
+ required this .filename,
21
+ required this .progress,
22
+ });
23
+
24
+ ExpUploadStatus copyWith ({
25
+ String ? taskId,
26
+ String ? filename,
27
+ double ? progress,
28
+ }) {
29
+ return ExpUploadStatus (
30
+ taskId: taskId ?? this .taskId,
31
+ filename: filename ?? this .filename,
32
+ progress: progress ?? this .progress,
33
+ );
34
+ }
35
+
36
+ Map <String , dynamic > toMap () {
37
+ return < String , dynamic > {
38
+ 'taskId' : taskId,
39
+ 'filename' : filename,
40
+ 'progress' : progress,
41
+ };
42
+ }
43
+
44
+ factory ExpUploadStatus .fromMap (Map <String , dynamic > map) {
45
+ return ExpUploadStatus (
46
+ taskId: map['taskId' ] as String ,
47
+ filename: map['filename' ] as String ,
48
+ progress: map['progress' ] as double ,
49
+ );
50
+ }
51
+
52
+ String toJson () => json.encode (toMap ());
53
+
54
+ factory ExpUploadStatus .fromJson (String source) =>
55
+ ExpUploadStatus .fromMap (json.decode (source) as Map <String , dynamic >);
56
+
57
+ @override
58
+ String toString () =>
59
+ 'ExpUploadStatus(taskId: $taskId , filename: $filename , progress: $progress )' ;
60
+
61
+ @override
62
+ bool operator == (covariant ExpUploadStatus other) {
63
+ if (identical (this , other)) return true ;
64
+
65
+ return other.taskId == taskId &&
66
+ other.filename == filename &&
67
+ other.progress == progress;
68
+ }
69
+
70
+ @override
71
+ int get hashCode => taskId.hashCode ^ filename.hashCode ^ progress.hashCode;
72
+ }
73
+
14
74
class ExpBackupState {
15
75
final int totalCount;
16
76
final int backupCount;
17
77
final int remainderCount;
78
+ final Map <String , ExpUploadStatus > uploadItems;
18
79
19
80
ExpBackupState ({
20
81
required this .totalCount,
21
82
required this .backupCount,
22
83
required this .remainderCount,
84
+ required this .uploadItems,
23
85
});
24
86
25
87
ExpBackupState copyWith ({
26
88
int ? totalCount,
27
89
int ? backupCount,
28
90
int ? remainderCount,
91
+ Map <String , ExpUploadStatus >? uploadItems,
29
92
}) {
30
93
return ExpBackupState (
31
94
totalCount: totalCount ?? this .totalCount,
32
95
backupCount: backupCount ?? this .backupCount,
33
96
remainderCount: remainderCount ?? this .remainderCount,
97
+ uploadItems: uploadItems ?? this .uploadItems,
34
98
);
35
99
}
36
100
@@ -39,6 +103,7 @@ class ExpBackupState {
39
103
'totalCount' : totalCount,
40
104
'backupCount' : backupCount,
41
105
'remainderCount' : remainderCount,
106
+ 'uploadItems' : uploadItems,
42
107
};
43
108
}
44
109
@@ -47,6 +112,14 @@ class ExpBackupState {
47
112
totalCount: map['totalCount' ] as int ,
48
113
backupCount: map['backupCount' ] as int ,
49
114
remainderCount: map['remainderCount' ] as int ,
115
+ uploadItems: Map <String , ExpUploadStatus >.from (
116
+ (map['uploadItems' ] as Map <String , dynamic >).map (
117
+ (key, value) => MapEntry (
118
+ key,
119
+ ExpUploadStatus .fromMap (value as Map <String , dynamic >),
120
+ ),
121
+ ),
122
+ ),
50
123
);
51
124
}
52
125
@@ -56,21 +129,28 @@ class ExpBackupState {
56
129
ExpBackupState .fromMap (json.decode (source) as Map <String , dynamic >);
57
130
58
131
@override
59
- String toString () =>
60
- 'ExpBackupState(totalCount: $totalCount , backupCount: $backupCount , remainderCount: $remainderCount )' ;
132
+ String toString () {
133
+ return 'ExpBackupState(totalCount: $totalCount , backupCount: $backupCount , remainderCount: $remainderCount , uploadItems: $uploadItems )' ;
134
+ }
61
135
62
136
@override
63
137
bool operator == (covariant ExpBackupState other) {
64
138
if (identical (this , other)) return true ;
139
+ final mapEquals = const DeepCollectionEquality ().equals;
65
140
66
141
return other.totalCount == totalCount &&
67
142
other.backupCount == backupCount &&
68
- other.remainderCount == remainderCount;
143
+ other.remainderCount == remainderCount &&
144
+ mapEquals (other.uploadItems, uploadItems);
69
145
}
70
146
71
147
@override
72
- int get hashCode =>
73
- totalCount.hashCode ^ backupCount.hashCode ^ remainderCount.hashCode;
148
+ int get hashCode {
149
+ return totalCount.hashCode ^
150
+ backupCount.hashCode ^
151
+ remainderCount.hashCode ^
152
+ uploadItems.hashCode;
153
+ }
74
154
}
75
155
76
156
final expBackupProvider =
@@ -92,6 +172,7 @@ class ExpBackupNotifier extends StateNotifier<ExpBackupState> {
92
172
totalCount: 0 ,
93
173
backupCount: 0 ,
94
174
remainderCount: 0 ,
175
+ uploadItems: {},
95
176
),
96
177
) {
97
178
{
@@ -120,8 +201,6 @@ class ExpBackupNotifier extends StateNotifier<ExpBackupState> {
120
201
remainderCount: state.remainderCount - 1 ,
121
202
);
122
203
123
- // TODO: find a better place to call this.
124
- _backgroundSyncManager.syncRemote ();
125
204
break ;
126
205
127
206
default :
@@ -130,10 +209,30 @@ class ExpBackupNotifier extends StateNotifier<ExpBackupState> {
130
209
}
131
210
132
211
void _taskProgressCallback (TaskProgressUpdate update) {
133
- debugPrint ("[_taskProgressCallback] $update " );
212
+ final uploadStatus = ExpUploadStatus (
213
+ taskId: update.task.taskId,
214
+ filename: update.task.displayName,
215
+ progress: update.progress,
216
+ );
217
+
218
+ state = state.copyWith (
219
+ uploadItems: {
220
+ for (final entry in state.uploadItems.entries)
221
+ if (entry.key == update.task.taskId)
222
+ entry.key: uploadStatus
223
+ else
224
+ entry.key: entry.value,
225
+ if (! state.uploadItems.containsKey (update.task.taskId))
226
+ update.task.taskId: uploadStatus,
227
+ },
228
+ );
229
+
230
+ print (update.task.taskId);
134
231
}
135
232
136
233
Future <void > getBackupStatus () async {
234
+ await _backgroundSyncManager.syncRemote ();
235
+
137
236
final [totalCount, backupCount, remainderCount] = await Future .wait ([
138
237
_backupService.getTotalCount (),
139
238
_backupService.getBackupCount (),
@@ -152,7 +251,6 @@ class ExpBackupNotifier extends StateNotifier<ExpBackupState> {
152
251
}
153
252
154
253
Future <void > cancel () async {
155
- await _uploadService.cancel ();
156
- debugPrint ("Cancel uploads" );
254
+ await _backupService.cancel ();
157
255
}
158
256
}
0 commit comments