|
@@ -1,16 +1,25 @@
|
|
|
+import 'dart:async';
|
|
|
+
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
import 'package:flutter/material.dart';
|
|
|
-import 'package:latlong/latlong.dart';
|
|
|
+import 'package:photos/models/location.dart';
|
|
|
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';
|
|
|
|
|
|
+class ViewPort {
|
|
|
+ final Location northEast;
|
|
|
+ final Location southWest;
|
|
|
+
|
|
|
+ ViewPort(this.northEast, this.southWest);
|
|
|
+}
|
|
|
+
|
|
|
class LocationSearchResultsPage extends StatefulWidget {
|
|
|
- final LatLng location;
|
|
|
+ final ViewPort viewPort;
|
|
|
final String name;
|
|
|
|
|
|
- LocationSearchResultsPage(this.location, this.name, {Key key})
|
|
|
+ LocationSearchResultsPage(this.viewPort, this.name, {Key key})
|
|
|
: super(key: key);
|
|
|
|
|
|
@override
|
|
@@ -36,10 +45,15 @@ class _LocationSearchResultsPageState extends State<LocationSearchResultsPage> {
|
|
|
future: _getResult(),
|
|
|
builder: (context, snapshot) {
|
|
|
if (snapshot.hasData) {
|
|
|
+ if (snapshot.data.isEmpty) {
|
|
|
+ return Center(child: Text("Nothing to see here."));
|
|
|
+ }
|
|
|
return Gallery(
|
|
|
snapshot.data,
|
|
|
Set<Photo>(),
|
|
|
);
|
|
|
+ } else if (snapshot.hasError) {
|
|
|
+ return Center(child: Text(snapshot.error.toString()));
|
|
|
} else {
|
|
|
return Center(child: loadWidget);
|
|
|
}
|
|
@@ -47,26 +61,25 @@ class _LocationSearchResultsPageState extends State<LocationSearchResultsPage> {
|
|
|
);
|
|
|
}
|
|
|
|
|
|
- Future<List<Photo>> _getResult() async {
|
|
|
+ FutureOr<List<Photo>> _getResult() async {
|
|
|
final photos = PhotoRepository.instance.photos;
|
|
|
final args = Map<String, dynamic>();
|
|
|
args['photos'] = photos;
|
|
|
- args['location'] = widget.location;
|
|
|
- args['maxDistance'] = 5000;
|
|
|
- return await compute(_filterPhotos, args);
|
|
|
+ args['viewPort'] = widget.viewPort;
|
|
|
+ return _filterPhotos(args);
|
|
|
}
|
|
|
|
|
|
static List<Photo> _filterPhotos(Map<String, dynamic> args) {
|
|
|
List<Photo> photos = args['photos'];
|
|
|
- LatLng location = args['location'];
|
|
|
- int maxDistance = args['maxDistance'];
|
|
|
+ ViewPort viewPort = args['viewPort'];
|
|
|
final result = List<Photo>();
|
|
|
for (final photo in photos) {
|
|
|
- final distance = Distance().as(LengthUnit.Meter, location,
|
|
|
- new LatLng(photo.latitude, photo.longitude));
|
|
|
- if (distance < maxDistance) {
|
|
|
+ if (viewPort.northEast.latitude > photo.location.latitude &&
|
|
|
+ viewPort.southWest.latitude < photo.location.latitude &&
|
|
|
+ viewPort.northEast.longitude > photo.location.longitude &&
|
|
|
+ viewPort.southWest.longitude < photo.location.longitude) {
|
|
|
result.add(photo);
|
|
|
- }
|
|
|
+ } else {}
|
|
|
}
|
|
|
return result;
|
|
|
}
|