ente/lib/ui/gallery.dart

176 lines
4.9 KiB
Dart
Raw Normal View History

import 'dart:collection';
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-04-18 18:46:38 +00:00
import 'package:flutter/services.dart';
2020-05-01 18:20:12 +00:00
import 'package:photos/models/photo.dart';
import 'package:photos/ui/detail_page.dart';
import 'package:photos/ui/thumbnail_widget.dart';
import 'package:photos/utils/date_time_util.dart';
2020-03-28 18:18:27 +00:00
class Gallery extends StatefulWidget {
2020-04-17 10:11:18 +00:00
final List<Photo> photos;
2020-04-27 15:11:29 +00:00
final Set<Photo> selectedPhotos;
2020-04-18 18:46:38 +00:00
final Function(Set<Photo>) photoSelectionChangeCallback;
2020-04-14 15:36:18 +00:00
2020-04-27 15:11:29 +00:00
Gallery(this.photos, this.selectedPhotos,
{this.photoSelectionChangeCallback});
2020-04-14 15:36:18 +00:00
2020-03-28 18:18:27 +00:00
@override
_GalleryState createState() {
2020-04-18 18:46:38 +00:00
return _GalleryState();
2020-03-28 18:18:27 +00:00
}
}
class _GalleryState extends State<Gallery> {
2020-04-12 12:38:49 +00:00
final ScrollController _scrollController = ScrollController();
2020-04-17 09:54:42 +00:00
final List<List<Photo>> _collatedPhotos = List<List<Photo>>();
2020-04-27 15:11:29 +00:00
Set<Photo> _selectedPhotos = HashSet<Photo>();
2020-04-25 22:57:43 +00:00
List<Photo> _photos;
2020-05-04 20:03:06 +00:00
2020-03-28 18:18:27 +00:00
@override
Widget build(BuildContext context) {
2020-04-27 15:16:04 +00:00
// TODO: Investigate reason for multiple rebuilds on selection change
2020-04-27 13:02:29 +00:00
_photos = widget.photos;
2020-04-27 15:11:29 +00:00
_selectedPhotos = widget.selectedPhotos;
2020-04-27 15:28:03 +00:00
_deduplicatePhotos();
2020-04-17 09:54:42 +00:00
_collatePhotos();
2020-04-14 15:36:18 +00:00
return ListView.builder(
2020-04-17 09:54:42 +00:00
itemCount: _collatedPhotos.length,
2020-04-14 15:36:18 +00:00
itemBuilder: _buildListItem,
controller: _scrollController,
2020-06-06 11:06:43 +00:00
cacheExtent: 1000,
2020-04-14 15:36:18 +00:00
);
2020-03-28 18:18:27 +00:00
}
2020-04-13 15:01:27 +00:00
Widget _buildListItem(BuildContext context, int index) {
2020-04-17 09:54:42 +00:00
var photos = _collatedPhotos[index];
2020-04-13 15:01:27 +00:00
return Column(
children: <Widget>[
_getDay(photos[0].createTimestamp),
_getGallery(photos)
],
2020-04-13 15:01:27 +00:00
);
}
Widget _getDay(int timestamp) {
2020-04-13 15:01:27 +00:00
return Container(
padding: const EdgeInsets.all(8.0),
2020-04-13 15:01:27 +00:00
alignment: Alignment.centerLeft,
child: Text(
getDayAndMonth(DateTime.fromMicrosecondsSinceEpoch(timestamp)),
style: TextStyle(fontSize: 16),
),
2020-04-13 15:01:27 +00:00
);
}
Widget _getGallery(List<Photo> photos) {
return GridView.builder(
shrinkWrap: true,
padding: EdgeInsets.only(bottom: 12),
physics: ScrollPhysics(), // to disable GridView's scrolling
itemBuilder: (context, index) {
return _buildPhoto(context, photos[index]);
},
itemCount: photos.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 4,
),
);
}
Widget _buildPhoto(BuildContext context, Photo photo) {
2020-03-28 18:18:27 +00:00
return GestureDetector(
2020-04-05 14:00:44 +00:00
onTap: () {
2020-04-27 15:11:29 +00:00
if (_selectedPhotos.isNotEmpty) {
_selectPhoto(photo);
} else {
routeToDetailPage(photo, context);
}
2020-03-30 10:57:04 +00:00
},
onLongPress: () {
2020-04-18 18:46:38 +00:00
HapticFeedback.lightImpact();
_selectPhoto(photo);
2020-03-28 18:18:27 +00:00
},
child: Container(
margin: const EdgeInsets.all(2.0),
decoration: BoxDecoration(
border: _selectedPhotos.contains(photo)
? Border.all(width: 4.0, color: Colors.blue)
: null,
2020-04-12 12:38:49 +00:00
),
2020-06-06 11:06:43 +00:00
child: Hero(
tag: photo.generatedId,
child: ThumbnailWidget(photo),
),
2020-04-12 12:38:49 +00:00
),
);
}
void _selectPhoto(Photo photo) {
setState(() {
if (_selectedPhotos.contains(photo)) {
_selectedPhotos.remove(photo);
} else {
_selectedPhotos.add(photo);
}
widget.photoSelectionChangeCallback(_selectedPhotos);
});
2020-04-12 12:38:49 +00:00
}
2020-04-05 14:00:44 +00:00
void routeToDetailPage(Photo photo, BuildContext context) {
2020-04-25 22:57:43 +00:00
final page = DetailPage(
_photos,
_photos.indexOf(photo),
);
2020-03-28 18:18:27 +00:00
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return page;
},
),
);
}
2020-04-17 09:54:42 +00:00
2020-04-27 15:28:03 +00:00
void _deduplicatePhotos() {
for (int index = 1; index < _photos.length; index++) {
final current = _photos[index], previous = _photos[index - 1];
if (current.localId != null && current.localId == previous.localId) {
2020-04-27 15:28:03 +00:00
_photos.removeAt(index);
index--;
}
}
}
2020-04-17 09:54:42 +00:00
void _collatePhotos() {
final dailyPhotos = List<Photo>();
final collatedPhotos = List<List<Photo>>();
2020-04-25 22:57:43 +00:00
for (int index = 0; index < _photos.length; index++) {
2020-04-17 09:54:42 +00:00
if (index > 0 &&
2020-04-25 22:57:43 +00:00
!_arePhotosFromSameDay(_photos[index], _photos[index - 1])) {
2020-04-17 09:54:42 +00:00
var collatedDailyPhotos = List<Photo>();
collatedDailyPhotos.addAll(dailyPhotos);
collatedPhotos.add(collatedDailyPhotos);
dailyPhotos.clear();
}
2020-04-25 22:57:43 +00:00
dailyPhotos.add(_photos[index]);
2020-04-17 09:54:42 +00:00
}
if (dailyPhotos.isNotEmpty) {
collatedPhotos.add(dailyPhotos);
}
_collatedPhotos.clear();
_collatedPhotos.addAll(collatedPhotos);
}
bool _arePhotosFromSameDay(Photo firstPhoto, Photo secondPhoto) {
var firstDate =
DateTime.fromMicrosecondsSinceEpoch(firstPhoto.createTimestamp);
var secondDate =
DateTime.fromMicrosecondsSinceEpoch(secondPhoto.createTimestamp);
return firstDate.year == secondDate.year &&
firstDate.month == secondDate.month &&
firstDate.day == secondDate.day;
}
2020-03-28 18:18:27 +00:00
}