2020-06-03 16:06:49 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
2020-06-03 14:11:34 +00:00
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2020-06-03 16:06:49 +00:00
|
|
|
import 'package:photos/models/location.dart';
|
2020-06-03 14:11:34 +00:00
|
|
|
import 'package:photos/models/photo.dart';
|
|
|
|
import 'package:photos/photo_repository.dart';
|
|
|
|
import 'package:photos/ui/gallery.dart';
|
|
|
|
import 'package:photos/ui/loading_widget.dart';
|
|
|
|
|
2020-06-03 16:06:49 +00:00
|
|
|
class ViewPort {
|
|
|
|
final Location northEast;
|
|
|
|
final Location southWest;
|
|
|
|
|
|
|
|
ViewPort(this.northEast, this.southWest);
|
2020-06-06 21:12:49 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
String toString() => 'ViewPort(northEast: $northEast, southWest: $southWest)';
|
2020-06-03 16:06:49 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 14:11:34 +00:00
|
|
|
class LocationSearchResultsPage extends StatefulWidget {
|
2020-06-03 16:06:49 +00:00
|
|
|
final ViewPort viewPort;
|
2020-06-03 14:11:34 +00:00
|
|
|
final String name;
|
|
|
|
|
2020-06-03 16:06:49 +00:00
|
|
|
LocationSearchResultsPage(this.viewPort, this.name, {Key key})
|
2020-06-03 14:11:34 +00:00
|
|
|
: super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
_LocationSearchResultsPageState createState() =>
|
|
|
|
_LocationSearchResultsPageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _LocationSearchResultsPageState extends State<LocationSearchResultsPage> {
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: Text(widget.name),
|
|
|
|
),
|
|
|
|
body: Container(
|
|
|
|
child: _getBody(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
FutureBuilder<List<Photo>> _getBody() {
|
|
|
|
return FutureBuilder<List<Photo>>(
|
|
|
|
future: _getResult(),
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (snapshot.hasData) {
|
2020-06-03 16:06:49 +00:00
|
|
|
if (snapshot.data.isEmpty) {
|
|
|
|
return Center(child: Text("Nothing to see here."));
|
|
|
|
}
|
2020-06-03 14:11:34 +00:00
|
|
|
return Gallery(
|
|
|
|
snapshot.data,
|
|
|
|
Set<Photo>(),
|
|
|
|
);
|
2020-06-03 16:06:49 +00:00
|
|
|
} else if (snapshot.hasError) {
|
|
|
|
return Center(child: Text(snapshot.error.toString()));
|
2020-06-03 14:11:34 +00:00
|
|
|
} else {
|
|
|
|
return Center(child: loadWidget);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-03 16:06:49 +00:00
|
|
|
FutureOr<List<Photo>> _getResult() async {
|
2020-06-03 14:11:34 +00:00
|
|
|
final photos = PhotoRepository.instance.photos;
|
|
|
|
final args = Map<String, dynamic>();
|
|
|
|
args['photos'] = photos;
|
2020-06-03 16:06:49 +00:00
|
|
|
args['viewPort'] = widget.viewPort;
|
|
|
|
return _filterPhotos(args);
|
2020-06-03 14:11:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static List<Photo> _filterPhotos(Map<String, dynamic> args) {
|
|
|
|
List<Photo> photos = args['photos'];
|
2020-06-03 16:06:49 +00:00
|
|
|
ViewPort viewPort = args['viewPort'];
|
2020-06-03 14:11:34 +00:00
|
|
|
final result = List<Photo>();
|
|
|
|
for (final photo in photos) {
|
2020-06-06 21:12:49 +00:00
|
|
|
if (photo.location != null &&
|
|
|
|
viewPort.northEast.latitude > photo.location.latitude &&
|
2020-06-03 16:06:49 +00:00
|
|
|
viewPort.southWest.latitude < photo.location.latitude &&
|
|
|
|
viewPort.northEast.longitude > photo.location.longitude &&
|
|
|
|
viewPort.southWest.longitude < photo.location.longitude) {
|
2020-06-03 14:11:34 +00:00
|
|
|
result.add(photo);
|
2020-06-03 16:06:49 +00:00
|
|
|
} else {}
|
2020-06-03 14:11:34 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|