Skip to content

Commit dbfefa8

Browse files
committed
Merge branch 'release/1.2.1' into main
2 parents 84efbb5 + 20b8ee3 commit dbfefa8

File tree

5 files changed

+203
-144
lines changed

5 files changed

+203
-144
lines changed

Sources/Pam/Pam+Static.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ extension Pam {
101101
}
102102

103103
public static func submitConsent(
104-
consents: [BaseConsentMessage],
104+
consents: [BaseConsentMessage?],
105105
onSubmit: @escaping ([String: AllowConsentResult], String) -> Void
106106
) {
107107
let api = ConsentAPI()
@@ -120,7 +120,7 @@ extension Pam {
120120
}
121121

122122
public static func submitConsent(
123-
consent: BaseConsentMessage,
123+
consent: BaseConsentMessage?,
124124
onSubmit: @escaping (AllowConsentResult, String) -> Void
125125
) {
126126
submitConsent(consents: [consent]) { result, consentIDs in

Sources/Pam/consent/ConsentAPI.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@ class ConsentAPI {
1111
private var resultMessages: [String: BaseConsentMessage]?
1212
private var _onConsentLoadCallBack: OnLoadConsentMessage?
1313

14-
private var submitConsentQueue: [BaseConsentMessage]?
14+
private var submitConsentQueue: [BaseConsentMessage?]?
1515
private var resultSubmit: [String: AllowConsentResult]?
1616
private var _onConsentSubmitCallBack: OnSubmitConsent?
1717

1818
private var resultUserConsentLoad: [String: UserConsentPermissions]?
1919
private var _onPermissionLoadCallBack: OnLoadPermission?
2020

21-
2221
func setOnPermissionLoadCallBack(callBack: @escaping OnLoadPermission){
2322
_onPermissionLoadCallBack = callBack
2423
}
@@ -40,7 +39,7 @@ class ConsentAPI {
4039
}
4140
}
4241

43-
func submitConsents(consents: [BaseConsentMessage]){
42+
func submitConsents(consents: [BaseConsentMessage?]){
4443
if (!isLoading) {
4544
submitConsentQueue = consents
4645
index = 0
@@ -82,11 +81,11 @@ class ConsentAPI {
8281
payload["_version"] = consent.version
8382

8483
consent.permission.forEach{ it in
85-
payload[it.getSubmitKey()] = it.accept
84+
payload[it.getSubmitKey()] = it.allow
8685

8786
if let trackingConsentMessageID = Pam.shared.config?.trackingConsentMessageID {
8887
if(consent.id == trackingConsentMessageID && it.getSubmitKey() == "_allow_preferences_cookies"){
89-
Pam.shared.allowTracking = it.accept
88+
Pam.shared.allowTracking = it.allow
9089
}
9190
}
9291
}

Sources/Pam/consent/ConsentMessage.swift

Lines changed: 62 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ extension UIColor {
66
if hex.hasPrefix("#") {
77
let start = hex.index(hex.startIndex, offsetBy: 1)
88
let hexColor = String(hex[start...])
9-
9+
1010
if hexColor.count == 8 {
1111
let scanner = Scanner(string: hexColor)
1212
var hexNumber: UInt64 = 0
13-
13+
1414
if scanner.scanHexInt64(&hexNumber) {
1515
r = CGFloat((hexNumber & 0xff000000) >> 24) / 255
1616
g = CGFloat((hexNumber & 0x00ff0000) >> 16) / 255
1717
b = CGFloat((hexNumber & 0x0000ff00) >> 8) / 255
1818
a = CGFloat(hexNumber & 0x000000ff) / 255
19-
19+
2020
self.init(red: r, green: g, blue: b, alpha: a)
2121
return
2222
}
@@ -53,7 +53,7 @@ public struct ConsentStyleConfiguration {
5353
let barBackgroundOpacity = (json[\.bar_background_opacity_percentage].double ?? 100.0)/100.0
5454
let buttonBackgroundColor = json[\.bar_text_color].string ?? "#FFFFFF"
5555
let buttonTextColor = json[\.button_text_color].string ?? "#000000"
56-
56+
5757
let it = json[\.consent_detail].json
5858
let icon = it[\.popup_main_icon].string
5959
let primaryColor = it[\.primary_color].string ?? "#FFFFFF"
@@ -66,7 +66,7 @@ public struct ConsentStyleConfiguration {
6666
secondaryColor: UIColor(hex: secondaryColor),
6767
buttonTextColor: UIColor(hex: dialogButtonTextColor),
6868
textColor: UIColor(hex: dialogTextColor))
69-
69+
7070
return ConsentStyleConfiguration(backgroundColor: UIColor(hex: backgroundColor),
7171
textColor: UIColor(hex: textColor),
7272
barBackgroundOpacity: barBackgroundOpacity,
@@ -128,29 +128,38 @@ public struct ConsentMessage: BaseConsentMessage{
128128
public let defaultLanguage: String
129129
public var permission: [ConsentPermission]
130130

131-
public func acceptAll(){
131+
public func allowAll(){
132132
for var item in permission{
133-
item.accept = true
133+
item.allow = true
134134
}
135135
}
136+
136137
public func denyAll(){
137138
for var item in permission{
138-
item.accept = false
139+
item.allow = false
139140
}
140141
}
141-
142+
143+
public func setPermission(name: ConsentPermissionName, isAllow: Bool) {
144+
for var item in self.permission{
145+
if item.name.rawValue == name.rawValue {
146+
item.allow = false
147+
}
148+
}
149+
}
150+
142151
public func validate()-> ValidationResult{
143152
var pass = true
144153
var errorField: [String]?
145154
var errorMessage:String?
146155

147156
for p in permission{
148-
if(p.require && !p.accept){
157+
if(p.require && !p.allow){
149158
pass = false
150159
if errorField == nil {
151160
errorField = []
152161
}
153-
errorField?.append(p.name)
162+
errorField?.append(p.name.nameStr)
154163
}
155164
}
156165
if(!pass){
@@ -208,20 +217,20 @@ public struct ConsentMessage: BaseConsentMessage{
208217

209218

210219
return ConsentMessage(id: id,
211-
type: type,
212-
name: name,
213-
description: description,
214-
style: style,
215-
version: version,
216-
revision: revision,
217-
displayText: displayText,
218-
acceptButtonText: acceptButtonText,
219-
consentDetailTitle: consentDetailTitle,
220-
availableLanguages: availableLanguages,
221-
defaultLanguage: defaultLanguage,
222-
permission: permissions)
220+
type: type,
221+
name: name,
222+
description: description,
223+
style: style,
224+
version: version,
225+
revision: revision,
226+
displayText: displayText,
227+
acceptButtonText: acceptButtonText,
228+
consentDetailTitle: consentDetailTitle,
229+
availableLanguages: availableLanguages,
230+
defaultLanguage: defaultLanguage,
231+
permission: permissions)
223232
}
224-
233+
225234
}
226235

227236

@@ -232,14 +241,12 @@ public struct ConsentMessageError: BaseConsentMessage{
232241

233242
public struct ConsentPermission{
234243

235-
public let name: String
236-
public let key: String
244+
public let name: ConsentPermissionName
237245
public let shortDescription: Text?
238246
public let fullDescription: Text?
239247
public let fullDescriptionEnabled: Bool
240248
public let require: Bool
241-
public var accept: Bool
242-
public var allow = true
249+
public var allow = false
243250

244251
private static func getText(_ json:Json?)-> Text?{
245252
guard let json = json else {return nil}
@@ -248,84 +255,83 @@ public struct ConsentPermission{
248255
th: json[\.th].string
249256
)
250257
}
251-
252-
private static func parsePermission(json:Json?, key: String, name: String, require: Bool )-> ConsentPermission?{
258+
259+
private static func parsePermission(json:Json?, key: ConsentPermissionName, require: Bool )-> ConsentPermission?{
253260

254-
if let item = json?[dynamicMember: key].json {
261+
if let item = json?[dynamicMember: key.key].json {
255262

256263
let shortDescription = item[\.brief_description].json
257264
let fullDescription = item[\.full_description].json
258265
let fullDescriptionEnabled = item[\.is_full_description_enabled].bool ?? false
259-
260-
return ConsentPermission(name: name,
261-
key: key,
266+
267+
return ConsentPermission(name: key,
262268
shortDescription: ConsentPermission.getText(shortDescription),
263269
fullDescription: ConsentPermission.getText(fullDescription),
264270
fullDescriptionEnabled: fullDescriptionEnabled,
265271
require: require,
266-
accept: true)
272+
allow: true)
267273
}
268-
274+
269275
return nil
270276
}
271-
277+
272278
static public func parse(json: Json?)-> [ConsentPermission]{
273279
var list:[ConsentPermission] = []
274-
275-
if let perm = parsePermission(json: json, key: "terms_and_conditions", name: "Terms and Conditions", require: true){
280+
281+
if let perm = parsePermission(json: json, key: .termsAndConditions, require: true){
276282
list.append(perm)
277283
}
278284

279-
if let perm = parsePermission(json: json, key: "privacy_overview", name: "Privacy overview", require: true){
285+
if let perm = parsePermission(json: json, key: .privacyOverview, require: true){
280286
list.append(perm)
281287
}
282-
283-
if let perm = parsePermission(json: json, key: "necessary_cookies", name: "Necessary cookies", require: true){
288+
289+
if let perm = parsePermission(json: json, key: .necessaryCookies, require: true){
284290
list.append(perm)
285291
}
286292

287-
if let perm = parsePermission(json: json, key: "preferences_cookies", name: "Preferences cookies", require: false){
293+
if let perm = parsePermission(json: json, key: .preferencesCookies, require: false){
288294
list.append(perm)
289295
}
290296

291-
if let perm = parsePermission(json: json, key: "analytics_cookies", name: "Analytics cookies", require: false){
297+
if let perm = parsePermission(json: json, key: .analyticsCookies, require: false){
292298
list.append(perm)
293299
}
294300

295-
if let perm = parsePermission(json: json, key: "marketing_cookies", name: "Marketing cookies", require: false){
301+
if let perm = parsePermission(json: json, key: .marketingCookies, require: false){
296302
list.append(perm)
297303
}
298304

299-
if let perm = parsePermission(json: json, key: "social_media_cookies", name: "Social media cookies", require: false){
305+
if let perm = parsePermission(json: json, key: .socialMediaCookies, require: false){
300306
list.append(perm)
301307
}
302-
303-
if let perm = parsePermission(json: json, key: "email", name: "Email", require: false){
308+
309+
if let perm = parsePermission(json: json, key: .email, require: false){
304310
list.append(perm)
305311
}
306312

307-
if let perm = parsePermission(json: json, key: "sms", name: "SMS", require: false){
313+
if let perm = parsePermission(json: json, key: .sms, require: false){
308314
list.append(perm)
309315
}
310316

311-
if let perm = parsePermission(json: json, key: "line", name: "LINE", require: false){
317+
if let perm = parsePermission(json: json, key:.line, require: false){
312318
list.append(perm)
313319
}
314-
315-
if let perm = parsePermission(json: json, key: "facebook_messenger", name: "Facebook Messenger", require: false){
320+
321+
if let perm = parsePermission(json: json, key: .facebookMessenger, require: false){
316322
list.append(perm)
317323
}
318324

319-
if let perm = parsePermission(json: json, key: "push_notification", name: "Push notification", require: false){
325+
if let perm = parsePermission(json: json, key: .pushNotification, require: false){
320326
list.append(perm)
321327
}
322-
328+
323329
return list
324330
}
325-
331+
326332

327333
public func getSubmitKey() -> String{
328-
return "_allow_\(key)"
334+
return "_allow_\(name)"
329335
}
330336
}
331337

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//
2+
// ConsentPermissionKey.swift
3+
//
4+
//
5+
// Created by narongrit kanhanoi on 1/9/2564 BE.
6+
7+
public enum ConsentPermissionName: String {
8+
case termsAndConditions = "terms_and_conditions"
9+
case privacyOverview = "privacy_overview"
10+
case necessaryCookies = "necessary_cookies"
11+
case preferencesCookies = "preferences_cookies"
12+
case analyticsCookies = "analytics_cookies"
13+
case marketingCookies = "marketing_cookies"
14+
case socialMediaCookies = "social_media_cookies"
15+
case email = "email"
16+
case sms = "sms"
17+
case line = "line"
18+
case facebookMessenger = "facebook_messenger"
19+
case pushNotification = "push_notification"
20+
21+
public var nameStr:String{
22+
get {
23+
switch self {
24+
case .termsAndConditions:
25+
return "Terms and Conditions"
26+
case .privacyOverview:
27+
return "Privacy overview"
28+
case .necessaryCookies:
29+
return "Necessary cookies"
30+
case .preferencesCookies:
31+
return "Preferences cookies"
32+
case .analyticsCookies:
33+
return "Analytics cookies"
34+
case .marketingCookies:
35+
return "Marketing cookies"
36+
case .socialMediaCookies:
37+
return "Social media cookies"
38+
case .email:
39+
return "Email"
40+
case .sms:
41+
return "SMS"
42+
case .line:
43+
return "LINE"
44+
case .facebookMessenger:
45+
return "Facebook Messenger"
46+
case .pushNotification:
47+
return "Push notification"
48+
}
49+
}
50+
}
51+
52+
public var key:String{
53+
get{
54+
return self.rawValue
55+
}
56+
}
57+
58+
}

0 commit comments

Comments
 (0)