Skip to content

Commit

Permalink
fix(backup): attach reason of error, use ListView.builder, use upload…
Browse files Browse the repository at this point in the history
… instead of forceUpload
  • Loading branch information
prateekmedia authored and ua741 committed Aug 29, 2024
1 parent 5662661 commit 325871f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 28 deletions.
9 changes: 7 additions & 2 deletions mobile/lib/models/backup/backup_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,35 @@ class BackupItem {
final EnteFile file;
final int collectionID;
final Completer<EnteFile> completer;
final Object? error;

BackupItem({
required this.status,
required this.file,
required this.collectionID,
required this.completer,
this.error,
});

BackupItem copyWith({
BackupItemStatus? status,
EnteFile? file,
int? collectionID,
Completer<EnteFile>? completer,
Object? error,
}) {
return BackupItem(
status: status ?? this.status,
file: file ?? this.file,
collectionID: collectionID ?? this.collectionID,
completer: completer ?? this.completer,
error: error ?? this.error,
);
}

@override
String toString() {
return 'BackupItem(status: $status, file: $file, collectionID: $collectionID)';
return 'BackupItem(status: $status, file: $file, collectionID: $collectionID, error: $error)';
}

@override
Expand All @@ -42,7 +46,8 @@ class BackupItem {
return other.status == status &&
other.file == file &&
other.collectionID == collectionID &&
other.completer == completer;
other.completer == completer &&
other.error == error;
}

@override
Expand Down
10 changes: 5 additions & 5 deletions mobile/lib/ui/settings/backup/backup_item_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,17 @@ class _BackupItemCardState extends State<BackupItemCard> {
color: Color(0xFFFDB816),
),
onPressed: () async {
await FileUploader.instance.forceUpload(
await FileUploader.instance.upload(
widget.item.file,
widget.item.collectionID,
);
},
),
BackupItemStatus.inBackground => SizedBox(
width: 24,
height: 24,
child: Icon(
Icons.lock_reset,
width: 16,
height: 16,
child: CircularProgressIndicator(
strokeWidth: 2.0,
color: Theme.of(context).brightness == Brightness.light
? const Color.fromRGBO(0, 0, 0, .6)
: const Color.fromRGBO(255, 255, 255, .6),
Expand Down
13 changes: 7 additions & 6 deletions mobile/lib/ui/settings/backup/backup_status_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,13 @@ class _BackupStatusScreenState extends State<BackupStatusScreen> {
vertical: 20,
horizontal: 16,
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
for (final item in items)
BackupItemCard(item: item),
],
child: ListView.builder(
shrinkWrap: true,
primary: false,
itemBuilder: (context, index) {
return BackupItemCard(item: items[index]);
},
itemCount: items.length,
),
);
},
Expand Down
44 changes: 29 additions & 15 deletions mobile/lib/utils/file_uploader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ class FileUploader {
_queue.remove(id)?.completer.completeError(reason);
_allBackups[id] = _allBackups[id]!.copyWith(
status: BackupItemStatus.retry,
error: reason,
);
Bus.instance.fire(BackupUpdatedEvent(_allBackups));
}
Expand All @@ -243,8 +244,8 @@ class FileUploader {
});
for (final id in uploadsToBeRemoved) {
_queue.remove(id)?.completer.completeError(reason);
_allBackups[id] =
_allBackups[id]!.copyWith(status: BackupItemStatus.retry);
_allBackups[id] = _allBackups[id]!
.copyWith(status: BackupItemStatus.retry, error: reason);
Bus.instance.fire(BackupUpdatedEvent(_allBackups));
}
_logger.info(
Expand Down Expand Up @@ -283,6 +284,10 @@ class FileUploader {
}
if (pendingEntry != null) {
pendingEntry.status = UploadStatus.inProgress;
_allBackups[pendingEntry.file.localID!] =
_allBackups[pendingEntry.file.localID]!
.copyWith(status: BackupItemStatus.uploading);
Bus.instance.fire(BackupUpdatedEvent(_allBackups));
_encryptAndUploadFileToCollection(
pendingEntry.file,
pendingEntry.collectionID,
Expand Down Expand Up @@ -314,6 +319,7 @@ class FileUploader {
_queue.remove(localID)!.completer.complete(uploadedFile);
_allBackups[localID] =
_allBackups[localID]!.copyWith(status: BackupItemStatus.completed);
Bus.instance.fire(BackupUpdatedEvent(_allBackups));
return uploadedFile;
} catch (e) {
if (e is LockAlreadyAcquiredError) {
Expand All @@ -324,8 +330,8 @@ class FileUploader {
return _queue[localID]!.completer.future;
} else {
_queue.remove(localID)!.completer.completeError(e);
_allBackups[localID] =
_allBackups[localID]!.copyWith(status: BackupItemStatus.retry);
_allBackups[localID] = _allBackups[localID]!
.copyWith(status: BackupItemStatus.retry, error: e);
Bus.instance.fire(BackupUpdatedEvent(_allBackups));
return null;
}
Expand Down Expand Up @@ -435,18 +441,24 @@ class FileUploader {

Future<EnteFile> forceUpload(EnteFile file, int collectionID) async {
_hasInitiatedForceUpload = true;
final isInQueue = _allBackups[file.localID!] != null;
try {
final result = await _tryToUpload(file, collectionID, true);
_allBackups[file.localID!] = _allBackups[file.localID]!.copyWith(
status: BackupItemStatus.completed,
);
Bus.instance.fire(BackupUpdatedEvent(_allBackups));
if (isInQueue) {
_allBackups[file.localID!] = _allBackups[file.localID]!.copyWith(
status: BackupItemStatus.completed,
);
Bus.instance.fire(BackupUpdatedEvent(_allBackups));
}
return result;
} catch (_) {
_allBackups[file.localID!] = _allBackups[file.localID]!.copyWith(
status: BackupItemStatus.retry,
);
Bus.instance.fire(BackupUpdatedEvent(_allBackups));
} catch (error) {
if (isInQueue) {
_allBackups[file.localID!] = _allBackups[file.localID]!.copyWith(
status: BackupItemStatus.retry,
error: error,
);
Bus.instance.fire(BackupUpdatedEvent(_allBackups));
}
rethrow;
}
}
Expand Down Expand Up @@ -1335,8 +1347,10 @@ class FileUploader {
} else {
_logger.info("Background upload failure detected");
completer?.completeError(SilentlyCancelUploadsError());
_allBackups[upload.key] =
_allBackups[upload.key]!.copyWith(status: BackupItemStatus.retry);
_allBackups[upload.key] = _allBackups[upload.key]!.copyWith(
status: BackupItemStatus.retry,
error: SilentlyCancelUploadsError(),
);
}
Bus.instance.fire(BackupUpdatedEvent(_allBackups));
}
Expand Down

0 comments on commit 325871f

Please sign in to comment.