Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[mob][photos] Create separate widget for home tab's app bar

ashilkn0e642cd25493
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<HomeAppBarWidget> createState() => _HomeAppBarWidgetState();
+}
+
+class _HomeAppBarWidgetState extends State<HomeAppBarWidget> {
+ bool _showStatus = false;
+ bool _showErrorBanner = false;
+
+ late StreamSubscription<SyncStatusUpdate> _subscription;
+ final _logger = Logger("HomeAppBarWidget");
+
+ @override
+ void initState() {
+ super.initState();
+ _subscription = Bus.instance.on<SyncStatusUpdate>().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<StatusBarWidget> {
@override
void initState() {
+ super.initState();
+
_subscription = Bus.instance.on<SyncStatusUpdate>().listen((event) {
_logger.info("Received event " + event.status.toString());
if (event.status == SyncStatus.error) {
@@ -72,7 +74,6 @@ class _StatusBarWidgetState extends State<StatusBarWidget> {
setState(() {});
}
});
- super.initState();
}
@override
@@ -142,13 +143,14 @@ class _SyncStatusWidgetState extends State<SyncStatusWidget> {
@override
void initState() {
+ super.initState();
+
_subscription = Bus.instance.on<SyncStatusUpdate>().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<HomeWidget> {
},
),
),
+ appBar: const PreferredSize(
+ preferredSize: const Size.fromHeight(kToolbarHeight),
+ child: HomeAppBarWidget(),
+ ),
resizeToAvoidBottomInset: false,
),
),