ente/lib/ui/huge_listview/lazy_loading_gallery.dart

226 lines
6.4 KiB
Dart
Raw Normal View History

import 'dart:async';
2021-04-20 12:26:42 +00:00
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:logging/logging.dart';
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;
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,
{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;
StreamSubscription<FilesUpdatedEvent> _reloadEventSubscription;
2021-04-23 19:55:50 +00:00
@override
void initState() {
super.initState();
_files = widget.files;
final galleryDate =
DateTime.fromMicrosecondsSinceEpoch(_files[0].creationTime);
_reloadEventSubscription = widget.reloadEvent.listen((event) async {
bool isOnSameDay = event.updatedFiles.where((file) {
final fileDate = DateTime.fromMicrosecondsSinceEpoch(file.creationTime);
return fileDate.year == galleryDate.year &&
fileDate.month == galleryDate.month &&
fileDate.day == galleryDate.day;
}).isNotEmpty;
if (isOnSameDay) {
final dayStartTime =
DateTime(galleryDate.year, galleryDate.month, galleryDate.day);
final files = await widget.asyncLoader(
dayStartTime.microsecondsSinceEpoch,
dayStartTime.microsecondsSinceEpoch + kMicroSecondsInADay - 1);
if (mounted) {
setState(() {
_files = files;
});
2021-04-23 19:55:50 +00:00
}
}
});
}
@override
void dispose() {
_reloadEventSubscription.cancel();
super.dispose();
}
2021-04-20 12:26:42 +00:00
@override
Widget build(BuildContext context) {
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 {
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,
{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-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,
),
);
}
}
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,
widget.asyncLoader,
2021-04-21 08:41:58 +00:00
widget.files.indexOf(file),
widget.tag,
);
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return page;
},
),
);
2021-04-20 12:26:42 +00:00
}
}