Updated on 2026-08-14
This commit is contained in:
parent
144876bd4b
commit
7b68f938a8
8 changed files with 54 additions and 69 deletions
|
|
@ -5,7 +5,7 @@ import com.tangem.datasource.api.promotion.PromotionApi
|
|||
import com.tangem.datasource.api.promotion.models.*
|
||||
import com.tangem.feature.learn2earn.data.api.Learn2earnPreferenceStorage
|
||||
import com.tangem.feature.learn2earn.data.api.Learn2earnRepository
|
||||
import com.tangem.feature.learn2earn.data.api.UsedCardsPreferenceStorage
|
||||
import com.tangem.feature.learn2earn.data.models.PromoUserData
|
||||
import com.tangem.utils.coroutines.AppCoroutineDispatcherProvider
|
||||
import kotlinx.coroutines.withContext
|
||||
import timber.log.Timber
|
||||
|
|
@ -15,38 +15,32 @@ import timber.log.Timber
|
|||
*/
|
||||
internal class DefaultLearn2earnRepository(
|
||||
private val preferencesStorage: Learn2earnPreferenceStorage,
|
||||
private val usedCardsPreferenceStorage: UsedCardsPreferenceStorage,
|
||||
private val moshi: Moshi,
|
||||
private val api: PromotionApi,
|
||||
private val dispatchers: AppCoroutineDispatcherProvider,
|
||||
moshi: Moshi,
|
||||
) : Learn2earnRepository {
|
||||
|
||||
private val promotionInfoAdapter = moshi.adapter(PromotionInfoResponse::class.java)
|
||||
private val userInfoAdapter = moshi.adapter(PromoUserData::class.java)
|
||||
|
||||
override fun isHadActivatedCards(): Boolean {
|
||||
return usedCardsPreferenceStorage.isHadActivatedCards()
|
||||
private var userData: PromoUserData = restoreUserData()
|
||||
|
||||
override fun getUserData(): PromoUserData {
|
||||
return userData
|
||||
}
|
||||
|
||||
override fun getPromoCode(): String? {
|
||||
return preferencesStorage.promoCode
|
||||
}
|
||||
|
||||
override fun savePromoCode(code: String) {
|
||||
preferencesStorage.promoCode = code
|
||||
override fun updateUserData(userData: PromoUserData) {
|
||||
this.userData = userData
|
||||
saveUserData(userData)
|
||||
}
|
||||
|
||||
override fun getProgramName(): String {
|
||||
return PROGRAM_NAME
|
||||
}
|
||||
|
||||
override fun isAlreadyReceivedAward(): Boolean {
|
||||
return preferencesStorage.alreadyReceivedAward
|
||||
}
|
||||
|
||||
override suspend fun getPromotionInfo(): Result<PromotionInfoResponse> {
|
||||
return withContext(dispatchers.io) {
|
||||
val info = restorePromotionInfo()
|
||||
|
||||
if (info?.status == PromotionInfoResponse.Status.FINISHED) {
|
||||
Result.success(info)
|
||||
} else {
|
||||
|
|
@ -68,9 +62,9 @@ internal class DefaultLearn2earnRepository(
|
|||
}
|
||||
}
|
||||
|
||||
override suspend fun requestAward(walletId: String): AwardResponse {
|
||||
override suspend fun requestAward(walletId: String, address: String): AwardResponse {
|
||||
return withContext(dispatchers.io) {
|
||||
api.requestAward(AwardRequestBody(walletId, getProgramName()))
|
||||
api.requestAward(AwardRequestBody(walletId, address, getProgramName()))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -104,6 +98,31 @@ internal class DefaultLearn2earnRepository(
|
|||
}
|
||||
}
|
||||
|
||||
private fun restoreUserData(): PromoUserData {
|
||||
return try {
|
||||
preferencesStorage.userData?.let { userInfoAdapter.fromJson(it) }
|
||||
?: createNewUser().apply { saveUserData(this) }
|
||||
} catch (ex: Exception) {
|
||||
Timber.e(ex)
|
||||
createNewUser().apply { saveUserData(this) }
|
||||
}
|
||||
}
|
||||
|
||||
private fun saveUserData(userData: PromoUserData) {
|
||||
try {
|
||||
preferencesStorage.userData = userInfoAdapter.toJson(userData)
|
||||
} catch (ex: Exception) {
|
||||
Timber.e(ex)
|
||||
preferencesStorage.userData = null
|
||||
}
|
||||
}
|
||||
|
||||
private fun createNewUser(): PromoUserData = PromoUserData(
|
||||
promoCode = null,
|
||||
isRegisteredInPromotion = false,
|
||||
alreadyReceivedAward = false,
|
||||
)
|
||||
|
||||
private companion object {
|
||||
const val PROGRAM_NAME: String = "1inch"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ package com.tangem.feature.learn2earn.data
|
|||
import android.content.Context
|
||||
import com.tangem.feature.learn2earn.data.api.Learn2earnPreferenceStorage
|
||||
import hu.autsoft.krate.SimpleKrate
|
||||
import hu.autsoft.krate.booleanPref
|
||||
import hu.autsoft.krate.default.withDefault
|
||||
import hu.autsoft.krate.stringPref
|
||||
|
||||
|
|
@ -14,9 +13,7 @@ internal class DefaultPreferenceStorage(
|
|||
context: Context,
|
||||
) : SimpleKrate(context = context, name = "Lear2earnPromotion"), Learn2earnPreferenceStorage {
|
||||
|
||||
override var promoCode: String? by stringPref().withDefault(null)
|
||||
|
||||
override var promotionInfo: String? by stringPref().withDefault(null)
|
||||
|
||||
override var alreadyReceivedAward: Boolean by booleanPref().withDefault(false)
|
||||
override var userData: String? by stringPref().withDefault(null)
|
||||
}
|
||||
|
|
@ -1,14 +0,0 @@
|
|||
package com.tangem.feature.learn2earn.data
|
||||
|
||||
import com.tangem.data.source.preferences.storage.UsedCardsPrefStorage
|
||||
import com.tangem.feature.learn2earn.data.api.UsedCardsPreferenceStorage
|
||||
|
||||
/**
|
||||
[REDACTED_AUTHOR]
|
||||
*/
|
||||
class DefaultUsedCardsPreferenceStorage(
|
||||
private val usedCardsPrefStorage: UsedCardsPrefStorage,
|
||||
) : UsedCardsPreferenceStorage {
|
||||
|
||||
override fun isHadActivatedCards(): Boolean = usedCardsPrefStorage.hadFinishedActivation()
|
||||
}
|
||||
|
|
@ -5,9 +5,7 @@ package com.tangem.feature.learn2earn.data.api
|
|||
*/
|
||||
interface Learn2earnPreferenceStorage {
|
||||
|
||||
var promoCode: String?
|
||||
|
||||
var promotionInfo: String?
|
||||
|
||||
var alreadyReceivedAward: Boolean
|
||||
var userData: String?
|
||||
}
|
||||
|
|
@ -1,27 +1,24 @@
|
|||
package com.tangem.feature.learn2earn.data.api
|
||||
|
||||
import com.tangem.datasource.api.promotion.models.*
|
||||
import com.tangem.feature.learn2earn.data.models.PromoUserData
|
||||
|
||||
/**
|
||||
[REDACTED_AUTHOR]
|
||||
*/
|
||||
interface Learn2earnRepository {
|
||||
|
||||
fun isHadActivatedCards(): Boolean
|
||||
fun getUserData(): PromoUserData
|
||||
|
||||
fun getPromoCode(): String?
|
||||
|
||||
fun savePromoCode(code: String)
|
||||
fun updateUserData(userData: PromoUserData)
|
||||
|
||||
fun getProgramName(): String
|
||||
|
||||
fun isAlreadyReceivedAward(): Boolean
|
||||
|
||||
suspend fun getPromotionInfo(): Result<PromotionInfoResponse>
|
||||
|
||||
suspend fun validate(walletId: String): ValidateResponse
|
||||
|
||||
suspend fun requestAward(walletId: String): AwardResponse
|
||||
suspend fun requestAward(walletId: String, address: String): AwardResponse
|
||||
|
||||
suspend fun validateCode(walletId: String, code: String?): CodeValidateResponse
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +0,0 @@
|
|||
package com.tangem.feature.learn2earn.data.api
|
||||
|
||||
/**
|
||||
[REDACTED_AUTHOR]
|
||||
* An interface that encapsulates access to deprecated PreferencesDataSource.usedCardsPrefStorage.
|
||||
*/
|
||||
interface UsedCardsPreferenceStorage {
|
||||
fun isHadActivatedCards(): Boolean
|
||||
}
|
||||
|
|
@ -2,16 +2,13 @@ package com.tangem.feature.learn2earn.data.di
|
|||
|
||||
import android.content.Context
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.tangem.data.source.preferences.PreferencesDataSource
|
||||
import com.tangem.datasource.api.promotion.PromotionApi
|
||||
import com.tangem.datasource.di.NetworkMoshi
|
||||
import com.tangem.datasource.di.PromotionOneInch
|
||||
import com.tangem.feature.learn2earn.data.DefaultLearn2earnRepository
|
||||
import com.tangem.feature.learn2earn.data.DefaultPreferenceStorage
|
||||
import com.tangem.feature.learn2earn.data.DefaultUsedCardsPreferenceStorage
|
||||
import com.tangem.feature.learn2earn.data.api.Learn2earnPreferenceStorage
|
||||
import com.tangem.feature.learn2earn.data.api.Learn2earnRepository
|
||||
import com.tangem.feature.learn2earn.data.api.UsedCardsPreferenceStorage
|
||||
import com.tangem.utils.coroutines.AppCoroutineDispatcherProvider
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
|
|
@ -30,29 +27,19 @@ class Learn2earnDataModule {
|
|||
return DefaultPreferenceStorage(context)
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideDeprecatedPreferenceDataSource(
|
||||
preferenceDataSource: PreferencesDataSource,
|
||||
): UsedCardsPreferenceStorage {
|
||||
return DefaultUsedCardsPreferenceStorage(preferenceDataSource.usedCardsPrefStorage)
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideRepository(
|
||||
preferenceStorage: Learn2earnPreferenceStorage,
|
||||
usedCardsPreferenceStorage: UsedCardsPreferenceStorage,
|
||||
@NetworkMoshi moshi: Moshi,
|
||||
@PromotionOneInch promotionApi: PromotionApi,
|
||||
dispatchers: AppCoroutineDispatcherProvider,
|
||||
): Learn2earnRepository {
|
||||
return DefaultLearn2earnRepository(
|
||||
preferencesStorage = preferenceStorage,
|
||||
usedCardsPreferenceStorage = usedCardsPreferenceStorage,
|
||||
moshi = moshi,
|
||||
api = promotionApi,
|
||||
dispatchers = dispatchers,
|
||||
moshi = moshi,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
package com.tangem.feature.learn2earn.data.models
|
||||
|
||||
/**
|
||||
[REDACTED_AUTHOR]
|
||||
*/
|
||||
data class PromoUserData(
|
||||
val promoCode: String?,
|
||||
val isRegisteredInPromotion: Boolean,
|
||||
val alreadyReceivedAward: Boolean,
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue