From 75474acb593868265855c61188548f2bd048c71e Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Sat, 10 Nov 2018 18:15:43 +0530 Subject: [PATCH] Model: ActivityType enum to represent VPN-on-demand options --- WireGuard/Shared/Model/ActivationType.swift | 66 +++++++++++++++++++ WireGuard/WireGuard.xcodeproj/project.pbxproj | 6 ++ 2 files changed, 72 insertions(+) create mode 100644 WireGuard/Shared/Model/ActivationType.swift diff --git a/WireGuard/Shared/Model/ActivationType.swift b/WireGuard/Shared/Model/ActivationType.swift new file mode 100644 index 0000000..56b0e6c --- /dev/null +++ b/WireGuard/Shared/Model/ActivationType.swift @@ -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 + } +} diff --git a/WireGuard/WireGuard.xcodeproj/project.pbxproj b/WireGuard/WireGuard.xcodeproj/project.pbxproj index f2b7a2e..5145ce1 100644 --- a/WireGuard/WireGuard.xcodeproj/project.pbxproj +++ b/WireGuard/WireGuard.xcodeproj/project.pbxproj @@ -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 = ""; }; 6FFA5D942194454A0001E2F7 /* NETunnelProviderProtocol+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NETunnelProviderProtocol+Extension.swift"; sourceTree = ""; }; 6FFA5D9F21958ECC0001E2F7 /* ErrorNotifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ErrorNotifier.swift; sourceTree = ""; }; + 6FFA5DA32197085D0001E2F7 /* ActivationType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActivationType.swift; sourceTree = ""; }; /* 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 = ""; @@ -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; };