2021-04-23 21:37:04 +00:00
|
|
|
import 'dart:async';
|
2021-04-20 12:26:42 +00:00
|
|
|
|
2021-04-27 20:02:39 +00:00
|
|
|
import 'package:flutter/foundation.dart';
|
2021-04-20 12:26:42 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:logging/logging.dart';
|
2021-05-20 23:18:45 +00:00
|
|
|
import 'package:photos/core/constants.dart';
|
2021-04-23 21:37:04 +00:00
|
|
|
import 'package:photos/events/files_updated_event.dart';
|
2021-04-20 12:26:42 +00:00
|
|
|
import 'package:photos/models/file.dart';
|
2021-04-23 19:55:50 +00:00
|
|
|
import 'package:photos/models/selected_files.dart';
|
2022-11-09 04:42:32 +00:00
|
|
|
import 'package:photos/theme/ente_theme.dart';
|
2023-05-31 19:38:01 +00:00
|
|
|
import "package:photos/ui/viewer/gallery/component/grid/place_holder_grid_view_widget.dart";
|
2023-05-31 19:34:19 +00:00
|
|
|
import "package:photos/ui/viewer/gallery/component/group/group_gallery.dart";
|
2023-05-31 19:38:01 +00:00
|
|
|
import "package:photos/ui/viewer/gallery/component/group/group_header_widget.dart";
|
2022-07-01 14:18:05 +00:00
|
|
|
import 'package:photos/ui/viewer/gallery/gallery.dart';
|
2023-06-06 16:08:11 +00:00
|
|
|
import "package:photos/ui/viewer/gallery/state/gallery_sort_order.dart";
|
2021-04-20 12:26:42 +00:00
|
|
|
|
2023-05-31 19:34:19 +00:00
|
|
|
class LazyGroupGallery extends StatefulWidget {
|
2022-12-30 18:24:25 +00:00
|
|
|
final List<File> files;
|
2021-05-06 20:16:53 +00:00
|
|
|
final int index;
|
2022-12-30 12:10:17 +00:00
|
|
|
final Stream<FilesUpdatedEvent>? reloadEvent;
|
2021-10-29 23:55:29 +00:00
|
|
|
final Set<EventType> removalEventTypes;
|
2021-05-08 17:49:06 +00:00
|
|
|
final GalleryLoader asyncLoader;
|
2023-03-20 15:15:10 +00:00
|
|
|
final SelectedFiles? selectedFiles;
|
2021-04-23 19:55:50 +00:00
|
|
|
final String tag;
|
2022-12-30 12:10:17 +00:00
|
|
|
final String? logTag;
|
2021-05-06 20:16:53 +00:00
|
|
|
final Stream<int> currentIndexStream;
|
2023-05-03 09:52:06 +00:00
|
|
|
final int photoGridSize;
|
2023-05-31 19:08:10 +00:00
|
|
|
final bool enableFileGrouping;
|
2023-04-04 11:20:15 +00:00
|
|
|
final bool limitSelectionToOne;
|
2023-06-12 10:48:33 +00:00
|
|
|
final bool showSelectAllByDefault;
|
2023-07-11 06:50:37 +00:00
|
|
|
const LazyGroupGallery(
|
2021-05-06 21:15:40 +00:00
|
|
|
this.files,
|
|
|
|
this.index,
|
|
|
|
this.reloadEvent,
|
2021-10-29 23:55:29 +00:00
|
|
|
this.removalEventTypes,
|
2021-05-06 21:15:40 +00:00
|
|
|
this.asyncLoader,
|
|
|
|
this.selectedFiles,
|
|
|
|
this.tag,
|
2023-03-21 01:50:55 +00:00
|
|
|
this.currentIndexStream,
|
2023-06-12 10:48:33 +00:00
|
|
|
this.enableFileGrouping,
|
|
|
|
this.showSelectAllByDefault, {
|
2022-11-11 09:30:07 +00:00
|
|
|
this.logTag = "",
|
2023-05-03 09:52:06 +00:00
|
|
|
this.photoGridSize = photoGridSizeDefault,
|
2023-04-04 11:20:15 +00:00
|
|
|
this.limitSelectionToOne = false,
|
2022-12-30 12:10:17 +00:00
|
|
|
Key? key,
|
2023-07-11 06:50:37 +00:00
|
|
|
}) : super(key: key);
|
2021-04-20 12:26:42 +00:00
|
|
|
|
2021-04-23 19:55:50 +00:00
|
|
|
@override
|
2023-05-31 19:34:19 +00:00
|
|
|
State<LazyGroupGallery> createState() => _LazyGroupGalleryState();
|
2021-04-23 19:55:50 +00:00
|
|
|
}
|
|
|
|
|
2023-05-31 19:34:19 +00:00
|
|
|
class _LazyGroupGalleryState extends State<LazyGroupGallery> {
|
2023-07-07 16:14:12 +00:00
|
|
|
static const numberOfGroupsToRenderBeforeAndAfter = 8;
|
2023-07-12 10:32:37 +00:00
|
|
|
late final ValueNotifier<bool> _showSelectAllButton;
|
|
|
|
final _areAllFromGroupSelected = ValueNotifier(false);
|
2021-04-23 19:55:50 +00:00
|
|
|
|
2022-12-30 12:10:17 +00:00
|
|
|
late Logger _logger;
|
2021-04-23 19:55:50 +00:00
|
|
|
|
2022-12-30 18:24:25 +00:00
|
|
|
late List<File> _files;
|
2023-03-20 15:15:10 +00:00
|
|
|
late StreamSubscription<FilesUpdatedEvent>? _reloadEventSubscription;
|
2022-12-30 12:10:17 +00:00
|
|
|
late StreamSubscription<int> _currentIndexSubscription;
|
|
|
|
bool? _shouldRender;
|
2021-04-23 19:55:50 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
2022-11-09 13:51:36 +00:00
|
|
|
//this is for removing the 'select all from day' icon on unselecting all files with 'cancel'
|
2023-07-12 10:32:37 +00:00
|
|
|
|
2021-04-23 19:55:50 +00:00
|
|
|
super.initState();
|
2023-06-12 10:48:33 +00:00
|
|
|
widget.selectedFiles?.addListener(_selectedFilesListener);
|
|
|
|
_showSelectAllButton = ValueNotifier(widget.showSelectAllByDefault);
|
2021-04-27 20:30:15 +00:00
|
|
|
_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _init() {
|
2022-11-11 09:30:07 +00:00
|
|
|
_logger = Logger("LazyLoading_${widget.logTag}");
|
2021-05-06 20:16:53 +00:00
|
|
|
_shouldRender = true;
|
2021-04-23 19:55:50 +00:00
|
|
|
_files = widget.files;
|
2023-03-20 15:15:10 +00:00
|
|
|
_reloadEventSubscription = widget.reloadEvent?.listen((e) => _onReload(e));
|
2021-05-06 20:16:53 +00:00
|
|
|
|
2022-07-03 09:49:33 +00:00
|
|
|
_currentIndexSubscription =
|
|
|
|
widget.currentIndexStream.listen((currentIndex) {
|
2022-08-29 14:43:31 +00:00
|
|
|
final bool shouldRender = (currentIndex - widget.index).abs() <
|
2023-07-07 16:14:12 +00:00
|
|
|
numberOfGroupsToRenderBeforeAndAfter;
|
2021-05-06 20:16:53 +00:00
|
|
|
if (mounted && shouldRender != _shouldRender) {
|
|
|
|
setState(() {
|
|
|
|
_shouldRender = shouldRender;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2021-04-23 19:55:50 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 21:15:40 +00:00
|
|
|
Future _onReload(FilesUpdatedEvent event) async {
|
2023-06-02 02:41:14 +00:00
|
|
|
final DateTime groupDate =
|
2022-12-30 18:24:25 +00:00
|
|
|
DateTime.fromMicrosecondsSinceEpoch(_files[0].creationTime!);
|
2023-06-02 02:41:14 +00:00
|
|
|
// iterate over files and check if any of the belongs to this group
|
|
|
|
final anyCandidateForGroup = event.updatedFiles.any((file) {
|
2022-12-30 12:10:17 +00:00
|
|
|
final fileDate = DateTime.fromMicrosecondsSinceEpoch(file.creationTime!);
|
2023-06-01 06:35:30 +00:00
|
|
|
return fileDate.year == groupDate.year &&
|
|
|
|
fileDate.month == groupDate.month &&
|
|
|
|
fileDate.day == groupDate.day;
|
2021-05-06 21:15:40 +00:00
|
|
|
});
|
2023-06-02 02:41:14 +00:00
|
|
|
if (anyCandidateForGroup) {
|
2022-11-11 09:30:07 +00:00
|
|
|
if (kDebugMode) {
|
|
|
|
_logger.info(
|
2023-06-02 02:41:14 +00:00
|
|
|
" files were updated due to ${event.reason} on " +
|
2023-05-04 10:19:47 +00:00
|
|
|
DateTime.fromMicrosecondsSinceEpoch(
|
2023-06-01 06:35:30 +00:00
|
|
|
groupDate.microsecondsSinceEpoch,
|
2023-05-04 10:19:47 +00:00
|
|
|
).toIso8601String(),
|
2022-11-11 09:30:07 +00:00
|
|
|
);
|
|
|
|
}
|
2023-06-02 02:29:14 +00:00
|
|
|
if (event.type == EventType.addedOrUpdated ||
|
|
|
|
widget.removalEventTypes.contains(event.type)) {
|
2023-06-01 06:35:30 +00:00
|
|
|
// We are reloading the whole group
|
2022-07-03 09:49:33 +00:00
|
|
|
final dayStartTime =
|
2023-06-01 06:35:30 +00:00
|
|
|
DateTime(groupDate.year, groupDate.month, groupDate.day);
|
2021-06-29 05:13:16 +00:00
|
|
|
final result = await widget.asyncLoader(
|
2022-06-11 08:23:52 +00:00
|
|
|
dayStartTime.microsecondsSinceEpoch,
|
2022-09-20 11:53:32 +00:00
|
|
|
dayStartTime.microsecondsSinceEpoch + microSecondsInDay - 1,
|
2023-06-06 16:08:11 +00:00
|
|
|
asc: GallerySortOrder.of(context)!.sortOrderAsc,
|
2022-06-11 08:23:52 +00:00
|
|
|
);
|
2021-09-23 11:08:39 +00:00
|
|
|
if (mounted) {
|
|
|
|
setState(() {
|
|
|
|
_files = result.files;
|
|
|
|
});
|
2021-05-06 21:15:40 +00:00
|
|
|
}
|
2023-06-02 02:29:14 +00:00
|
|
|
} else if (kDebugMode) {
|
|
|
|
debugPrint("Unexpected event ${event.type.name}");
|
2021-05-06 21:15:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-23 21:37:04 +00:00
|
|
|
@override
|
|
|
|
void dispose() {
|
2023-03-20 15:15:10 +00:00
|
|
|
_reloadEventSubscription?.cancel();
|
2021-05-06 20:16:53 +00:00
|
|
|
_currentIndexSubscription.cancel();
|
2023-03-20 15:15:10 +00:00
|
|
|
widget.selectedFiles?.removeListener(_selectedFilesListener);
|
2021-04-23 21:37:04 +00:00
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2021-04-27 18:57:14 +00:00
|
|
|
@override
|
2023-05-31 19:34:19 +00:00
|
|
|
void didUpdateWidget(LazyGroupGallery oldWidget) {
|
2021-04-27 18:57:14 +00:00
|
|
|
super.didUpdateWidget(oldWidget);
|
2021-04-27 20:02:39 +00:00
|
|
|
if (!listEquals(_files, widget.files)) {
|
2023-03-20 15:15:10 +00:00
|
|
|
_reloadEventSubscription?.cancel();
|
2021-05-07 20:32:33 +00:00
|
|
|
_init();
|
2021-04-27 18:57:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 12:26:42 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-12-30 18:24:25 +00:00
|
|
|
if (_files.isEmpty) {
|
2022-07-28 09:54:32 +00:00
|
|
|
return const SizedBox.shrink();
|
2021-04-23 21:21:18 +00:00
|
|
|
}
|
2022-11-09 04:42:32 +00:00
|
|
|
return Column(
|
|
|
|
children: [
|
2022-11-21 09:02:51 +00:00
|
|
|
Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
children: [
|
2023-05-31 19:08:10 +00:00
|
|
|
if (widget.enableFileGrouping)
|
2023-05-31 19:38:01 +00:00
|
|
|
GroupHeaderWidget(
|
2023-05-03 09:52:06 +00:00
|
|
|
timestamp: _files[0].creationTime!,
|
2023-05-05 06:17:36 +00:00
|
|
|
gridSize: widget.photoGridSize,
|
2023-03-21 01:50:55 +00:00
|
|
|
),
|
2023-04-07 05:19:00 +00:00
|
|
|
widget.limitSelectionToOne
|
|
|
|
? const SizedBox.shrink()
|
|
|
|
: ValueListenableBuilder(
|
|
|
|
valueListenable: _showSelectAllButton,
|
|
|
|
builder: (context, dynamic value, _) {
|
|
|
|
return !value
|
|
|
|
? const SizedBox.shrink()
|
|
|
|
: GestureDetector(
|
|
|
|
behavior: HitTestBehavior.translucent,
|
|
|
|
child: SizedBox(
|
|
|
|
width: 48,
|
|
|
|
height: 44,
|
|
|
|
child: ValueListenableBuilder(
|
2023-07-12 10:32:37 +00:00
|
|
|
valueListenable: _areAllFromGroupSelected,
|
2023-04-07 05:19:00 +00:00
|
|
|
builder: (context, dynamic value, _) {
|
|
|
|
return value
|
|
|
|
? const Icon(
|
|
|
|
Icons.check_circle,
|
|
|
|
size: 18,
|
|
|
|
)
|
|
|
|
: Icon(
|
|
|
|
Icons.check_circle_outlined,
|
|
|
|
color: getEnteColorScheme(context)
|
|
|
|
.strokeMuted,
|
|
|
|
size: 18,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
onTap: () {
|
|
|
|
//this value has no significance
|
|
|
|
//changing only to notify the listeners
|
2023-07-12 10:32:37 +00:00
|
|
|
// _toggleSelectAllFromDay.value =
|
|
|
|
// !_toggleSelectAllFromDay.value;
|
2023-04-07 05:19:00 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
)
|
2022-11-21 09:02:51 +00:00
|
|
|
],
|
2022-11-09 04:42:32 +00:00
|
|
|
),
|
2022-12-30 12:10:17 +00:00
|
|
|
_shouldRender!
|
2023-05-31 19:34:19 +00:00
|
|
|
? GroupGallery(
|
2023-05-03 09:52:06 +00:00
|
|
|
photoGridSize: widget.photoGridSize,
|
|
|
|
files: _files,
|
|
|
|
tag: widget.tag,
|
|
|
|
asyncLoader: widget.asyncLoader,
|
|
|
|
selectedFiles: widget.selectedFiles,
|
|
|
|
limitSelectionToOne: widget.limitSelectionToOne,
|
|
|
|
)
|
2023-05-31 19:34:19 +00:00
|
|
|
// todo: perf eval should we have separate PlaceHolder for Groups
|
|
|
|
// instead of creating a large cached view
|
2023-05-31 19:01:42 +00:00
|
|
|
: PlaceHolderGridViewWidget(
|
2022-12-30 18:24:25 +00:00
|
|
|
_files.length,
|
2023-05-03 09:52:06 +00:00
|
|
|
widget.photoGridSize,
|
2022-12-07 04:08:42 +00:00
|
|
|
),
|
2022-11-09 04:42:32 +00:00
|
|
|
],
|
2021-04-20 12:26:42 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-05-03 09:52:06 +00:00
|
|
|
void _selectedFilesListener() {
|
2023-07-12 10:32:37 +00:00
|
|
|
if (widget.selectedFiles == null) return;
|
|
|
|
_areAllFromGroupSelected.value =
|
|
|
|
widget.selectedFiles!.files.containsAll(widget.files.toSet());
|
|
|
|
|
|
|
|
//Can remove this if we decide to show select all by default for all galleries
|
2023-06-12 10:48:33 +00:00
|
|
|
if (widget.selectedFiles!.files.isEmpty && !widget.showSelectAllByDefault) {
|
2023-05-03 09:52:06 +00:00
|
|
|
_showSelectAllButton.value = false;
|
|
|
|
} else {
|
|
|
|
_showSelectAllButton.value = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|