Skip to content

armgit / ente

Public Imported from GitHub
0

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

Refactor cover photo selection to use BottomSheetComponent and improve state management

AmanRajSinghMourya0541ab09aa2b
diff --git a/mobile/apps/photos/lib/ui/viewer/gallery/hooks/pick_cover_photo.dart b/mobile/apps/photos/lib/ui/viewer/gallery/hooks/pick_cover_photo.dart
index 2b59c1fe22..d0c649677f 100644
--- a/mobile/apps/photos/lib/ui/viewer/gallery/hooks/pick_cover_photo.dart
+++ b/mobile/apps/photos/lib/ui/viewer/gallery/hooks/pick_cover_photo.dart
@@ -1,6 +1,7 @@
+import "package:ente_components/ente_components.dart";
import "package:ente_strings/ente_strings.dart";
import "package:flutter/material.dart";
-import "package:modal_bottom_sheet/modal_bottom_sheet.dart";
+import "package:hugeicons/hugeicons.dart";
import "package:photos/core/event_bus.dart";
import "package:photos/db/files_db.dart";
import "package:photos/events/collection_updated_event.dart";
@@ -8,180 +9,165 @@ import 'package:photos/models/collection/collection.dart';
import "package:photos/models/file_load_result.dart";
import "package:photos/models/selected_files.dart";
import "package:photos/services/ignored_files_service.dart";
-import "package:photos/theme/colors.dart";
-import "package:photos/theme/ente_theme.dart";
-import "package:photos/ui/components/bottom_of_title_bar_widget.dart";
-import "package:photos/ui/components/buttons/button_widget.dart";
-import "package:photos/ui/components/models/button_type.dart";
-import "package:photos/ui/components/title_bar_title_widget.dart";
import "package:photos/ui/viewer/gallery/gallery.dart";
import "package:photos/ui/viewer/gallery/state/gallery_files_inherited_widget.dart";
Future<int?> showPickCoverPhotoSheet(
BuildContext context,
Collection collection,
-) async {
- return await showBarModalBottomSheet(
+) {
+ return showBottomSheetComponent<int>(
context: context,
- builder: (context) {
- return PickCoverPhotoWidget(collection);
- },
- shape: const RoundedRectangleBorder(
- side: BorderSide(width: 0),
- borderRadius: BorderRadius.vertical(top: Radius.circular(5)),
- ),
- topControl: const SizedBox.shrink(),
- backgroundColor: getEnteColorScheme(context).backgroundElevated,
- barrierColor: backdropFaintDark,
+ builder: (_) => PickCoverPhotoWidget(collection),
enableDrag: true,
);
}
-class PickCoverPhotoWidget extends StatelessWidget {
+class PickCoverPhotoWidget extends StatefulWidget {
final Collection collection;
const PickCoverPhotoWidget(this.collection, {super.key});
+ @override
+ State<PickCoverPhotoWidget> createState() => _PickCoverPhotoWidgetState();
+}
+
+class _PickCoverPhotoWidgetState extends State<PickCoverPhotoWidget> {
+ late final SelectedFiles _selectedFiles;
+
+ @override
+ void initState() {
+ super.initState();
+ _selectedFiles = SelectedFiles()..addListener(_onSelectionChanged);
+ }
+
+ @override
+ void dispose() {
+ _selectedFiles.dispose();
+ super.dispose();
+ }
+
+ void _onSelectionChanged() => setState(() {});
+
@override
Widget build(BuildContext context) {
- final ValueNotifier<bool> isFileSelected = ValueNotifier(false);
- final selectedFiles = SelectedFiles();
- selectedFiles.addListener(() {
- isFileSelected.value = selectedFiles.files.isNotEmpty;
- });
+ final l10n = context.strings;
+ final hasSelection = _selectedFiles.files.isNotEmpty;
+ return SizedBox(
+ height: MediaQuery.sizeOf(context).height,
+ child: BottomSheetComponent(
+ header: _buildHeader(context, l10n),
+ showCloseButton: false,
+ padding: const EdgeInsets.symmetric(vertical: Spacing.xl),
+ content: Expanded(
+ child: GalleryFilesState(
+ child: Gallery(
+ asyncLoader:
+ (creationStartTime, creationEndTime, {limit, asc}) async {
+ final FileLoadResult result = await FilesDB.instance
+ .getFilesInCollection(
+ widget.collection.id,
+ creationStartTime,
+ creationEndTime,
+ limit: limit,
+ asc: asc,
+ );
+ // Hide ignored files from the picker.
+ final ignoredIDs =
+ await IgnoredFilesService.instance.idToIgnoreReasonMap;
+ result.files.removeWhere(
+ (file) =>
+ file.uploadedFileID == null &&
+ IgnoredFilesService.instance.shouldSkipUpload(
+ ignoredIDs,
+ file,
+ ),
+ );
+ return result;
+ },
+ reloadEvent: Bus.instance.on<CollectionUpdatedEvent>().where(
+ (event) => event.collectionID == widget.collection.id,
+ ),
+ tagPrefix: "pick_cover_photo_gallery",
+ selectedFiles: _selectedFiles,
+ limitSelectionToOne: true,
+ showSelectAll: false,
+ sortAsyncFn: () =>
+ widget.collection.pubMagicMetadata.asc ?? false,
+ disablePinnedGroupHeader: true,
+ disableVerticalPaddingForScrollbar: true,
+ ),
+ ),
+ ),
+ actions: [
+ Padding(
+ padding: const EdgeInsets.symmetric(horizontal: Spacing.xl),
+ child: Column(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ ButtonComponent(
+ isDisabled: !hasSelection,
+ label: l10n.useSelectedPhoto,
+ density: ButtonComponentDensity.compact,
+ shouldSurfaceExecutionStates: false,
+ onTap: hasSelection
+ ? () {
+ Navigator.pop(
+ context,
+ _selectedFiles.files.first.uploadedFileID!,
+ );
+ }
+ : null,
+ ),
+ if (widget.collection.hasCover) ...[
+ const SizedBox(height: Spacing.md),
+ ButtonComponent(
+ variant: ButtonComponentVariant.secondary,
+ label: l10n.resetToDefault,
+ density: ButtonComponentDensity.compact,
+ leading: const HugeIcon(
+ icon: HugeIcons.strokeRoundedRefresh,
+ size: IconSizes.small,
+ ),
+ shouldSurfaceExecutionStates: false,
+ onTap: () => Navigator.pop(context, 0),
+ ),
+ ],
+ ],
+ ),
+ ),
+ ],
+ ),
+ );
+ }
+
+ Widget _buildHeader(BuildContext context, StringsLocalizations l10n) {
+ final colors = context.componentColors;
return Padding(
- padding: const EdgeInsets.fromLTRB(0, 32, 0, 8),
+ padding: const EdgeInsets.symmetric(horizontal: Spacing.xl),
child: SizedBox(
- width: double.infinity,
- child: Column(
- mainAxisSize: MainAxisSize.max,
+ height: 38,
+ child: Row(
children: [
Expanded(
- child: Column(
- children: [
- BottomOfTitleBarWidget(
- title: TitleBarTitleWidget(
- title: context.strings.selectCoverPhoto,
- ),
- caption: collection.displayName,
- showCloseButton: true,
- ),
- Expanded(
- child: GalleryFilesState(
- child: Gallery(
- asyncLoader:
- (
- creationStartTime,
- creationEndTime, {
- limit,
- asc,
- }) async {
- final FileLoadResult result = await FilesDB
- .instance
- .getFilesInCollection(
- collection.id,
- creationStartTime,
- creationEndTime,
- limit: limit,
- asc: asc,
- );
- // hide ignored files from home page UI
- final ignoredIDs = await IgnoredFilesService
- .instance
- .idToIgnoreReasonMap;
- result.files.removeWhere(
- (f) =>
- f.uploadedFileID == null &&
- IgnoredFilesService.instance
- .shouldSkipUpload(ignoredIDs, f),
- );
- return result;
- },
- reloadEvent: Bus.instance
- .on<CollectionUpdatedEvent>()
- .where(
- (event) => event.collectionID == collection.id,
- ),
- tagPrefix: "pick_cover_photo_gallery",
- selectedFiles: selectedFiles,
- limitSelectionToOne: true,
- showSelectAll: false,
- sortAsyncFn: () =>
- collection.pubMagicMetadata.asc ?? false,
- disablePinnedGroupHeader: true,
- disableVerticalPaddingForScrollbar: true,
- ),
- ),
- ),
- ],
+ child: Text(
+ l10n.selectCoverPhoto,
+ maxLines: 1,
+ overflow: TextOverflow.ellipsis,
+ style: TextStyles.h1Bold.copyWith(color: colors.textBase),
),
),
- SafeArea(
- child: Container(
- decoration: BoxDecoration(
- border: Border(
- top: BorderSide(
- color: getEnteColorScheme(context).strokeFaint,
- ),
- ),
- ),
- child: Center(
- child: ConstrainedBox(
- constraints: const BoxConstraints(maxWidth: 428),
- child: Padding(
- //inner stroke of 1pt + 15 pts of top padding = 16 pts
- padding: const EdgeInsets.fromLTRB(16, 15, 16, 8),
- child: Column(
- children: [
- ValueListenableBuilder(
- valueListenable: isFileSelected,
- builder: (context, bool value, _) {
- return AnimatedSwitcher(
- duration: const Duration(milliseconds: 300),
- switchInCurve: Curves.easeInOutExpo,
- switchOutCurve: Curves.easeInOutExpo,
- child: ButtonWidget(
- key: ValueKey(value),
- isDisabled: !value,
- buttonType: ButtonType.neutral,
- labelText: context.strings.useSelectedPhoto,
- onTap: () async {
- final selectedFile =
- selectedFiles.files.first;
- Navigator.pop(
- context,
- selectedFile.uploadedFileID!,
- );
- },
- ),
- );
- },
- ),
- const SizedBox(height: 8),
- ButtonWidget(
- buttonType: ButtonType.secondary,
- buttonAction: ButtonAction.cancel,
- labelText: collection.hasCover
- ? context.strings.resetToDefault
- : context.strings.cancel,
- icon: collection.hasCover
- ? Icons.restore_outlined
- : null,
- onTap: () async {
- if (collection.hasCover) {
- Navigator.pop(context, 0);
- } else {
- Navigator.of(context).pop();
- }
- },
- ),
- ],
- ),
- ),
- ),
- ),
+ const SizedBox(width: Spacing.md),
+ IconButtonComponent(
+ tooltip: l10n.close,
+ variant: IconButtonComponentVariant.circular,
+ shouldSurfaceExecutionStates: false,
+ icon: const HugeIcon(
+ icon: HugeIcons.strokeRoundedCancel01,
+ size: IconSizes.small,
),
+ onTap: () => Navigator.of(context).pop(),
),
],
),