2020-04-17 20:42:38 +00:00
|
|
|
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-03-30 14:28:46 +00:00
|
|
|
import 'package:myapp/models/photo.dart';
|
2020-03-28 18:18:27 +00:00
|
|
|
import 'package:myapp/photo_loader.dart';
|
2020-04-18 18:46:38 +00:00
|
|
|
import 'package:myapp/ui/detail_page.dart';
|
2020-03-28 18:18:27 +00:00
|
|
|
import 'package:myapp/ui/image_widget.dart';
|
2020-04-13 17:53:03 +00:00
|
|
|
import 'package:myapp/utils/date_time_util.dart';
|
2020-03-28 18:18:27 +00:00
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
|
|
|
|
class Gallery extends StatefulWidget {
|
2020-04-17 10:11:18 +00:00
|
|
|
final List<Photo> photos;
|
2020-04-18 18:46:38 +00:00
|
|
|
final Function(Set<Photo>) photoSelectionChangeCallback;
|
2020-04-14 15:36:18 +00:00
|
|
|
|
2020-04-17 20:42:38 +00:00
|
|
|
Gallery(this.photos, {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-17 20:42:38 +00:00
|
|
|
final Set<Photo> _selectedPhotos = HashSet<Photo>();
|
|
|
|
PhotoLoader get photoLoader => Provider.of<PhotoLoader>(context);
|
|
|
|
bool _shouldSelectOnTap = false;
|
2020-03-30 10:57:04 +00:00
|
|
|
|
2020-03-28 18:18:27 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
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-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(
|
2020-04-13 17:53:03 +00:00
|
|
|
children: <Widget>[
|
|
|
|
_getDay(photos[0].createTimestamp),
|
|
|
|
_getGallery(photos)
|
|
|
|
],
|
2020-04-13 15:01:27 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-13 17:53:03 +00:00
|
|
|
Widget _getDay(int timestamp) {
|
2020-04-13 15:01:27 +00:00
|
|
|
return Container(
|
2020-04-13 17:53:03 +00:00
|
|
|
padding: const EdgeInsets.all(8.0),
|
2020-04-13 15:01:27 +00:00
|
|
|
alignment: Alignment.centerLeft,
|
2020-04-13 17:53:03 +00:00
|
|
|
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-17 20:42:38 +00:00
|
|
|
if (_shouldSelectOnTap) {
|
|
|
|
_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();
|
2020-04-17 20:42:38 +00:00
|
|
|
_selectPhoto(photo);
|
2020-03-28 18:18:27 +00:00
|
|
|
},
|
2020-04-17 20:42:38 +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-04-17 20:42:38 +00:00
|
|
|
child: ImageWidget(photo),
|
2020-04-12 12:38:49 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-17 20:42:38 +00:00
|
|
|
void _selectPhoto(Photo photo) {
|
|
|
|
setState(() {
|
|
|
|
if (_selectedPhotos.contains(photo)) {
|
|
|
|
_selectedPhotos.remove(photo);
|
|
|
|
} else {
|
|
|
|
_selectedPhotos.add(photo);
|
|
|
|
}
|
|
|
|
if (_selectedPhotos.isNotEmpty) {
|
|
|
|
_shouldSelectOnTap = true;
|
|
|
|
} else {
|
|
|
|
_shouldSelectOnTap = false;
|
|
|
|
}
|
|
|
|
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-17 09:54:42 +00:00
|
|
|
final page = DetailPage(widget.photos, widget.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
|
|
|
|
|
|
|
void _collatePhotos() {
|
|
|
|
final dailyPhotos = List<Photo>();
|
|
|
|
final collatedPhotos = List<List<Photo>>();
|
|
|
|
for (int index = 0; index < widget.photos.length; index++) {
|
|
|
|
if (index > 0 &&
|
|
|
|
!_arePhotosFromSameDay(
|
|
|
|
widget.photos[index], widget.photos[index - 1])) {
|
|
|
|
var collatedDailyPhotos = List<Photo>();
|
|
|
|
collatedDailyPhotos.addAll(dailyPhotos);
|
|
|
|
collatedPhotos.add(collatedDailyPhotos);
|
|
|
|
dailyPhotos.clear();
|
|
|
|
}
|
|
|
|
dailyPhotos.add(widget.photos[index]);
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|