Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Load and parse cities

vishnukvmd5d25785eddfc
diff --git a/lib/services/location_service.dart b/lib/services/location_service.dart
index 10e3bfd727..516adff01b 100644
--- a/lib/services/location_service.dart
+++ b/lib/services/location_service.dart
@@ -10,11 +10,13 @@ import "package:photos/models/local_entity_data.dart";
import "package:photos/models/location/location.dart";
import 'package:photos/models/location_tag/location_tag.dart';
import "package:photos/services/entity_service.dart";
+import "package:photos/services/remote_assets_service.dart";
import "package:shared_preferences/shared_preferences.dart";
class LocationService {
late SharedPreferences prefs;
final Logger _logger = Logger((LocationService).toString());
+ final List<City> _cities = [];
LocationService._privateConstructor();
@@ -22,6 +24,7 @@ class LocationService {
void init(SharedPreferences preferences) {
prefs = preferences;
+ _loadCities();
}
Future<Iterable<LocalEntity<LocationTag>>> _getStoredLocationTags() async {
@@ -203,6 +206,52 @@ class LocationService {
rethrow;
}
}
+
+ Future<void> _loadCities() async {
+ try {
+ final data = await RemoteAssetsService.instance
+ .getAsset("https://assets.ente.io/world_cities.json");
+ final citiesJson = json.decode(await data.readAsString());
+ final List<dynamic> jsonData = citiesJson['data'];
+ final cities =
+ jsonData.map<City>((jsonItem) => City.fromMap(jsonItem)).toList();
+ _cities.clear();
+ _cities.addAll(cities);
+ _logger.info("Loaded cities");
+ } catch (e, s) {
+ _logger.severe("Failed to load cities", e, s);
+ }
+ }
+}
+
+class City {
+ final String city;
+ final String country;
+ final double lat;
+ final double lng;
+
+ City({
+ required this.city,
+ required this.country,
+ required this.lat,
+ required this.lng,
+ });
+
+ factory City.fromMap(Map<String, dynamic> map) {
+ return City(
+ city: map['city'] ?? '',
+ country: map['country'] ?? '',
+ lat: map['lat']?.toDouble() ?? 0.0,
+ lng: map['lng']?.toDouble() ?? 0.0,
+ );
+ }
+
+ factory City.fromJson(String source) => City.fromMap(json.decode(source));
+
+ @override
+ String toString() {
+ return 'City(city: $city, country: $country, lat: $lat, lng: $lng)';
+ }
}
class GPSData {