Skip to content

Commit

Permalink
MBL-1767: SegmentTrackingClient migration to RXJava2 (#2145)
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkariang authored Oct 7, 2024
1 parent b8f0062 commit d061220
Show file tree
Hide file tree
Showing 19 changed files with 80 additions and 78 deletions.
6 changes: 3 additions & 3 deletions app/src/main/java/com/kickstarter/ApplicationModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -427,9 +427,9 @@ Application provideApplication() {
@Singleton
SegmentTrackingClient provideSegmentTrackingClient(
final @ApplicationContext @NonNull Context context,
final @NonNull CurrentUserType currentUser,
final @NonNull CurrentUserTypeV2 currentUser,
final @NonNull Build build,
final @NonNull CurrentConfigType currentConfig,
final @NonNull CurrentConfigTypeV2 currentConfig,
final @NonNull FeatureFlagClientType featureFlagClient) {
return new SegmentTrackingClient(build, context, currentConfig, currentUser, featureFlagClient, PreferenceManager.getDefaultSharedPreferences(context));
}
Expand Down Expand Up @@ -574,7 +574,7 @@ static Gson provideGson() {

@Provides
@Singleton
static KSCurrency provideKSCurrency(final @NonNull CurrentConfigType currentConfig) {
static KSCurrency provideKSCurrency(final @NonNull CurrentConfigTypeV2 currentConfig) {
return new KSCurrency(currentConfig);
}

Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/kickstarter/libs/KSCurrency.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fun KSCurrency?.getCurrencySymbols(project: Project): Pair<String, String> {
return currencySymbolStartAndEnd
}

class KSCurrency(private val currentConfig: CurrentConfigType) {
class KSCurrency(private val currentConfig: CurrentConfigTypeV2) {
/**
* Returns a currency string appropriate to the user's locale and location relative to a project.
*
Expand Down Expand Up @@ -116,8 +116,8 @@ class KSCurrency(private val currentConfig: CurrentConfigType) {
fun currencyNeedsCode(country: Country, excludeCurrencyCode: Boolean): Boolean {
val countryIsUS = country === Country.US
val config = currentConfig.observable()
.toBlocking()
.first()
.blockingFirst()

val currencyNeedsCode = config.currencyNeedsCode(country.currencySymbol)
val userInUS = config.countryCode() == Country.US.countryCode
return if (userInUS && excludeCurrencyCode && countryIsUS) {
Expand Down
19 changes: 10 additions & 9 deletions app/src/main/java/com/kickstarter/libs/SegmentTrackingClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.kickstarter.libs.braze.BrazeClient
import com.kickstarter.libs.featureflag.FeatureFlagClientType
import com.kickstarter.libs.utils.Secrets
import com.kickstarter.libs.utils.extensions.isKSApplication
import com.kickstarter.libs.utils.extensions.isNotNull
import com.kickstarter.models.User
import com.kickstarter.models.extensions.NAME
import com.kickstarter.models.extensions.getTraits
Expand All @@ -19,15 +18,15 @@ import com.segment.analytics.Traits
import com.segment.analytics.android.integrations.appboy.AppboyIntegration
import com.segment.analytics.integrations.BasePayload
import com.segment.analytics.integrations.IdentifyPayload
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import timber.log.Timber

open class SegmentTrackingClient(
build: Build,
private val context: Context,
currentConfig: CurrentConfigType,
currentUser: CurrentUserType,
currentConfig: CurrentConfigTypeV2,
currentUser: CurrentUserTypeV2,
ffClient: FeatureFlagClientType,
preference: SharedPreferences
) : TrackingClient(context, currentUser, build, currentConfig, ffClient, preference) {
Expand All @@ -44,7 +43,7 @@ open class SegmentTrackingClient(
this.currentConfig.observable()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe {
.map {
this.config = it
if (calledFromOnCreate) {
privateInitializer()
Expand All @@ -55,14 +54,16 @@ open class SegmentTrackingClient(
}
}
}
.subscribe()

this.currentUser.observable()
.filter { it.isNotNull() }
.map { requireNotNull(it) }
.subscribe {
.filter { it.isPresent() }
.map { requireNotNull(it.getValue()) }
.map {
this.loggedInUser = it
identify(it)
}
.subscribe()
}

override fun initialize() {
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/kickstarter/libs/TrackingClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import javax.inject.Inject

abstract class TrackingClient(
@param:ApplicationContext private val context: Context,
@set:Inject var currentUser: CurrentUserType,
@set:Inject var currentUser: CurrentUserTypeV2,
@set:Inject var build: Build,
@set:Inject var currentConfig: CurrentConfigType,
@set:Inject var currentConfig: CurrentConfigTypeV2,
@set:Inject var ffClient: FeatureFlagClientType,
@set:Inject var sharedPreferences: SharedPreferences
) : TrackingClientType() {
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/kickstarter/libs/braze/BrazeClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import com.braze.support.BrazeLogger
import com.braze.ui.inappmessage.BrazeInAppMessageManager
import com.google.firebase.messaging.RemoteMessage
import com.kickstarter.libs.Build
import com.kickstarter.libs.CurrentUserType
import com.kickstarter.libs.CurrentUserTypeV2
import com.kickstarter.libs.utils.Secrets
import com.kickstarter.libs.utils.extensions.isKSApplication
import com.kickstarter.libs.utils.extensions.registerActivityLifecycleCallbacks
Expand Down Expand Up @@ -151,7 +151,7 @@ open class BrazeClient(
* on the `onIntegrationReady` callback
*/
companion object {
fun setInAppCustomListener(currentUser: CurrentUserType, build: Build) {
fun setInAppCustomListener(currentUser: CurrentUserTypeV2, build: Build) {
BrazeInAppMessageManager.getInstance().setCustomInAppMessageManagerListener(InAppCustomListener(currentUser, build))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.braze.models.inappmessage.IInAppMessage
import com.braze.ui.inappmessage.InAppMessageOperation
import com.braze.ui.inappmessage.listeners.IInAppMessageManagerListener
import com.kickstarter.libs.Build
import com.kickstarter.libs.CurrentUserType
import com.kickstarter.libs.CurrentUserTypeV2
import timber.log.Timber

/**
Expand All @@ -15,7 +15,7 @@ import timber.log.Timber
* for now we just need `beforeInAppMessageDisplayed`.
*/
class InAppCustomListener(
loggedInUser: CurrentUserType,
loggedInUser: CurrentUserTypeV2,
private val build: Build
) : IInAppMessageManagerListener {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package com.kickstarter.libs.braze

import com.kickstarter.libs.CurrentUserType
import com.kickstarter.libs.CurrentUserTypeV2
import com.kickstarter.models.User

class InAppCustomListenerHandler(
private val currentUser: CurrentUserType
private val currentUser: CurrentUserTypeV2
) {
private var loggedInUser: User? = null

init {

this.currentUser.observable()
.filter { it.isPresent() }
.map { requireNotNull(it.getValue()) }
.distinctUntilChanged()
.subscribe {
.map {
this.loggedInUser = it
}
.subscribe()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ object ConfigFactory {

fun configForUSUser(): Config {
return config()
.toBuilder()
.countryCode("US")
.build()
}

fun configForCA(): Config {
Expand Down
6 changes: 3 additions & 3 deletions app/src/test/java/com/kickstarter/KSCurrencyTest.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.kickstarter

import com.kickstarter.libs.CurrentConfigType
import com.kickstarter.libs.CurrentConfigTypeV2
import com.kickstarter.libs.KSCurrency
import com.kickstarter.libs.models.Country
import com.kickstarter.mock.MockCurrentConfig
import com.kickstarter.mock.MockCurrentConfigV2
import com.kickstarter.mock.factories.ConfigFactory.config
import com.kickstarter.mock.factories.ProjectFactory.caProject
import com.kickstarter.mock.factories.ProjectFactory.project
Expand Down Expand Up @@ -690,7 +690,7 @@ class KSCurrencyTest : TestCase() {
val config = config().toBuilder()
.countryCode(countryCode)
.build()
val currentConfig: CurrentConfigType = MockCurrentConfig()
val currentConfig: CurrentConfigTypeV2 = MockCurrentConfigV2()
currentConfig.config(config)
return KSCurrency(currentConfig)
}
Expand Down
17 changes: 6 additions & 11 deletions app/src/test/java/com/kickstarter/KSRobolectricTestCase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ import com.kickstarter.libs.AttributionEvents
import com.kickstarter.libs.Environment
import com.kickstarter.libs.KSCurrency
import com.kickstarter.libs.KSString
import com.kickstarter.libs.MockCurrentUser
import com.kickstarter.libs.MockCurrentUserV2
import com.kickstarter.libs.MockTrackingClient
import com.kickstarter.libs.TrackingClientType
import com.kickstarter.libs.featureflag.FeatureFlagClientType
import com.kickstarter.libs.utils.Secrets
import com.kickstarter.mock.MockCurrentConfig
import com.kickstarter.mock.MockCurrentConfigV2
import com.kickstarter.mock.MockFeatureFlagClient
import com.kickstarter.mock.factories.ConfigFactory
Expand Down Expand Up @@ -56,28 +55,24 @@ abstract class KSRobolectricTestCase : TestCase() {
super.setUp()

val mockApolloClientV2 = MockApolloClientV2()
val mockCurrentConfig = MockCurrentConfig()
val mockCurrentConfigV2 = MockCurrentConfigV2()
val mockFeatureFlagClient = MockFeatureFlagClient()
val segmentTestClient = segmentTrackingClient(mockCurrentConfig, mockFeatureFlagClient)
val segmentTestClient = segmentTrackingClient(mockCurrentConfigV2, mockFeatureFlagClient)

val component = DaggerApplicationComponent.builder()
.applicationModule(TestApplicationModule(application()))
.build()

val config = ConfigFactory.config().toBuilder()
.build()
val config = ConfigFactory.configForUSUser()

mockCurrentConfig.config(config)
mockCurrentConfigV2.config(config)

environment = component.environment().toBuilder()
.ksCurrency(KSCurrency(mockCurrentConfig))
.apiClient(MockApiClient())
.apolloClient(MockApolloClient())
.apolloClientV2(mockApolloClientV2)
.currentConfig(mockCurrentConfig)
.currentConfig2(mockCurrentConfigV2)
.ksCurrency(KSCurrency(mockCurrentConfigV2))
.stripe(Stripe(context(), Secrets.StripePublishableKey.STAGING))
.analytics(AnalyticEvents(listOf(segmentTestClient)))
.attributionEvents(AttributionEvents(mockApolloClientV2))
Expand All @@ -100,11 +95,11 @@ abstract class KSRobolectricTestCase : TestCase() {

protected fun ksString() = KSString(application().packageName, application().resources)

private fun segmentTrackingClient(mockCurrentConfig: MockCurrentConfig, ffClient: FeatureFlagClientType): MockTrackingClient {
private fun segmentTrackingClient(mockCurrentConfig: MockCurrentConfigV2, ffClient: FeatureFlagClientType): MockTrackingClient {
segmentTrack = TestSubscriber()
segmentIdentify = TestSubscriber()
val segmentTrackingClient = MockTrackingClient(
MockCurrentUser(),
MockCurrentUserV2(),
mockCurrentConfig,
TrackingClientType.Type.SEGMENT,
ffClient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import com.kickstarter.features.pledgedprojectsoverview.data.PPOCardFactory
import com.kickstarter.features.pledgedprojectsoverview.data.PledgedProjectsOverviewEnvelope
import com.kickstarter.features.pledgedprojectsoverview.data.PledgedProjectsOverviewQueryData
import com.kickstarter.libs.AnalyticEvents
import com.kickstarter.libs.MockCurrentUser
import com.kickstarter.libs.MockCurrentUserV2
import com.kickstarter.libs.MockTrackingClient
import com.kickstarter.libs.TrackingClientType
import com.kickstarter.libs.utils.EventName
import com.kickstarter.mock.MockCurrentConfig
import com.kickstarter.mock.MockCurrentConfigV2
import com.kickstarter.mock.MockFeatureFlagClient
import com.kickstarter.mock.factories.ProjectFactory
import com.kickstarter.mock.factories.UserFactory
Expand Down Expand Up @@ -204,8 +204,8 @@ class PledgedProjectsOverviewViewModelTest : KSRobolectricTestCase() {
val mutableTotalAlerts = MutableStateFlow<Int>(0)
val user = UserFactory.user()
val trackingClient = MockTrackingClient(
MockCurrentUser(user),
MockCurrentConfig(),
MockCurrentUserV2(user),
MockCurrentConfigV2(),
TrackingClientType.Type.SEGMENT,
MockFeatureFlagClient()
)
Expand Down Expand Up @@ -246,8 +246,8 @@ class PledgedProjectsOverviewViewModelTest : KSRobolectricTestCase() {
val totalAlertsList = mutableListOf<Int>()
val user = UserFactory.user()
val trackingClient = MockTrackingClient(
MockCurrentUser(user),
MockCurrentConfig(),
MockCurrentUserV2(user),
MockCurrentConfigV2(),
TrackingClientType.Type.SEGMENT,
MockFeatureFlagClient()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class InAppCustomListenerHandlerTest : KSRobolectricTestCase() {
@Test
fun testMessageShouldShow_True() {
val user = UserFactory.user()
val mockUser = MockCurrentUser(user)
val mockUser = MockCurrentUserV2(user)
val handler = InAppCustomListenerHandler(mockUser)

Thread.sleep(100) // wait a bit until InAppCustomListenerHandler.init block executed
Expand All @@ -133,7 +133,7 @@ class InAppCustomListenerHandlerTest : KSRobolectricTestCase() {

@Test
fun testMessageShouldShow_False() {
val mockUser = MockCurrentUser() // - no user logged in
val mockUser = MockCurrentUserV2() // - no user logged in
val handler = InAppCustomListenerHandler(mockUser)

Thread.sleep(100) // wait a bit until InAppCustomListenerHandler.init block executed
Expand All @@ -143,7 +143,7 @@ class InAppCustomListenerHandlerTest : KSRobolectricTestCase() {
@Test
fun testInAppCustomListener_DisplayNow() {
val user = UserFactory.user()
val mockUser = MockCurrentUser(user)
val mockUser = MockCurrentUserV2(user)
val listener = InAppCustomListener(mockUser, build)

Thread.sleep(100) // wait a bit until InAppCustomListener.init block executed
Expand All @@ -152,7 +152,7 @@ class InAppCustomListenerHandlerTest : KSRobolectricTestCase() {

@Test
fun testInAppCustomListener_Discard() {
val mockUser = MockCurrentUser() // - no user logged in
val mockUser = MockCurrentUserV2() // - no user logged in
val listener = InAppCustomListener(mockUser, build)

Thread.sleep(100) // wait a bit until InAppCustomListener.init block executed
Expand Down
8 changes: 4 additions & 4 deletions app/src/test/java/com/kickstarter/libs/MockTrackingClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import rx.subjects.BehaviorSubject
import rx.subjects.PublishSubject

class MockTrackingClient(
currentUser: CurrentUserType,
currentConfig: CurrentConfigType,
currentUser: CurrentUserTypeV2,
currentConfig: CurrentConfigTypeV2,
private val type: Type,
private val ffClient: FeatureFlagClientType
) : TrackingClientType() {
Expand Down Expand Up @@ -47,8 +47,8 @@ class MockTrackingClient(
val identifiedUser = BehaviorSubject.create<User?>()

init {
currentUser.observable().subscribe { user: User? -> propagateUser(user) }
currentConfig.observable().subscribe { c: Config? -> config = c }
currentUser.observable().map { propagateUser(it.getValue()) }.subscribe()
currentConfig.observable().map { c: Config? -> config = c }.subscribe()
}

override fun track(eventName: String, additionalProperties: Map<String, Any>) {
Expand Down
Loading

0 comments on commit d061220

Please sign in to comment.