ente/lib/ui/gallery.dart

171 lines
4.6 KiB
Dart
Raw Normal View History

2020-04-12 12:38:49 +00:00
import 'dart:io';
import 'package:flutter/cupertino.dart';
2020-03-28 18:18:27 +00:00
import 'package:flutter/material.dart';
2020-04-12 12:38:49 +00:00
import 'package:myapp/db/db_helper.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';
import 'package:myapp/ui/image_widget.dart';
import 'package:myapp/utils/date_time_util.dart';
2020-03-28 18:18:27 +00:00
import 'package:provider/provider.dart';
2020-04-12 12:38:49 +00:00
import 'package:share_extend/share_extend.dart';
2020-03-28 18:18:27 +00:00
import 'detail_page.dart';
class Gallery extends StatefulWidget {
2020-04-14 15:36:18 +00:00
final List<List<Photo>> collatedPhotos;
const Gallery(this.collatedPhotos, {Key key}) : super(key: key);
2020-03-28 18:18:27 +00:00
@override
_GalleryState createState() {
return _GalleryState();
}
}
class _GalleryState extends State<Gallery> {
PhotoLoader get photoLoader => Provider.of<PhotoLoader>(context);
2020-04-12 12:38:49 +00:00
final ScrollController _scrollController = ScrollController();
2020-03-30 10:57:04 +00:00
2020-03-28 18:18:27 +00:00
@override
Widget build(BuildContext context) {
2020-04-14 15:36:18 +00:00
return ListView.builder(
itemCount: widget.collatedPhotos.length,
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-14 15:36:18 +00:00
var photos = widget.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-03-30 14:28:46 +00:00
routeToDetailPage(photo, context);
2020-03-30 10:57:04 +00:00
},
onLongPress: () {
2020-04-12 12:38:49 +00:00
_showPopup(photo, context);
2020-03-28 18:18:27 +00:00
},
2020-03-30 14:28:46 +00:00
child: Padding(
2020-04-11 22:11:33 +00:00
padding: const EdgeInsets.all(2.0),
2020-04-11 21:29:07 +00:00
child: ImageWidget(photo),
2020-03-28 18:18:27 +00:00
),
);
}
2020-04-12 12:38:49 +00:00
void _showPopup(Photo photo, BuildContext context) {
final action = CupertinoActionSheet(
actions: <Widget>[
CupertinoActionSheetAction(
child: Text("Share"),
isDefaultAction: true,
onPressed: () {
ShareExtend.share(photo.localPath, "image");
Navigator.pop(context);
},
),
CupertinoActionSheetAction(
child: Text("Delete"),
isDestructiveAction: true,
onPressed: () {
Navigator.pop(context);
_showDeletePopup(photo, context);
},
)
],
cancelButton: CupertinoActionSheetAction(
child: Text("Cancel"),
onPressed: () {
Navigator.pop(context);
},
),
);
showCupertinoModalPopup(context: context, builder: (_) => action);
}
void _showDeletePopup(Photo photo, BuildContext context) {
final action = CupertinoActionSheet(
actions: <Widget>[
CupertinoActionSheetAction(
child: Text("Delete on device"),
isDestructiveAction: true,
onPressed: () {
DatabaseHelper.instance.deletePhoto(photo).then((_) {
File file = File(photo.localPath);
file.delete().then((_) {
photoLoader.reloadPhotos();
Navigator.pop(context);
});
});
},
),
CupertinoActionSheetAction(
child: Text("Delete everywhere [WiP]"),
isDestructiveAction: true,
onPressed: () {
DatabaseHelper.instance.markPhotoAsDeleted(photo).then((_) {
File file = File(photo.localPath);
file.delete().then((_) {
photoLoader.reloadPhotos();
Navigator.pop(context);
});
});
},
)
],
cancelButton: CupertinoActionSheetAction(
child: Text("Cancel"),
onPressed: () {
Navigator.pop(context);
},
),
);
showCupertinoModalPopup(context: context, builder: (_) => action);
}
2020-04-05 14:00:44 +00:00
void routeToDetailPage(Photo photo, BuildContext context) {
2020-04-05 14:45:04 +00:00
final page = DetailPage(photo);
2020-03-28 18:18:27 +00:00
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return page;
},
),
);
}
}