|
| 1 | +import FirebaseAnalytics |
| 2 | +import FirebaseCore |
| 3 | + |
| 4 | +@objc enum ConsentTypeRawValue: Int, CustomStringConvertible, CaseIterable { |
| 5 | + case adPersonalization = 1 |
| 6 | + case adStorage = 2 |
| 7 | + case adUserData = 3 |
| 8 | + case analyticsStorage = 4 |
| 9 | + |
| 10 | + var description: String { |
| 11 | + return switch self { |
| 12 | + case .adPersonalization: "ad_personalization" |
| 13 | + case .adStorage: "ad_storage" |
| 14 | + case .adUserData: "ad_user_data" |
| 15 | + case .analyticsStorage: "analytics_storage" |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + static func allOptionsString() -> String { |
| 20 | + let capitalizedDescriptions = allCases.map { $0.description.uppercased() } |
| 21 | + |
| 22 | + if capitalizedDescriptions.count > 1 { |
| 23 | + let lastOption = capitalizedDescriptions.last! |
| 24 | + let allButLast = capitalizedDescriptions.dropLast().joined(separator: ", ") |
| 25 | + return "\(allButLast), or \(lastOption)" |
| 26 | + } else { |
| 27 | + return capitalizedDescriptions.first ?? "" |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +@objc enum ConsentStatusRawValue: Int, CustomStringConvertible, CaseIterable { |
| 33 | + case granted = 1 |
| 34 | + case denied = 2 |
| 35 | + |
| 36 | + var description: String { |
| 37 | + return switch self { |
| 38 | + case .granted: "granted" |
| 39 | + case .denied: "denied" |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + static func allOptionsString() -> String { |
| 44 | + let capitalizedDescriptions = allCases.map { $0.description.uppercased() } |
| 45 | + |
| 46 | + if capitalizedDescriptions.count > 1 { |
| 47 | + let lastOption = capitalizedDescriptions.last! |
| 48 | + let allButLast = capitalizedDescriptions.dropLast().joined(separator: ", ") |
| 49 | + return "\(allButLast), or \(lastOption)" |
| 50 | + } else { |
| 51 | + return capitalizedDescriptions.first ?? "" |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +@objc class OSFANLConsentHelper: NSObject { |
| 57 | + @objc static func createConsentModel(_ commandArguments: NSArray) throws -> [ConsentType: ConsentStatus] { |
| 58 | + guard let jsonString = commandArguments[0] as? String, |
| 59 | + let jsonData = jsonString.data(using: .utf8), |
| 60 | + let array = try JSONSerialization.jsonObject(with: jsonData, options: []) as? [[String: Any]] else { |
| 61 | + throw OSFANLError.invalidType("ConsentSettings", type: "JSON") |
| 62 | + } |
| 63 | + |
| 64 | + var firebaseConsentDict: [ConsentType: ConsentStatus] = [:] |
| 65 | + |
| 66 | + for item in array { |
| 67 | + guard let typeRawValue = item["Type"] as? Int, |
| 68 | + let statusRawValue = item["Status"] as? Int else { |
| 69 | + throw OSFANLError.invalidType("JSON passed Consent Type or Status", type: "Integer") |
| 70 | + } |
| 71 | + |
| 72 | + guard let consentTypeRawValue = ConsentTypeRawValue(rawValue: typeRawValue) else { |
| 73 | + throw OSFANLError.invalidType("Consent Type", type: ConsentTypeRawValue.allOptionsString()) |
| 74 | + } |
| 75 | + |
| 76 | + guard let consentStatusRawValue = ConsentStatusRawValue(rawValue: statusRawValue) else { |
| 77 | + throw OSFANLError.invalidType("Consent Status", type: ConsentStatusRawValue.allOptionsString()) |
| 78 | + } |
| 79 | + |
| 80 | + let consentType = ConsentType(rawValue: String(describing: consentTypeRawValue)) |
| 81 | + let consentStatus = ConsentStatus(rawValue: String(describing: consentStatusRawValue)) |
| 82 | + |
| 83 | + if firebaseConsentDict.keys.contains(consentType) { |
| 84 | + throw OSFANLError.duplicateItemsIn(parameter: "ConsentSettings") |
| 85 | + } else { |
| 86 | + firebaseConsentDict[consentType] = consentStatus |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if firebaseConsentDict.isEmpty { |
| 91 | + throw OSFANLError.missing("ConsentSettings") |
| 92 | + } else { |
| 93 | + return firebaseConsentDict |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments