-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathIterableEmbeddedView.kt
More file actions
240 lines (200 loc) · 11.4 KB
/
IterableEmbeddedView.kt
File metadata and controls
240 lines (200 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package com.iterable.iterableapi.ui.embedded
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.bumptech.glide.Glide
import com.iterable.iterableapi.EmbeddedMessageElementsButton
import com.iterable.iterableapi.IterableApi
import com.iterable.iterableapi.IterableEmbeddedMessage
import com.iterable.iterableapi.ui.R
class IterableEmbeddedView() : Fragment() {
private lateinit var viewType: IterableEmbeddedViewType
private lateinit var message: IterableEmbeddedMessage
private var config: IterableEmbeddedViewConfig? = null
/**
* @deprecated This constructor violates Android Fragment best practices and will cause crashes
* when the Fragment is recreated by the system (e.g., after configuration changes or process death).
* Use [newInstance] factory method instead.
*
* Migration example:
* ```
* // Old (unstable / not-recommended):
* val fragment = IterableEmbeddedView(viewType, message, config)
*
* // New (more stable / recommended):
* val fragment = IterableEmbeddedView.newInstance(viewType, message, config)
* ```
*
* This constructor will be removed in a future version.
*/
@Deprecated(
message = "Use newInstance() factory method instead. This constructor causes crashes when Fragment is recreated by the system.",
replaceWith = ReplaceWith("IterableEmbeddedView.newInstance(viewType, message, config)"),
level = DeprecationLevel.WARNING
)
constructor(
viewType: IterableEmbeddedViewType,
message: IterableEmbeddedMessage,
config: IterableEmbeddedViewConfig?
) : this() {
arguments = IterableEmbeddedViewArguments.toBundle(viewType, message, config)
}
private val defaultBackgroundColor : Int by lazy { getDefaultColor(viewType, R.color.notification_background_color, R.color.banner_background_color, R.color.banner_background_color) }
private val defaultBorderColor : Int by lazy { getDefaultColor(viewType, R.color.notification_border_color, R.color.banner_border_color, R.color.banner_border_color) }
private val defaultPrimaryBtnBackgroundColor: Int by lazy { getDefaultColor(viewType, R.color.white, R.color.white, R.color.banner_button_color) }
private val defaultPrimaryBtnTextColor: Int by lazy { getDefaultColor(viewType, R.color.notification_text_color, R.color.banner_button_color, R.color.white) }
private val defaultSecondaryBtnBackgroundColor: Int by lazy { getDefaultColor(viewType, R.color.notification_background_color, R.color.white, R.color.white) }
private val defaultSecondaryBtnTextColor: Int by lazy { getDefaultColor(viewType, R.color.notification_text_color, R.color.banner_button_color, R.color.banner_button_color) }
private val defaultTitleTextColor: Int by lazy { getDefaultColor(viewType, R.color.notification_text_color, R.color.title_text_color, R.color.title_text_color) }
private val defaultBodyTextColor: Int by lazy { getDefaultColor(viewType, R.color.notification_text_color, R.color.body_text_color, R.color.body_text_color) }
private val defaultBorderWidth = 1
private val defaultBorderCornerRadius = 8f
companion object {
/**
* Factory method to create a new instance of IterableEmbeddedView with the required parameters.
*
* @param viewType The type of embedded view to display
* @param message The embedded message to display
* @param config Optional configuration for customizing the view appearance
* @return A new instance of IterableEmbeddedView
*/
@JvmStatic
fun newInstance(
viewType: IterableEmbeddedViewType,
message: IterableEmbeddedMessage,
config: IterableEmbeddedViewConfig? = null
): IterableEmbeddedView {
return IterableEmbeddedView().apply {
arguments = IterableEmbeddedViewArguments.toBundle(viewType, message, config)
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let { args ->
viewType = IterableEmbeddedViewArguments.getViewType(args)
message = IterableEmbeddedViewArguments.getMessage(args)
config = IterableEmbeddedViewArguments.getConfig(args)
} ?: throw IllegalStateException(
"IterableEmbeddedView requires arguments. Use newInstance() factory method to create this fragment."
)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = when (viewType) {
IterableEmbeddedViewType.BANNER -> {
val bannerView = inflater.inflate(R.layout.banner_view, container, false)
bind(viewType, bannerView, message, config)
bannerView
}
IterableEmbeddedViewType.CARD -> {
val cardView = inflater.inflate(R.layout.card_view, container, false)
bind(viewType, cardView, message, config)
cardView
}
IterableEmbeddedViewType.NOTIFICATION -> {
val notificationView = inflater.inflate(R.layout.notification_view, container, false)
bind(viewType, notificationView, message, config)
notificationView
}
}
setDefaultAction(view, message)
configure(view, viewType, config)
return view
}
private fun configure(view: View, viewType: IterableEmbeddedViewType, config: IterableEmbeddedViewConfig?) {
val backgroundColor = config?.backgroundColor.takeIf { it != null } ?: defaultBackgroundColor
val borderColor = config?.borderColor.takeIf { it != null } ?: defaultBorderColor
val borderWidth = config?.borderWidth.takeIf { it != null } ?: defaultBorderWidth
val borderCornerRadius = config?.borderCornerRadius.takeIf { it != null } ?: defaultBorderCornerRadius
val primaryBtnBackgroundColor = config?.primaryBtnBackgroundColor.takeIf { it != null } ?: defaultPrimaryBtnBackgroundColor
val primaryBtnTextColor = config?.primaryBtnTextColor.takeIf { it != null } ?: defaultPrimaryBtnTextColor
val secondaryBtnBackgroundColor = config?.secondaryBtnBackgroundColor.takeIf { it != null } ?: defaultSecondaryBtnBackgroundColor
val secondaryBtnTextColor = config?.secondaryBtnTextColor.takeIf { it != null } ?: defaultSecondaryBtnTextColor
val titleTextColor = config?.titleTextColor.takeIf { it != null } ?: defaultTitleTextColor
val bodyTextColor = config?.bodyTextColor.takeIf { it != null } ?: defaultBodyTextColor
val gradientDrawable = GradientDrawable()
gradientDrawable.setColor(backgroundColor)
gradientDrawable.setStroke(borderWidth, borderColor)
gradientDrawable.cornerRadius = borderCornerRadius
view.background = gradientDrawable
val firstButton = view.findViewById<Button>(R.id.embedded_message_first_button)
val secondButton = view.findViewById<Button>(R.id.embedded_message_second_button)
val titleText = view.findViewById<TextView>(R.id.embedded_message_title)
val bodyText = view.findViewById<TextView>(R.id.embedded_message_body)
firstButton.setBackgroundColor(primaryBtnBackgroundColor)
firstButton.setTextColor(primaryBtnTextColor)
secondButton.setBackgroundColor(secondaryBtnBackgroundColor)
secondButton.setTextColor(secondaryBtnTextColor)
firstButton.setTextColor(primaryBtnTextColor)
secondButton.setTextColor(secondaryBtnTextColor)
titleText.setTextColor(titleTextColor)
bodyText.setTextColor(bodyTextColor)
}
private fun bind(viewType: IterableEmbeddedViewType, view: View, message: IterableEmbeddedMessage, config: IterableEmbeddedViewConfig?): View {
val embeddedMessageViewTitle: TextView = view.findViewById(R.id.embedded_message_title)
val embeddedMessageViewBody: TextView = view.findViewById(R.id.embedded_message_body)
val embeddedMessageViewButton: Button = view.findViewById(R.id.embedded_message_first_button)
val embeddedMessageViewButton2: Button = view.findViewById(R.id.embedded_message_second_button)
if(viewType != IterableEmbeddedViewType.NOTIFICATION) {
val embeddedMessageImageView: ImageView = view.findViewById(R.id.embedded_message_image)
if(message.elements?.mediaURL?.isEmpty() == true) {
embeddedMessageImageView.visibility = View.GONE
} else {
config?.imageScaleType?.let { embeddedMessageImageView.scaleType = it }
Glide.with(view.context).load(message.elements?.mediaURL).into(embeddedMessageImageView)
embeddedMessageImageView.contentDescription = message.elements?.mediaUrlCaption
}
}
embeddedMessageViewTitle.text = message.elements?.title
embeddedMessageViewBody.text = message.elements?.body
val buttons = message.elements?.buttons
if (buttons != null) {
setButton(embeddedMessageViewButton, buttons.getOrNull(0), message)
if (buttons.size > 1) {
setButton(embeddedMessageViewButton2, buttons.getOrNull(1), message)
} else {
embeddedMessageViewButton2.visibility = View.GONE
}
} else {
embeddedMessageViewButton.visibility = View.INVISIBLE
embeddedMessageViewButton2.visibility = View.INVISIBLE
}
return view
}
private fun setDefaultAction(view: View, message: IterableEmbeddedMessage) {
if(message.elements?.defaultAction != null) {
val clickedUrl = message.elements?.defaultAction?.data.takeIf { it?.isNotEmpty() == true } ?: message.elements?.defaultAction?.type
view.setOnClickListener {
IterableApi.getInstance().embeddedManager.handleEmbeddedClick(message, null, clickedUrl)
IterableApi.getInstance().trackEmbeddedClick(message, null, clickedUrl)
}
}
}
private fun setButton(buttonView: Button, button: EmbeddedMessageElementsButton?, message: IterableEmbeddedMessage) {
buttonView.visibility = if (button?.title == null) View.GONE else View.VISIBLE
buttonView.text = button?.title.orEmpty()
val clickedUrl = if (button?.action?.data?.isNotEmpty() == true) button.action?.data else button?.action?.type
buttonView.setOnClickListener {
IterableApi.getInstance().embeddedManager.handleEmbeddedClick(message, button?.id, clickedUrl)
IterableApi.getInstance().trackEmbeddedClick(message, button?.id, clickedUrl)
}
}
private fun getDefaultColor(viewType: IterableEmbeddedViewType, notificationColor: Int, cardColor: Int, bannerColor: Int): Int {
return when (viewType) {
IterableEmbeddedViewType.NOTIFICATION -> ContextCompat.getColor(requireContext(), notificationColor)
IterableEmbeddedViewType.CARD -> ContextCompat.getColor(requireContext(), cardColor)
else -> ContextCompat.getColor(requireContext(), bannerColor)
}
}
}