From 0e642cd254933e07c47a177ab71ed0547cfd5ca9 Mon Sep 17 00:00:00 2001 From: ashilkn Date: Thu, 22 Aug 2024 16:46:11 +0530 Subject: [PATCH] [mob][photos] Create separate widget for home tab's app bar --- mobile/lib/ui/home/home_app_bar_widget.dart | 135 ++++++++++++++++++++ mobile/lib/ui/home/status_bar_widget.dart | 6 +- mobile/lib/ui/tabs/home_widget.dart | 5 + 3 files changed, 144 insertions(+), 2 deletions(-) create mode 100644 mobile/lib/ui/home/home_app_bar_widget.dart diff --git a/mobile/lib/ui/home/home_app_bar_widget.dart b/mobile/lib/ui/home/home_app_bar_widget.dart new file mode 100644 index 0000000000..5cda20e593 --- /dev/null +++ b/mobile/lib/ui/home/home_app_bar_widget.dart @@ -0,0 +1,135 @@ +import "dart:async"; +import "dart:io"; + +import "package:flutter/material.dart"; +import "package:logging/logging.dart"; +import "package:photo_manager/photo_manager.dart"; +import "package:photos/core/event_bus.dart"; +import "package:photos/events/sync_status_update_event.dart"; +import "package:photos/generated/l10n.dart"; +import "package:photos/services/local_sync_service.dart"; +import "package:photos/theme/text_style.dart"; +import "package:photos/ui/components/buttons/icon_button_widget.dart"; +import "package:photos/ui/home/status_bar_widget.dart"; +import "package:photos/ui/settings/backup/backup_folder_selection_page.dart"; +import "package:photos/utils/dialog_util.dart"; +import "package:photos/utils/navigation_util.dart"; +import "package:photos/utils/photo_manager_util.dart"; + +class HomeAppBarWidget extends StatefulWidget { + const HomeAppBarWidget({super.key}); + + @override + State createState() => _HomeAppBarWidgetState(); +} + +class _HomeAppBarWidgetState extends State { + bool _showStatus = false; + bool _showErrorBanner = false; + + late StreamSubscription _subscription; + final _logger = Logger("HomeAppBarWidget"); + + @override + void initState() { + super.initState(); + _subscription = Bus.instance.on().listen((event) { + _logger.info("Received event " + event.status.toString()); + + if (event.status == SyncStatus.error) { + setState(() { + _showErrorBanner = true; + }); + } else { + setState(() { + _showErrorBanner = false; + }); + } + + if (event.status == SyncStatus.completedFirstGalleryImport || + event.status == SyncStatus.completedBackup) { + Future.delayed(const Duration(milliseconds: 2000), () { + if (mounted) { + setState(() { + _showStatus = false; + }); + } + }); + } else { + setState(() { + _showStatus = true; + }); + } + }); + } + + dispose() { + _subscription.cancel(); + super.dispose(); + } + + @override + AppBar build(BuildContext context) { + return AppBar( + centerTitle: true, + title: _showStatus + ? _showErrorBanner + ? const Text("ente", style: brandStyleMedium) + : const SyncStatusWidget() + : const Text("ente", style: brandStyleMedium), + actions: [ + IconButtonWidget( + icon: Icons.add_photo_alternate_outlined, + iconButtonType: IconButtonType.primary, + onTap: () async { + try { + final PermissionState state = + await requestPhotoMangerPermissions(); + await LocalSyncService.instance.onUpdatePermission(state); + } on Exception catch (e) { + Logger("HomeHeaderWidget").severe( + "Failed to request permission: ${e.toString()}", + e, + ); + } + if (!LocalSyncService.instance.hasGrantedFullPermission()) { + if (Platform.isAndroid) { + await PhotoManager.openSetting(); + } else { + final bool hasGrantedLimit = + LocalSyncService.instance.hasGrantedLimitedPermissions(); + // ignore: unawaited_futures + showChoiceActionSheet( + context, + title: S.of(context).preserveMore, + body: S.of(context).grantFullAccessPrompt, + firstButtonLabel: S.of(context).openSettings, + firstButtonOnTap: () async { + await PhotoManager.openSetting(); + }, + secondButtonLabel: hasGrantedLimit + ? S.of(context).selectMorePhotos + : S.of(context).cancel, + secondButtonOnTap: () async { + if (hasGrantedLimit) { + await PhotoManager.presentLimited(); + } + }, + ); + } + } else { + unawaited( + routeToPage( + context, + BackupFolderSelectionPage( + buttonText: S.of(context).backup, + ), + ), + ); + } + }, + ), + ], + ); + } +} diff --git a/mobile/lib/ui/home/status_bar_widget.dart b/mobile/lib/ui/home/status_bar_widget.dart index 8df1a90242..3815e4cf3a 100644 --- a/mobile/lib/ui/home/status_bar_widget.dart +++ b/mobile/lib/ui/home/status_bar_widget.dart @@ -38,6 +38,8 @@ class _StatusBarWidgetState extends State { @override void initState() { + super.initState(); + _subscription = Bus.instance.on().listen((event) { _logger.info("Received event " + event.status.toString()); if (event.status == SyncStatus.error) { @@ -72,7 +74,6 @@ class _StatusBarWidgetState extends State { setState(() {}); } }); - super.initState(); } @override @@ -142,13 +143,14 @@ class _SyncStatusWidgetState extends State { @override void initState() { + super.initState(); + _subscription = Bus.instance.on().listen((event) { setState(() { _event = event; }); }); _event = SyncService.instance.getLastSyncStatusEvent(); - super.initState(); } @override diff --git a/mobile/lib/ui/tabs/home_widget.dart b/mobile/lib/ui/tabs/home_widget.dart index 3c902ceb1a..dd31847c3e 100644 --- a/mobile/lib/ui/tabs/home_widget.dart +++ b/mobile/lib/ui/tabs/home_widget.dart @@ -41,6 +41,7 @@ import 'package:photos/ui/collections/collection_action_sheet.dart'; import 'package:photos/ui/extents_page_view.dart'; import 'package:photos/ui/home/grant_permissions_widget.dart'; import 'package:photos/ui/home/header_widget.dart'; +import "package:photos/ui/home/home_app_bar_widget.dart"; import 'package:photos/ui/home/home_bottom_nav_bar.dart'; import 'package:photos/ui/home/home_gallery_widget.dart'; import 'package:photos/ui/home/landing_page_widget.dart'; @@ -359,6 +360,10 @@ class _HomeWidgetState extends State { }, ), ), + appBar: const PreferredSize( + preferredSize: const Size.fromHeight(kToolbarHeight), + child: HomeAppBarWidget(), + ), resizeToAvoidBottomInset: false, ), ),