Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/core/models/available_servers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,25 @@ class Location_ {
tag: "",
);

Location_ copyWith({
String? country,
String? countryCode,
String? city,
double? latitude,
double? longitude,
String? tag,
String? protocol,
}) {
return Location_(
country: country ?? this.country,
countryCode: countryCode ?? this.countryCode,
city: city ?? this.city,
latitude: latitude ?? this.latitude,
longitude: longitude ?? this.longitude,
tag: tag ?? this.tag,
)..protocol = protocol ?? this.protocol;
}

Map<String, dynamic> toJson() => {
"country": country,
"city": city,
Expand Down
26 changes: 23 additions & 3 deletions lib/features/vpn/server_selection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -768,9 +768,29 @@ class _PrivateServerLocationListViewState
Map<String, List<Location_>> _groupLocationsByCountry(
List<Location_> locations,
) {
final Map<String, List<Location_>> result = {};
final grouped = <String, List<Location_>>{};
final cityCounts = <String, int>{};

// Pass 1: group by country and count city occurrences simultaneously
for (final loc in locations) {
result.putIfAbsent(loc.country, () => <Location_>[]).add(loc);
grouped.putIfAbsent(loc.country, () => <Location_>[]).add(loc);
final key = '${loc.country}/${loc.city}';
cityCounts[key] = (cityCounts[key] ?? 0) + 1;
}
return result;

// Pass 2: number duplicate cities within each country
for (final countryLocations in grouped.values) {
final cityIndex = <String, int>{};
for (var i = 0; i < countryLocations.length; i++) {
final loc = countryLocations[i];
final key = '${loc.country}/${loc.city}';
if (cityCounts[key]! > 1) {
cityIndex[loc.city] = (cityIndex[loc.city] ?? 0) + 1;
countryLocations[i] =
loc.copyWith(city: '${loc.city} - ${cityIndex[loc.city]}');
}
}
}

return grouped;
}
Loading