From 85a0c504d6fbb0067f625c579160a74419fec3e2 Mon Sep 17 00:00:00 2001 From: Thibault Deckers Date: Sun, 17 Oct 2021 19:46:29 +0900 Subject: [PATCH] country locale fixes --- lib/model/entry.dart | 22 ++++++------------- lib/model/settings/settings.dart | 12 ++++++++++ lib/model/source/collection_source.dart | 2 +- lib/model/source/location.dart | 13 ++++++++--- lib/services/analysis_service.dart | 4 +--- lib/services/geocoding_service.dart | 5 +++-- lib/widgets/map/map_info_row.dart | 2 +- lib/widgets/viewer/entry_vertical_pager.dart | 3 ++- .../info/entry_info_action_delegate.dart | 3 ++- lib/widgets/viewer/info/location_section.dart | 2 +- 10 files changed, 40 insertions(+), 28 deletions(-) diff --git a/lib/model/entry.dart b/lib/model/entry.dart index 3adbd3b08..a3bd9f7d3 100644 --- a/lib/model/entry.dart +++ b/lib/model/entry.dart @@ -1,5 +1,6 @@ import 'dart:async'; import 'dart:io'; +import 'dart:ui'; import 'package:aves/geo/countries.dart'; import 'package:aves/model/entry_cache.dart'; @@ -9,7 +10,6 @@ import 'package:aves/model/metadata/catalog.dart'; import 'package:aves/model/metadata/date_modifier.dart'; import 'package:aves/model/metadata/enums.dart'; import 'package:aves/model/multipage.dart'; -import 'package:aves/model/settings/settings.dart'; import 'package:aves/model/video/metadata.dart'; import 'package:aves/ref/mime_types.dart'; import 'package:aves/services/common/service_policy.dart'; @@ -21,7 +21,6 @@ import 'package:aves/utils/change_notifier.dart'; import 'package:collection/collection.dart'; import 'package:country_code/country_code.dart'; import 'package:flutter/foundation.dart'; -import 'package:flutter/material.dart'; import 'package:latlong2/latlong.dart'; class AvesEntry { @@ -469,11 +468,11 @@ class AvesEntry { addressChangeNotifier.notifyListeners(); } - Future locate({required bool background, required bool force}) async { + Future locate({required bool background, required bool force, required Locale geocoderLocale}) async { if (!hasGps) return; await _locateCountry(force: force); if (await availability.canLocatePlaces) { - await locatePlace(background: background, force: force); + await locatePlace(background: background, force: force, geocoderLocale: geocoderLocale); } } @@ -493,15 +492,8 @@ class AvesEntry { ); } - String? _geocoderLocale; - - String get geocoderLocale { - _geocoderLocale ??= (settings.locale ?? WidgetsBinding.instance!.window.locale).toString(); - return _geocoderLocale!; - } - // full reverse geocoding, requiring Play Services and some connectivity - Future locatePlace({required bool background, required bool force}) async { + Future locatePlace({required bool background, required bool force, required Locale geocoderLocale}) async { if (!hasGps || (hasFineAddress && !force)) return; try { Future> call() => GeocodingService.getAddress(latLng!, geocoderLocale); @@ -531,7 +523,7 @@ class AvesEntry { } } - Future findAddressLine() async { + Future findAddressLine({required Locale geocoderLocale}) async { if (!hasGps) return null; try { @@ -608,7 +600,7 @@ class AvesEntry { metadataChangeNotifier.notifyListeners(); } - Future refresh({required bool background, required bool persist, required bool force}) async { + Future refresh({required bool background, required bool persist, required bool force, required Locale geocoderLocale}) async { _catalogMetadata = null; _addressDetails = null; _bestDate = null; @@ -622,7 +614,7 @@ class AvesEntry { if (updated != null) { await _applyNewFields(updated.toMap(), persist: persist); await catalog(background: background, persist: persist, force: force); - await locate(background: background, force: force); + await locate(background: background, force: force, geocoderLocale: geocoderLocale); } } diff --git a/lib/model/settings/settings.dart b/lib/model/settings/settings.dart index 65259cf4f..cccc52f81 100644 --- a/lib/model/settings/settings.dart +++ b/lib/model/settings/settings.dart @@ -15,6 +15,7 @@ import 'package:aves/services/common/services.dart'; import 'package:collection/collection.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; +import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:shared_preferences/shared_preferences.dart'; final Settings settings = Settings._private(); @@ -202,6 +203,17 @@ class Settings extends ChangeNotifier { ].join(localeSeparator); } setAndNotify(localeKey, tag); + _appliedLocale = null; + } + + Locale? _appliedLocale; + + Locale get appliedLocale { + if (_appliedLocale == null) { + final preferredLocale = locale; + _appliedLocale = basicLocaleListResolution(preferredLocale != null ? [preferredLocale] : null, AppLocalizations.supportedLocales); + } + return _appliedLocale!; } bool get mustBackTwiceToExit => getBoolOrDefault(mustBackTwiceToExitKey, SettingsDefaults.mustBackTwiceToExit); diff --git a/lib/model/source/collection_source.dart b/lib/model/source/collection_source.dart index 1ffa90da9..95f8cd9c7 100644 --- a/lib/model/source/collection_source.dart +++ b/lib/model/source/collection_source.dart @@ -264,7 +264,7 @@ abstract class CollectionSource with SourceBase, AlbumMixin, LocationMixin, TagM Future> refreshUris(Set changedUris, {AnalysisController? analysisController}); Future refreshEntry(AvesEntry entry) async { - await entry.refresh(background: false, persist: true, force: true); + await entry.refresh(background: false, persist: true, force: true, geocoderLocale: settings.appliedLocale); updateDerivedFilters({entry}); eventBus.fire(EntryRefreshedEvent({entry})); } diff --git a/lib/model/source/location.dart b/lib/model/source/location.dart index cd5f34168..db1c096ad 100644 --- a/lib/model/source/location.dart +++ b/lib/model/source/location.dart @@ -4,6 +4,7 @@ import 'package:aves/geo/countries.dart'; import 'package:aves/model/entry.dart'; import 'package:aves/model/filters/location.dart'; import 'package:aves/model/metadata/address.dart'; +import 'package:aves/model/settings/settings.dart'; import 'package:aves/model/source/analysis_controller.dart'; import 'package:aves/model/source/collection_source.dart'; import 'package:aves/model/source/enums.dart'; @@ -110,7 +111,7 @@ mixin LocationMixin on SourceBase { if (knownLocations.containsKey(latLng)) { entry.addressDetails = knownLocations[latLng]?.copyWith(contentId: entry.contentId); } else { - await entry.locatePlace(background: true, force: force); + await entry.locatePlace(background: true, force: force, geocoderLocale: settings.appliedLocale); // it is intended to insert `null` if the geocoder failed, // so that we skip geocoding of following entries with the same coordinates knownLocations[latLng] = entry.addressDetails; @@ -153,9 +154,15 @@ mixin LocationMixin on SourceBase { // so we merge countries by code, keeping only one name for each code final countriesByCode = Map.fromEntries(locations.map((address) { final code = address.countryCode; - return code?.isNotEmpty == true ? MapEntry(code, address.countryName) : null; + if (code == null || code.isEmpty) return null; + return MapEntry(code, address.countryName); }).whereNotNull()); - final updatedCountries = countriesByCode.entries.map((kv) => '${kv.value}${LocationFilter.locationSeparator}${kv.key}').toList()..sort(compareAsciiUpperCase); + final updatedCountries = countriesByCode.entries.map((kv) { + final code = kv.key; + final name = kv.value; + return '${name != null && name.isNotEmpty ? name : code}${LocationFilter.locationSeparator}$code'; + }).toList() + ..sort(compareAsciiUpperCase); if (!listEquals(updatedCountries, sortedCountries)) { sortedCountries = List.unmodifiable(updatedCountries); invalidateCountryFilterSummary(); diff --git a/lib/services/analysis_service.dart b/lib/services/analysis_service.dart index 8350d66a6..dee908146 100644 --- a/lib/services/analysis_service.dart +++ b/lib/services/analysis_service.dart @@ -98,9 +98,7 @@ class Analyzer { debugPrint('$runtimeType start'); _serviceStateNotifier.value = AnalyzerState.running; - final preferredLocale = settings.locale; - final appLocale = basicLocaleListResolution(preferredLocale != null ? [preferredLocale] : null, AppLocalizations.supportedLocales); - _l10n = await AppLocalizations.delegate.load(appLocale); + _l10n = await AppLocalizations.delegate.load(settings.appliedLocale); _controller.stopSignal.value = false; await _source.init(); diff --git a/lib/services/geocoding_service.dart b/lib/services/geocoding_service.dart index 9a567adbd..d411dfea2 100644 --- a/lib/services/geocoding_service.dart +++ b/lib/services/geocoding_service.dart @@ -1,4 +1,5 @@ import 'dart:async'; +import 'dart:ui'; import 'package:aves/services/common/services.dart'; import 'package:flutter/foundation.dart'; @@ -9,12 +10,12 @@ class GeocodingService { static const platform = MethodChannel('deckers.thibault/aves/geocoding'); // geocoding requires Google Play Services - static Future> getAddress(LatLng coordinates, String locale) async { + static Future> getAddress(LatLng coordinates, Locale locale) async { try { final result = await platform.invokeMethod('getAddress', { 'latitude': coordinates.latitude, 'longitude': coordinates.longitude, - 'locale': locale, + 'locale': locale.toString(), // we only really need one address, but sometimes the native geocoder // returns nothing with `maxResults` of 1, but succeeds with `maxResults` of 2+ 'maxResults': 2, diff --git a/lib/widgets/map/map_info_row.dart b/lib/widgets/map/map_info_row.dart index bfb7c32b4..ee267c138 100644 --- a/lib/widgets/map/map_info_row.dart +++ b/lib/widgets/map/map_info_row.dart @@ -147,7 +147,7 @@ class _AddressRowState extends State<_AddressRow> { Future _getAddressLine(AvesEntry? entry) async { if (entry != null && await availability.canLocatePlaces) { - final addresses = await GeocodingService.getAddress(entry.latLng!, entry.geocoderLocale); + final addresses = await GeocodingService.getAddress(entry.latLng!, settings.appliedLocale); if (addresses.isNotEmpty) { final address = addresses.first; return address.addressLine; diff --git a/lib/widgets/viewer/entry_vertical_pager.dart b/lib/widgets/viewer/entry_vertical_pager.dart index 706076645..526d67c4c 100644 --- a/lib/widgets/viewer/entry_vertical_pager.dart +++ b/lib/widgets/viewer/entry_vertical_pager.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'dart:math'; import 'package:aves/model/entry.dart'; +import 'package:aves/model/settings/settings.dart'; import 'package:aves/model/source/collection_lens.dart'; import 'package:aves/theme/durations.dart'; import 'package:aves/widgets/common/magnifier/pan/scroll_physics.dart'; @@ -169,7 +170,7 @@ class _ViewerVerticalPageViewState extends State { // so that we can display the address instead of coordinates // even when initial collection locating has not reached this entry yet await _entry.catalog(background: false, persist: true, force: false); - await _entry.locate(background: false, force: false); + await _entry.locate(background: false, force: false, geocoderLocale: settings.appliedLocale); } else { Navigator.pop(context); } diff --git a/lib/widgets/viewer/info/entry_info_action_delegate.dart b/lib/widgets/viewer/info/entry_info_action_delegate.dart index e26002e90..2992a711d 100644 --- a/lib/widgets/viewer/info/entry_info_action_delegate.dart +++ b/lib/widgets/viewer/info/entry_info_action_delegate.dart @@ -3,6 +3,7 @@ import 'package:aves/model/actions/entry_info_actions.dart'; import 'package:aves/model/entry.dart'; import 'package:aves/model/metadata/date_modifier.dart'; import 'package:aves/model/metadata/enums.dart'; +import 'package:aves/model/settings/settings.dart'; import 'package:aves/model/source/collection_source.dart'; import 'package:aves/widgets/common/action_mixins/feedback.dart'; import 'package:aves/widgets/common/action_mixins/permission_aware.dart'; @@ -42,7 +43,7 @@ class EntryInfoActionDelegate with FeedbackMixin, PermissionAwareMixin { if (_isMainMode(context) && source != null) { await source.refreshEntry(entry); } else { - await entry.refresh(background: false, persist: false, force: true); + await entry.refresh(background: false, persist: false, force: true, geocoderLocale: settings.appliedLocale); } showFeedback(context, l10n.genericSuccessFeedback); } else { diff --git a/lib/widgets/viewer/info/location_section.dart b/lib/widgets/viewer/info/location_section.dart index 9edcbf933..f830ccdbb 100644 --- a/lib/widgets/viewer/info/location_section.dart +++ b/lib/widgets/viewer/info/location_section.dart @@ -158,7 +158,7 @@ class _AddressInfoGroupState extends State<_AddressInfoGroup> { super.initState(); _addressLineLoader = availability.canLocatePlaces.then((connected) { if (connected) { - return entry.findAddressLine(); + return entry.findAddressLine(geocoderLocale: settings.appliedLocale); } return null; });