Skip to content

armgit / ente

Public Imported from GitHub
0

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

Refactor swipe-to-select: Extract logic into SwipeSelectableFileWidget

ashilkna3f4091c2aac
diff --git a/mobile/apps/photos/lib/ui/viewer/gallery/component/gallery_file_widget.dart b/mobile/apps/photos/lib/ui/viewer/gallery/component/gallery_file_widget.dart
index 6d6979231f..e870601ffe 100644
--- a/mobile/apps/photos/lib/ui/viewer/gallery/component/gallery_file_widget.dart
+++ b/mobile/apps/photos/lib/ui/viewer/gallery/component/gallery_file_widget.dart
@@ -1,5 +1,3 @@
-import "dart:async";
-
import "package:flutter/material.dart";
import "package:flutter/services.dart";
import "package:media_extension/media_extension.dart";
@@ -13,6 +11,7 @@ import "package:photos/theme/ente_theme.dart";
import "package:photos/ui/common/touch_cross_detector.dart";
import "package:photos/ui/viewer/file/detail_page.dart";
import "package:photos/ui/viewer/file/thumbnail_widget.dart";
+import "package:photos/ui/viewer/gallery/component/swipe_selectable_file_widget.dart";
import "package:photos/ui/viewer/gallery/state/gallery_context_state.dart";
import "package:photos/ui/viewer/gallery/state/gallery_files_inherited_widget.dart";
import "package:photos/ui/viewer/gallery/state/gallery_swipe_helper.dart";
@@ -62,255 +61,120 @@ class _GalleryFileWidgetState extends State<GalleryFileWidget> {
@override
Widget build(BuildContext context) {
- if (flagService.internalUser) {
- final swipeHelper = GallerySwipeHelper.of(context);
- Color selectionColor = Colors.white;
- if (_isFileSelected &&
- widget.file.isUploaded &&
- widget.file.ownerID != widget.currentUserID) {
- final avatarColors = getEnteColorScheme(context).avatarColors;
- selectionColor =
- avatarColors[(widget.file.ownerID!).remainder(avatarColors.length)];
- }
- final String heroTag = widget.tag + widget.file.tag;
- final Widget thumbnailWidget = ThumbnailWidget(
- widget.file,
- diskLoadDeferDuration: galleryThumbnailDiskLoadDeferDuration,
- serverLoadDeferDuration: galleryThumbnailServerLoadDeferDuration,
- shouldShowLivePhotoOverlay: true,
- key: Key(heroTag),
- thumbnailSize: widget.photoGridSize < photoGridSizeDefault
- ? thumbnailLargeSize
- : thumbnailSmallSize,
- shouldShowOwnerAvatar: !_isFileSelected,
- shouldShowVideoDuration: true,
- );
- final swipeActiveNotifier =
- GallerySwipeHelper.swipeActiveNotifierOf(context);
+ // Check if swipe selection should be enabled
+ final shouldEnableSwipeSelection =
+ flagService.internalUser && !widget.limitSelectionToOne;
- return ValueListenableBuilder<bool>(
- valueListenable: swipeActiveNotifier ?? ValueNotifier(false),
- builder: (context, isSwipeActive, child) {
- // Check if we need to start selection when swipe becomes active
- if (isSwipeActive &&
- _isPointerInside &&
- swipeHelper != null &&
- !swipeHelper.isActive &&
- widget.selectedFiles != null &&
- widget.selectedFiles!.files.isNotEmpty) {
- // Schedule the selection to happen after the current build
- WidgetsBinding.instance.addPostFrameCallback((_) {
- if (mounted &&
- isSwipeActive &&
- _isPointerInside &&
- !swipeHelper.isActive) {
- swipeHelper.startSelection(widget.file);
- }
- });
- }
+ final Widget fileContent = _buildFileContent(context);
- return TouchCrossDetector(
- onPointerDown: (event) {
- _currentPointerId = event.pointer;
- _isPointerInside = true;
- // If files are already selected and swipe is not active, prepare for potential swipe
- // The actual selection will start when horizontal movement is detected
- },
- onHover: (event) {
- _isPointerInside = true;
- // Handle the initial file selection when swipe becomes active
- // This is mainly for horizontal swipe detection since vertical after long press
- // is already handled by swipeHelper being active
- if (swipeActiveNotifier?.value == true &&
- swipeHelper != null &&
- !swipeHelper.isActive &&
- widget.selectedFiles != null &&
- widget.selectedFiles!.files.isNotEmpty) {
- // Start selection for the first file when horizontal swipe is detected
- swipeHelper.startSelection(widget.file);
- }
- },
- onEnter: (event) {
- _isPointerInside = true;
- // Check if swipe is active (either from horizontal swipe or from long press)
- if ((swipeActiveNotifier?.value == true ||
- swipeHelper?.isActive == true) &&
- swipeHelper != null) {
- if (!swipeHelper.isActive) {
- // Start selection when first entering a file during active swipe
- swipeHelper.startSelection(widget.file);
- } else {
- // Update selection for subsequent files
- swipeHelper.updateSelection(widget.file);
- }
- }
- },
- onExit: (event) {
- _isPointerInside = false;
- if (_currentPointerId == event.pointer) {
- _currentPointerId = null;
- }
- },
- child: GestureDetector(
- onTap: () {
- widget.limitSelectionToOne
- ? _onTapWithSelectionLimit(widget.file)
- : _onTapNoSelectionLimit(context, widget.file);
- },
- onLongPress: () {
- widget.limitSelectionToOne
- ? _onLongPressWithSelectionLimit(context, widget.file)
- : _onLongPressNoSelectionLimit(context, widget.file);
- },
- child: _isFileSelected
- ? Stack(
- clipBehavior: Clip.none,
- children: [
- ClipRRect(
- key: ValueKey(heroTag),
- borderRadius: borderRadius,
- child: Hero(
- tag: heroTag,
- flightShuttleBuilder: (
- flightContext,
- animation,
- flightDirection,
- fromHeroContext,
- toHeroContext,
- ) =>
- thumbnailWidget,
- transitionOnUserGestures: true,
- child: thumbnailWidget,
- ),
- ),
- Container(
- decoration: const BoxDecoration(
- color: Color.fromARGB(102, 0, 0, 0),
- borderRadius: borderRadius,
- ),
- ),
- Positioned(
- right: 4,
- top: 4,
- child: Icon(
- Icons.check_circle_rounded,
- size: 20,
- color: selectionColor, //same for both themes
- ),
- ),
- ],
- )
- : ClipRRect(
- key: ValueKey(heroTag),
- borderRadius: borderRadius,
- child: Hero(
- tag: heroTag,
- flightShuttleBuilder: (
- flightContext,
- animation,
- flightDirection,
- fromHeroContext,
- toHeroContext,
- ) =>
- thumbnailWidget,
- transitionOnUserGestures: true,
- child: thumbnailWidget,
- ),
- ),
- ),
- );
+ // Wrap with swipe selection if enabled
+ if (shouldEnableSwipeSelection) {
+ return SwipeSelectableFileWidget(
+ file: widget.file,
+ selectedFiles: widget.selectedFiles,
+ onPointerStateChanged: (pointerId, isInside) {
+ _currentPointerId = pointerId ?? _currentPointerId;
+ _isPointerInside = isInside;
+ if (pointerId == null) {
+ _currentPointerId = null;
+ }
},
+ child: fileContent,
);
- } else {
- Color selectionColor = Colors.white;
- if (_isFileSelected &&
- widget.file.isUploaded &&
- widget.file.ownerID != widget.currentUserID) {
- final avatarColors = getEnteColorScheme(context).avatarColors;
- selectionColor =
- avatarColors[(widget.file.ownerID!).remainder(avatarColors.length)];
- }
- final String heroTag = widget.tag + widget.file.tag;
- final Widget thumbnailWidget = ThumbnailWidget(
- widget.file,
- diskLoadDeferDuration: galleryThumbnailDiskLoadDeferDuration,
- serverLoadDeferDuration: galleryThumbnailServerLoadDeferDuration,
- shouldShowLivePhotoOverlay: true,
- key: Key(heroTag),
- thumbnailSize: widget.photoGridSize < photoGridSizeDefault
- ? thumbnailLargeSize
- : thumbnailSmallSize,
- shouldShowOwnerAvatar: !_isFileSelected,
- shouldShowVideoDuration: true,
- );
- return GestureDetector(
- onTap: () {
- widget.limitSelectionToOne
- ? _onTapWithSelectionLimit(widget.file)
- : _onTapNoSelectionLimit(context, widget.file);
- },
- onLongPress: () {
- widget.limitSelectionToOne
- ? _onLongPressWithSelectionLimit(context, widget.file)
- : _onLongPressNoSelectionLimit(context, widget.file);
- },
- child: _isFileSelected
- ? Stack(
- clipBehavior: Clip.none,
- children: [
- ClipRRect(
- key: ValueKey(heroTag),
- borderRadius: borderRadius,
- child: Hero(
- tag: heroTag,
- flightShuttleBuilder: (
- flightContext,
- animation,
- flightDirection,
- fromHeroContext,
- toHeroContext,
- ) =>
- thumbnailWidget,
- transitionOnUserGestures: true,
- child: thumbnailWidget,
- ),
+ }
+
+ return fileContent;
+ }
+
+ Widget _buildFileContent(BuildContext context) {
+ Color selectionColor = Colors.white;
+ if (_isFileSelected &&
+ widget.file.isUploaded &&
+ widget.file.ownerID != widget.currentUserID) {
+ final avatarColors = getEnteColorScheme(context).avatarColors;
+ selectionColor =
+ avatarColors[(widget.file.ownerID!).remainder(avatarColors.length)];
+ }
+ final String heroTag = widget.tag + widget.file.tag;
+ final Widget thumbnailWidget = ThumbnailWidget(
+ widget.file,
+ diskLoadDeferDuration: galleryThumbnailDiskLoadDeferDuration,
+ serverLoadDeferDuration: galleryThumbnailServerLoadDeferDuration,
+ shouldShowLivePhotoOverlay: true,
+ key: Key(heroTag),
+ thumbnailSize: widget.photoGridSize < photoGridSizeDefault
+ ? thumbnailLargeSize
+ : thumbnailSmallSize,
+ shouldShowOwnerAvatar: !_isFileSelected,
+ shouldShowVideoDuration: true,
+ );
+ return GestureDetector(
+ onTap: () {
+ widget.limitSelectionToOne
+ ? _onTapWithSelectionLimit(widget.file)
+ : _onTapNoSelectionLimit(context, widget.file);
+ },
+ onLongPress: () {
+ widget.limitSelectionToOne
+ ? _onLongPressWithSelectionLimit(context, widget.file)
+ : _onLongPressNoSelectionLimit(context, widget.file);
+ },
+ child: _isFileSelected
+ ? Stack(
+ clipBehavior: Clip.none,
+ children: [
+ ClipRRect(
+ key: ValueKey(heroTag),
+ borderRadius: borderRadius,
+ child: Hero(
+ tag: heroTag,
+ flightShuttleBuilder: (
+ flightContext,
+ animation,
+ flightDirection,
+ fromHeroContext,
+ toHeroContext,
+ ) =>
+ thumbnailWidget,
+ transitionOnUserGestures: true,
+ child: thumbnailWidget,
),
- Container(
- decoration: const BoxDecoration(
- color: Color.fromARGB(102, 0, 0, 0),
- borderRadius: borderRadius,
- ),
+ ),
+ Container(
+ decoration: const BoxDecoration(
+ color: Color.fromARGB(102, 0, 0, 0),
+ borderRadius: borderRadius,
),
- Positioned(
- right: 4,
- top: 4,
- child: Icon(
- Icons.check_circle_rounded,
- size: 20,
- color: selectionColor, //same for both themes
- ),
+ ),
+ Positioned(
+ right: 4,
+ top: 4,
+ child: Icon(
+ Icons.check_circle_rounded,
+ size: 20,
+ color: selectionColor, //same for both themes
),
- ],
- )
- : ClipRRect(
- key: ValueKey(heroTag),
- borderRadius: borderRadius,
- child: Hero(
- tag: heroTag,
- flightShuttleBuilder: (
- flightContext,
- animation,
- flightDirection,
- fromHeroContext,
- toHeroContext,
- ) =>
- thumbnailWidget,
- transitionOnUserGestures: true,
- child: thumbnailWidget,
),
+ ],
+ )
+ : ClipRRect(
+ key: ValueKey(heroTag),
+ borderRadius: borderRadius,
+ child: Hero(
+ tag: heroTag,
+ flightShuttleBuilder: (
+ flightContext,
+ animation,
+ flightDirection,
+ fromHeroContext,
+ toHeroContext,
+ ) =>
+ thumbnailWidget,
+ transitionOnUserGestures: true,
+ child: thumbnailWidget,
),
- );
- }
+ ),
+ );
}
void _selectedFilesListener() {
@@ -362,18 +226,21 @@ class _GalleryFileWidgetState extends State<GalleryFileWidget> {
} else {
HapticFeedback.lightImpact();
_toggleFileSelection(file);
+ // Notify SwipeSelectableFileWidget if it exists
+ _handleLongPressForSwipe();
+ }
+ }
- if (flagService.internalUser) {
-// Start swipe selection if pointer is still down after long press
- // Force selecting mode since we just selected this file
- final swipeHelper = GallerySwipeHelper.of(context);
- if (_currentPointerId != null &&
- TouchCrossDetector.isPointerActive(_currentPointerId!) &&
- swipeHelper != null &&
- widget.selectedFiles!.files.isNotEmpty) {
- swipeHelper.startSelection(file, forceSelecting: true);
- }
- }
+ void _handleLongPressForSwipe() {
+ // Use local state to determine if swipe should start
+ final swipeHelper = GallerySwipeHelper.of(context);
+ if (_currentPointerId != null &&
+ _isPointerInside &&
+ TouchCrossDetector.isPointerActive(_currentPointerId!) &&
+ swipeHelper != null &&
+ widget.selectedFiles != null &&
+ widget.selectedFiles!.files.isNotEmpty) {
+ swipeHelper.startSelection(widget.file, forceSelecting: true);
}
}
diff --git a/mobile/apps/photos/lib/ui/viewer/gallery/component/swipe_selectable_file_widget.dart b/mobile/apps/photos/lib/ui/viewer/gallery/component/swipe_selectable_file_widget.dart
new file mode 100644
index 0000000000..f411f3bb43
--- /dev/null
+++ b/mobile/apps/photos/lib/ui/viewer/gallery/component/swipe_selectable_file_widget.dart
@@ -0,0 +1,106 @@
+import "package:flutter/material.dart";
+import "package:photos/models/file/file.dart";
+import "package:photos/models/selected_files.dart";
+import "package:photos/ui/common/touch_cross_detector.dart";
+import "package:photos/ui/viewer/gallery/state/gallery_swipe_helper.dart";
+
+/// A wrapper widget that adds swipe-to-select functionality to a file widget.
+///
+/// This widget encapsulates all the swipe selection logic, keeping the
+/// GalleryFileWidget clean and focused on file display.
+class SwipeSelectableFileWidget extends StatefulWidget {
+ final Widget child;
+ final EnteFile file;
+ final SelectedFiles? selectedFiles;
+ final Function(int? pointerId, bool isInside)? onPointerStateChanged;
+
+ const SwipeSelectableFileWidget({
+ super.key,
+ required this.child,
+ required this.file,
+ required this.selectedFiles,
+ this.onPointerStateChanged,
+ });
+
+ @override
+ State<SwipeSelectableFileWidget> createState() =>
+ _SwipeSelectableFileWidgetState();
+}
+
+class _SwipeSelectableFileWidgetState extends State<SwipeSelectableFileWidget> {
+ bool _isPointerInside = false;
+
+ @override
+ Widget build(BuildContext context) {
+ final swipeHelper = GallerySwipeHelper.of(context);
+ final swipeActiveNotifier =
+ GallerySwipeHelper.swipeActiveNotifierOf(context);
+
+ return ValueListenableBuilder<bool>(
+ valueListenable: swipeActiveNotifier ?? ValueNotifier(false),
+ builder: (context, isSwipeActive, child) {
+ // Check if we need to start selection when swipe becomes active
+ if (isSwipeActive &&
+ _isPointerInside &&
+ swipeHelper != null &&
+ !swipeHelper.isActive &&
+ widget.selectedFiles != null &&
+ widget.selectedFiles!.files.isNotEmpty) {
+ // Schedule the selection to happen after the current build
+ WidgetsBinding.instance.addPostFrameCallback((_) {
+ if (mounted &&
+ isSwipeActive &&
+ _isPointerInside &&
+ !swipeHelper.isActive) {
+ swipeHelper.startSelection(widget.file);
+ }
+ });
+ }
+
+ return TouchCrossDetector(
+ onPointerDown: (event) {
+ _isPointerInside = true;
+ widget.onPointerStateChanged?.call(event.pointer, true);
+ // If files are already selected and swipe is not active, prepare for potential swipe
+ // The actual selection will start when horizontal movement is detected
+ },
+ onHover: (event) {
+ _isPointerInside = true;
+ // Handle the initial file selection when swipe becomes active
+ // This is mainly for horizontal swipe detection since vertical after long press
+ // is already handled by swipeHelper being active
+ if (swipeActiveNotifier?.value == true &&
+ swipeHelper != null &&
+ !swipeHelper.isActive &&
+ widget.selectedFiles != null &&
+ widget.selectedFiles!.files.isNotEmpty) {
+ // Start selection for the first file when horizontal swipe is detected
+ swipeHelper.startSelection(widget.file);
+ }
+ },
+ onEnter: (event) {
+ _isPointerInside = true;
+ widget.onPointerStateChanged?.call(event.pointer, true);
+ // Check if swipe is active (either from horizontal swipe or from long press)
+ if ((swipeActiveNotifier?.value == true ||
+ swipeHelper?.isActive == true) &&
+ swipeHelper != null) {
+ if (!swipeHelper.isActive) {
+ // Start selection when first entering a file during active swipe
+ swipeHelper.startSelection(widget.file);
+ } else {
+ // Update selection for subsequent files
+ swipeHelper.updateSelection(widget.file);
+ }
+ }
+ },
+ onExit: (event) {
+ _isPointerInside = false;
+ widget.onPointerStateChanged?.call(null, false);
+ },
+ child: widget.child,
+ );
+ },
+ );
+ }
+}