|
| 1 | +package com.iterable.iterableapi.ui.embedded |
| 2 | + |
| 3 | +import android.os.Bundle |
| 4 | +import com.iterable.iterableapi.IterableEmbeddedMessage |
| 5 | +import com.iterable.iterableapi.IterableLogger |
| 6 | +import org.json.JSONException |
| 7 | +import org.json.JSONObject |
| 8 | + |
| 9 | +internal object IterableEmbeddedViewArguments { |
| 10 | + |
| 11 | + private const val TAG = "IterableEmbeddedViewArgs" |
| 12 | + |
| 13 | + // Argument keys |
| 14 | + private const val KEY_VIEW_TYPE = "view_type" |
| 15 | + private const val KEY_MESSAGE_JSON = "message_json" |
| 16 | + private const val KEY_BG_COLOR = "bg_color" |
| 17 | + private const val KEY_BORDER_COLOR = "border_color" |
| 18 | + private const val KEY_BORDER_WIDTH = "border_width" |
| 19 | + private const val KEY_BORDER_RADIUS = "border_radius" |
| 20 | + private const val KEY_PRIMARY_BTN_BG = "primary_btn_bg" |
| 21 | + private const val KEY_PRIMARY_BTN_TEXT = "primary_btn_text" |
| 22 | + private const val KEY_SECONDARY_BTN_BG = "secondary_btn_bg" |
| 23 | + private const val KEY_SECONDARY_BTN_TEXT = "secondary_btn_text" |
| 24 | + private const val KEY_TITLE_COLOR = "title_color" |
| 25 | + private const val KEY_BODY_COLOR = "body_color" |
| 26 | + |
| 27 | + fun toBundle( |
| 28 | + viewType: IterableEmbeddedViewType, |
| 29 | + message: IterableEmbeddedMessage, |
| 30 | + config: IterableEmbeddedViewConfig? |
| 31 | + ): Bundle { |
| 32 | + return Bundle().apply { |
| 33 | + putString(KEY_VIEW_TYPE, viewType.name) |
| 34 | + putString(KEY_MESSAGE_JSON, IterableEmbeddedMessage.toJSONObject(message).toString()) |
| 35 | + putConfig(config) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + fun getViewType(arguments: Bundle): IterableEmbeddedViewType { |
| 40 | + val viewTypeName = arguments.getString(KEY_VIEW_TYPE) |
| 41 | + return viewTypeName?.let { |
| 42 | + try { |
| 43 | + IterableEmbeddedViewType.valueOf(it) |
| 44 | + } catch (e: IllegalArgumentException) { |
| 45 | + IterableLogger.e(TAG, "Invalid view type: $it, defaulting to BANNER") |
| 46 | + IterableEmbeddedViewType.BANNER |
| 47 | + } |
| 48 | + } ?: IterableEmbeddedViewType.BANNER |
| 49 | + } |
| 50 | + |
| 51 | + fun getMessage(arguments: Bundle): IterableEmbeddedMessage { |
| 52 | + val messageJsonString = arguments.getString(KEY_MESSAGE_JSON) |
| 53 | + return if (messageJsonString != null) { |
| 54 | + try { |
| 55 | + val messageJson = JSONObject(messageJsonString) |
| 56 | + IterableEmbeddedMessage.fromJSONObject(messageJson) |
| 57 | + } catch (e: JSONException) { |
| 58 | + IterableLogger.e(TAG, "Failed to parse message JSON", e) |
| 59 | + throw IllegalStateException( |
| 60 | + "IterableEmbeddedView failed to restore message from saved state. Use newInstance() factory method to create this fragment." |
| 61 | + ) |
| 62 | + } |
| 63 | + } else { |
| 64 | + throw IllegalStateException( |
| 65 | + "IterableEmbeddedView requires a message argument. Use newInstance() factory method to create this fragment." |
| 66 | + ) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + fun getConfig(arguments: Bundle): IterableEmbeddedViewConfig? { |
| 71 | + // Check if any config properties exist |
| 72 | + val hasConfig = arguments.containsKey(KEY_BG_COLOR) || |
| 73 | + arguments.containsKey(KEY_BORDER_COLOR) || |
| 74 | + arguments.containsKey(KEY_BORDER_WIDTH) || |
| 75 | + arguments.containsKey(KEY_BORDER_RADIUS) || |
| 76 | + arguments.containsKey(KEY_PRIMARY_BTN_BG) || |
| 77 | + arguments.containsKey(KEY_PRIMARY_BTN_TEXT) || |
| 78 | + arguments.containsKey(KEY_SECONDARY_BTN_BG) || |
| 79 | + arguments.containsKey(KEY_SECONDARY_BTN_TEXT) || |
| 80 | + arguments.containsKey(KEY_TITLE_COLOR) || |
| 81 | + arguments.containsKey(KEY_BODY_COLOR) |
| 82 | + |
| 83 | + return if (hasConfig) { |
| 84 | + IterableEmbeddedViewConfig( |
| 85 | + backgroundColor = arguments.getIntOrNull(KEY_BG_COLOR), |
| 86 | + borderColor = arguments.getIntOrNull(KEY_BORDER_COLOR), |
| 87 | + borderWidth = arguments.getIntOrNull(KEY_BORDER_WIDTH), |
| 88 | + borderCornerRadius = arguments.getFloatOrNull(KEY_BORDER_RADIUS), |
| 89 | + primaryBtnBackgroundColor = arguments.getIntOrNull(KEY_PRIMARY_BTN_BG), |
| 90 | + primaryBtnTextColor = arguments.getIntOrNull(KEY_PRIMARY_BTN_TEXT), |
| 91 | + secondaryBtnBackgroundColor = arguments.getIntOrNull(KEY_SECONDARY_BTN_BG), |
| 92 | + secondaryBtnTextColor = arguments.getIntOrNull(KEY_SECONDARY_BTN_TEXT), |
| 93 | + titleTextColor = arguments.getIntOrNull(KEY_TITLE_COLOR), |
| 94 | + bodyTextColor = arguments.getIntOrNull(KEY_BODY_COLOR) |
| 95 | + ) |
| 96 | + } else { |
| 97 | + null |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private fun Bundle.putConfig(config: IterableEmbeddedViewConfig?) { |
| 102 | + config?.let { cfg -> |
| 103 | + cfg.backgroundColor?.let { putInt(KEY_BG_COLOR, it) } |
| 104 | + cfg.borderColor?.let { putInt(KEY_BORDER_COLOR, it) } |
| 105 | + cfg.borderWidth?.let { putInt(KEY_BORDER_WIDTH, it) } |
| 106 | + cfg.borderCornerRadius?.let { putFloat(KEY_BORDER_RADIUS, it) } |
| 107 | + cfg.primaryBtnBackgroundColor?.let { putInt(KEY_PRIMARY_BTN_BG, it) } |
| 108 | + cfg.primaryBtnTextColor?.let { putInt(KEY_PRIMARY_BTN_TEXT, it) } |
| 109 | + cfg.secondaryBtnBackgroundColor?.let { putInt(KEY_SECONDARY_BTN_BG, it) } |
| 110 | + cfg.secondaryBtnTextColor?.let { putInt(KEY_SECONDARY_BTN_TEXT, it) } |
| 111 | + cfg.titleTextColor?.let { putInt(KEY_TITLE_COLOR, it) } |
| 112 | + cfg.bodyTextColor?.let { putInt(KEY_BODY_COLOR, it) } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private fun Bundle.getIntOrNull(key: String): Int? { |
| 117 | + return if (containsKey(key)) getInt(key) else null |
| 118 | + } |
| 119 | + |
| 120 | + private fun Bundle.getFloatOrNull(key: String): Float? { |
| 121 | + return if (containsKey(key)) getFloat(key) else null |
| 122 | + } |
| 123 | +} |
0 commit comments