Skip to content

Commit

Permalink
Merge pull request #21 from ivpn/bugfix/IOS-703-fastest-server-ignore…
Browse files Browse the repository at this point in the history
…s-custom-country-selection

Bugfix/IOS-703 Fastest Server ignores custom country selection
  • Loading branch information
jurajhilje authored Oct 8, 2020
2 parents 42adf72 + cb30b7d commit 60eaab5
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 35 deletions.
15 changes: 6 additions & 9 deletions IVPNClient/Managers/StorageManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -313,21 +313,20 @@ extension StorageManager {

extension StorageManager {

static func saveServer(gateway: String, group: String, isFastestEnabled: Bool) {
static func saveServer(gateway: String, isFastestEnabled: Bool) {
if let server = fetchServer(gateway: gateway) {
server.isFastestEnabled = isFastestEnabled
} else {
let newServer = Server(context: context)
newServer.gateway = gateway
newServer.group = group
newServer.gateway = gateway.replacingOccurrences(of: ".wg.", with: ".gw.")
newServer.isFastestEnabled = isFastestEnabled
}

saveContext()
}

static func fetchServers(gateway: String = "", group: String = "", isFastestEnabled: Bool = false) -> [Server]? {
let request: NSFetchRequest<Server> = Server.fetchRequest(gateway: gateway, group: group, isFastestEnabled: isFastestEnabled)
static func fetchServers(gateway: String = "", isFastestEnabled: Bool = false) -> [Server]? {
let request: NSFetchRequest<Server> = Server.fetchRequest(gateway: gateway, isFastestEnabled: isFastestEnabled)

do {
let result = try context.fetch(request)
Expand Down Expand Up @@ -362,10 +361,8 @@ extension StorageManager {
static func canUpdateServer(isOn: Bool) -> Bool {
guard !isOn else { return true }

let group = Application.shared.settings.fastestServerConfiguredKey

if UserDefaults.standard.bool(forKey: group) {
if let servers = fetchServers(group: group, isFastestEnabled: true) {
if UserDefaults.standard.bool(forKey: Application.shared.settings.fastestServerConfiguredKey) {
if let servers = fetchServers(isFastestEnabled: true) {
if servers.count == 1 {
return false
}
Expand Down
17 changes: 8 additions & 9 deletions IVPNClient/Model.xcdatamodeld/Model.xcdatamodel/contents
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="14460.32" systemVersion="18B75" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="17192" systemVersion="19H2" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<entity name="Network" representedClassName="Network" syncable="YES">
<attribute name="isDefault" optional="YES" attributeType="Boolean" usesScalarValueType="YES" syncable="YES"/>
<attribute name="name" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="trust" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="type" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="isDefault" optional="YES" attributeType="Boolean" usesScalarValueType="YES"/>
<attribute name="name" optional="YES" attributeType="String"/>
<attribute name="trust" optional="YES" attributeType="String"/>
<attribute name="type" optional="YES" attributeType="String"/>
</entity>
<entity name="Server" representedClassName="Server" syncable="YES">
<attribute name="gateway" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="group" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="isFastestEnabled" optional="YES" attributeType="Boolean" usesScalarValueType="YES" syncable="YES"/>
<attribute name="gateway" optional="YES" attributeType="String"/>
<attribute name="isFastestEnabled" optional="YES" attributeType="Boolean" usesScalarValueType="YES"/>
</entity>
<elements>
<element name="Network" positionX="-54" positionY="-9" width="128" height="105"/>
<element name="Server" positionX="-54" positionY="18" width="128" height="90"/>
<element name="Server" positionX="-54" positionY="18" width="128" height="73"/>
</elements>
</model>
9 changes: 2 additions & 7 deletions IVPNClient/Models/CoreData/Server+CoreDataProperties.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,19 @@ import CoreData
extension Server {

@NSManaged public var gateway: String?
@NSManaged public var group: String?
@NSManaged public var isFastestEnabled: Bool

convenience init(context: NSManagedObjectContext, needToSave: Bool) {
let entity = NSEntityDescription.entity(forEntityName: "Server", in: context)
self.init(entity: entity!, insertInto: needToSave ? context : nil)
}

@nonobjc public class func fetchRequest(gateway: String = "", group: String = "", isFastestEnabled: Bool = false) -> NSFetchRequest<Server> {
@nonobjc public class func fetchRequest(gateway: String = "", isFastestEnabled: Bool = false) -> NSFetchRequest<Server> {
let fetchRequest = NSFetchRequest<Server>(entityName: "Server")
var filters = [NSPredicate]()

if !gateway.isEmpty {
filters.append(NSPredicate(format: "gateway == %@", gateway))
}

if !group.isEmpty {
filters.append(NSPredicate(format: "group == %@", group))
filters.append(NSPredicate(format: "gateway == %@", gateway.replacingOccurrences(of: ".wg.", with: ".gw.")))
}

if isFastestEnabled {
Expand Down
6 changes: 1 addition & 5 deletions IVPNClient/Models/Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,7 @@ class Settings {
}

var fastestServerConfiguredKey: String {
if connectionProtocol.tunnelType() == .wireguard {
return "FastestServerConfiguredForWireGuard"
} else {
return "FastestServerConfiguredForOpenVPN"
}
return "FastestServerConfiguredForOpenVPN"
}

init(serverList: VPNServerList) {
Expand Down
7 changes: 4 additions & 3 deletions IVPNClient/Models/VPNServerList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ class VPNServerList {
func getFastestServer() -> VPNServer? {
let servers = filteredFastestServers
let serversWithPing = servers.filter { $0.pingMs ?? -1 >= 0 }
if serversWithPing.isEmpty { return nil }
if serversWithPing.isEmpty {
return filteredFastestServers.first
}

return servers.min {
let leftPingMs = $0.pingMs ?? -1
Expand Down Expand Up @@ -210,10 +212,9 @@ class VPNServerList {
}

func saveAllServers(exceptionGateway: String) {
let group = Application.shared.settings.fastestServerConfiguredKey
for server in servers {
let isFastestEnabled = server.gateway != exceptionGateway
StorageManager.saveServer(gateway: server.gateway, group: group, isFastestEnabled: isFastestEnabled)
StorageManager.saveServer(gateway: server.gateway, isFastestEnabled: isFastestEnabled)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,6 @@ class ControlPanelViewController: UITableViewController {
tableView.backgroundColor = UIColor.init(named: Theme.ivpnBackgroundPrimary)
tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 1))
isMultiHop = UserDefaults.shared.isMultiHop
Application.shared.connectionManager.needsUpdateSelectedServer()
controlPanelView.updateServerNames()
controlPanelView.updateServerLabels(viewModel: vpnStatusViewModel)
controlPanelView.updateAntiTracker()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ extension ServersConfigurationTableViewController: ServerConfigurationCellDelega
let fastestServerConfiguredKey = Application.shared.settings.fastestServerConfiguredKey

if UserDefaults.standard.bool(forKey: fastestServerConfiguredKey) {
StorageManager.saveServer(gateway: gateway, group: fastestServerConfiguredKey, isFastestEnabled: isOn)
StorageManager.saveServer(gateway: gateway, isFastestEnabled: isOn)
} else {
Application.shared.serverList.saveAllServers(exceptionGateway: gateway)
UserDefaults.standard.set(true, forKey: fastestServerConfiguredKey)
Expand Down

0 comments on commit 60eaab5

Please sign in to comment.