Skip to content

Commit b58cae8

Browse files
committed
Adding model hydration
Models should take a [string : any] not [string : string]
1 parent 192ec01 commit b58cae8

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

iOS_SDK/OneSignalSDK/OneSignalOSCore/Source/OSModel.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ open class OSModel: NSObject, NSCoding {
6363
/**
6464
This function receives a server response and updates the model's properties.
6565
*/
66-
public func hydrate(_ response: [String: String]) {
66+
public func hydrate(_ response: [String: Any]) {
6767
// TODO: Thread safety when processing server responses to hydrate models.
6868
self.hydrating = true
6969
hydrateModel(response) // Calls model-specific hydration logic
7070
self.hydrating = false
7171
}
7272

73-
open func hydrateModel(_ response: [String: String]) {
73+
open func hydrateModel(_ response: [String: Any]) {
7474
// Log as an error.
7575
print("Error: Function must be overridden.")
7676
fatalError("hydrateModel(response:) has not been implemented")

iOS_SDK/OneSignalSDK/OneSignalUser/Source/OSIdentityModel.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,17 @@ class OSIdentityModel: OSModel {
8585
self.set(property: "aliases", newValue: aliasesToSend)
8686
}
8787

88-
public override func hydrateModel(_ response: [String: String]) {
88+
public override func hydrateModel(_ response: [String: Any]) {
8989
print("🔥 OSIdentityModel hydrateModel()")
9090
// TODO: Update Model properties with the response
9191
for property in response {
92-
if property.key != "external_id" && property.key != "onesignal_id" {
93-
aliases[property.key] = property.value
92+
switch property.key {
93+
case "external_id":
94+
aliases[OS_EXTERNAL_ID] = property.value as? String
95+
case "onesignal_id":
96+
aliases[OS_ONESIGNAL_ID] = property.value as? String
97+
default:
98+
aliases[property.key] = property.value as? String
9499
}
95100
}
96101
}

iOS_SDK/OneSignalSDK/OneSignalUser/Source/OSPropertiesModel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class OSPropertiesModel: OSModel {
109109
self.set(property: "aliases", newValue: tagsToSend)
110110
}
111111

112-
public override func hydrateModel(_ response: [String: String]) {
112+
public override func hydrateModel(_ response: [String: Any]) {
113113
// TODO: Update Model properties with the response
114114
}
115115
}

iOS_SDK/OneSignalSDK/OneSignalUser/Source/OSSubscriptionModel.swift

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,32 @@ class OSSubscriptionModel: OSModel {
213213
super.init(coder: coder)
214214
}
215215

216-
public override func hydrateModel(_ response: [String: String]) {
216+
public override func hydrateModel(_ response: [String: Any]) {
217217
print("🔥 OSSubscriptionModel hydrateModel()")
218-
// TODO: Update Model properties with the response
218+
for property in response {
219+
switch property.key {
220+
case "id":
221+
self.subscriptionId = property.value as? String
222+
case "type":
223+
if let type = OSSubscriptionType(rawValue: property.value as? String ?? "") {
224+
self.type = type
225+
}
226+
case "token":
227+
self.address = property.value as? String
228+
case "enabled":
229+
if let enabled = property.value as? Bool {
230+
if self.enabled != enabled {
231+
_isDisabled = enabled
232+
}
233+
}
234+
case "notification_types":
235+
if let notificationTypes = property.value as? Int {
236+
self.notificationTypes = notificationTypes
237+
}
238+
default:
239+
OneSignalLog.onesignalLog(.LL_ERROR, message: "Unknown property on subscription model")
240+
}
241+
}
219242
}
220243
}
221244

0 commit comments

Comments
 (0)