|
| 1 | +// |
| 2 | +// Created by Tapash Majumder on 5/6/20. |
| 3 | +// Copyright © 2020 Iterable. All rights reserved. |
| 4 | +// |
| 5 | +// This file contains static pure helper functions for creating |
| 6 | +// the dataFields dictionary. |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +struct DataFieldsHelper { |
| 11 | + static func createDataFields(sdkVersion: String?, deviceId: String, device: UIDevice, bundle: Bundle, notificationsEnabled: Bool, deviceAttributes: [String: String]) -> [String: Any] { |
| 12 | + var dataFields = [String: Any]() |
| 13 | + |
| 14 | + deviceAttributes.forEach { deviceAttribute in |
| 15 | + dataFields[deviceAttribute.key] = deviceAttribute.value |
| 16 | + } |
| 17 | + |
| 18 | + dataFields[JsonKey.deviceId.jsonKey] = deviceId |
| 19 | + if let sdkVersion = sdkVersion { |
| 20 | + dataFields[JsonKey.iterableSdkVersion.jsonKey] = sdkVersion |
| 21 | + } |
| 22 | + dataFields[JsonKey.notificationsEnabled.jsonKey] = notificationsEnabled |
| 23 | + |
| 24 | + dataFields.addAll(other: createBundleFields(bundle: bundle)) |
| 25 | + |
| 26 | + dataFields.addAll(other: createUIDeviceFields(device: device)) |
| 27 | + |
| 28 | + return dataFields |
| 29 | + } |
| 30 | + |
| 31 | + private static func createBundleFields(bundle: Bundle) -> [String: Any] { |
| 32 | + var fields = [String: Any]() |
| 33 | + |
| 34 | + if let appPackageName = bundle.appPackageName { |
| 35 | + fields[JsonKey.appPackageName.jsonKey] = appPackageName |
| 36 | + } |
| 37 | + if let appVersion = bundle.appVersion { |
| 38 | + fields[JsonKey.appVersion.jsonKey] = appVersion |
| 39 | + } |
| 40 | + if let appBuild = bundle.appBuild { |
| 41 | + fields[JsonKey.appBuild.jsonKey] = appBuild |
| 42 | + } |
| 43 | + |
| 44 | + return fields |
| 45 | + } |
| 46 | + |
| 47 | + private static func createUIDeviceFields(device: UIDevice) -> [String: Any] { |
| 48 | + var fields = [String: Any]() |
| 49 | + |
| 50 | + fields[JsonKey.Device.localizedModel] = device.localizedModel |
| 51 | + fields[JsonKey.Device.userInterfaceIdiom] = userInterfaceIdiomEnumToString(device.userInterfaceIdiom) |
| 52 | + fields[JsonKey.Device.systemName] = device.systemName |
| 53 | + fields[JsonKey.Device.systemVersion] = device.systemVersion |
| 54 | + fields[JsonKey.Device.model] = device.model |
| 55 | + |
| 56 | + if let identifierForVendor = device.identifierForVendor?.uuidString { |
| 57 | + fields[JsonKey.Device.vendorId] = identifierForVendor |
| 58 | + } |
| 59 | + |
| 60 | + return fields |
| 61 | + } |
| 62 | + |
| 63 | + private static func userInterfaceIdiomEnumToString(_ idiom: UIUserInterfaceIdiom) -> String { |
| 64 | + switch idiom { |
| 65 | + case .phone: |
| 66 | + return JsonValue.DeviceIdiom.phone |
| 67 | + case .pad: |
| 68 | + return JsonValue.DeviceIdiom.pad |
| 69 | + case .tv: |
| 70 | + return JsonValue.DeviceIdiom.tv |
| 71 | + case .carPlay: |
| 72 | + return JsonValue.DeviceIdiom.carPlay |
| 73 | + default: |
| 74 | + return JsonValue.DeviceIdiom.unspecified |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +extension Dictionary { |
| 80 | + mutating func addAll(other: [Key: Value]) { |
| 81 | + for (k, v) in other { |
| 82 | + self[k] = v |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments