From 56df75a7a686eb0c913b33e0146283b65c6712a9 Mon Sep 17 00:00:00 2001 From: Thibault Deckers Date: Sun, 20 Sep 2020 10:38:55 +0900 Subject: [PATCH] fixed listing countries with multiple names --- lib/model/filters/location.dart | 2 +- lib/model/source/location.dart | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/model/filters/location.dart b/lib/model/filters/location.dart index 1042430d2..9f31a8e2f 100644 --- a/lib/model/filters/location.dart +++ b/lib/model/filters/location.dart @@ -31,7 +31,7 @@ class LocationFilter extends CollectionFilter { }; @override - bool filter(ImageEntry entry) => entry.isLocated && ((level == LocationLevel.country && entry.addressDetails.countryName == _location) || (level == LocationLevel.place && entry.addressDetails.place == _location)); + bool filter(ImageEntry entry) => entry.isLocated && ((level == LocationLevel.country && entry.addressDetails.countryCode == _countryCode) || (level == LocationLevel.place && entry.addressDetails.place == _location)); @override String get label => _location; diff --git a/lib/model/source/location.dart b/lib/model/source/location.dart index f5cd1f125..789a1e1eb 100644 --- a/lib/model/source/location.dart +++ b/lib/model/source/location.dart @@ -76,9 +76,13 @@ mixin LocationMixin on SourceBase { void updateLocations() { final locations = rawEntries.where((entry) => entry.isLocated).map((entry) => entry.addressDetails).toList(); - List lister(String Function(AddressDetails a) f) => List.unmodifiable(locations.map(f).where((s) => s != null && s.isNotEmpty).toSet().toList()..sort(compareAsciiUpperCase)); - sortedCountries = lister((address) => '${address.countryName}${LocationFilter.locationSeparator}${address.countryCode}'); - sortedPlaces = lister((address) => address.place); + sortedPlaces = List.unmodifiable(locations.map((address) => address.place).where((s) => s != null && s.isNotEmpty).toSet().toList()..sort(compareAsciiUpperCase)); + + // the same country code could be found with different country names + // e.g. if the locale changed between geolocating calls + // so we merge countries by code, keeping only one name for each code + final countriesByCode = Map.fromEntries(locations.map((address) => MapEntry(address.countryCode, address.countryName)).where((kv) => kv.key != null && kv.key.isNotEmpty)); + sortedCountries = List.unmodifiable(countriesByCode.entries.map((kv) => '${kv.value}${LocationFilter.locationSeparator}${kv.key}').toList()..sort(compareAsciiUpperCase)); invalidateFilterEntryCounts(); eventBus.fire(LocationsChangedEvent());