Skip to content

Commit

Permalink
For mozilla-mobile#26777 - Part 2: Refactor the Jump Back In onboardi…
Browse files Browse the repository at this point in the history
…ng CFR to use the CFRPopup
  • Loading branch information
gabrielluong committed Sep 7, 2022
1 parent ffae5e8 commit 9e76ac7
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 160 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import org.mozilla.fenix.home.Mode
import org.mozilla.fenix.home.OnboardingState
import org.mozilla.fenix.home.recentbookmarks.RecentBookmark
import org.mozilla.fenix.home.recentvisits.RecentlyVisitedItem
import org.mozilla.fenix.onboarding.JumpBackInCFRDialog
import org.mozilla.fenix.onboarding.HomeCFRPresenter
import org.mozilla.fenix.utils.Settings

Expand Down Expand Up @@ -213,17 +212,15 @@ class SessionControlView(
override fun onLayoutCompleted(state: RecyclerView.State?) {
super.onLayoutCompleted(state)

if (!context.settings().showHomeOnboardingDialog) {
if (context.settings().showSyncCFR) {
HomeCFRPresenter(
context = context,
recyclerView = view,
).showSyncCFR()
}

if (context.settings().shouldShowJumpBackInCFR) {
JumpBackInCFRDialog(view).showIfNeeded()
}
if (!context.settings().showHomeOnboardingDialog && (
context.settings().showSyncCFR ||
context.settings().shouldShowJumpBackInCFR
)
) {
HomeCFRPresenter(
context = context,
recyclerView = view,
).show()
}

// We want some parts of the home screen UI to be rendered first if they are
Expand Down
115 changes: 85 additions & 30 deletions app/src/main/java/org/mozilla/fenix/onboarding/HomeCFRPresenter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,70 +10,125 @@ import androidx.compose.ui.unit.dp
import androidx.recyclerview.widget.RecyclerView
import mozilla.components.service.glean.private.NoExtras
import org.mozilla.fenix.GleanMetrics.Onboarding
import org.mozilla.fenix.GleanMetrics.RecentTabs
import org.mozilla.fenix.R
import org.mozilla.fenix.compose.cfr.CFRPopup
import org.mozilla.fenix.compose.cfr.CFRPopupProperties
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.home.recentsyncedtabs.view.RecentSyncedTabViewHolder
import org.mozilla.fenix.home.recenttabs.view.RecentTabsHeaderViewHolder

/**
* Vertical padding needed to improve the visual alignment of the popup and respect the UX design.
*/
private const val CFR_TO_ANCHOR_VERTICAL_PADDING = -16

/**
* Delegate for handling synced tab onboarding CFR.
* Delegate for handling home onboarding CFR.
*
* @param context [Context] used for various Android interactions.
* @param recyclerView [RecyclerView] will serve as anchor for the sync CFR.
* @param recyclerView [RecyclerView] will serve as anchor for the CFR.
*/
class HomeCFRPresenter(
private val context: Context,
private val recyclerView: RecyclerView,
) {

private var syncCFR: CFRPopup? = null
private val popupProperties = CFRPopupProperties(
indicatorDirection = CFRPopup.IndicatorDirection.DOWN,
popupVerticalOffset = CFR_TO_ANCHOR_VERTICAL_PADDING.dp,
)

/**
* Find the synced view and if it is available, then show the synced tab CFR.
* Determine the CFR to be shown on the Home screen and show a CFR for the resultant view.
*/
fun showSyncCFR() {
findSyncTabsView()?.let {
CFRPopup(
text = context.getString(R.string.sync_cfr_message),
anchor = it,
properties = CFRPopupProperties(
indicatorDirection = CFRPopup.IndicatorDirection.DOWN,
popupVerticalOffset = CFR_TO_ANCHOR_VERTICAL_PADDING.dp,
),
onDismiss = {
when (it) {
true -> Onboarding.syncCfrExplicitDismissal.record(NoExtras())
false -> Onboarding.syncCfrImplicitDismissal.record(NoExtras())
}
fun show() {
when (val result = getCFRToShow()) {
is Result.SyncedTab -> showSyncCFR(result.view)
is Result.JumpBackIn -> showJumpBackInCFR(result.view)
else -> { /* noop */ }
}
}

private fun showSyncCFR(view: View) {
CFRPopup(
text = context.getString(R.string.sync_cfr_message),
anchor = view,
properties = popupProperties,
onDismiss = {
when (it) {
true -> Onboarding.syncCfrExplicitDismissal.record(NoExtras())
false -> Onboarding.syncCfrImplicitDismissal.record(NoExtras())
}
).apply {
syncCFR = this
show()
}
).show()

context.settings().showSyncCFR = false
context.settings().shouldShowJumpBackInCFR = false
context.settings().showSyncCFR = false
context.settings().shouldShowJumpBackInCFR = false

Onboarding.synCfrShown.record(NoExtras())
}
Onboarding.synCfrShown.record(NoExtras())
}

private fun findSyncTabsView(): View? {
val count = recyclerView.adapter?.itemCount ?: return null
private fun showJumpBackInCFR(view: View) {
CFRPopup(
text = context.getString(R.string.onboarding_home_screen_jump_back_contextual_hint_2),
anchor = view,
properties = popupProperties,
onDismiss = {
when (it) {
true -> RecentTabs.jumpBackInCfrDismissed.record(NoExtras())
false -> RecentTabs.jumpBackInCfrCancelled.record(NoExtras())
}
}
).show()

context.settings().shouldShowJumpBackInCFR = false

RecentTabs.jumpBackInCfrShown.record(NoExtras())
}

/**
* Returns a [Result] that indicates if the CFR that should be shown on the Home screen if any
* based on the views available and the preferences.
*/
private fun getCFRToShow(): Result {
var result: Result = Result.None
val count = recyclerView.adapter?.itemCount ?: return result

for (index in count downTo 0) {
val viewHolder = recyclerView.findViewHolderForAdapterPosition(index)
if (viewHolder is RecentSyncedTabViewHolder) {
return viewHolder.composeView

if (context.settings().showSyncCFR && viewHolder is RecentSyncedTabViewHolder) {
result = Result.SyncedTab(viewHolder.composeView)
break
} else if (context.settings().shouldShowJumpBackInCFR &&
viewHolder is RecentTabsHeaderViewHolder
) {
result = Result.JumpBackIn(viewHolder.composeView)
}
}

return null
return result
}

/**
* The result of determining which CFR to show on the Home screen.
*/
sealed class Result {
/**
* Indicates no CFR should be shown on the Home screen.
*/
object None : Result()

/**
* Indicates a CFR should be shown for a Synced Tab and the associated [view] to anchor
* the CFR.
*/
data class SyncedTab(val view: View) : Result()

/**
* Indicates a CFR should be for Jump Back In and the associated [view] to anchor the CFR.
*/
data class JumpBackIn(val view: View) : Result()
}
}
118 changes: 0 additions & 118 deletions app/src/main/java/org/mozilla/fenix/onboarding/JumpBackInCFRDialog.kt

This file was deleted.

0 comments on commit 9e76ac7

Please sign in to comment.