From 80aebe3869030f29bb17c8626cb82180239e486f Mon Sep 17 00:00:00 2001 From: Prateek Sunal Date: Mon, 9 Sep 2024 20:53:14 +0530 Subject: [PATCH] fix(upload-status): show all uploads, even from past --- mobile/lib/models/backup/backup_item.dart | 2 +- .../lib/models/backup/backup_item_status.dart | 2 +- mobile/lib/services/search_service.dart | 1 + .../ui/settings/backup/backup_item_card.dart | 2 +- .../settings/backup/backup_status_screen.dart | 161 ++++++++++-------- mobile/lib/utils/file_uploader.dart | 6 +- 6 files changed, 101 insertions(+), 73 deletions(-) diff --git a/mobile/lib/models/backup/backup_item.dart b/mobile/lib/models/backup/backup_item.dart index 957f0f883c..a1c57a6fe3 100644 --- a/mobile/lib/models/backup/backup_item.dart +++ b/mobile/lib/models/backup/backup_item.dart @@ -7,7 +7,7 @@ class BackupItem { final BackupItemStatus status; final EnteFile file; final int collectionID; - final Completer completer; + final Completer? completer; final Object? error; BackupItem({ diff --git a/mobile/lib/models/backup/backup_item_status.dart b/mobile/lib/models/backup/backup_item_status.dart index 3133903500..75579e77b6 100644 --- a/mobile/lib/models/backup/backup_item_status.dart +++ b/mobile/lib/models/backup/backup_item_status.dart @@ -3,5 +3,5 @@ enum BackupItemStatus { inQueue, inBackground, uploading, - completed, + uploaded, } diff --git a/mobile/lib/services/search_service.dart b/mobile/lib/services/search_service.dart index 88462d7add..f88fbbf86d 100644 --- a/mobile/lib/services/search_service.dart +++ b/mobile/lib/services/search_service.dart @@ -67,6 +67,7 @@ class SearchService { Future> getAllFiles() async { if (_cachedFilesFuture != null) { + _logger.fine("Reading all files from cache"); return _cachedFilesFuture!; } _logger.fine("Reading all files from db"); diff --git a/mobile/lib/ui/settings/backup/backup_item_card.dart b/mobile/lib/ui/settings/backup/backup_item_card.dart index 40be26cbdc..e25bc659d8 100644 --- a/mobile/lib/ui/settings/backup/backup_item_card.dart +++ b/mobile/lib/ui/settings/backup/backup_item_card.dart @@ -123,7 +123,7 @@ class _BackupItemCardState extends State { color: colorScheme.primary700, ), ), - BackupItemStatus.completed => const SizedBox( + BackupItemStatus.uploaded => const SizedBox( width: 24, height: 24, child: Icon( diff --git a/mobile/lib/ui/settings/backup/backup_status_screen.dart b/mobile/lib/ui/settings/backup/backup_status_screen.dart index a32b1e8e09..6f7618173d 100644 --- a/mobile/lib/ui/settings/backup/backup_status_screen.dart +++ b/mobile/lib/ui/settings/backup/backup_status_screen.dart @@ -5,10 +5,12 @@ import "package:collection/collection.dart"; import 'package:flutter/material.dart'; import "package:photos/core/event_bus.dart"; import "package:photos/events/backup_updated_event.dart"; +import "package:photos/events/file_uploaded_event.dart"; import "package:photos/generated/l10n.dart"; import "package:photos/models/backup/backup_item.dart"; -import 'package:photos/ui/components/title_bar_title_widget.dart'; -import 'package:photos/ui/components/title_bar_widget.dart'; +import "package:photos/models/backup/backup_item_status.dart"; +import "package:photos/services/search_service.dart"; +import "package:photos/ui/components/title_bar_widget.dart"; import "package:photos/ui/settings/backup/backup_item_card.dart"; import "package:photos/utils/file_uploader.dart"; @@ -21,12 +23,47 @@ class BackupStatusScreen extends StatefulWidget { class _BackupStatusScreenState extends State { LinkedHashMap items = FileUploader.instance.allBackups; + List? result; @override void initState() { super.initState(); checkBackupUpdatedEvent(); + getAllFiles(); + } + + Future getAllFiles() async { + result = (await SearchService.instance.getAllFiles()) + .map( + (e) { + return BackupItem( + status: BackupItemStatus.uploaded, + file: e, + collectionID: e.collectionID ?? 0, + completer: null, + ); + }, + ) + .toList() + .sorted( + (a, b) => (a.file.uploadedFileID ?? 0) + .compareTo(b.file.uploadedFileID ?? 0), + ); + Bus.instance.on().listen((event) { + setState(() { + result!.insert( + 0, + BackupItem( + status: BackupItemStatus.uploaded, + file: event.file, + collectionID: event.file.collectionID ?? 0, + completer: null, + ), + ); + }); + }); + setState(() {}); } void checkBackupUpdatedEvent() { @@ -42,73 +79,63 @@ class _BackupStatusScreenState extends State { (a, b) => a.status.index.compareTo(b.status.index), ); + final allItems = [ + ...items.where((element) => element.status != BackupItemStatus.uploaded), + if (result != null) ...result!, + ]; + return Scaffold( - body: CustomScrollView( - primary: false, - slivers: [ - TitleBarWidget( - flexibleSpaceTitle: TitleBarTitleWidget( - title: S.of(context).backupStatus, - ), - ), - items.isEmpty - ? SliverFillRemaining( - child: Padding( - padding: const EdgeInsets.symmetric( - horizontal: 60, - vertical: 12, - ), - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Icon( - Icons.cloud_upload_outlined, - color: - Theme.of(context).brightness == Brightness.light - ? const Color.fromRGBO(0, 0, 0, 0.6) - : const Color.fromRGBO(255, 255, 255, 0.6), - ), - const SizedBox(height: 16), - Text( - S.of(context).backupStatusDescription, - textAlign: TextAlign.center, - style: TextStyle( - fontSize: 16, - height: 20 / 16, - color: - Theme.of(context).brightness == Brightness.light - ? const Color(0xFF000000).withOpacity(0.7) - : const Color(0xFFFFFFFF).withOpacity(0.7), - ), - ), - const SizedBox(height: 48), - ], - ), - ), - ) - : SliverList( - delegate: SliverChildBuilderDelegate( - (delegateBuildContext, index) { - return Padding( - padding: const EdgeInsets.symmetric( - vertical: 20, - horizontal: 16, - ), - child: ListView.builder( - shrinkWrap: true, - primary: false, - itemBuilder: (context, index) { - return BackupItemCard(item: items[index]); - }, - itemCount: items.length, - ), - ); - }, - childCount: 1, - ), - ), - ], + appBar: AppBar( + leadingWidth: 32, + title: TitleWidget( + title: S.of(context).backupStatus, + caption: null, + isTitleH2WithoutLeading: false, + ), ), + body: allItems.isEmpty + ? Padding( + padding: const EdgeInsets.symmetric( + horizontal: 60, + vertical: 12, + ), + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Icon( + Icons.cloud_upload_outlined, + color: Theme.of(context).brightness == Brightness.light + ? const Color.fromRGBO(0, 0, 0, 0.6) + : const Color.fromRGBO(255, 255, 255, 0.6), + ), + const SizedBox(height: 16), + Text( + S.of(context).backupStatusDescription, + textAlign: TextAlign.center, + style: TextStyle( + fontSize: 16, + height: 20 / 16, + color: Theme.of(context).brightness == Brightness.light + ? const Color(0xFF000000).withOpacity(0.7) + : const Color(0xFFFFFFFF).withOpacity(0.7), + ), + ), + const SizedBox(height: 48), + ], + ), + ) + : ListView.builder( + padding: const EdgeInsets.symmetric( + vertical: 20, + horizontal: 16, + ), + shrinkWrap: true, + primary: false, + itemBuilder: (context, index) { + return BackupItemCard(item: allItems[index]); + }, + itemCount: allItems.length, + ), ); } } diff --git a/mobile/lib/utils/file_uploader.dart b/mobile/lib/utils/file_uploader.dart index 0b3ee1c4bc..4be9e2d8fd 100644 --- a/mobile/lib/utils/file_uploader.dart +++ b/mobile/lib/utils/file_uploader.dart @@ -318,7 +318,7 @@ class FileUploader { ); _queue.remove(localID)!.completer.complete(uploadedFile); _allBackups[localID] = - _allBackups[localID]!.copyWith(status: BackupItemStatus.completed); + _allBackups[localID]!.copyWith(status: BackupItemStatus.uploaded); Bus.instance.fire(BackupUpdatedEvent(_allBackups)); return uploadedFile; } catch (e) { @@ -446,7 +446,7 @@ class FileUploader { final result = await _tryToUpload(file, collectionID, true); if (isInQueue) { _allBackups[file.localID!] = _allBackups[file.localID]!.copyWith( - status: BackupItemStatus.completed, + status: BackupItemStatus.uploaded, ); Bus.instance.fire(BackupUpdatedEvent(_allBackups)); } @@ -1343,7 +1343,7 @@ class FileUploader { _logger.info("Background upload success detected"); completer?.complete(dbFile); _allBackups[upload.key] = _allBackups[upload.key]! - .copyWith(status: BackupItemStatus.completed); + .copyWith(status: BackupItemStatus.uploaded); } else { _logger.info("Background upload failure detected"); completer?.completeError(SilentlyCancelUploadsError());