Skip to content

Commit

Permalink
refactor (network): create UserSettingsDataSource with constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
xeewii committed Aug 16, 2024
1 parent bc0c979 commit c06dd5e
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ open class SDK {
shopId = shopId,
shopSecretKey = shopSecretKey,
segment = segment,
stream = stream,
userAgent = userAgent()
stream = stream
)
initNetworkUseCase.invoke(
baseUrl = apiUrl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import com.personalization.sdk.data.repositories.network.NetworkDataSource
import com.personalization.sdk.data.repositories.notification.NotificationDataSource
import com.personalization.sdk.data.repositories.preferences.PreferencesDataSource
import com.personalization.sdk.data.repositories.recommendation.RecommendationDataSource
import com.personalization.sdk.data.repositories.user.UserSettingsDataSource
import com.personalization.sdk.data.repositories.userSettings.UserSettingsDataSource
import dagger.Module
import dagger.Provides
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import javax.inject.Singleton

Expand All @@ -19,16 +20,21 @@ class DataSourcesModule {

@AssistedFactory
interface NetworkDataSourceFactory {
fun create(baseUrl: String): NetworkDataSource
fun create(
baseUrl: String
): NetworkDataSource
}

@Provides
@Singleton
fun provideUserDataSource(
preferencesDataSource: PreferencesDataSource
) = UserSettingsDataSource(
preferencesDataSource = preferencesDataSource
)
@AssistedFactory
interface UserSettingsDataSourceFactory {
@Singleton
fun create(
@Assisted("shopId") shopId: String,
@Assisted("shopSecretKey") shopSecretKey: String,
@Assisted("segment") segment: String,
@Assisted("stream") stream: String
): UserSettingsDataSource
}

@Provides
@Singleton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import com.personalization.sdk.data.repositories.network.NetworkRepositoryImpl
import com.personalization.sdk.data.repositories.notification.NotificationRepositoryImpl
import com.personalization.sdk.data.repositories.preferences.PreferencesRepositoryImpl
import com.personalization.sdk.data.repositories.recommendation.RecommendationRepositoryImpl
import com.personalization.sdk.data.repositories.user.UserSettingsRepositoryImpl
import com.personalization.sdk.data.repositories.userSettings.UserSettingsRepositoryImpl
import com.personalization.sdk.domain.repositories.NetworkRepository
import com.personalization.sdk.domain.repositories.NotificationRepository
import com.personalization.sdk.domain.repositories.PreferencesRepository
import com.personalization.sdk.domain.repositories.RecommendationRepository
import com.personalization.sdk.domain.repositories.UserSettingsRepository
import dagger.Binds
import dagger.Module
import javax.inject.Singleton

@Module
abstract class RepositoriesModule {
Expand All @@ -20,9 +21,11 @@ abstract class RepositoriesModule {
abstract fun bindPreferencesRepository(impl: PreferencesRepositoryImpl): PreferencesRepository

@Binds
@Singleton
abstract fun bindUserRepository(impl: UserSettingsRepositoryImpl): UserSettingsRepository

@Binds
@Singleton
abstract fun bindNetworkRepository(impl: NetworkRepositoryImpl): NetworkRepository

@Binds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import android.net.Uri
import com.personalization.SDK
import com.personalization.api.OnApiCallbackListener
import com.personalization.sdk.data.di.DataSourcesModule
import com.personalization.sdk.data.repositories.user.UserSettingsDataSource
import com.personalization.sdk.domain.models.NetworkMethod
import com.personalization.sdk.domain.repositories.NetworkRepository
import com.personalization.sdk.domain.repositories.NotificationRepository
import com.personalization.sdk.domain.repositories.UserSettingsRepository
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
Expand All @@ -27,7 +27,7 @@ import javax.inject.Inject

class NetworkRepositoryImpl @Inject constructor(
private val networkDataSourceFactory: DataSourcesModule.NetworkDataSourceFactory,
private val userDataSource: UserSettingsDataSource,
private val userSettingsRepository: UserSettingsRepository,
private val notificationRepository: NotificationRepository
) : NetworkRepository {

Expand Down Expand Up @@ -125,7 +125,7 @@ class NetworkRepositoryImpl @Inject constructor(

private fun sendAsync(sendFunction: () -> Unit) {
val thread = Thread(sendFunction)
if (userDataSource.getDid().isNotEmpty() && userDataSource.getIsInitialized()) {
if (userSettingsRepository.getDid().isNotEmpty() && userSettingsRepository.getIsInitialized()) {
thread.start()
} else {
addTaskToQueue(thread)
Expand Down Expand Up @@ -162,12 +162,12 @@ class NetworkRepositoryImpl @Inject constructor(
listener: OnApiCallbackListener?,
isSecret: Boolean = false
) {
userDataSource.saveSidLastActTime(System.currentTimeMillis())
userSettingsRepository.updateSidLastActTime()

val notificationSource = notificationRepository.getNotificationSource(NetworkDataSource.TWO_DAYS_MILLISECONDS)

try {
val newParams = userDataSource.addParams(
val newParams = userSettingsRepository.addParams(
params = params,
notificationSource = notificationSource,
isSecret = isSecret
Expand Down
Original file line number Diff line number Diff line change
@@ -1,66 +1,52 @@
package com.personalization.sdk.data.repositories.user
package com.personalization.sdk.data.repositories.userSettings

import com.personalization.sdk.data.repositories.preferences.PreferencesDataSource
import com.personalization.sdk.domain.models.NotificationSource
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import org.json.JSONObject
import javax.inject.Inject

class UserSettingsDataSource @Inject constructor(
private val preferencesDataSource: PreferencesDataSource
class UserSettingsDataSource @AssistedInject constructor(
private val preferencesDataSource: PreferencesDataSource,
@Assisted("shopId") private val shopId: String,
@Assisted("shopSecretKey") private val shopSecretKey: String,
@Assisted("segment") private val segment: String,
@Assisted("stream") private val stream: String
) {

private var shopId: String = ""
private var shopSecretKey: String = ""
private var segment: String = ""
private var stream: String = ""
private var userAgent: String = ""

private var isInitialized: Boolean = false

fun initialize(
shopId: String,
shopSecretKey: String,
segment: String,
stream: String,
userAgent: String
) {
this.shopId = shopId
this.shopSecretKey = shopSecretKey
this.segment = segment
this.stream = stream
this.userAgent = userAgent
}

private fun addOptionalParam(params: JSONObject, key: String, value: String?) {
value?.let { params.put(key, it) }
}

internal fun addParams(
params: JSONObject,
notificationSource: NotificationSource?,
isSecret: Boolean = false
): JSONObject {
params.put(SHOP_ID_PARAMS_FIELD, shopId)

if (isSecret) {
params.put(SHOP_SECRET_KEY_PARAMS_FIELD, shopSecretKey)
internal fun addParams(
params: JSONObject,
notificationSource: NotificationSource?,
isSecret: Boolean = false
): JSONObject {
params.put(SHOP_ID_PARAMS_FIELD, shopId)

if (isSecret) {
params.put(SHOP_SECRET_KEY_PARAMS_FIELD, shopSecretKey)
}

addOptionalParam(params, DID_PARAMS_FIELD, getDid())
addOptionalParam(params, SID_PARAMS_FIELD, getSid())
addOptionalParam(params, SEANCE_PARAMS_FIELD, getSid())
params.put(SEGMENT_PARAMS_FIELD, segment)
params.put(STREAM_PARAMS_FIELD, stream)

notificationSource?.let {
val notificationObject = JSONObject()
.put(SOURCE_FROM_FIELD, it.type)
.put(SOURCE_CODE_FIELD, it.id)
params.put(SOURCE_PARAMS_FIELD, notificationObject)
}

return params
}

addOptionalParam(params, DID_PARAMS_FIELD, getDid())
addOptionalParam(params, SEANCE_PARAMS_FIELD, getSid())
params.put(SEGMENT_PARAMS_FIELD, segment)
params.put(STREAM_PARAMS_FIELD, stream)

notificationSource?.let {
val notificationObject = JSONObject()
.put(SOURCE_FROM_FIELD, it.type)
.put(SOURCE_CODE_FIELD, it.id)
params.put(SOURCE_PARAMS_FIELD, notificationObject)
}

return params
}

internal fun getSidLastActTime(): Long = preferencesDataSource.getValue(SID_LAST_ACT_KEY, DEFAULT_SID_LAST_ACT_TIME)
internal fun saveSidLastActTime(value: Long) = preferencesDataSource.saveValue(SID_LAST_ACT_KEY, value)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
package com.personalization.sdk.data.repositories.user
package com.personalization.sdk.data.repositories.userSettings

import com.personalization.sdk.data.di.DataSourcesModule
import com.personalization.sdk.domain.models.NotificationSource
import com.personalization.sdk.domain.repositories.UserSettingsRepository
import org.json.JSONObject
import javax.inject.Inject

class UserSettingsRepositoryImpl @Inject constructor(
private val userSettingsDataSource: UserSettingsDataSource
private val userSettingsDataSourceFactory: DataSourcesModule.UserSettingsDataSourceFactory,
) : UserSettingsRepository {

private lateinit var userSettingsDataSource: UserSettingsDataSource

override fun initialize(
shopId: String,
shopSecretKey: String,
segment: String,
stream: String,
userAgent: String
stream: String
) {
userSettingsDataSource.initialize(
userSettingsDataSource = userSettingsDataSourceFactory.create(
shopId = shopId,
shopSecretKey = shopSecretKey,
segment = segment,
stream = stream,
userAgent = userAgent
stream = stream
)
}

Expand Down Expand Up @@ -51,4 +54,15 @@ class UserSettingsRepositoryImpl @Inject constructor(
override fun updateIsInitialized(value: Boolean) {
userSettingsDataSource.setIsInitialized(value)
}

override fun addParams(
params: JSONObject,
notificationSource: NotificationSource?,
isSecret: Boolean
): JSONObject =
userSettingsDataSource.addParams(
params = params,
notificationSource = notificationSource,
isSecret = isSecret
)
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.personalization.sdk.domain.repositories

import com.personalization.sdk.domain.models.NotificationSource
import org.json.JSONObject

interface UserSettingsRepository {

fun initialize(
shopId: String,
shopSecretKey: String,
segment: String,
stream: String,
userAgent: String
stream: String
)

fun getDid(): String
Expand All @@ -21,4 +23,10 @@ interface UserSettingsRepository {

fun getIsInitialized(): Boolean
fun updateIsInitialized(value: Boolean)

fun addParams(
params: JSONObject,
notificationSource: NotificationSource?,
isSecret: Boolean = false
): JSONObject
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ class InitUserSettingsUseCase @Inject constructor(
shopId: String,
shopSecretKey: String,
segment: String,
stream: String,
userAgent: String
stream: String
) {
userSettingsRepository.initialize(
shopId = shopId,
shopSecretKey = shopSecretKey,
segment = segment,
stream = stream,
userAgent = userAgent
stream = stream
)
}
}

0 comments on commit c06dd5e

Please sign in to comment.