@@ -149,7 +149,6 @@ public sealed class Item(
149
149
val addPrompt : AddPrompt ,
150
150
val removePrompt : RemovePrompt ,
151
151
val emptyLabel : String? ,
152
- val registrationOptions : RegistrationOptions ,
153
152
override val conditions : Conditions ,
154
153
) : Item(
155
154
type = TYPE_CONTACT_MANAGEMENT ,
@@ -160,52 +159,67 @@ public sealed class Item(
160
159
@Throws(JsonException ::class )
161
160
override fun toJson (): JsonMap =
162
161
jsonMapBuilder()
163
- .put(KEY_PLATFORM , platform.toJson())
162
+ .apply {
163
+ when (platform) {
164
+ is Platform .Sms -> {
165
+ this .put(KEY_PLATFORM , PLATFORM_SMS )
166
+ .put(KEY_REGISTRATION_OPTIONS , platform.registrationOptions.toJson())
167
+ }
168
+ is Platform .Email -> {
169
+ this .put(KEY_PLATFORM , PLATFORM_EMAIL )
170
+ .put(KEY_REGISTRATION_OPTIONS , platform.registrationOptions.toJson())
171
+ }
172
+ }
173
+ }
164
174
.put(KEY_ADD , addPrompt.toJson())
165
175
.put(KEY_REMOVE , removePrompt.toJson())
166
176
.put(KEY_EMPTY_LABEL , emptyLabel)
167
- .put(KEY_REGISTRATION_OPTIONS , registrationOptions.toJson())
168
177
.build()
169
178
170
179
internal companion object {
180
+
181
+ private const val PLATFORM_SMS = " sms"
182
+ private const val PLATFORM_EMAIL = " email"
183
+
171
184
@Throws(JsonException ::class )
172
185
fun fromJson (json : JsonMap ): ContactManagement {
173
186
return ContactManagement (
174
187
id = json.requireField(KEY_ID ),
175
- platform = Platform .fromJson(json.requireField(KEY_PLATFORM )),
188
+ platform = json.requireField<String >(KEY_PLATFORM ).let {
189
+ when (it) {
190
+ PLATFORM_SMS -> Platform .Sms (RegistrationOptions .Sms .fromJson(json.requireField(KEY_REGISTRATION_OPTIONS )))
191
+ PLATFORM_EMAIL -> Platform .Email (RegistrationOptions .Email .fromJson(json.requireField(KEY_REGISTRATION_OPTIONS )))
192
+ else -> throw JsonException (" Invalid registration type: $it " )
193
+ }
194
+ },
176
195
display = CommonDisplay .parse(json.requireField<JsonMap >(KEY_DISPLAY )),
177
196
addPrompt = AddPrompt .fromJson(json.requireField(KEY_ADD )),
178
197
removePrompt = RemovePrompt .fromJson(json.requireField(KEY_REMOVE )),
179
198
emptyLabel = json.optionalField(KEY_EMPTY_LABEL ),
180
- registrationOptions = RegistrationOptions .fromJson(json.requireField(KEY_REGISTRATION_OPTIONS )),
181
- conditions = Condition .parse(json.requireField<JsonValue >(KEY_CONDITIONS ))
199
+ conditions = Condition .parse(json.opt(KEY_CONDITIONS ))
182
200
)
183
201
}
184
202
}
185
203
186
- public enum class Platform (public val jsonValue : String ) {
187
- SMS (" sms" ),
188
- EMAIL (" email" );
189
-
190
- public fun toJson (): JsonValue = JsonValue .wrap(jsonValue)
204
+ public sealed class Platform (public val channelType : ChannelType ) {
205
+ public class Sms (public val registrationOptions : RegistrationOptions .Sms ): Platform(ChannelType .SMS )
206
+ public class Email (public val registrationOptions : RegistrationOptions .Email ): Platform(ChannelType .EMAIL )
191
207
192
- internal companion object {
193
- @Throws(JsonException ::class )
194
- fun fromJson (jsonValue : JsonValue ): Platform {
195
- val valueString = jsonValue.optString()
196
- for (platform in entries) {
197
- if (platform.jsonValue.equals(valueString, true )) {
198
- return platform
199
- }
208
+ internal val resendOptions: ResendOptions
209
+ get() {
210
+ return when (this ) {
211
+ is Sms -> this .registrationOptions.resendOptions
212
+ is Email -> this .registrationOptions.resendOptions
200
213
}
201
- throw JsonException (" Invalid platform: $valueString " )
202
214
}
203
- }
204
215
205
- internal fun toChannelType (): ChannelType = when (this ) {
206
- SMS -> ChannelType .SMS
207
- EMAIL -> ChannelType .EMAIL
208
- }
216
+ internal val errorMessages: ErrorMessages
217
+ get() {
218
+ return when (this ) {
219
+ is Sms -> this .registrationOptions.errorMessages
220
+ is Email -> this .registrationOptions.errorMessages
221
+ }
222
+ }
209
223
}
210
224
211
225
public data class AddPrompt (
@@ -254,6 +268,7 @@ public sealed class Item(
254
268
val type : String ,
255
269
val display : PromptDisplay ,
256
270
val submitButton : LabeledButton ,
271
+ val closeButton : IconButton ? ,
257
272
val cancelButton : LabeledButton ? ,
258
273
val onSubmit : ActionableMessage ? ,
259
274
) {
@@ -263,6 +278,7 @@ public sealed class Item(
263
278
KEY_DISPLAY to display.toJson(),
264
279
KEY_SUBMIT_BUTTON to submitButton.toJson(),
265
280
KEY_CANCEL_BUTTON to cancelButton?.toJson(),
281
+ KEY_CLOSE_BUTTON to closeButton?.toJson(),
266
282
KEY_ON_SUBMIT to onSubmit?.toJson(),
267
283
)
268
284
@@ -273,6 +289,7 @@ public sealed class Item(
273
289
type = json.requireField(KEY_TYPE ),
274
290
display = PromptDisplay .fromJson(json.requireField(KEY_DISPLAY )),
275
291
submitButton = json.requireMap(KEY_SUBMIT_BUTTON ).let { LabeledButton .fromJson(it) },
292
+ closeButton = json.optionalMap(KEY_CLOSE_BUTTON )?.let { IconButton .fromJson(it) },
276
293
cancelButton = json.optionalMap(KEY_CANCEL_BUTTON )?.let { LabeledButton .fromJson(it) },
277
294
onSubmit = json.optionalMap(KEY_ON_SUBMIT )?.let { ActionableMessage .fromJson(it) },
278
295
)
@@ -284,6 +301,7 @@ public sealed class Item(
284
301
val type : String ,
285
302
val display : PromptDisplay ,
286
303
val submitButton : LabeledButton ,
304
+ val closeButton : IconButton ? ,
287
305
val cancelButton : LabeledButton ? ,
288
306
val onSubmit : ActionableMessage ? ,
289
307
) {
@@ -293,6 +311,7 @@ public sealed class Item(
293
311
KEY_DISPLAY to display.toJson(),
294
312
KEY_SUBMIT_BUTTON to submitButton.toJson(),
295
313
KEY_CANCEL_BUTTON to cancelButton?.toJson(),
314
+ KEY_CLOSE_BUTTON to closeButton?.toJson(),
296
315
KEY_ON_SUBMIT to onSubmit?.toJson(),
297
316
)
298
317
@@ -303,57 +322,24 @@ public sealed class Item(
303
322
type = json.requireField(KEY_TYPE ),
304
323
display = PromptDisplay .fromJson(json.requireField(KEY_DISPLAY )),
305
324
submitButton = json.requireMap(KEY_SUBMIT_BUTTON ).let { LabeledButton .fromJson(it) },
325
+ closeButton = json.optionalMap(KEY_CLOSE_BUTTON )?.let { IconButton .fromJson(it) },
306
326
cancelButton = json.optionalMap(KEY_CANCEL_BUTTON )?.let { LabeledButton .fromJson(it) },
307
327
onSubmit = json.optionalMap(KEY_ON_SUBMIT )?.let { ActionableMessage .fromJson(it) },
308
328
)
309
329
}
310
330
}
311
331
}
312
332
313
- public data class FormattedText (
314
- val text : String ,
315
- val type : FormatType
316
- ) {
317
- public enum class FormatType (public val value : String ) {
318
- PLAIN (" string" ),
319
- MARKDOWN (" markdown" ),
320
- UNKNOWN (" unknown" );
321
-
322
- internal companion object {
323
- fun from (value : String ): FormatType =
324
- entries.firstOrNull { it.value.equals(value, true ) } ? : UNKNOWN
325
- }
326
- }
327
-
328
- internal val isMarkdown = type == FormatType .MARKDOWN
329
-
330
- @Throws(JsonException ::class )
331
- public fun toJson (): JsonMap = jsonMapOf(
332
- KEY_DESCRIPTION to text,
333
- KEY_TYPE to type.value
334
- )
335
-
336
- internal companion object {
337
- @Throws(JsonException ::class )
338
- fun fromJson (json : JsonMap ): FormattedText {
339
- return FormattedText (
340
- text = json.requireField(KEY_DESCRIPTION ),
341
- type = FormatType .from(json.requireField<String >(KEY_TYPE ))
342
- )
343
- }
344
- }
345
- }
346
-
347
333
public data class PromptDisplay (
348
334
val title : String ,
349
335
val description : String? ,
350
- val footer : FormattedText ? ,
336
+ val footer : String ? ,
351
337
) {
352
338
@Throws(JsonException ::class )
353
339
public fun toJson (): JsonMap = jsonMapOf(
354
340
KEY_TITLE to title,
355
341
KEY_DESCRIPTION to description,
356
- KEY_FOOTER to footer?.toJson()
342
+ KEY_FOOTER to footer
357
343
)
358
344
359
345
internal companion object {
@@ -365,7 +351,7 @@ public sealed class Item(
365
351
return PromptDisplay (
366
352
title = json.requireField(KEY_TITLE ),
367
353
description = json.optionalField(KEY_DESCRIPTION ),
368
- footer = json.optionalMap (KEY_FOOTER )?. let { FormattedText .fromJson(it) } ,
354
+ footer = json.optionalField (KEY_FOOTER ),
369
355
)
370
356
}
371
357
}
@@ -475,20 +461,21 @@ public sealed class Item(
475
461
KEY_INTERVAL to interval,
476
462
KEY_MESSAGE to message,
477
463
KEY_BUTTON to button.toJson(),
478
- KEY_ON_SUBMIT to onSuccess?.toJson()
464
+ KEY_ON_SUCCESS to onSuccess?.toJson()
479
465
)
480
466
481
467
internal companion object {
482
468
private const val KEY_INTERVAL = " interval"
483
469
private const val KEY_MESSAGE = " message"
470
+ private const val KEY_ON_SUCCESS = " on_success"
484
471
485
472
@Throws(JsonException ::class )
486
473
fun fromJson (json : JsonMap ): ResendOptions {
487
474
return ResendOptions (
488
475
interval = json.requireField(KEY_INTERVAL ),
489
476
message = json.requireField(KEY_MESSAGE ),
490
477
button = LabeledButton .fromJson(json.requireField(KEY_BUTTON )),
491
- onSuccess = json.optionalMap(KEY_ON_SUBMIT )?.let { ActionableMessage .fromJson(it) }
478
+ onSuccess = json.optionalMap(KEY_ON_SUCCESS )?.let { ActionableMessage .fromJson(it) }
492
479
)
493
480
}
494
481
}
@@ -572,17 +559,6 @@ public sealed class Item(
572
559
}
573
560
}
574
561
}
575
-
576
- internal companion object {
577
- @Throws(JsonException ::class )
578
- fun fromJson (json : JsonMap ): RegistrationOptions {
579
- return when (val type = json.requireField<String >(KEY_TYPE )) {
580
- Platform .SMS .jsonValue -> Sms .fromJson(json)
581
- Platform .EMAIL .jsonValue -> Email .fromJson(json)
582
- else -> throw JsonException (" Invalid registration type: $type " )
583
- }
584
- }
585
- }
586
562
}
587
563
588
564
public data class SmsSenderInfo (
@@ -644,6 +620,7 @@ public sealed class Item(
644
620
private const val KEY_ON_SUBMIT = " on_submit"
645
621
private const val KEY_CANCEL_BUTTON = " cancel_button"
646
622
private const val KEY_SUBMIT_BUTTON = " submit_button"
623
+ private const val KEY_CLOSE_BUTTON = " close_button"
647
624
648
625
private const val KEY_NAME = " name"
649
626
private const val KEY_DESCRIPTION = " description"
@@ -695,16 +672,7 @@ public sealed class Item(
695
672
button = Button .parse(json.optionalField(KEY_BUTTON )),
696
673
conditions = Condition .parse(json.get(KEY_CONDITIONS ))
697
674
)
698
- TYPE_CONTACT_MANAGEMENT -> ContactManagement (
699
- id = id,
700
- platform = ContactManagement .Platform .fromJson(json.requireField(KEY_PLATFORM )),
701
- display = CommonDisplay .parse(json.get(KEY_DISPLAY )),
702
- emptyLabel = json.optionalField(KEY_EMPTY_LABEL ),
703
- conditions = Condition .parse(json.get(KEY_CONDITIONS )),
704
- addPrompt = ContactManagement .AddPrompt .fromJson(json.requireField(KEY_ADD )),
705
- removePrompt = ContactManagement .RemovePrompt .fromJson(json.requireField(KEY_REMOVE )),
706
- registrationOptions = ContactManagement .RegistrationOptions .fromJson(json.requireField(KEY_REGISTRATION_OPTIONS ))
707
- )
675
+ TYPE_CONTACT_MANAGEMENT -> ContactManagement .fromJson(json)
708
676
else -> throw JsonException (" Unknown Preference Center Item type: '$type '" )
709
677
}
710
678
}
0 commit comments