-
Notifications
You must be signed in to change notification settings - Fork 338
/
Copy pathPackageType.swift
112 lines (94 loc) · 3.24 KB
/
PackageType.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
//
// Copyright RevenueCat Inc. All Rights Reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// PackageType.swift
//
// Created by Nacho Soto on 5/26/22.
import Foundation
///
/// Enumeration of all possible ``Package`` types, as configured on the package.
///
/// #### Related Articles
/// - ``Package``
/// - [Displaying Products](https://docs.revenuecat.com/docs/displaying-products)
///
@objc(RCPackageType) public enum PackageType: Int {
/// A package that was defined with an unknown identifier.
case unknown = -2,
/// A package that was defined with a custom identifier.
custom,
/// A package configured with the predefined lifetime identifier.
lifetime,
/// A package configured with the predefined annual identifier.
annual,
/// A package configured with the predefined six month identifier.
sixMonth,
/// A package configured with the predefined three month identifier.
threeMonth,
/// A package configured with the predefined two month identifier.
twoMonth,
/// A package configured with the predefined monthly identifier.
monthly,
/// A package configured with the predefined weekly identifier.
weekly
}
extension PackageType: CaseIterable {}
extension PackageType: Sendable {}
extension PackageType: CustomDebugStringConvertible {
/// A textual description of the type suitable for debugging.
public var debugDescription: String {
let className = String(describing: PackageType.self)
switch self {
case .unknown: return "\(className).unknown"
case .custom: return "\(className).custom"
default: return "\(className).\(self.description ?? "")"
}
}
}
extension PackageType: Codable {
// swiftlint:disable:next missing_docs
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
if let description = self.description {
try container.encode(description)
} else {
try container.encodeNil()
}
}
// swiftlint:disable:next missing_docs
public init(from decoder: Decoder) throws {
do {
self = Package.packageType(from: try decoder.singleValueContainer().decode(String.self))
} catch {
ErrorUtils.logDecodingError(error, type: Self.self)
self = .unknown
}
}
}
extension PackageType {
var description: String? {
switch self {
case .unknown: return nil
case .custom: return nil
case .lifetime: return "$rc_lifetime"
case .annual: return "$rc_annual"
case .sixMonth: return "$rc_six_month"
case .threeMonth: return "$rc_three_month"
case .twoMonth: return "$rc_two_month"
case .monthly: return "$rc_monthly"
case .weekly: return "$rc_weekly"
}
}
// swiftlint:disable force_unwrapping
static let typesByDescription: [String: PackageType] = PackageType
.allCases
.filter { $0.description != nil }
.dictionaryWithKeys { $0.description! }
// swiftlint:enable force_unwrapping
}