π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Refactor backup flow with enum for different permission handling types
Prateek Sunal2708fe98ad7c
diff --git a/mobile/apps/photos/lib/ui/common/backup_flow_helper.dart b/mobile/apps/photos/lib/ui/common/backup_flow_helper.dartindex 520441d183..e44d1d05e1 100644--- a/mobile/apps/photos/lib/ui/common/backup_flow_helper.dart+++ b/mobile/apps/photos/lib/ui/common/backup_flow_helper.dart@@ -4,7 +4,6 @@ import "dart:io";import "package:flutter/material.dart";import "package:logging/logging.dart";import "package:photo_manager/photo_manager.dart";-import "package:photos/generated/l10n.dart";import "package:photos/l10n/l10n.dart";import "package:photos/service_locator.dart";import "package:photos/ui/home/loading_photos_widget.dart";@@ -12,37 +11,46 @@ 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";+final _logger = Logger("BackupFlowHelper");++/// Defines how the backup entry flow should handle permissions.+enum BackupFlowType {+ /// Full permission flow - request permissions, show dialogs if denied/limited.+ /// Used by: home_header_widget+ fullPermission,++ /// If limited permission, show presentLimited; else go to folder selection.+ /// Used by: start_backup_hook_widget+ limitedOrFolderSelection,++ /// Just navigate to folder selection without permission handling.+ /// Used by: tab_empty_state, backup_section_widget+ folderSelectionOnly,+}+/// Helper to centralize navigation when users choose to add photos/backup.////// [isFirstBackup] indicates if this is the first backup flow.-/// [askPermission] (only when onlyNewPhotos flag is disabled) controls whether-/// to request permissions before navigating.+/// [flowType] determines how permissions are handled (only when onlyNewPhotos+/// flag is disabled).Future<void> handleBackupEntryFlow(BuildContext context, {bool isFirstBackup = false,- bool askPermission = true,+ BackupFlowType flowType = BackupFlowType.fullPermission,}) async {- // When onlyNewPhotos flag is disabled, use the original simple flow+ // When onlyNewPhotos flag is disabled, use the original simple flowsif (!flagService.enableOnlyBackupFuturePhotos) {- await _handleLegacyBackupFlow(context, askPermission: askPermission);- return;- }-- PermissionState state;- try {- // Always fetch fresh permission state from the platform instead of relying- // on cached prefs; this avoids stale reads when users change OS settings.- state = await permissionService.requestPhotoMangerPermissions();- await permissionService.onUpdatePermission(state);- } catch (e, s) {- Logger("BackupEntryFlow").severe(- "Failed to request permission",- e,- s,+ await _handleLegacyBackupFlow(+ context,+ isFirstBackup: isFirstBackup,+ flowType: flowType,);return;}+ final state = await _requestPermissions();+ if (state == null) return;+// Permission denied or restricted - offer to open settingsif (state != PermissionState.authorized && state != PermissionState.limited) {if (!context.mounted) return;@@ -61,19 +69,7 @@ Future<void> handleBackupEntryFlow(// iOS limited permission - offer to expand access or open settingsif (state == PermissionState.limited && Platform.isIOS) {if (!context.mounted) return;- await showChoiceActionSheet(- context,- title: context.l10n.preserveMore,- body: context.l10n.grantFullAccessPrompt,- firstButtonLabel: context.l10n.openSettings,- firstButtonOnTap: () async {- await PhotoManager.openSetting();- },- secondButtonLabel: context.l10n.selectMorePhotos,- secondButtonOnTap: () async {- await PhotoManager.presentLimited();- },- );+ await _showLimitedPermissionSheet(context, hasGrantedLimit: true);// Fall through to folder selection after dialog}@@ -98,58 +94,96 @@ Future<void> handleBackupEntryFlow(}/// Legacy backup flow (pre-onlyNewPhotos feature).-/// Matches the original home_header_widget inline implementation.+/// Handles different flow types matching original widget implementations.Future<void> _handleLegacyBackupFlow(BuildContext context, {- required bool askPermission,+ required bool isFirstBackup,+ required BackupFlowType flowType,}) async {- if (askPermission) {- try {- final PermissionState state =- await permissionService.requestPhotoMangerPermissions();- await permissionService.onUpdatePermission(state);- } on Exception catch (e) {- Logger("HomeHeaderWidget").severe(- "Failed to request permission: ${e.toString()}",- e,+ switch (flowType) {+ case BackupFlowType.folderSelectionOnly:+ // tab_empty_state, backup_section_widget: just navigate+ await routeToPage(+ context,+ BackupFolderSelectionPage(+ isFirstBackup: isFirstBackup,+ ),);- }- }- if (!permissionService.hasGrantedFullPermission()) {- if (!context.mounted) return;- if (Platform.isAndroid) {- await PhotoManager.openSetting();- } else {- final bool hasGrantedLimit =- permissionService.hasGrantedLimitedPermissions();- // ignore: unawaited_futures- showChoiceActionSheet(- context,- title: AppLocalizations.of(context).preserveMore,- body: AppLocalizations.of(context).grantFullAccessPrompt,- firstButtonLabel: AppLocalizations.of(context).openSettings,- firstButtonOnTap: () async {+ case BackupFlowType.limitedOrFolderSelection:+ // start_backup_hook_widget: presentLimited or folder selection+ if (permissionService.hasGrantedLimitedPermissions()) {+ unawaited(PhotoManager.presentLimited());+ } else {+ // ignore: unawaited_futures+ routeToPage(+ context,+ BackupFolderSelectionPage(+ isFirstBackup: isFirstBackup,+ ),+ );+ }++ case BackupFlowType.fullPermission:+ // home_header_widget: full permission flow+ await _requestPermissions();++ if (!permissionService.hasGrantedFullPermission()) {+ if (!context.mounted) return;+ if (Platform.isAndroid) {await PhotoManager.openSetting();- },- secondButtonLabel: hasGrantedLimit- ? AppLocalizations.of(context).selectMorePhotos- : AppLocalizations.of(context).cancel,- secondButtonOnTap: () async {- if (hasGrantedLimit) {- await PhotoManager.presentLimited();- }- },- );- }- } else {- unawaited(- routeToPage(- context,- const BackupFolderSelectionPage(- isFirstBackup: false,- ),- ),- );+ } else {+ final bool hasGrantedLimit =+ permissionService.hasGrantedLimitedPermissions();+ // ignore: unawaited_futures+ _showLimitedPermissionSheet(context, hasGrantedLimit: hasGrantedLimit);+ }+ } else {+ unawaited(+ routeToPage(+ context,+ BackupFolderSelectionPage(+ isFirstBackup: isFirstBackup,+ ),+ ),+ );+ }}}++/// Requests photo permissions and updates the permission service.+/// Returns the fresh [PermissionState] or null if an error occurred.+Future<PermissionState?> _requestPermissions() async {+ try {+ final state = await permissionService.requestPhotoMangerPermissions();+ await permissionService.onUpdatePermission(state);+ return state;+ } catch (e, s) {+ _logger.severe("Failed to request permission", e, s);+ return null;+ }+}++/// Shows an action sheet for iOS limited permission, offering to open settings+/// or select more photos.+Future<void> _showLimitedPermissionSheet(+ BuildContext context, {+ required bool hasGrantedLimit,+}) {+ return showChoiceActionSheet(+ context,+ title: context.l10n.preserveMore,+ body: context.l10n.grantFullAccessPrompt,+ firstButtonLabel: context.l10n.openSettings,+ firstButtonOnTap: () async {+ await PhotoManager.openSetting();+ },+ secondButtonLabel:+ hasGrantedLimit ? context.l10n.selectMorePhotos : context.l10n.cancel,+ secondButtonOnTap: () async {+ if (hasGrantedLimit) {+ await PhotoManager.presentLimited();+ }+ },+ );+}