@capgo/capacitor-updater 8.45.10 → 8.46.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -31,18 +31,94 @@ func == (lhs: LocalizedString, rhs: LocalizedString) -> Bool {
31
31
  return lhs.value == rhs.value
32
32
  }
33
33
 
34
- enum BundleStatus: LocalizedString, Decodable, Encodable {
34
+ enum BundleStatus: LocalizedString, CaseIterable, Decodable, Encodable {
35
35
  case SUCCESS = "success"
36
36
  case ERROR = "error"
37
37
  case PENDING = "pending"
38
38
  case DELETED = "deleted"
39
39
  case DOWNLOADING = "downloading"
40
40
 
41
+ var storedValue: String {
42
+ switch self {
43
+ case .SUCCESS:
44
+ return "success"
45
+ case .ERROR:
46
+ return "error"
47
+ case .PENDING:
48
+ return "pending"
49
+ case .DELETED:
50
+ return "deleted"
51
+ case .DOWNLOADING:
52
+ return "downloading"
53
+ }
54
+ }
55
+
41
56
  var localizedString: String {
42
57
  return self.rawValue.value
43
58
  }
44
59
 
45
60
  init?(localizedString: String) {
46
- self.init(rawValue: LocalizedString(localized: localizedString))
61
+ let normalized = localizedString.trimmingCharacters(in: .whitespacesAndNewlines)
62
+ guard let status = BundleStatus.allCases.first(where: { $0.localizedString == normalized }) else {
63
+ return nil
64
+ }
65
+ self = status
66
+ }
67
+
68
+ init?(storedValue: String) {
69
+ guard let status = BundleStatus.fromStoredValue(storedValue) else {
70
+ return nil
71
+ }
72
+ self = status
73
+ }
74
+
75
+ private static func fromStoredValue(_ value: String) -> BundleStatus? {
76
+ let normalized = value.trimmingCharacters(in: .whitespacesAndNewlines)
77
+ if let status = BundleStatus(localizedString: normalized) {
78
+ return status
79
+ }
80
+
81
+ let storedValue = normalized.lowercased()
82
+ return BundleStatus.allCases.first(where: { $0.storedValue == storedValue })
83
+ }
84
+
85
+ init(from decoder: Decoder) throws {
86
+ if let container = try? decoder.singleValueContainer(),
87
+ let value = try? container.decode(String.self),
88
+ let status = BundleStatus.fromStoredValue(value) {
89
+ self = status
90
+ return
91
+ }
92
+
93
+ if let container = try? decoder.container(keyedBy: DynamicCodingKey.self),
94
+ let key = container.allKeys.first,
95
+ let status = BundleStatus.fromStoredValue(key.stringValue) {
96
+ self = status
97
+ return
98
+ }
99
+
100
+ throw DecodingError.dataCorrupted(
101
+ DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Invalid bundle status")
102
+ )
103
+ }
104
+
105
+ func encode(to encoder: Encoder) throws {
106
+ var container = encoder.singleValueContainer()
107
+ try container.encode(self.storedValue)
108
+ }
109
+ }
110
+
111
+ private struct DynamicCodingKey: CodingKey {
112
+ var stringValue: String
113
+ var intValue: Int?
114
+
115
+ init?(stringValue: String) {
116
+ self.stringValue = stringValue
117
+ self.intValue = nil
118
+ }
119
+
120
+ init?(intValue: Int) {
121
+ self.stringValue = String(intValue)
122
+ self.intValue = intValue
47
123
  }
48
124
  }