mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-26 11:45:19 +00:00
fix: use country-level resolution for low-precision geohashes (#1044)
* fix: use country-level resolution for low-precision geohashes (#887) * fix: skip admin fallback when country exists but is duplicate Prevents mixed labels like "United Kingdom and Scotland" for single-country geohashes. Admin fallback now only triggers when no country is available from the placemark. * fix: migrate stale low-precision bookmark names so country-first logic applies Users who bookmarked a <=2-char geohash before the country-first resolver kept the old administrativeArea cache (e.g. "England" for `gc`) because resolveBookmarkNameIfNeeded bails when bookmarkNames[gh] is non-nil. Add a one-shot, versioned migration that drops cached names for <=2-char geohashes on load; the next LocationChannelsSheet .onAppear re-resolves them via the fixed logic. Higher-precision entries are untouched. * Fix low-precision geohash country names --------- Co-authored-by: jack <212554440+jackjackbits@users.noreply.github.com> Co-authored-by: jack <jackjackbits@users.noreply.github.com>
This commit is contained in:
co-authored by
jack
jack
parent
820c933958
commit
733098bb63
@@ -109,6 +109,8 @@ final class LocationStateManager: NSObject, CLLocationManagerDelegate, Observabl
|
||||
private let teleportedStoreKey = "locationChannel.teleportedSet"
|
||||
private let bookmarksKey = "locationChannel.bookmarks"
|
||||
private let bookmarkNamesKey = "locationChannel.bookmarkNames"
|
||||
private let bookmarkNamesSchemaVersionKey = "locationChannel.bookmarkNamesSchemaVersion"
|
||||
private let bookmarkNamesCurrentSchemaVersion = 1
|
||||
|
||||
// MARK: - Published State (Channel)
|
||||
|
||||
@@ -222,6 +224,26 @@ final class LocationStateManager: NSObject, CLLocationManagerDelegate, Observabl
|
||||
let dict = try? JSONDecoder().decode([String: String].self, from: data) {
|
||||
bookmarkNames = dict
|
||||
}
|
||||
|
||||
migrateBookmarkNamesIfNeeded()
|
||||
}
|
||||
|
||||
/// Drops names cached before low-precision geohashes became country-first.
|
||||
/// Correctly resolved names written after this migration must survive
|
||||
/// subsequent launches, so the migration is explicitly versioned.
|
||||
private func migrateBookmarkNamesIfNeeded() {
|
||||
guard storage.integer(forKey: bookmarkNamesSchemaVersionKey) < bookmarkNamesCurrentSchemaVersion else {
|
||||
return
|
||||
}
|
||||
|
||||
let retainedNames = bookmarkNames.filter {
|
||||
Self.normalizeGeohash($0.key).count > 2
|
||||
}
|
||||
if retainedNames.count != bookmarkNames.count {
|
||||
bookmarkNames = retainedNames
|
||||
persistBookmarkNames()
|
||||
}
|
||||
storage.set(bookmarkNamesCurrentSchemaVersion, forKey: bookmarkNamesSchemaVersionKey)
|
||||
}
|
||||
|
||||
private func initializePermissionState() {
|
||||
@@ -525,15 +547,15 @@ final class LocationStateManager: NSObject, CLLocationManagerDelegate, Observabl
|
||||
}
|
||||
|
||||
private func resolveCompositeAdminName(geohash gh: String, points: [CLLocation]) {
|
||||
var uniqueAdmins: [String] = []
|
||||
var seenAdmins = Set<String>()
|
||||
var uniqueRegions: [String] = []
|
||||
var seenRegions = Set<String>()
|
||||
var idx = 0
|
||||
|
||||
func step() {
|
||||
if idx >= points.count {
|
||||
let finalName: String? = {
|
||||
if uniqueAdmins.count >= 2 { return uniqueAdmins[0] + " and " + uniqueAdmins[1] }
|
||||
return uniqueAdmins.first
|
||||
if uniqueRegions.count >= 2 { return uniqueRegions[0] + " and " + uniqueRegions[1] }
|
||||
return uniqueRegions.first
|
||||
}()
|
||||
if let finalName = finalName, !finalName.isEmpty {
|
||||
DispatchQueue.main.async {
|
||||
@@ -549,12 +571,16 @@ final class LocationStateManager: NSObject, CLLocationManagerDelegate, Observabl
|
||||
geocoder.reverseGeocodeLocation(loc) { [weak self] placemarks, _ in
|
||||
guard self != nil else { return }
|
||||
if let pm = placemarks?.first {
|
||||
if let admin = pm.administrativeArea, !admin.isEmpty, !seenAdmins.contains(admin) {
|
||||
seenAdmins.insert(admin)
|
||||
uniqueAdmins.append(admin)
|
||||
} else if let country = pm.country, !country.isEmpty, !seenAdmins.contains(country) {
|
||||
seenAdmins.insert(country)
|
||||
uniqueAdmins.append(country)
|
||||
if let country = pm.country, !country.isEmpty {
|
||||
if !seenRegions.contains(country) {
|
||||
seenRegions.insert(country)
|
||||
uniqueRegions.append(country)
|
||||
}
|
||||
} else if let admin = pm.administrativeArea,
|
||||
!admin.isEmpty,
|
||||
!seenRegions.contains(admin) {
|
||||
seenRegions.insert(admin)
|
||||
uniqueRegions.append(admin)
|
||||
}
|
||||
}
|
||||
step()
|
||||
@@ -566,7 +592,7 @@ final class LocationStateManager: NSObject, CLLocationManagerDelegate, Observabl
|
||||
private static func nameForGeohashLength(_ len: Int, from pm: CLPlacemark) -> String? {
|
||||
switch len {
|
||||
case 0...2:
|
||||
return pm.administrativeArea ?? pm.country
|
||||
return pm.country ?? pm.administrativeArea
|
||||
case 3...4:
|
||||
return pm.administrativeArea ?? pm.subAdministrativeArea ?? pm.country
|
||||
case 5:
|
||||
|
||||
Reference in New Issue
Block a user