Model: ActivityType enum to represent VPN-on-demand options

Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
Roopesh Chander 2018-11-10 18:15:43 +05:30
parent 1502bd42d3
commit 0f03ffc920
2 changed files with 72 additions and 0 deletions

View File

@ -0,0 +1,66 @@
// SPDX-License-Identifier: MIT
// Copyright © 2018 WireGuard LLC. All Rights Reserved.
enum ActivationType {
case activateManually
case useOnDemandForAnyInternetActivity
case useOnDemandOnlyOverWifi
case useOnDemandOnlyOverCellular
}
extension ActivationType: Codable {
// We use separate coding keys in case we might have a enum with associated values in the future
enum CodingKeys: CodingKey {
case activateManually
case useOnDemandForAnyInternetActivity
case useOnDemandOnlyOverWifi
case useOnDemandOnlyOverCellular
}
// Decoding error
enum DecodingError: Error {
case invalidInput
}
// Encoding
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch self {
case .activateManually:
try container.encode(true, forKey: CodingKeys.activateManually)
case .useOnDemandForAnyInternetActivity:
try container.encode(true, forKey: CodingKeys.useOnDemandForAnyInternetActivity)
case .useOnDemandOnlyOverWifi:
try container.encode(true, forKey: CodingKeys.useOnDemandOnlyOverWifi)
case .useOnDemandOnlyOverCellular:
try container.encode(true, forKey: CodingKeys.useOnDemandOnlyOverCellular)
}
}
// Decoding
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
if let isValid = try? container.decode(Bool.self, forKey: CodingKeys.activateManually), isValid {
self = .activateManually
return
}
if let isValid = try? container.decode(Bool.self, forKey: CodingKeys.useOnDemandForAnyInternetActivity), isValid {
self = .useOnDemandForAnyInternetActivity
return
}
if let isValid = try? container.decode(Bool.self, forKey: CodingKeys.useOnDemandOnlyOverWifi), isValid {
self = .useOnDemandOnlyOverWifi
return
}
if let isValid = try? container.decode(Bool.self, forKey: CodingKeys.useOnDemandOnlyOverCellular), isValid {
self = .useOnDemandOnlyOverCellular
return
}
throw DecodingError.invalidInput
}
}

View File

@ -49,6 +49,8 @@
6FFA5D952194454A0001E2F7 /* NETunnelProviderProtocol+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FFA5D942194454A0001E2F7 /* NETunnelProviderProtocol+Extension.swift */; };
6FFA5D96219446380001E2F7 /* NETunnelProviderProtocol+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FFA5D942194454A0001E2F7 /* NETunnelProviderProtocol+Extension.swift */; };
6FFA5DA021958ECC0001E2F7 /* ErrorNotifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FFA5D9F21958ECC0001E2F7 /* ErrorNotifier.swift */; };
6FFA5DA42197085D0001E2F7 /* ActivationType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FFA5DA32197085D0001E2F7 /* ActivationType.swift */; };
6FFA5DA521970B370001E2F7 /* ActivationType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FFA5DA32197085D0001E2F7 /* ActivationType.swift */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@ -133,6 +135,7 @@
6FF4AC482120B9E0002C96EB /* WireGuard.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = WireGuard.entitlements; sourceTree = "<group>"; };
6FFA5D942194454A0001E2F7 /* NETunnelProviderProtocol+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NETunnelProviderProtocol+Extension.swift"; sourceTree = "<group>"; };
6FFA5D9F21958ECC0001E2F7 /* ErrorNotifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ErrorNotifier.swift; sourceTree = "<group>"; };
6FFA5DA32197085D0001E2F7 /* ActivationType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActivationType.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -229,6 +232,7 @@
6F7774E9217229DB006A79B3 /* IPAddressRange.swift */,
6F693A552179E556008551C1 /* Endpoint.swift */,
6F628C3E217F3413003482A3 /* DNSServer.swift */,
6FFA5DA32197085D0001E2F7 /* ActivationType.swift */,
);
path = Model;
sourceTree = "<group>";
@ -494,6 +498,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
6FFA5DA521970B370001E2F7 /* ActivationType.swift in Sources */,
6FFA5DA021958ECC0001E2F7 /* ErrorNotifier.swift in Sources */,
6FFA5D96219446380001E2F7 /* NETunnelProviderProtocol+Extension.swift in Sources */,
6FFA5D8E2194370D0001E2F7 /* Configuration.swift in Sources */,
@ -535,6 +540,7 @@
6FDEF802218646BA00D8FBF6 /* ZipArchive.swift in Sources */,
6FDEF806218725D200D8FBF6 /* SettingsTableViewController.swift in Sources */,
6F7774E1217181B1006A79B3 /* MainViewController.swift in Sources */,
6FFA5DA42197085D0001E2F7 /* ActivationType.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};