ente/lib/ui/gallery.dart

242 lines
7.6 KiB
Dart
Raw Normal View History

2020-06-10 18:17:54 +00:00
import 'dart:async';
2020-11-12 13:25:57 +00:00
import 'dart:math';
2020-04-12 12:38:49 +00:00
import 'package:flutter/cupertino.dart';
2020-03-28 18:18:27 +00:00
import 'package:flutter/material.dart';
2020-06-17 15:09:47 +00:00
import 'package:logging/logging.dart';
2020-06-15 19:03:43 +00:00
import 'package:photos/events/event.dart';
2020-06-19 23:03:26 +00:00
import 'package:photos/models/file.dart';
import 'package:photos/models/selected_files.dart';
2020-08-09 14:58:41 +00:00
import 'package:photos/ui/common_elements.dart';
2021-02-05 19:40:55 +00:00
import 'package:photos/ui/gallery_app_bar_widget.dart';
2021-04-20 20:11:39 +00:00
import 'package:photos/ui/huge_listview/huge_listview.dart';
import 'package:photos/ui/huge_listview/lazy_loading_gallery.dart';
import 'package:photos/ui/huge_listview/page_result.dart';
2021-04-20 20:11:39 +00:00
import 'package:photos/ui/huge_listview/place_holder_widget.dart';
import 'package:photos/ui/loading_widget.dart';
2020-05-01 18:20:12 +00:00
import 'package:photos/utils/date_time_util.dart';
import 'package:quiver/cache.dart';
import 'package:quiver/collection.dart';
2020-11-12 13:25:57 +00:00
import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';
2020-03-28 18:18:27 +00:00
class Gallery extends StatefulWidget {
2021-04-20 20:11:39 +00:00
final Future<List<File>> Function(int creationStartTime, int creationEndTime,
{int limit}) asyncLoader;
2021-04-21 13:09:18 +00:00
final Future<List<int>> Function() creationTimesLoader;
2020-06-16 12:56:23 +00:00
final Stream<Event> reloadEvent;
final SelectedFiles selectedFiles;
final String tagPrefix;
2020-07-20 13:32:30 +00:00
final Widget headerWidget;
2021-02-05 19:40:55 +00:00
final bool isHomePageGallery;
2020-04-14 15:36:18 +00:00
Gallery({
2021-04-20 20:11:39 +00:00
@required this.asyncLoader,
2021-04-21 13:09:18 +00:00
@required this.creationTimesLoader,
@required this.selectedFiles,
@required this.tagPrefix,
2021-04-20 20:11:39 +00:00
this.reloadEvent,
this.headerWidget,
2021-02-08 08:43:36 +00:00
this.isHomePageGallery = false,
});
2020-04-14 15:36:18 +00:00
2020-03-28 18:18:27 +00:00
@override
_GalleryState createState() {
return _GalleryState();
2020-03-28 18:18:27 +00:00
}
}
class _GalleryState extends State<Gallery> {
2021-04-21 11:39:54 +00:00
static const int kPageSize = 10;
static const int kCacheSize = 256;
final _hugeListViewKey = GlobalKey<HugeListViewState>();
Logger _logger;
Map<int, HugeListViewPageResult<List<File>>> _map;
MapCache<int, HugeListViewPageResult<List<File>>> _cache;
2021-04-20 20:11:39 +00:00
int _pageIndex = 0;
List<int> _creationTimes;
bool _hasLoadedCreationTimes = false;
2020-06-10 18:17:54 +00:00
@override
void initState() {
_logger = Logger("Gallery_" + widget.tagPrefix);
_map = LruMap<int, HugeListViewPageResult<List<File>>>(
2021-04-21 11:39:54 +00:00
maximumSize: kCacheSize ~/ kPageSize);
_cache = MapCache<int, HugeListViewPageResult<List<File>>>(map: _map);
2020-06-16 12:56:23 +00:00
if (widget.reloadEvent != null) {
widget.reloadEvent.listen((event) {
if (mounted) {
_logger.info("Building gallery because reload event fired");
setState(() {
_hasLoadedCreationTimes = false;
_map.clear();
_loadCreationTimes();
});
}
2020-06-15 19:03:43 +00:00
});
2020-06-16 12:56:23 +00:00
}
widget.selectedFiles.addListener(() {
2021-02-05 18:13:06 +00:00
_logger.info("Building gallery because selected files updated");
2021-04-20 20:11:39 +00:00
setState(() {});
});
_loadCreationTimes();
super.initState();
}
void _loadCreationTimes() {
widget.creationTimesLoader().then((times) async {
if (mounted) {
setState(() {
_creationTimes = times;
_hasLoadedCreationTimes = true;
});
}
});
}
2020-11-12 13:25:57 +00:00
@override
void dispose() {
super.dispose();
}
2020-03-28 18:18:27 +00:00
@override
Widget build(BuildContext context) {
2020-11-16 08:28:43 +00:00
_logger.info("Building " + widget.tagPrefix);
if (!_hasLoadedCreationTimes) {
return loadWidget;
}
_logger.info("Creation times fetched " + _creationTimes.length.toString());
final collatedTimes = _collateCreationTimes(_creationTimes);
var gallery = _getListView(collatedTimes);
if (widget.isHomePageGallery) {
gallery = Container(
margin: const EdgeInsets.only(bottom: 50),
child: gallery,
);
if (widget.selectedFiles.files.isNotEmpty) {
gallery = Stack(children: [
gallery,
Container(
height: 60,
child: GalleryAppBarWidget(
GalleryAppBarType.homepage,
null,
widget.selectedFiles,
),
),
]);
}
}
return gallery;
2020-04-12 12:38:49 +00:00
}
2021-04-21 13:09:18 +00:00
Widget _getListView(List<List<int>> collatedTimes) {
return HugeListView<List<File>>(
key: _hugeListViewKey,
controller: ItemScrollController(),
pageSize: kPageSize,
startIndex: _pageIndex,
totalCount: collatedTimes.length,
isDraggableScrollbarEnabled: collatedTimes.length > 30,
cache: _cache,
map: _map,
pageFuture: (pageIndex) {
_pageIndex = pageIndex;
final endTimeIndex =
min(pageIndex * kPageSize, collatedTimes.length - 1);
final endTime = collatedTimes[endTimeIndex][0];
final startTimeIndex =
min((pageIndex + 1) * kPageSize, collatedTimes.length - 1);
final startTime = collatedTimes[startTimeIndex]
[collatedTimes[startTimeIndex].length - 1];
return widget
.asyncLoader(startTime, endTime)
.then((files) => _clubFiles(files));
},
placeholderBuilder: (context, pageIndex) {
var day = getDayWidget(collatedTimes[pageIndex][0]);
return Padding(
padding: const EdgeInsets.only(bottom: 12),
child: Column(
children: [
day,
PlaceHolderWidget(collatedTimes[pageIndex].length),
],
),
);
},
waitBuilder: (_) {
return loadWidget;
},
emptyResultBuilder: (_) {
return nothingToSeeHere;
},
itemBuilder: (context, index, files) {
var gallery;
gallery = LazyLoadingGallery(
files, widget.asyncLoader, widget.selectedFiles, widget.tagPrefix);
if (widget.headerWidget != null && index == 0) {
gallery = Column(children: [widget.headerWidget, gallery]);
}
return gallery;
},
labelTextBuilder: (int index) {
return getMonthAndYear(
DateTime.fromMicrosecondsSinceEpoch(collatedTimes[index][0]));
},
thumbBackgroundColor: Color(0xFF151515),
thumbDrawColor: Colors.white.withOpacity(0.5),
velocityThreshold: 128,
);
}
2021-04-20 20:11:39 +00:00
List<List<File>> _clubFiles(List<File> files) {
_logger.info("Clubbing file count " + files.length.toString());
2021-04-20 20:11:39 +00:00
final List<File> dailyFiles = [];
final List<List<File>> collatedFiles = [];
for (int index = 0; index < files.length; index++) {
2020-04-17 09:54:42 +00:00
if (index > 0 &&
2021-04-20 20:11:39 +00:00
!_areFromSameDay(
files[index - 1].creationTime, files[index].creationTime)) {
final List<File> collatedDailyFiles = [];
2020-06-19 23:03:26 +00:00
collatedDailyFiles.addAll(dailyFiles);
collatedFiles.add(collatedDailyFiles);
dailyFiles.clear();
2020-04-17 09:54:42 +00:00
}
2021-04-20 20:11:39 +00:00
dailyFiles.add(files[index]);
2020-04-17 09:54:42 +00:00
}
2020-06-19 23:03:26 +00:00
if (dailyFiles.isNotEmpty) {
collatedFiles.add(dailyFiles);
2020-04-17 09:54:42 +00:00
}
2021-04-20 20:11:39 +00:00
return collatedFiles;
}
List<List<int>> _collateCreationTimes(List<int> creationTimes) {
final List<int> dailyTimes = [];
final List<List<int>> collatedTimes = [];
for (int index = 0; index < creationTimes.length; index++) {
if (index > 0 &&
!_areFromSameDay(creationTimes[index - 1], creationTimes[index])) {
final List<int> collatedDailyTimes = [];
collatedDailyTimes.addAll(dailyTimes);
collatedTimes.add(collatedDailyTimes);
dailyTimes.clear();
}
dailyTimes.add(creationTimes[index]);
}
if (dailyTimes.isNotEmpty) {
collatedTimes.add(dailyTimes);
}
return collatedTimes;
2020-04-17 09:54:42 +00:00
}
2021-04-20 20:11:39 +00:00
bool _areFromSameDay(int firstCreationTime, int secondCreationTime) {
var firstDate = DateTime.fromMicrosecondsSinceEpoch(firstCreationTime);
var secondDate = DateTime.fromMicrosecondsSinceEpoch(secondCreationTime);
2020-04-17 09:54:42 +00:00
return firstDate.year == secondDate.year &&
firstDate.month == secondDate.month &&
firstDate.day == secondDate.day;
}
2020-03-28 18:18:27 +00:00
}