2020-04-14 15:36:18 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/widgets.dart';
|
2020-04-17 09:54:42 +00:00
|
|
|
import 'package:logger/logger.dart';
|
2020-04-14 15:36:18 +00:00
|
|
|
import 'package:myapp/models/photo.dart';
|
|
|
|
import 'package:myapp/ui/change_notifier_builder.dart';
|
|
|
|
import 'package:myapp/ui/search_page.dart';
|
2020-04-17 09:54:42 +00:00
|
|
|
import 'package:myapp/utils/important_items_filter.dart';
|
2020-04-14 15:36:18 +00:00
|
|
|
import 'package:myapp/utils/gallery_items_filter.dart';
|
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
|
|
|
|
import '../photo_loader.dart';
|
|
|
|
import 'gallery.dart';
|
|
|
|
import 'loading_widget.dart';
|
|
|
|
|
2020-04-17 20:42:38 +00:00
|
|
|
// TODO: Remove redundant layer
|
|
|
|
class GalleryContainer extends StatefulWidget {
|
2020-04-14 15:36:18 +00:00
|
|
|
final GalleryType type;
|
2020-04-17 20:42:38 +00:00
|
|
|
final Function(Set<Photo>) photoSelectionChangeCallback;
|
2020-04-14 15:36:18 +00:00
|
|
|
|
2020-04-17 09:54:42 +00:00
|
|
|
static final importantItemsFilter = ImportantItemsFilter();
|
2020-04-14 15:36:18 +00:00
|
|
|
static final galleryItemsFilter = GalleryItemsFilter();
|
|
|
|
|
|
|
|
const GalleryContainer(
|
|
|
|
this.type, {
|
|
|
|
Key key,
|
2020-04-17 20:42:38 +00:00
|
|
|
this.photoSelectionChangeCallback,
|
2020-04-14 15:36:18 +00:00
|
|
|
}) : super(key: key);
|
|
|
|
|
2020-04-17 20:42:38 +00:00
|
|
|
@override
|
|
|
|
_GalleryContainerState createState() => _GalleryContainerState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _GalleryContainerState extends State<GalleryContainer> {
|
|
|
|
PhotoLoader get photoLoader => Provider.of<PhotoLoader>(context);
|
|
|
|
|
2020-04-14 15:36:18 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Column(
|
|
|
|
children: <Widget>[
|
|
|
|
Hero(
|
|
|
|
child: TextField(
|
|
|
|
readOnly: true,
|
|
|
|
onTap: () {
|
|
|
|
Navigator.of(context).push(
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return SearchPage();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
decoration: InputDecoration(
|
|
|
|
border: InputBorder.none,
|
|
|
|
hintText: 'Search "Paris"',
|
|
|
|
contentPadding: const EdgeInsets.all(12.0),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
tag: "search"),
|
|
|
|
FutureBuilder<bool>(
|
|
|
|
future: photoLoader.loadPhotos(),
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (snapshot.hasData) {
|
2020-04-17 20:42:38 +00:00
|
|
|
return ChangeNotifierBuilder(
|
|
|
|
value: photoLoader,
|
|
|
|
builder: (_, __) {
|
|
|
|
return Flexible(child: _getGallery(photoLoader.photos));
|
|
|
|
});
|
2020-04-14 15:36:18 +00:00
|
|
|
} else if (snapshot.hasError) {
|
|
|
|
return Text("Error!");
|
|
|
|
} else {
|
|
|
|
return loadWidget;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-17 09:54:42 +00:00
|
|
|
Gallery _getGallery(List<Photo> photos) {
|
2020-04-17 20:42:38 +00:00
|
|
|
return widget.type == GalleryType.important_photos
|
|
|
|
? Gallery(
|
|
|
|
getFilteredPhotos(photos, GalleryContainer.importantItemsFilter),
|
|
|
|
photoSelectionChangeCallback: widget.photoSelectionChangeCallback,
|
|
|
|
)
|
|
|
|
: Gallery(
|
|
|
|
getFilteredPhotos(photos, GalleryContainer.galleryItemsFilter),
|
|
|
|
photoSelectionChangeCallback: widget.photoSelectionChangeCallback,
|
|
|
|
);
|
2020-04-14 15:36:18 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 09:54:42 +00:00
|
|
|
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);
|
2020-04-14 15:36:18 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-17 09:54:42 +00:00
|
|
|
return filteredPhotos;
|
2020-04-14 15:36:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum GalleryType {
|
|
|
|
important_photos,
|
|
|
|
all_photos,
|
|
|
|
}
|