-
Notifications
You must be signed in to change notification settings - Fork 8
/
ContentCardsFragment.kt
369 lines (337 loc) · 17.4 KB
/
ContentCardsFragment.kt
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package com.braze.ui.contentcards
import android.os.Build
import android.os.Bundle
import android.os.Parcelable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SimpleItemAnimator
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.braze.Braze
import com.braze.coroutine.BrazeCoroutineScope
import com.braze.events.ContentCardsUpdatedEvent
import com.braze.events.ContentCardsUpdatedEvent.Companion.emptyUpdate
import com.braze.events.IEventSubscriber
import com.braze.events.SdkDataWipeEvent
import com.braze.support.BrazeLogger.Priority.I
import com.braze.support.BrazeLogger.Priority.V
import com.braze.support.BrazeLogger.brazelog
import com.braze.ui.R
import com.braze.ui.contentcards.adapters.ContentCardAdapter
import com.braze.ui.contentcards.adapters.EmptyContentCardsAdapter
import com.braze.ui.contentcards.handlers.DefaultContentCardsUpdateHandler
import com.braze.ui.contentcards.handlers.DefaultContentCardsViewBindingHandler
import com.braze.ui.contentcards.handlers.IContentCardsUpdateHandler
import com.braze.ui.contentcards.handlers.IContentCardsViewBindingHandler
import com.braze.ui.contentcards.recycler.ContentCardsDividerItemDecoration
import com.braze.ui.contentcards.recycler.SimpleItemTouchHelperCallback
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
/**
* A fragment to display Braze ContentCards.
*/
@Suppress("TooManyFunctions")
open class ContentCardsFragment : Fragment(), SwipeRefreshLayout.OnRefreshListener {
/**
* A runnable to execute when the network is determined
* to be unavailable.
*/
protected var networkUnavailableJob: Job? = null
/**
* A [RecyclerView] associated with [ContentCardsFragment].
* Note that this will be null until [Fragment.onCreateView] is called.
*/
var contentCardsRecyclerView: RecyclerView? = null
protected set
@JvmField
var cardAdapter: ContentCardAdapter? = null
protected var defaultEmptyContentCardsAdapter: EmptyContentCardsAdapter = EmptyContentCardsAdapter()
protected var contentCardsSwipeLayout: SwipeRefreshLayout? = null
protected var contentCardsUpdatedSubscriber: IEventSubscriber<ContentCardsUpdatedEvent>? = null
protected var sdkDataWipeEventSubscriber: IEventSubscriber<SdkDataWipeEvent>? = null
protected val defaultContentCardUpdateHandler: IContentCardsUpdateHandler = DefaultContentCardsUpdateHandler()
protected var customContentCardUpdateHandler: IContentCardsUpdateHandler? = null
protected val defaultContentCardsViewBindingHandler: IContentCardsViewBindingHandler = DefaultContentCardsViewBindingHandler()
protected var customContentCardsViewBindingHandler: IContentCardsViewBindingHandler? = null
/**
* An adapter to display when no cards are available for display.
*/
protected val emptyCardsAdapter: RecyclerView.Adapter<*>
get() = defaultEmptyContentCardsAdapter
// Since the get always returns non-null, but we want to allow passing null into the setter to
// clear the instance, we split this out into a custom getter and setter rather than using a var
/**
* @return the [IContentCardsUpdateHandler] for this [ContentCardsFragment].
*/
fun getContentCardUpdateHandler() =
customContentCardUpdateHandler ?: defaultContentCardUpdateHandler
/**
* Set the [IContentCardsUpdateHandler] for this [ContentCardsFragment]. This handler is for doing
* any work on [Card]s before being rendered in the ContentCards.
* If not set or set to null, the default handler will be used.
*/
fun setContentCardUpdateHandler(value: IContentCardsUpdateHandler?) {
customContentCardUpdateHandler = value
}
// Since the get always returns non-null, but we want to allow passing null into the setter to
// clear the instance, we split this out into a custom getter and setter rather than using a var
/**
* @return the [IContentCardsViewBindingHandler] responsible for rendering each [Card] in the [RecyclerView].
*/
fun getContentCardsViewBindingHandler() =
customContentCardsViewBindingHandler ?: defaultContentCardsViewBindingHandler
/**
* Set the [IContentCardsViewBindingHandler] responsible for rendering each [Card] in the [RecyclerView].
* Note that this should only be set before the [ContentCardsFragment] is first displayed or the
* [ContentCardAdapter] will not update correctly.
*/
fun setContentCardsViewBindingHandler(value: IContentCardsViewBindingHandler?) {
customContentCardsViewBindingHandler = value
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val rootView = inflater.inflate(R.layout.com_braze_content_cards, container, false)
contentCardsRecyclerView = rootView.findViewById(R.id.com_braze_content_cards_recycler)
contentCardsSwipeLayout = rootView.findViewById(R.id.braze_content_cards_swipe_container)
contentCardsSwipeLayout?.setOnRefreshListener(this)
contentCardsSwipeLayout?.setColorSchemeResources(
R.color.com_braze_content_cards_swipe_refresh_color_1,
R.color.com_braze_content_cards_swipe_refresh_color_2,
R.color.com_braze_content_cards_swipe_refresh_color_3,
R.color.com_braze_content_cards_swipe_refresh_color_4
)
return rootView
}
/**
* Called when the user swipes down and requests a feed refresh.
*/
override fun onRefresh() {
Braze.getInstance(requireContext()).requestContentCardsRefresh()
BrazeCoroutineScope.launchDelayed(AUTO_HIDE_REFRESH_INDICATOR_DELAY_MS) { contentCardsSwipeLayout?.isRefreshing = false }
}
override fun onResume() {
super.onResume()
// Remove the previous subscriber before rebuilding a new one with our new activity.
Braze.getInstance(requireContext()).removeSingleSubscription(contentCardsUpdatedSubscriber, ContentCardsUpdatedEvent::class.java)
if (contentCardsUpdatedSubscriber == null) {
contentCardsUpdatedSubscriber = IEventSubscriber { event: ContentCardsUpdatedEvent -> handleContentCardsUpdatedEvent(event) }
}
contentCardsUpdatedSubscriber?.let {
Braze.getInstance(requireContext()).subscribeToContentCardsUpdates(it)
}
Braze.getInstance(requireContext()).requestContentCardsRefreshFromCache()
Braze.getInstance(requireContext()).removeSingleSubscription(sdkDataWipeEventSubscriber, SdkDataWipeEvent::class.java)
if (sdkDataWipeEventSubscriber == null) {
// If the SDK data is wiped, then we want to clear any cached Content Cards
sdkDataWipeEventSubscriber = IEventSubscriber { handleContentCardsUpdatedEvent(emptyUpdate) }
}
sdkDataWipeEventSubscriber?.let {
Braze.getInstance(requireContext()).addSingleSynchronousSubscription(it, SdkDataWipeEvent::class.java)
}
}
override fun onPause() {
super.onPause()
brazelog(V) { "Invoked com.braze.ui.contentcards.ContentCardsFragment.onPause()" }
// If the view is going away, we don't care about updating it anymore. Remove the subscription immediately.
Braze.getInstance(requireContext()).removeSingleSubscription(contentCardsUpdatedSubscriber, ContentCardsUpdatedEvent::class.java)
Braze.getInstance(requireContext()).removeSingleSubscription(sdkDataWipeEventSubscriber, SdkDataWipeEvent::class.java)
networkUnavailableJob?.cancel()
networkUnavailableJob = null
cardAdapter?.markOnScreenCardsAsRead()
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
contentCardsRecyclerView?.layoutManager?.let {
outState.putParcelable(LAYOUT_MANAGER_SAVED_INSTANCE_STATE_KEY, it.onSaveInstanceState())
}
cardAdapter?.let {
outState.putStringArrayList(KNOWN_CARD_IMPRESSIONS_SAVED_INSTANCE_STATE_KEY, ArrayList(it.impressedCardIds))
}
customContentCardsViewBindingHandler?.let {
outState.putParcelable(VIEW_BINDING_HANDLER_SAVED_INSTANCE_STATE_KEY, it)
}
customContentCardUpdateHandler?.let {
outState.putParcelable(UPDATE_HANDLER_SAVED_INSTANCE_STATE_KEY, it)
}
}
override fun onViewStateRestored(savedInstanceState: Bundle?) {
super.onViewStateRestored(savedInstanceState)
if (savedInstanceState != null) {
val updateHandlerParcelable: IContentCardsUpdateHandler? =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
savedInstanceState.getParcelable(UPDATE_HANDLER_SAVED_INSTANCE_STATE_KEY, IContentCardsUpdateHandler::class.java)
} else {
@Suppress("DEPRECATION")
savedInstanceState.getParcelable(UPDATE_HANDLER_SAVED_INSTANCE_STATE_KEY)
}
if (updateHandlerParcelable != null) {
setContentCardUpdateHandler(updateHandlerParcelable)
}
val viewBindingHandlerParcelable: IContentCardsViewBindingHandler? =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
savedInstanceState.getParcelable(VIEW_BINDING_HANDLER_SAVED_INSTANCE_STATE_KEY, IContentCardsViewBindingHandler::class.java)
} else {
@Suppress("DEPRECATION")
savedInstanceState.getParcelable(VIEW_BINDING_HANDLER_SAVED_INSTANCE_STATE_KEY)
}
if (viewBindingHandlerParcelable != null) {
setContentCardsViewBindingHandler(viewBindingHandlerParcelable)
}
BrazeCoroutineScope.launch(Dispatchers.Main) {
val layoutManagerState =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
savedInstanceState.getParcelable(LAYOUT_MANAGER_SAVED_INSTANCE_STATE_KEY, Parcelable::class.java)
} else {
@Suppress("DEPRECATION")
savedInstanceState.getParcelable(LAYOUT_MANAGER_SAVED_INSTANCE_STATE_KEY)
}
contentCardsRecyclerView?.let {
val layoutManager = it.layoutManager
if (layoutManagerState != null && layoutManager != null) {
layoutManager.onRestoreInstanceState(layoutManagerState)
}
}
cardAdapter?.let {
val savedCardIdImpressions: List<String>? = savedInstanceState.getStringArrayList(KNOWN_CARD_IMPRESSIONS_SAVED_INSTANCE_STATE_KEY)
if (savedCardIdImpressions != null) {
it.impressedCardIds = savedCardIdImpressions
}
}
}
}
initializeRecyclerView()
}
protected fun initializeRecyclerView() {
val layoutManager = LinearLayoutManager(activity)
cardAdapter = ContentCardAdapter(
requireContext(),
layoutManager,
mutableListOf(),
getContentCardsViewBindingHandler()
)
contentCardsRecyclerView?.adapter = cardAdapter
contentCardsRecyclerView?.layoutManager = layoutManager
attachSwipeHelperCallback()
// Disable any animations when the items change to avoid any issues when the data changes
// see https://stackoverflow.com/questions/29331075/recyclerview-blinking-after-notifydatasetchanged
val animator = contentCardsRecyclerView?.itemAnimator
if (animator is SimpleItemAnimator) {
animator.supportsChangeAnimations = false
}
// Add an item divider
contentCardsRecyclerView?.addItemDecoration(ContentCardsDividerItemDecoration(requireContext()))
}
/**
* Creates and attaches a [SimpleItemTouchHelperCallback] to handle swipe-to-dismiss functionality.
*/
protected fun attachSwipeHelperCallback() {
cardAdapter?.let {
val itemTouchHelper = ItemTouchHelper(SimpleItemTouchHelperCallback(it))
itemTouchHelper.attachToRecyclerView(contentCardsRecyclerView)
}
}
/**
* Handles the processing and rendering for a [ContentCardsUpdatedEvent] on the UI thread.
*/
protected fun handleContentCardsUpdatedEvent(event: ContentCardsUpdatedEvent) {
BrazeCoroutineScope.launch(Dispatchers.Main) { contentCardsUpdate(event) }
}
/**
* A main thread runnable to handle [ContentCardsUpdatedEvent] on the main thread.
*/
protected suspend fun contentCardsUpdate(event: ContentCardsUpdatedEvent) {
brazelog(V) { "Updating Content Cards views in response to ContentCardsUpdatedEvent: $event" }
// This list of cards could undergo filtering in the card update handler
// and be a smaller list of cards compared to the original list
// in the update event. Thus, any "empty feed" checks should be
// performed on this filtered list and not the original list of cards.
val cardsForRendering = getContentCardUpdateHandler().handleCardUpdate(event)
cardAdapter?.replaceCards(cardsForRendering)
networkUnavailableJob?.cancel()
networkUnavailableJob = null
// If the update came from storage and is stale, then request a refresh.
if (event.isFromOfflineStorage && event.isTimestampOlderThan(MAX_CONTENT_CARDS_TTL_SECONDS.toLong())) {
brazelog(I) {
"ContentCards received was older than the max time to live of " +
"$MAX_CONTENT_CARDS_TTL_SECONDS seconds, displaying it for now, but " +
"requesting an updated view from the server."
}
Braze.getInstance(requireContext()).requestContentCardsRefresh()
// If we don't have any cards to display, we put up the spinner while
// we wait for the network to return.
// Eventually displaying an error message if it doesn't.
if (cardsForRendering.isEmpty()) {
// Display a loading indicator
contentCardsSwipeLayout?.isRefreshing = true
brazelog {
"Old Content Cards was empty, putting up a network spinner and " +
"registering the network error message on a delay of " +
"$NETWORK_PROBLEM_WARNING_MS ms."
}
networkUnavailableJob?.cancel()
networkUnavailableJob = BrazeCoroutineScope.launchDelayed(NETWORK_PROBLEM_WARNING_MS, Dispatchers.Main) {
networkUnavailable()
}
return
}
}
// The cards are either fresh from the cache, or came directly from a
// network request. An empty Content Cards should just display
// an "empty ContentCards" message.
if (cardsForRendering.isNotEmpty()) {
// The Content Cards contains cards and should be displayed.
// The card adapter shouldn't be null at this point
cardAdapter?.let {
swapRecyclerViewAdapter(it)
}
} else {
// The Content Cards is empty and should display an "empty" message to the user.
swapRecyclerViewAdapter(emptyCardsAdapter)
}
// Stop the refresh animation
contentCardsSwipeLayout?.isRefreshing = false
}
/**
* A main thread runnable to handle displaying network unavailable messages on the main thread.
*/
protected suspend fun networkUnavailable() {
brazelog(V) { "Displaying network unavailable toast." }
context?.applicationContext?.let { applicationContext ->
Toast.makeText(
applicationContext,
applicationContext.getString(R.string.com_braze_feed_connection_error_title),
Toast.LENGTH_LONG
).show()
}
swapRecyclerViewAdapter(emptyCardsAdapter)
contentCardsSwipeLayout?.isRefreshing = false
}
/**
* Swaps the current [RecyclerView] [RecyclerView.Adapter] for a new one. If
* the current adapter matches the new adapter, then this method does nothing.
*/
protected fun swapRecyclerViewAdapter(newAdapter: RecyclerView.Adapter<*>) {
val recyclerView = contentCardsRecyclerView
if (recyclerView != null && recyclerView.adapter !== newAdapter) {
recyclerView.adapter = newAdapter
}
}
companion object {
private const val MAX_CONTENT_CARDS_TTL_SECONDS = 60
private const val NETWORK_PROBLEM_WARNING_MS = 5000L
private const val AUTO_HIDE_REFRESH_INDICATOR_DELAY_MS = 2500L
private const val LAYOUT_MANAGER_SAVED_INSTANCE_STATE_KEY = "LAYOUT_MANAGER_SAVED_INSTANCE_STATE_KEY"
private const val KNOWN_CARD_IMPRESSIONS_SAVED_INSTANCE_STATE_KEY = "KNOWN_CARD_IMPRESSIONS_SAVED_INSTANCE_STATE_KEY"
private const val VIEW_BINDING_HANDLER_SAVED_INSTANCE_STATE_KEY = "VIEW_BINDING_HANDLER_SAVED_INSTANCE_STATE_KEY"
private const val UPDATE_HANDLER_SAVED_INSTANCE_STATE_KEY = "UPDATE_HANDLER_SAVED_INSTANCE_STATE_KEY"
}
}