Skip to content

Commit

Permalink
refactor(UserSettingDataSource): Removed UserSettingsDataSource classes
Browse files Browse the repository at this point in the history
Logic from UserSettingsDataSource moved directly into UserSettingsRepository.
All basic user parameters (shop_id, seance, segment, did, sid) goes through SharedPreferences.
  • Loading branch information
looee1q committed Jan 9, 2025
1 parent ebc2402 commit 0d60144
Show file tree
Hide file tree
Showing 16 changed files with 127 additions and 201 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import javax.inject.Inject
open class SDK {

internal lateinit var context: Context
private lateinit var segment: String

private var onMessageListener: OnMessageListener? = null
private var search: Search = Search(JSONObject())
Expand Down Expand Up @@ -137,13 +136,11 @@ open class SDK {
context = context,
preferencesKey = preferencesKey
)
segment = getPreferencesValueUseCase.getSegment()

notificationHandler.initialize(context = context)

initUserSettingsUseCase.invoke(
shopId = shopId,
segment = segment,
stream = stream
)

Expand Down Expand Up @@ -662,7 +659,7 @@ open class SDK {
/**
* Returns the current segment for A/B testing
*/
fun getSegment(): String = instance.segment
fun getSegment(): String = getUserSettingsValueUseCase.getSegmentForABTesting()

/**
* Add user to a segment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@ import com.personalization.sdk.data.repositories.preferences.PreferencesDataSour
import com.personalization.sdk.data.repositories.preferences.PreferencesDataSourceImpl
import com.personalization.sdk.data.repositories.recommendation.RecommendationDataSource
import com.personalization.sdk.data.repositories.recommendation.RecommendationDataSourceImpl
import com.personalization.sdk.data.repositories.userSettings.UserSettingsDataSource
import com.personalization.sdk.data.repositories.userSettings.UserSettingsDataSourceImpl
import dagger.Binds
import dagger.Module
import dagger.Provides
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import javax.inject.Singleton

@Module
Expand All @@ -34,28 +30,5 @@ interface DataSourcesModule {
): NotificationDataSource = NotificationDataSourceImpl(
preferencesDataSource = preferencesDataSource
)

@Provides
@Singleton
fun provideUserSettingsDataSource(
preferencesDataSource: PreferencesDataSource,
shopId: String,
segment: String,
stream: String
): UserSettingsDataSource = UserSettingsDataSourceImpl(
preferencesDataSource = preferencesDataSource,
shopId = shopId,
segment = segment,
stream = stream
)
}
}

@AssistedFactory
interface UserSettingsDataSourceFactory {
fun create(
@Assisted("shopId") shopId: String,
@Assisted("segment") segment: String,
@Assisted("stream") stream: String
): UserSettingsDataSourceImpl
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.personalization.sdk.data.models.params

object UserBasicParams {
const val SHOP_ID = "shop_id"
const val DID = "did"
const val SEANCE = "seance"
const val SID = "sid"
const val SEGMENT = "segment"
const val STREAM = "stream"
const val SOURCE_FROM = "from"
const val SOURCE_CODE = "code"
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package com.personalization.sdk.data.repositories.network
import android.net.Uri
import com.personalization.SDK
import com.personalization.api.OnApiCallbackListener
import com.personalization.sdk.data.models.params.UserBasicParams
import com.personalization.sdk.data.utils.QueryParamsUtils.addMultipleParams
import com.personalization.sdk.domain.models.NetworkMethod
import com.personalization.sdk.domain.models.NotificationSource
import com.personalization.sdk.domain.repositories.NetworkRepository
import com.personalization.sdk.domain.repositories.NotificationRepository
import com.personalization.sdk.domain.repositories.UserSettingsRepository
Expand Down Expand Up @@ -165,7 +168,7 @@ class NetworkRepositoryImpl @Inject constructor(
notificationRepository.getNotificationSource(TimeUtils.TWO_DAYS.inWholeMilliseconds)

try {
val newParams = userSettingsRepository.addParams(
val newParams = addBasicQueryParams(
params = params,
notificationSource = notificationSource,
)
Expand All @@ -176,6 +179,26 @@ class NetworkRepositoryImpl @Inject constructor(
}
}

private fun addBasicQueryParams(
params: JSONObject,
notificationSource: NotificationSource?
): JSONObject {
addMultipleParams(
params = params,
paramsToAdd = mapOf(
UserBasicParams.SHOP_ID to userSettingsRepository.getShopId(),
UserBasicParams.DID to userSettingsRepository.getDid(),
UserBasicParams.SID to userSettingsRepository.getSid(),
UserBasicParams.SEANCE to userSettingsRepository.getSid(),
UserBasicParams.SEGMENT to userSettingsRepository.getSegmentForABTesting(),
UserBasicParams.STREAM to userSettingsRepository.getStream(),
UserBasicParams.SOURCE_FROM to notificationSource?.type,
UserBasicParams.SOURCE_CODE to notificationSource?.id
)
)
return params
}

private fun executeMethod(
networkMethod: NetworkMethod,
params: JSONObject,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ interface PreferencesDataSource {

fun saveLastPushTokenDate(value: Long)

fun getSegment(): String

fun getValue(field: String, defaultValue: String): String

fun getValue(field: String, defaultValue: Long): Long
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ import javax.inject.Singleton

private const val DEFAULT_TOKEN = ""
private const val DEFAULT_LAST_PUSH_TOKEN_DATE = 0L
private val DEFAULT_SEGMENT = arrayOf("A", "B").random()

private const val TOKEN_KEY = "token"
private const val LAST_PUSH_TOKEN_DATE_KEY = "last_push_token_date"
private const val SEGMENT_KEY = ".segment"

@Singleton
class PreferencesDataSourceImpl @Inject constructor() : PreferencesDataSource {
Expand Down Expand Up @@ -44,14 +42,6 @@ class PreferencesDataSourceImpl @Inject constructor() : PreferencesDataSource {
)
}

override fun getSegment(): String {
val field = preferencesKey + SEGMENT_KEY
return getValue(
field = field,
defaultValue = DEFAULT_SEGMENT
)
}

override fun getValue(field: String, defaultValue: String): String {
return sharedPreferences?.getString(field, defaultValue) ?: defaultValue
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,4 @@ class PreferencesRepositoryImpl @Inject constructor(
override fun saveLastPushTokenDate(value: Long) {
preferencesDataSource.saveLastPushTokenDate(value)
}

override fun getSegment(): String = preferencesDataSource.getSegment()
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 0d60144

Please sign in to comment.