Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.gemwallet.android.application.referral.coordinators.CreateReferral
import com.gemwallet.android.application.referral.coordinators.GetRewards
import com.gemwallet.android.application.referral.coordinators.Redeem
import com.gemwallet.android.application.referral.coordinators.UseReferralCode
import com.gemwallet.android.cases.device.GetDeviceId
import com.gemwallet.android.data.coordinates.referral.CreateReferralImpl
import com.gemwallet.android.data.coordinates.referral.GetRewardsImpl
import com.gemwallet.android.data.coordinates.referral.RedeemImpl
Expand All @@ -26,10 +27,12 @@ object ReferralModule {
@Singleton
fun provideCreateReferral(
gemApiClient: GemApiClient,
getAuthPayload: GetAuthPayload
getDeviceId: GetDeviceId,
getAuthPayload: GetAuthPayload,
): CreateReferral {
return CreateReferralImpl(
gemApiClient = gemApiClient,
getDeviceId = getDeviceId,
getAuthPayload = getAuthPayload
)
}
Expand All @@ -38,9 +41,11 @@ object ReferralModule {
@Singleton
fun provideGetRewards(
gemApiClient: GemApiClient,
getDeviceId: GetDeviceId,
): GetRewards {
return GetRewardsImpl(
gemApiClient = gemApiClient,
getDeviceId = getDeviceId,
)
}

Expand All @@ -49,13 +54,15 @@ object ReferralModule {
fun provideRedeem(
sessionRepository: SessionRepository,
gemApiClient: GemApiClient,
getDeviceId: GetDeviceId,
getAuthPayload: GetAuthPayload,
tokensRepository: TokensRepository,
assetsRepository: AssetsRepository,
): Redeem {
return RedeemImpl(
sessionRepository = sessionRepository,
gemApiClient = gemApiClient,
getDeviceId = getDeviceId,
getAuthPayload = getAuthPayload,
tokensRepository = tokensRepository,
assetsRepository = assetsRepository,
Expand All @@ -66,10 +73,12 @@ object ReferralModule {
@Singleton
fun provideUseReferralCode(
gemApiClient: GemApiClient,
getDeviceId: GetDeviceId,
getAuthPayload: GetAuthPayload
): UseReferralCode {
return UseReferralCodeImpl(
gemApiClient = gemApiClient,
getDeviceId = getDeviceId,
getAuthPayload = getAuthPayload
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.wallet.core.primitives.Currency
import com.wallet.core.primitives.PerpetualBalance
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.map

Expand All @@ -19,6 +20,7 @@ class GetPerpetualBalancesImpl(

override fun getPerpetualBalance(): Flow<com.gemwallet.android.domains.perpetual.values.PerpetualBalance> {
return sessionRepository.session().map { it?.wallet?.accounts?.map { it.address } ?: emptyList()}
.filter { it.isNotEmpty() }
.flatMapLatest { perpetualRepository.getBalances(it) }
.map { items ->
items.fold(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.gemwallet.android.data.coordinates.referral

import com.gemwallet.android.application.GetAuthPayload
import com.gemwallet.android.application.referral.coordinators.CreateReferral
import com.gemwallet.android.cases.device.GetDeviceId
import com.gemwallet.android.data.services.gemapi.GemApiClient
import com.gemwallet.android.data.services.gemapi.models.ResponseError
import com.gemwallet.android.domains.referral.values.ReferralError
Expand All @@ -17,6 +18,7 @@ import retrofit2.HttpException

class CreateReferralImpl(
private val gemApiClient: GemApiClient,
private val getDeviceId: GetDeviceId,
private val getAuthPayload: GetAuthPayload
) : CreateReferral {

Expand All @@ -26,7 +28,9 @@ class CreateReferralImpl(
val authPayload = getAuthPayload.getAuthPayload(wallet, account.chain)
return try {
gemApiClient.createReferral(
AuthenticatedRequest(
deviceId = getDeviceId.getDeviceId(),
walletId = wallet.id,
body = AuthenticatedRequest(
auth = authPayload,
data = ReferralCode(
code = code
Expand All @@ -41,4 +45,4 @@ class CreateReferralImpl(
throw err
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package com.gemwallet.android.data.coordinates.referral

import com.gemwallet.android.application.referral.coordinators.GetRewards
import com.gemwallet.android.cases.device.GetDeviceId
import com.gemwallet.android.data.services.gemapi.GemApiClient
import com.gemwallet.android.domains.referral.values.ReferralError
import com.wallet.core.primitives.Rewards

class GetRewardsImpl(
private val gemApiClient: GemApiClient,
private val getDeviceId: GetDeviceId,
) : GetRewards {
override suspend fun getRewards(address: String): Rewards {
val response = gemApiClient.getRewards(address)
override suspend fun getRewards(walletId: String): Rewards {
val response = gemApiClient.getRewards(getDeviceId.getDeviceId(), walletId)
if (response.code == null) {
throw ReferralError.NotCreated
}
return response
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.gemwallet.android.data.coordinates.referral

import com.gemwallet.android.application.GetAuthPayload
import com.gemwallet.android.application.referral.coordinators.Redeem
import com.gemwallet.android.cases.device.GetDeviceId
import com.gemwallet.android.data.repositoreis.assets.AssetsRepository
import com.gemwallet.android.data.repositoreis.session.SessionRepository
import com.gemwallet.android.data.repositoreis.tokens.TokensRepository
Expand All @@ -24,6 +25,7 @@ import retrofit2.HttpException
class RedeemImpl(
private val sessionRepository: SessionRepository,
private val gemApiClient: GemApiClient,
private val getDeviceId: GetDeviceId,
private val getAuthPayload: GetAuthPayload,
private val tokensRepository: TokensRepository,
private val assetsRepository: AssetsRepository,
Expand All @@ -37,7 +39,8 @@ class RedeemImpl(
}
return try {
val result = gemApiClient.redeem(
address = account.address,
deviceId = getDeviceId.getDeviceId(),
walletId = wallet.id,
request = AuthenticatedRequest(
auth = authPayload,
data = RedemptionRequest(option.id)
Expand All @@ -59,4 +62,4 @@ class RedeemImpl(
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.gemwallet.android.data.coordinates.referral

import com.gemwallet.android.application.GetAuthPayload
import com.gemwallet.android.application.referral.coordinators.UseReferralCode
import com.gemwallet.android.cases.device.GetDeviceId
import com.gemwallet.android.data.services.gemapi.GemApiClient
import com.gemwallet.android.data.services.gemapi.models.ResponseError
import com.gemwallet.android.domains.referral.values.ReferralError
Expand All @@ -16,6 +17,7 @@ import retrofit2.HttpException

class UseReferralCodeImpl(
private val gemApiClient: GemApiClient,
private val getDeviceId: GetDeviceId,
private val getAuthPayload: GetAuthPayload,
) : UseReferralCode {

Expand All @@ -25,6 +27,8 @@ class UseReferralCodeImpl(
val auth = getAuthPayload.getAuthPayload(wallet, account.chain)
return try {
gemApiClient.useReferralCode(
deviceId = getDeviceId.getDeviceId(),
walletId = wallet.id,
body = AuthenticatedRequest(
auth = auth,
data = ReferralCode(code)
Expand All @@ -39,4 +43,4 @@ class UseReferralCodeImpl(
throw err
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ class AssetsRepository @Inject constructor(
}

val availableAssetsId = try {
gemApi.getAssets(getDeviceId.getDeviceId(), wallet.index)
gemApi.getAssets(deviceId = getDeviceId.getDeviceId(), walletId = wallet.id)
} catch (_: Throwable) {
return@launch
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import com.gemwallet.android.ext.model
import com.gemwallet.android.ext.os
import com.wallet.core.primitives.ChainAddress
import com.wallet.core.primitives.Device
import com.wallet.core.primitives.NewSupportDevice
import com.wallet.core.primitives.SupportDeviceRequest
import com.wallet.core.primitives.Platform
import com.wallet.core.primitives.PlatformStore
import com.wallet.core.primitives.Subscription
Expand Down Expand Up @@ -107,18 +107,15 @@ class DeviceRepository(
currency = getCurrentCurrencyCase.getCurrentCurrency().string,
subscriptionsVersion = getSubscriptionVersion(),
)
val register: () -> Unit = {
CoroutineScope(Dispatchers.IO).launch {
callRegisterDevice(device.copy(token = getPushToken()))
}
}
if (pushEnabled && pushToken.isEmpty()) {
requestPushToken.requestToken { pushToken ->
setPushToken(pushToken)
register()
CoroutineScope(Dispatchers.IO).launch {
handlePushToken(pushToken, device)
}
}
} else {
register()
handlePushToken(pushToken, device)
}
}

Expand Down Expand Up @@ -207,36 +204,56 @@ class DeviceRepository(
val supportId = UUID.randomUUID().toString().substring(0, 31)
try {
gemApiClient.registerSupport(
NewSupportDevice(
deviceId = getDeviceId.getDeviceId(),
request = SupportDeviceRequest(
supportDeviceId = supportId,
deviceId = getDeviceId.getDeviceId(),
)
)
context.dataStore.edit { it[Key.SupportId] = supportId }
} catch (_: Throwable) { }
}

private suspend fun callRegisterDevice(device: Device) {
val remoteDeviceInfo = try {
gemApiClient.getDevice(device.id)
} catch (_: Throwable) {
null
private suspend fun handlePushToken(pushToken: String, device: Device) {
val device = device.copy(token = pushToken)

if (isDeviceRegistered(device.id)) {
updateDevice(device)
} else {
registerDevice(device)
}
}

private suspend fun isDeviceRegistered(deviceId: String): Boolean {
val local = context.dataStore.data.map { it[Key.DeviceRegistered] }.firstOrNull() == true
return local || gemApiClient.isDeviceRegistered(deviceId)
}

private suspend fun registerDevice(device: Device) = try {
gemApiClient.registerDevice(device)
val isRegistered = (gemApiClient.isDeviceRegistered(device.id))
setDeviceRegistered(isRegistered)
} catch (_: Throwable) {}

private suspend fun updateDevice(device: Device) {
try {
when {
remoteDeviceInfo == null -> gemApiClient.registerDevice(device)
remoteDeviceInfo.hasChanges(device) -> {
val subVersion = max(device.subscriptionsVersion, remoteDeviceInfo.subscriptionsVersion) + 1
setSubscriptionVersion(subVersion)
gemApiClient.updateDevice(
device.id,
device.copy(subscriptionsVersion = subVersion)
)
}
val remote = gemApiClient.getDevice(device.id)

if (remote?.hasChanges(device) == true) {
val subscriptionsVersion = max(device.subscriptionsVersion, remote.subscriptionsVersion) + 1
setSubscriptionVersion(subscriptionsVersion)
gemApiClient.updateDevice(
deviceId = device.id,
request = device.copy(subscriptionsVersion = subscriptionsVersion)
)
setDeviceRegistered(true)
}
} catch (_: Throwable) { }
}

private suspend fun setDeviceRegistered(isRegistered: Boolean = true) {
context.dataStore.edit { it[Key.DeviceRegistered] = isRegistered }
}

private suspend fun getRemoteSubscriptions(deviceId: String): List<WalletSubscriptionChains> {
return try {
gemApiClient.getSubscriptions(deviceId) ?: throw Exception()
Expand Down Expand Up @@ -356,6 +373,7 @@ class DeviceRepository(
private object Key {
val PushEnabled = booleanPreferencesKey("push_enabled")
val SupportId = stringPreferencesKey("support-id")
val DeviceRegistered = booleanPreferencesKey("device_registered")
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class NftRepository(
override suspend fun loadNFT(wallet: Wallet) {
val deviceId = getDeviceId.getDeviceId()

val response = gemApiClient.getNFTs(deviceId, wallet.index)
val response = gemApiClient.getNFTs(deviceId = deviceId, walletId = wallet.id)
val collections = response.map {
DbNFTCollection(
id = it.collection.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class SyncTransactionsService @Inject constructor(
val lastSyncTime = getTransactionUpdateTime.getTransactionUpdateTime(wallet.id) / 1000L
val response = runCatching {
val result: List<Transaction>? = try {
gemApiClient.getTransactions(deviceId, wallet.index, lastSyncTime)
gemApiClient.getTransactions(deviceId, wallet.id, lastSyncTime)
} catch (_: Throwable) {
null
}
Expand Down
Loading
Loading