-
Notifications
You must be signed in to change notification settings - Fork 85
/
ClientModels.swift
121 lines (100 loc) · 2.95 KB
/
ClientModels.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//
// Models.swift
// Lockdown
//
// Created by Johnny Lin on 7/31/19.
// Copyright © 2019 Confirmed Inc. All rights reserved.
//
import Foundation
import UIKit
struct IP: Codable {
let ip: String
}
struct SpeedTestBucket: Codable {
let bucket: String
}
struct GetKey: Codable {
let id: String
let b64: String
}
struct SubscriptionEvent: Codable {
let message: String
}
public struct Subscription: Codable {
let planType: PlanType
let receiptId: String
let expirationDate: String
let expirationDateString: String
let expirationDateMs: Int
let cancellationDate: String?
let cancellationDateString: String?
let cancellationDateMs: Int?
struct PlanType: RawRepresentable, RawValueCodable, Hashable {
let rawValue: String
init(rawValue: String) {
self.rawValue = rawValue
}
static let monthly = PlanType(rawValue: "ios-monthly")
static let annual = PlanType(rawValue: "ios-annual")
static let proMonthly = PlanType(rawValue: "all-monthly")
static let proAnnual = PlanType(rawValue: "all-annual")
static let proAnnualLTO = PlanType(rawValue: "all-annual")
}
var correspondingProductGroup: AppStoreProductGroup {
switch planType {
case .monthly, .annual:
return .firewallAndVpn
case .proMonthly, .proAnnual, .proAnnualLTO:
return .pro
default:
return .firewallAndVpn
}
}
public var hasVPN: Bool { [.firewallAndVpn, .pro].contains(correspondingProductGroup) }
var correspondingPeriodUnit: SubscriptionOfferPeriodUnit {
switch planType {
case .monthly, .proMonthly:
return .month
case .annual, .proAnnual, .proAnnualLTO:
return .year
default:
return .year
}
}
func isSubscription(in group: AppStoreProductGroup, of period: SubscriptionOfferPeriodUnit) -> Bool {
return group == correspondingProductGroup && period == correspondingPeriodUnit
}
}
struct SignIn: Codable {
let code: Int
let message: String
}
struct Signup: Codable {
let code: Int
let message: String
}
struct ApiError: Codable, Error {
let code: Int
let message: String
}
// MARK: - Helpers
public enum RawValueCodableError: Error {
case wrongRawValue
}
public protocol RawValueCodable: RawRepresentable, Codable {
}
public extension RawValueCodable where RawValue: Codable {
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let rawValue = try container.decode(RawValue.self)
if let value = Self.init(rawValue: rawValue) {
self = value
} else {
throw RawValueCodableError.wrongRawValue
}
}
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(rawValue)
}
}