Refactor gallery
This commit is contained in:
parent
9fb790eefc
commit
d53c34659a
5 changed files with 67 additions and 68 deletions
|
@ -6,7 +6,6 @@ import 'package:myapp/models/photo.dart';
|
|||
class PhotoLoader extends ChangeNotifier {
|
||||
final logger = Logger();
|
||||
final _photos = List<Photo>();
|
||||
final _collatedPhotos = List<List<Photo>>();
|
||||
|
||||
PhotoLoader._privateConstructor();
|
||||
static final PhotoLoader instance = PhotoLoader._privateConstructor();
|
||||
|
@ -15,10 +14,6 @@ class PhotoLoader extends ChangeNotifier {
|
|||
return _photos;
|
||||
}
|
||||
|
||||
List<List<Photo>> get collatedPhotos {
|
||||
return _collatedPhotos;
|
||||
}
|
||||
|
||||
Future<bool> loadPhotos() async {
|
||||
DatabaseHelper db = DatabaseHelper.instance;
|
||||
var photos = await db.getAllPhotos();
|
||||
|
@ -26,24 +21,6 @@ class PhotoLoader extends ChangeNotifier {
|
|||
_photos.clear();
|
||||
_photos.addAll(photos);
|
||||
|
||||
final dailyPhotos = List<Photo>();
|
||||
final collatedPhotos = List<List<Photo>>();
|
||||
for (int index = 0; index < photos.length; index++) {
|
||||
if (index > 0 &&
|
||||
!_arePhotosFromSameDay(photos[index], photos[index - 1])) {
|
||||
var collatedDailyPhotos = List<Photo>();
|
||||
collatedDailyPhotos.addAll(dailyPhotos);
|
||||
collatedPhotos.add(collatedDailyPhotos);
|
||||
dailyPhotos.clear();
|
||||
}
|
||||
dailyPhotos.add(photos[index]);
|
||||
}
|
||||
if (dailyPhotos.isNotEmpty) {
|
||||
collatedPhotos.add(dailyPhotos);
|
||||
}
|
||||
_collatedPhotos.clear();
|
||||
_collatedPhotos.addAll(collatedPhotos);
|
||||
|
||||
logger.i("Imported photo size: " + _photos.length.toString());
|
||||
|
||||
return true;
|
||||
|
@ -54,14 +31,4 @@ class PhotoLoader extends ChangeNotifier {
|
|||
logger.i("Reloading...");
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,9 +14,11 @@ import 'package:share_extend/share_extend.dart';
|
|||
import 'detail_page.dart';
|
||||
|
||||
class Gallery extends StatefulWidget {
|
||||
final List<List<Photo>> collatedPhotos;
|
||||
final List<Photo> photos = List<Photo>();
|
||||
|
||||
const Gallery(this.collatedPhotos, {Key key}) : super(key: key);
|
||||
Gallery(List<Photo> photoList) {
|
||||
this.photos.addAll(photoList);
|
||||
}
|
||||
|
||||
@override
|
||||
_GalleryState createState() {
|
||||
|
@ -27,18 +29,21 @@ class Gallery extends StatefulWidget {
|
|||
class _GalleryState extends State<Gallery> {
|
||||
PhotoLoader get photoLoader => Provider.of<PhotoLoader>(context);
|
||||
final ScrollController _scrollController = ScrollController();
|
||||
final List<List<Photo>> _collatedPhotos = List<List<Photo>>();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
_collatePhotos();
|
||||
|
||||
return ListView.builder(
|
||||
itemCount: widget.collatedPhotos.length,
|
||||
itemCount: _collatedPhotos.length,
|
||||
itemBuilder: _buildListItem,
|
||||
controller: _scrollController,
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildListItem(BuildContext context, int index) {
|
||||
var photos = widget.collatedPhotos[index];
|
||||
var photos = _collatedPhotos[index];
|
||||
return Column(
|
||||
children: <Widget>[
|
||||
_getDay(photos[0].createTimestamp),
|
||||
|
@ -160,7 +165,7 @@ class _GalleryState extends State<Gallery> {
|
|||
}
|
||||
|
||||
void routeToDetailPage(Photo photo, BuildContext context) {
|
||||
final page = DetailPage(photoLoader.photos, photoLoader.photos.indexOf(photo));
|
||||
final page = DetailPage(widget.photos, widget.photos.indexOf(photo));
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (BuildContext context) {
|
||||
|
@ -169,4 +174,35 @@ class _GalleryState extends State<Gallery> {
|
|||
),
|
||||
);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:logger/logger.dart';
|
||||
import 'package:myapp/models/photo.dart';
|
||||
import 'package:myapp/ui/change_notifier_builder.dart';
|
||||
import 'package:myapp/ui/search_page.dart';
|
||||
import 'package:myapp/utils/camera_items_filter.dart';
|
||||
import 'package:myapp/utils/important_items_filter.dart';
|
||||
import 'package:myapp/utils/gallery_items_filter.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
|
@ -14,7 +15,7 @@ import 'loading_widget.dart';
|
|||
class GalleryContainer extends StatelessWidget {
|
||||
final GalleryType type;
|
||||
|
||||
static final importantItemsFilter = CameraItemsFilter();
|
||||
static final importantItemsFilter = ImportantItemsFilter();
|
||||
static final galleryItemsFilter = GalleryItemsFilter();
|
||||
|
||||
const GalleryContainer(
|
||||
|
@ -55,8 +56,7 @@ class GalleryContainer extends StatelessWidget {
|
|||
child: ChangeNotifierBuilder(
|
||||
value: photoLoader,
|
||||
builder: (_, __) {
|
||||
var collatedPhotos = photoLoader.collatedPhotos;
|
||||
return Flexible(child: _getGallery(collatedPhotos));
|
||||
return Flexible(child: _getGallery(photoLoader.photos));
|
||||
}),
|
||||
);
|
||||
} else if (snapshot.hasError) {
|
||||
|
@ -70,27 +70,21 @@ class GalleryContainer extends StatelessWidget {
|
|||
);
|
||||
}
|
||||
|
||||
Gallery _getGallery(List<List<Photo>> collatedPhotos) {
|
||||
Gallery _getGallery(List<Photo> photos) {
|
||||
return type == GalleryType.important_photos
|
||||
? Gallery(getCollatedPhotos(collatedPhotos, importantItemsFilter))
|
||||
: Gallery(getCollatedPhotos(collatedPhotos, galleryItemsFilter));
|
||||
? Gallery(getFilteredPhotos(photos, importantItemsFilter))
|
||||
: Gallery(getFilteredPhotos(photos, galleryItemsFilter));
|
||||
}
|
||||
|
||||
List<List<Photo>> getCollatedPhotos(
|
||||
List<List<Photo>> source, GalleryItemsFilter filter) {
|
||||
final List<List<Photo>> collatedList = List<List<Photo>>();
|
||||
for (List<Photo> unfilteredPhotos in source) {
|
||||
final List<Photo> filteredPhotos = List<Photo>();
|
||||
for (Photo photo in unfilteredPhotos) {
|
||||
if (filter.shouldInclude(photo)) {
|
||||
filteredPhotos.add(photo);
|
||||
}
|
||||
}
|
||||
if (filteredPhotos.isNotEmpty) {
|
||||
collatedList.add(filteredPhotos);
|
||||
List<Photo> getFilteredPhotos(
|
||||
List<Photo> unfilteredPhotos, GalleryItemsFilter filter) {
|
||||
final List<Photo> filteredPhotos = List<Photo>();
|
||||
for (Photo photo in unfilteredPhotos) {
|
||||
if (filter.shouldInclude(photo)) {
|
||||
filteredPhotos.add(photo);
|
||||
}
|
||||
}
|
||||
return collatedList;
|
||||
return filteredPhotos;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,10 +0,0 @@
|
|||
import 'package:myapp/models/photo.dart';
|
||||
import 'package:myapp/utils/gallery_items_filter.dart';
|
||||
|
||||
class CameraItemsFilter implements GalleryItemsFilter {
|
||||
@override
|
||||
bool shouldInclude(Photo photo) {
|
||||
// TODO: Improve logic
|
||||
return photo.localPath.contains("Camera");
|
||||
}
|
||||
}
|
12
lib/utils/important_items_filter.dart
Normal file
12
lib/utils/important_items_filter.dart
Normal file
|
@ -0,0 +1,12 @@
|
|||
import 'package:myapp/models/photo.dart';
|
||||
import 'package:myapp/utils/gallery_items_filter.dart';
|
||||
|
||||
class ImportantItemsFilter implements GalleryItemsFilter {
|
||||
@override
|
||||
bool shouldInclude(Photo photo) {
|
||||
// TODO: Improve logic
|
||||
return photo.localPath.contains("/Camera/") ||
|
||||
photo.localPath.contains("/Download/") ||
|
||||
photo.localPath.contains("/Screenshots/");
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue