-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathClientModels.swift
142 lines (120 loc) · 3.67 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//
// Models.swift
// Lockdown
//
// Created by Johnny Lin on 7/31/19.
// Copyright © 2019 Confirmed Inc. All rights reserved.
//
import Foundation
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
}
enum pt {
case advancedMonthly
case advancedAnnual
case anonymousMonthly
case anonymousAnnual
case universalMonthly
case universalAnnual
case universalWeekly
}
struct Subscription: Codable {
let planType: PlanType
let receiptId: String
let expirationDate: Date
let expirationDateString: String
let expirationDateMs: Int
let cancellationDate: Date?
let cancellationDateString: String?
let cancellationDateMs: Int?
struct PlanType: RawRepresentable, RawValueCodable, Hashable {
let rawValue: String
init(rawValue: String) {
self.rawValue = rawValue
}
static func precedence(p: PlanType) -> Int {
switch p {
case Self.universalAnnual:
return 0
case Self.universalMonthly:
return 1
case Self.anonymousAnnual:
return 2
case Self.anonymousWeekly:
return 3
case Self.advancedAnnual:
return 4
case Self.advancedMonthly:
return 5
case Self.anonymousMonthly:
return 6
default:
return 7
}
}
static let advancedMonthly = PlanType(rawValue: "ios-fw-monthly")
static let advancedAnnual = PlanType(rawValue: "ios-fw-annual")
static let anonymousMonthly = PlanType(rawValue: "ios-monthly")
static let anonymousAnnual = PlanType(rawValue: "ios-annual")
static let universalMonthly = PlanType(rawValue: "all-monthly")
static let universalAnnual = PlanType(rawValue: "all-annual")
static let anonymousWeekly = PlanType(rawValue: "ios-weekly")
var isAdvanced: Bool {
self == Self.advancedAnnual || self == Self.advancedMonthly
}
var isAnonymous: Bool {
self == Self.anonymousAnnual || self == Self.anonymousMonthly || self == Self.anonymousWeekly
}
var isUniversal: Bool {
self == Self.universalAnnual || self == Self.universalMonthly
}
}
func isSameType(_ subscrition: Subscription) -> Bool {
self.planType.isAdvanced == subscrition.planType.isAdvanced ||
self.planType.isAnonymous == subscrition.planType.isAnonymous ||
self.planType.isUniversal == subscrition.planType.isUniversal
}
}
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)
}
}