Skip to content

Commit

Permalink
Add withExperiment extension method to Nimbus (mozilla-mobile#16926) …
Browse files Browse the repository at this point in the history
…r=christian, sebastian

* Fixes mozilla-mobile#16925 Add withExperiment extension method to Nimbus
* Don't call Nimbus at all if not enabled by FeatureFlag
  • Loading branch information
jhugman committed Dec 10, 2020
1 parent 2c625da commit 9a984f1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 14 deletions.
42 changes: 42 additions & 0 deletions app/src/main/java/org/mozilla/fenix/ext/Nimbus.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.fenix.ext

import mozilla.components.service.nimbus.NimbusApi
import mozilla.components.support.base.log.logger.Logger
import org.mozilla.fenix.FeatureFlags

/**
* Gets the branch of the given `experimentId` and transforms it with given closure.
*
* If we're enrolled in the experiment, the transform is passed the branch id/slug as a `String`.
*
* If we're not enrolled in the experiment, or the experiment is not valid then the transform
* is passed a `null`.
*/
@Suppress("TooGenericExceptionCaught")
fun <T> NimbusApi.withExperiment(experimentId: String, transform: (String?) -> T): T {
val branch = if (FeatureFlags.nimbusExperiments) {
try {
getExperimentBranch(experimentId)
} catch (e: Throwable) {
Logger.error("Failed to getExperimentBranch($experimentId)", e)
null
}
} else {
null
}
return transform(branch)
}

/**
* The degenerate case of `withExperiment(String, (String?) -> T))`, with an identity transform.
*
* Short-hand for `mozilla.components.service.nimbus.NimbusApi.getExperimentBranch`.
*/
fun NimbusApi.withExperiment(experimentId: String) =
this.withExperiment(experimentId, ::identity)

private fun <T> identity(value: T) = value
26 changes: 12 additions & 14 deletions app/src/main/java/org/mozilla/fenix/home/HomeMenu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import org.mozilla.fenix.experiments.ExperimentBranch
import org.mozilla.fenix.experiments.Experiments
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.ext.withExperiment
import org.mozilla.fenix.theme.ThemeManager
import org.mozilla.fenix.whatsnew.WhatsNew

Expand Down Expand Up @@ -99,14 +100,12 @@ class HomeMenu(
}

val experiments = context.components.analytics.experiments
val bookmarksIcon = experiments.getExperimentBranch(Experiments.BOOKMARK_ICON)
.let {
when (it) {
ExperimentBranch.TREATMENT -> R.drawable.ic_bookmark_list
else -> R.drawable.ic_bookmark_filled
}
val bookmarksIcon = experiments.withExperiment(Experiments.BOOKMARK_ICON) {
when (it) {
ExperimentBranch.TREATMENT -> R.drawable.ic_bookmark_list
else -> R.drawable.ic_bookmark_filled
}

}
val bookmarksItem = BrowserMenuImageText(
context.getString(R.string.library_bookmarks),
bookmarksIcon,
Expand All @@ -122,14 +121,13 @@ class HomeMenu(
// user isn't targeted, then we get still get the same treatment.
// The `let` block is degenerate here, but left here so as to document the form of how experiments
// are implemented here.
val historyIcon = experiments.getExperimentBranch(Experiments.A_A_NIMBUS_VALIDATION)
.let {
when (it) {
ExperimentBranch.A1 -> R.drawable.ic_history
ExperimentBranch.A2 -> R.drawable.ic_history
else -> R.drawable.ic_history
}
val historyIcon = experiments.withExperiment(Experiments.A_A_NIMBUS_VALIDATION) {
when (it) {
ExperimentBranch.A1 -> R.drawable.ic_history
ExperimentBranch.A2 -> R.drawable.ic_history
else -> R.drawable.ic_history
}
}
val historyItem = BrowserMenuImageText(
context.getString(R.string.library_history),
historyIcon,
Expand Down

0 comments on commit 9a984f1

Please sign in to comment.