on-demand: ActivateOnDemandViewModel: Uniquify SSIDs list

And if SSIDs list is empty, fall back to .anySSID option
This commit is contained in:
Roopesh Chander 2019-03-08 11:51:27 +05:30 committed by Jason A. Donenfeld
parent 8c1c490da2
commit 5be8d2f069
1 changed files with 16 additions and 2 deletions

View File

@ -156,9 +156,23 @@ private extension ActivateOnDemandViewModel {
case .anySSID:
return .anySSID
case .onlySpecificSSIDs:
return .onlySpecificSSIDs(selectedSSIDs)
let ssids = uniquifiedNonEmptySelectedSSIDs()
return ssids.isEmpty ? .anySSID : .onlySpecificSSIDs(selectedSSIDs)
case .exceptSpecificSSIDs:
return .exceptSpecificSSIDs(selectedSSIDs)
let ssids = uniquifiedNonEmptySelectedSSIDs()
return ssids.isEmpty ? .anySSID : .exceptSpecificSSIDs(selectedSSIDs)
}
}
func uniquifiedNonEmptySelectedSSIDs() -> [String] {
let nonEmptySSIDs = selectedSSIDs.filter { !$0.isEmpty }
var seenSSIDs = Set<String>()
var uniquified = [String]()
for ssid in nonEmptySSIDs {
guard !seenSSIDs.contains(ssid) else { continue }
uniquified.append(ssid)
seenSSIDs.insert(ssid)
}
return uniquified
}
}