Browse Source

Change location search logic

Vishnu Mohandas 5 years ago
parent
commit
125cbb0816

+ 4 - 4
lib/db/photo_db.dart

@@ -1,6 +1,7 @@
 import 'dart:io';
 
 import 'package:logging/logging.dart';
+import 'package:photos/models/location.dart';
 import 'package:photos/models/photo.dart';
 import 'package:path/path.dart';
 import 'package:sqflite/sqflite.dart';
@@ -287,8 +288,8 @@ class PhotoDB {
         photo.uploadedFileId == null ? -1 : photo.uploadedFileId;
     row[columnTitle] = photo.title;
     row[columnDeviceFolder] = photo.deviceFolder;
-    row[columnLatitude] = photo.latitude;
-    row[columnLongitude] = photo.longitude;
+    row[columnLatitude] = photo.location.latitude;
+    row[columnLongitude] = photo.location.longitude;
     row[columnRemoteFolderId] = photo.remoteFolderId;
     row[columnRemotePath] = photo.remotePath;
     row[columnThumbnailPath] = photo.thumbnailPath;
@@ -304,8 +305,7 @@ class PhotoDB {
     photo.uploadedFileId = row[columnUploadedFileId];
     photo.title = row[columnTitle];
     photo.deviceFolder = row[columnDeviceFolder];
-    photo.latitude = row[columnLatitude];
-    photo.longitude = row[columnLongitude];
+    photo.location = Location(row[columnLatitude], row[columnLongitude]);
     photo.remoteFolderId = row[columnRemoteFolderId];
     photo.remotePath = row[columnRemotePath];
     photo.thumbnailPath = row[columnThumbnailPath];

+ 6 - 0
lib/models/location.dart

@@ -0,0 +1,6 @@
+class Location {
+  final double latitude;
+  final double longitude;
+
+  Location(this.latitude, this.longitude);
+}

+ 3 - 4
lib/models/photo.dart

@@ -6,6 +6,7 @@ import 'package:flutter_image_compress/flutter_image_compress.dart';
 import 'package:photo_manager/photo_manager.dart';
 import 'package:path/path.dart';
 import 'package:photos/core/configuration.dart';
+import 'package:photos/models/location.dart';
 
 class Photo {
   int generatedId;
@@ -18,8 +19,7 @@ class Photo {
   String thumbnailPath;
   int createTimestamp;
   int updateTimestamp;
-  double latitude;
-  double longitude;
+  Location location;
 
   Photo();
   Photo.fromJson(Map<String, dynamic> json)
@@ -40,8 +40,7 @@ class Photo {
     photo.title = asset.title;
     photo.deviceFolder = pathEntity.name;
     final location = await asset.latlngAsync();
-    photo.latitude = location.latitude;
-    photo.longitude = location.longitude;
+    photo.location = Location(location.latitude, location.longitude);
     photo.createTimestamp = asset.createDateTime.microsecondsSinceEpoch;
     if (photo.createTimestamp == 0) {
       try {

+ 26 - 13
lib/ui/location_search_results_page.dart

@@ -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;
   }

+ 13 - 12
lib/ui/search_page.dart

@@ -1,14 +1,10 @@
-import 'dart:developer';
-
 import 'package:dio/dio.dart';
 import 'package:flutter/material.dart';
 import 'package:flutter_typeahead/flutter_typeahead.dart';
-import 'package:latlong/latlong.dart';
 import 'package:photos/core/configuration.dart';
 import 'package:photos/face_search_manager.dart';
 import 'package:photos/models/face.dart';
-import 'package:photos/models/photo.dart';
-import 'package:photos/photo_repository.dart';
+import 'package:photos/models/location.dart';
 import 'package:photos/ui/circular_network_image_widget.dart';
 import 'package:photos/ui/face_search_results_page.dart';
 import 'package:photos/ui/loading_widget.dart';
@@ -41,7 +37,7 @@ class _SearchPageState extends State<SearchPage> {
           loadingBuilder: (context) {
             return loadWidget;
           },
-          debounceDuration: Duration(milliseconds: 100),
+          debounceDuration: Duration(milliseconds: 300),
           suggestionsCallback: (pattern) async {
             if (pattern.isEmpty) {
               return null;
@@ -55,18 +51,23 @@ class _SearchPageState extends State<SearchPage> {
                 .data["results"];
           },
           itemBuilder: (context, suggestion) {
-            if (suggestion == null) {
-              return null;
-            }
             return LocationSearchResultWidget(suggestion['name']);
           },
           onSuggestionSelected: (suggestion) {
-            double latitude = suggestion['geometry']['location']['lat'];
-            double longitude = suggestion['geometry']['location']['lng'];
             Navigator.pop(context);
             Navigator.of(context).push(MaterialPageRoute(
                 builder: (context) => LocationSearchResultsPage(
-                      new LatLng(latitude, longitude),
+                      ViewPort(
+                          Location(
+                              suggestion['geometry']['viewport']['northeast']
+                                  ['lat'],
+                              suggestion['geometry']['viewport']['northeast']
+                                  ['lng']),
+                          Location(
+                              suggestion['geometry']['viewport']['southwest']
+                                  ['lat'],
+                              suggestion['geometry']['viewport']['southwest']
+                                  ['lng'])),
                       suggestion['name'],
                     )));
           },

+ 0 - 28
pubspec.lock

@@ -1,13 +1,6 @@
 # Generated by pub
 # See https://dart.dev/tools/pub/glossary#lockfile
 packages:
-  ansicolor:
-    dependency: transitive
-    description:
-      name: ansicolor
-      url: "https://pub.dartlang.org"
-    source: hosted
-    version: "1.0.2"
   archive:
     dependency: "direct main"
     description:
@@ -71,13 +64,6 @@ packages:
       url: "https://pub.dartlang.org"
     source: hosted
     version: "1.0.5"
-  console_log_handler:
-    dependency: transitive
-    description:
-      name: console_log_handler
-      url: "https://pub.dartlang.org"
-    source: hosted
-    version: "1.1.6"
   convert:
     dependency: transitive
     description:
@@ -226,13 +212,6 @@ packages:
       url: "https://pub.dartlang.org"
     source: hosted
     version: "3.0.1"
-  latlong:
-    dependency: "direct main"
-    description:
-      name: latlong
-      url: "https://pub.dartlang.org"
-    source: hosted
-    version: "0.6.1"
   like_button:
     dependency: "direct main"
     description:
@@ -504,13 +483,6 @@ packages:
       url: "https://pub.dartlang.org"
     source: hosted
     version: "3.4.1"
-  validate:
-    dependency: transitive
-    description:
-      name: validate
-      url: "https://pub.dartlang.org"
-    source: hosted
-    version: "1.7.0"
   vector_math:
     dependency: transitive
     description:

+ 0 - 1
pubspec.yaml

@@ -48,7 +48,6 @@ dependencies:
   logging: ^0.11.4
   flutter_image_compress: ^0.6.5+1
   flutter_typeahead: ^1.8.1
-  latlong: ^0.6.1
 
 dev_dependencies:
   flutter_test: