2021-04-23 21:37:04 +00:00
|
|
|
import 'dart:async';
|
2021-04-20 12:26:42 +00:00
|
|
|
import 'dart:math';
|
|
|
|
|
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:flutter/services.dart';
|
|
|
|
import 'package:logging/logging.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';
|
2021-04-21 08:41:58 +00:00
|
|
|
import 'package:photos/ui/detail_page.dart';
|
2021-04-20 12:26:42 +00:00
|
|
|
import 'package:photos/ui/huge_listview/place_holder_widget.dart';
|
|
|
|
import 'package:photos/ui/thumbnail_widget.dart';
|
|
|
|
import 'package:photos/utils/date_time_util.dart';
|
|
|
|
import 'package:visibility_detector/visibility_detector.dart';
|
|
|
|
|
2021-04-23 19:55:50 +00:00
|
|
|
class LazyLoadingGallery extends StatefulWidget {
|
|
|
|
final List<File> files;
|
2021-04-23 21:37:04 +00:00
|
|
|
final Stream<FilesUpdatedEvent> reloadEvent;
|
2021-04-23 19:55:50 +00:00
|
|
|
final Future<List<File>> Function(int creationStartTime, int creationEndTime,
|
|
|
|
{int limit}) asyncLoader;
|
|
|
|
final SelectedFiles selectedFiles;
|
|
|
|
final String tag;
|
|
|
|
|
|
|
|
LazyLoadingGallery(this.files, this.reloadEvent, this.asyncLoader,
|
|
|
|
this.selectedFiles, this.tag,
|
2021-04-21 10:13:02 +00:00
|
|
|
{Key key})
|
2021-04-27 15:41:29 +00:00
|
|
|
: super(key: key);
|
2021-04-20 12:26:42 +00:00
|
|
|
|
2021-04-23 19:55:50 +00:00
|
|
|
@override
|
|
|
|
_LazyLoadingGalleryState createState() => _LazyLoadingGalleryState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _LazyLoadingGalleryState extends State<LazyLoadingGallery> {
|
2021-04-27 14:26:11 +00:00
|
|
|
static const kSubGalleryItemLimit = 24;
|
2021-04-23 19:55:50 +00:00
|
|
|
static const kMicroSecondsInADay = 86400000000;
|
|
|
|
|
|
|
|
static final Logger _logger = Logger("LazyLoadingGallery");
|
|
|
|
|
|
|
|
List<File> _files;
|
2021-04-23 21:37:04 +00:00
|
|
|
StreamSubscription<FilesUpdatedEvent> _reloadEventSubscription;
|
2021-04-23 19:55:50 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2021-04-27 20:30:15 +00:00
|
|
|
_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _init() {
|
2021-04-23 19:55:50 +00:00
|
|
|
_files = widget.files;
|
|
|
|
final galleryDate =
|
|
|
|
DateTime.fromMicrosecondsSinceEpoch(_files[0].creationTime);
|
2021-04-23 21:37:04 +00:00
|
|
|
_reloadEventSubscription = widget.reloadEvent.listen((event) async {
|
2021-04-30 19:28:07 +00:00
|
|
|
final filesUpdatedThisDay = event.updatedFiles
|
2021-04-27 22:21:31 +00:00
|
|
|
.where((file) =>
|
|
|
|
file.creationTime !=
|
|
|
|
null) // Filtering out noise of deleted files diff from server
|
|
|
|
.where((file) {
|
2021-04-23 21:37:04 +00:00
|
|
|
final fileDate = DateTime.fromMicrosecondsSinceEpoch(file.creationTime);
|
|
|
|
return fileDate.year == galleryDate.year &&
|
|
|
|
fileDate.month == galleryDate.month &&
|
|
|
|
fileDate.day == galleryDate.day;
|
2021-04-27 22:21:31 +00:00
|
|
|
});
|
2021-04-30 19:28:07 +00:00
|
|
|
if (filesUpdatedThisDay.isNotEmpty) {
|
2021-05-02 10:31:04 +00:00
|
|
|
_logger.info(filesUpdatedThisDay.length.toString() +
|
|
|
|
" files were updated on " +
|
|
|
|
getDayTitle(galleryDate.microsecondsSinceEpoch));
|
|
|
|
if (event.type == EventType.added_or_updated) {
|
|
|
|
final dayStartTime =
|
|
|
|
DateTime(galleryDate.year, galleryDate.month, galleryDate.day);
|
|
|
|
final files = await widget.asyncLoader(
|
|
|
|
dayStartTime.microsecondsSinceEpoch,
|
|
|
|
dayStartTime.microsecondsSinceEpoch + kMicroSecondsInADay - 1);
|
|
|
|
if (files.isEmpty) {
|
|
|
|
// All files on this day were deleted, let gallery trigger the reload
|
|
|
|
} else {
|
|
|
|
if (mounted) {
|
|
|
|
setState(() {
|
|
|
|
_files = files;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2021-04-30 19:24:18 +00:00
|
|
|
} else {
|
2021-05-02 10:31:04 +00:00
|
|
|
// Files were deleted
|
|
|
|
final updateFileIDs = Set<int>();
|
|
|
|
for (final file in filesUpdatedThisDay) {
|
|
|
|
updateFileIDs.add(file.generatedID);
|
|
|
|
}
|
|
|
|
final List<File> files = [];
|
|
|
|
files.addAll(_files);
|
|
|
|
files.removeWhere((file) => updateFileIDs.contains(file.generatedID));
|
|
|
|
if (files.isNotEmpty && mounted) {
|
2021-05-02 11:19:05 +00:00
|
|
|
// If all files on this day were deleted, ignore and let the gallery reload itself
|
2021-04-30 19:24:18 +00:00
|
|
|
setState(() {
|
|
|
|
_files = files;
|
|
|
|
});
|
2021-04-27 22:21:31 +00:00
|
|
|
}
|
2021-04-30 19:24:18 +00:00
|
|
|
}
|
2021-04-23 19:55:50 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-23 21:37:04 +00:00
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_reloadEventSubscription.cancel();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2021-04-27 18:57:14 +00:00
|
|
|
@override
|
|
|
|
void didUpdateWidget(LazyLoadingGallery oldWidget) {
|
|
|
|
super.didUpdateWidget(oldWidget);
|
2021-04-27 20:02:39 +00:00
|
|
|
if (!listEquals(_files, widget.files)) {
|
2021-04-27 18:57:14 +00:00
|
|
|
setState(() {
|
2021-04-27 22:21:31 +00:00
|
|
|
_reloadEventSubscription.cancel();
|
2021-04-27 20:30:15 +00:00
|
|
|
_init();
|
2021-04-27 18:57:14 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 12:26:42 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2021-04-23 21:21:18 +00:00
|
|
|
if (_files.length == 0) {
|
|
|
|
return Container();
|
|
|
|
}
|
2021-04-20 12:26:42 +00:00
|
|
|
return Column(
|
|
|
|
children: <Widget>[
|
2021-04-23 19:55:50 +00:00
|
|
|
getDayWidget(_files[0].creationTime),
|
|
|
|
_getGallery(),
|
2021-04-20 12:26:42 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-04-23 19:55:50 +00:00
|
|
|
Widget _getGallery() {
|
2021-04-20 12:26:42 +00:00
|
|
|
List<Widget> childGalleries = [];
|
2021-04-23 19:55:50 +00:00
|
|
|
for (int index = 0; index < _files.length; index += kSubGalleryItemLimit) {
|
2021-04-20 12:26:42 +00:00
|
|
|
childGalleries.add(LazyLoadingGridView(
|
2021-04-23 19:55:50 +00:00
|
|
|
widget.tag,
|
|
|
|
_files.sublist(index, min(index + kSubGalleryItemLimit, _files.length)),
|
|
|
|
widget.asyncLoader,
|
|
|
|
widget.selectedFiles,
|
2021-04-24 07:44:56 +00:00
|
|
|
index == 0,
|
2021-04-20 12:26:42 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
return Padding(
|
|
|
|
padding: const EdgeInsets.only(bottom: 12),
|
|
|
|
child: Column(
|
|
|
|
children: childGalleries,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class LazyLoadingGridView extends StatefulWidget {
|
2021-04-26 07:45:00 +00:00
|
|
|
static const kThumbnailDiskLoadDeferDuration = Duration(milliseconds: 40);
|
2021-04-26 07:46:36 +00:00
|
|
|
static const kThumbnailServerLoadDeferDuration = Duration(milliseconds: 80);
|
2021-04-27 14:26:11 +00:00
|
|
|
|
2021-04-25 11:39:04 +00:00
|
|
|
final String tag;
|
|
|
|
final List<File> files;
|
|
|
|
final Future<List<File>> Function(int creationStartTime, int creationEndTime,
|
|
|
|
{int limit}) asyncLoader;
|
|
|
|
final SelectedFiles selectedFiles;
|
|
|
|
final bool isVisible;
|
2021-04-20 12:26:42 +00:00
|
|
|
|
2021-04-24 07:44:56 +00:00
|
|
|
LazyLoadingGridView(this.tag, this.files, this.asyncLoader,
|
|
|
|
this.selectedFiles, this.isVisible,
|
2021-04-21 10:13:02 +00:00
|
|
|
{Key key})
|
2021-04-27 14:26:11 +00:00
|
|
|
: super(key: key ?? GlobalKey<_LazyLoadingGridViewState>());
|
2021-04-20 12:26:42 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
_LazyLoadingGridViewState createState() => _LazyLoadingGridViewState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _LazyLoadingGridViewState extends State<LazyLoadingGridView> {
|
2021-04-24 07:44:56 +00:00
|
|
|
bool _isVisible;
|
2021-04-25 11:39:04 +00:00
|
|
|
|
2021-04-24 07:44:56 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
_isVisible = widget.isVisible;
|
2021-04-30 19:13:49 +00:00
|
|
|
widget.selectedFiles.addListener(() {
|
|
|
|
bool shouldRefresh = false;
|
|
|
|
for (final file in widget.files) {
|
2021-05-02 11:19:05 +00:00
|
|
|
if (widget.selectedFiles.lastSelections.contains(file)) {
|
2021-04-30 19:13:49 +00:00
|
|
|
shouldRefresh = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (shouldRefresh && mounted) {
|
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
});
|
2021-04-24 07:44:56 +00:00
|
|
|
}
|
2021-04-20 12:26:42 +00:00
|
|
|
|
2021-04-27 20:02:39 +00:00
|
|
|
@override
|
|
|
|
void didUpdateWidget(LazyLoadingGridView oldWidget) {
|
|
|
|
super.didUpdateWidget(oldWidget);
|
|
|
|
if (!listEquals(widget.files, oldWidget.files)) {
|
|
|
|
setState(() {
|
|
|
|
_isVisible = widget.isVisible;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 12:26:42 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
if (!_isVisible) {
|
|
|
|
return VisibilityDetector(
|
|
|
|
key: Key(widget.tag + widget.files[0].creationTime.toString()),
|
|
|
|
onVisibilityChanged: (visibility) {
|
|
|
|
if (visibility.visibleFraction > 0 && !_isVisible) {
|
|
|
|
setState(() {
|
|
|
|
_isVisible = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
child: PlaceHolderWidget(widget.files.length),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return GridView.builder(
|
|
|
|
shrinkWrap: true,
|
|
|
|
physics:
|
|
|
|
NeverScrollableScrollPhysics(), // to disable GridView's scrolling
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
return _buildFile(context, widget.files[index]);
|
|
|
|
},
|
|
|
|
itemCount: widget.files.length,
|
|
|
|
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
|
|
|
|
crossAxisCount: 4,
|
|
|
|
),
|
2021-05-02 11:19:05 +00:00
|
|
|
padding: EdgeInsets.all(0),
|
2021-04-20 12:26:42 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildFile(BuildContext context, File file) {
|
|
|
|
return GestureDetector(
|
|
|
|
onTap: () {
|
|
|
|
if (widget.selectedFiles.files.isNotEmpty) {
|
|
|
|
_selectFile(file);
|
|
|
|
} else {
|
|
|
|
_routeToDetailPage(file, context);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onLongPress: () {
|
|
|
|
HapticFeedback.lightImpact();
|
|
|
|
_selectFile(file);
|
|
|
|
},
|
|
|
|
child: Container(
|
|
|
|
margin: const EdgeInsets.all(2.0),
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
border: widget.selectedFiles.files.contains(file)
|
|
|
|
? Border.all(
|
|
|
|
width: 4.0,
|
|
|
|
color: Theme.of(context).accentColor,
|
|
|
|
)
|
|
|
|
: null,
|
|
|
|
),
|
|
|
|
child: Hero(
|
|
|
|
tag: widget.tag + file.tag(),
|
2021-04-26 07:29:01 +00:00
|
|
|
child: ThumbnailWidget(
|
|
|
|
file,
|
2021-04-27 14:26:11 +00:00
|
|
|
diskLoadDeferDuration:
|
|
|
|
LazyLoadingGridView.kThumbnailDiskLoadDeferDuration,
|
|
|
|
serverLoadDeferDuration:
|
|
|
|
LazyLoadingGridView.kThumbnailServerLoadDeferDuration,
|
2021-04-26 07:29:01 +00:00
|
|
|
),
|
2021-04-20 12:26:42 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _selectFile(File file) {
|
|
|
|
widget.selectedFiles.toggleSelection(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _routeToDetailPage(File file, BuildContext context) {
|
2021-04-21 08:41:58 +00:00
|
|
|
final page = DetailPage(
|
|
|
|
widget.files,
|
2021-04-21 10:13:02 +00:00
|
|
|
widget.asyncLoader,
|
2021-04-21 08:41:58 +00:00
|
|
|
widget.files.indexOf(file),
|
|
|
|
widget.tag,
|
|
|
|
);
|
|
|
|
Navigator.of(context).push(
|
2021-05-03 16:06:58 +00:00
|
|
|
PageRouteBuilder(
|
|
|
|
pageBuilder: (BuildContext context, Animation<double> animation,
|
|
|
|
Animation<double> secondaryAnimation) {
|
2021-04-21 08:41:58 +00:00
|
|
|
return page;
|
|
|
|
},
|
2021-05-03 16:06:58 +00:00
|
|
|
transitionsBuilder: (BuildContext context, Animation<double> animation,
|
|
|
|
Animation<double> secondaryAnimation, Widget child) {
|
|
|
|
return Align(
|
|
|
|
child: FadeTransition(
|
|
|
|
opacity: animation,
|
|
|
|
child: child,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
transitionDuration: Duration(milliseconds: 200),
|
|
|
|
opaque: false,
|
2021-04-21 08:41:58 +00:00
|
|
|
),
|
|
|
|
);
|
2021-04-20 12:26:42 +00:00
|
|
|
}
|
|
|
|
}
|