From d6cd879f4895d00ea239fb23bdd0df435200ea2b Mon Sep 17 00:00:00 2001 From: gemcoder21 <104884878+gemcoder21@users.noreply.github.com> Date: Thu, 29 Jan 2026 20:42:03 +0000 Subject: [PATCH 1/4] Refactor GemApiClient endpoints to use walletId Updated API endpoints in GemApiClient and related repository calls to use walletId as a path parameter instead of walletIndex as a query parameter. Adjusted affected repository methods and service calls to align with the new API structure. Added isDeviceRegistered endpoint and refactored device registration logic for improved clarity and correctness. --- .../repositoreis/assets/AssetsRepository.kt | 2 +- .../repositoreis/device/DeviceRepository.kt | 28 +++++++++---------- .../data/repositoreis/nft/NftRepository.kt | 2 +- .../transactions/SyncTransactionsService.kt | 2 +- .../data/services/gemapi/GemApiClient.kt | 27 ++++++++++-------- 5 files changed, 31 insertions(+), 30 deletions(-) diff --git a/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/assets/AssetsRepository.kt b/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/assets/AssetsRepository.kt index 7290faf68..b0145f657 100644 --- a/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/assets/AssetsRepository.kt +++ b/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/assets/AssetsRepository.kt @@ -310,7 +310,7 @@ class AssetsRepository @Inject constructor( } val availableAssetsId = try { - gemApi.getAssets(getDeviceId.getDeviceId(), wallet.index) + gemApi.getAssets(getDeviceId.getDeviceId(), wallet.id) } catch (_: Throwable) { return@launch } diff --git a/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/device/DeviceRepository.kt b/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/device/DeviceRepository.kt index fff01ddc2..36228df4b 100644 --- a/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/device/DeviceRepository.kt +++ b/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/device/DeviceRepository.kt @@ -217,22 +217,20 @@ class DeviceRepository( } private suspend fun callRegisterDevice(device: Device) { - val remoteDeviceInfo = try { - gemApiClient.getDevice(device.id) - } catch (_: Throwable) { - null - } 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 isRegistered = gemApiClient.isDeviceRegistered(device.id) + if (!isRegistered) { + gemApiClient.registerDevice(device) + return + } + val remoteDeviceInfo = gemApiClient.getDevice(device.id) ?: return + if (remoteDeviceInfo.hasChanges(device)) { + val subVersion = max(device.subscriptionsVersion, remoteDeviceInfo.subscriptionsVersion) + 1 + setSubscriptionVersion(subVersion) + gemApiClient.updateDevice( + device.id, + device.copy(subscriptionsVersion = subVersion) + ) } } catch (_: Throwable) { } } diff --git a/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/nft/NftRepository.kt b/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/nft/NftRepository.kt index df1d6806c..c31dcbba9 100644 --- a/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/nft/NftRepository.kt +++ b/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/nft/NftRepository.kt @@ -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, wallet.id) val collections = response.map { DbNFTCollection( id = it.collection.id, diff --git a/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/transactions/SyncTransactionsService.kt b/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/transactions/SyncTransactionsService.kt index e9f6c057a..ebd529c6c 100644 --- a/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/transactions/SyncTransactionsService.kt +++ b/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/transactions/SyncTransactionsService.kt @@ -24,7 +24,7 @@ class SyncTransactionsService @Inject constructor( val lastSyncTime = getTransactionUpdateTime.getTransactionUpdateTime(wallet.id) / 1000L val response = runCatching { val result: List? = try { - gemApiClient.getTransactions(deviceId, wallet.index, lastSyncTime) + gemApiClient.getTransactions(deviceId, wallet.id, lastSyncTime) } catch (_: Throwable) { null } diff --git a/data/services/remote-gem/src/main/kotlin/com/gemwallet/android/data/services/gemapi/GemApiClient.kt b/data/services/remote-gem/src/main/kotlin/com/gemwallet/android/data/services/gemapi/GemApiClient.kt index 2120f4e7e..679a07005 100644 --- a/data/services/remote-gem/src/main/kotlin/com/gemwallet/android/data/services/gemapi/GemApiClient.kt +++ b/data/services/remote-gem/src/main/kotlin/com/gemwallet/android/data/services/gemapi/GemApiClient.kt @@ -71,10 +71,10 @@ interface GemApiClient { @GET("/v1/fiat/off_ramp/assets") suspend fun getOffRampAssets(): FiatAssets - @GET("/v1/transactions/device/{device_id}") + @GET("/v1/devices/{device_id}/wallets/{wallet_id}/transactions") suspend fun getTransactions( @Path("device_id") deviceId: String, - @Query("wallet_index") walletIndex: Int, + @Path("wallet_id") walletId: String, @Query("from_timestamp") from: Long ): List @@ -87,6 +87,9 @@ interface GemApiClient { @GET("/v1/devices/{device_id}") suspend fun getDevice(@Path("device_id") deviceId: String): Device? + @GET("/v1/devices/{device_id}/is_registered") + suspend fun isDeviceRegistered(@Path("device_id") deviceId: String): Boolean + @POST("/v1/support/add_device") suspend fun registerSupport(@Body request: NewSupportDevice): SupportDevice @@ -102,13 +105,13 @@ interface GemApiClient { @POST("/v1/subscriptions/{device_id}") suspend fun addOldSubscriptions(@Path("device_id") deviceId: String, @Body request: List): Int - @GET("/v2/subscriptions/{device_id}") + @GET("/v1/devices/{device_id}/subscriptions") suspend fun getSubscriptions(@Path("device_id") deviceId: String): List? - @HTTP(method = "DELETE", path = "/v2/subscriptions/{device_id}", hasBody = true) + @HTTP(method = "DELETE", path = "/v1/devices/{device_id}/subscriptions", hasBody = true) suspend fun deleteSubscriptions(@Path("device_id") deviceId: String, @Body request: List): Int - @POST("/v2/subscriptions/{device_id}") + @POST("/v1/devices/{device_id}/subscriptions") suspend fun addSubscriptions(@Path("device_id") deviceId: String, @Body request: List): Int @GET("/v1/charts/{asset_id}") @@ -130,20 +133,20 @@ interface GemApiClient { @Query("tags") tags: String, ): List - @GET("/v1/assets/device/{device_id}") - suspend fun getAssets(@Path("device_id") deviceId: String, @Query("wallet_index") walletIndex: Int, @Query("from_timestamp") fromTimestamp: Int = 0): List + @GET("/v1/devices/{device_id}/wallets/{wallet_id}/assets") + suspend fun getAssets(@Path("device_id") deviceId: String, @Path("wallet_id") walletId: String, @Query("from_timestamp") fromTimestamp: Int = 0): List - @POST("/v1/price_alerts/{device_id}") + @POST("/v1/devices/{device_id}/price_alerts") suspend fun includePriceAlert(@Path("device_id") deviceId: String, @Body alerts: List): String - @HTTP(method = "DELETE", path = "/v1/price_alerts/{device_id}", hasBody = true) + @HTTP(method = "DELETE", path = "/v1/devices/{device_id}/price_alerts", hasBody = true) suspend fun excludePriceAlert(@Path("device_id") deviceId: String, @Body assets: List): String - @GET("/v1/price_alerts/{device_id}") + @GET("/v1/devices/{device_id}/price_alerts") suspend fun getPriceAlerts(@Path("device_id") deviceId: String): List - @GET("/v2/nft/assets/device/{device_id}") - suspend fun getNFTs(@Path("device_id") deviceId: String, @Query("wallet_index") walletIndex: Int): List + @GET("/v1/devices/{device_id}/wallets/{wallet_id}/nft_assets") + suspend fun getNFTs(@Path("device_id") deviceId: String, @Path("wallet_id") walletId: String): List @POST("/v2/scan/transaction") suspend fun getScanTransaction(@Body payload: ScanTransactionPayload): ScanTransaction From 89a7fc264a7877870a017a77a5b925220c2d8dd5 Mon Sep 17 00:00:00 2001 From: gemcoder21 <104884878+gemcoder21@users.noreply.github.com> Date: Thu, 29 Jan 2026 21:18:34 +0000 Subject: [PATCH 2/4] Improve device registration with local state tracking Added local tracking for device registration status to avoid unnecessary API calls. Enhanced error handling for device lookup, including re-registration if the device is not found remotely. Introduced new preferences key for device registration state. --- .../repositoreis/device/DeviceRepository.kt | 37 ++++++++++++++++--- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/device/DeviceRepository.kt b/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/device/DeviceRepository.kt index 36228df4b..563f853c5 100644 --- a/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/device/DeviceRepository.kt +++ b/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/device/DeviceRepository.kt @@ -45,6 +45,7 @@ import java.util.Locale import java.util.UUID import kotlin.collections.contains import kotlin.math.max +import retrofit2.HttpException class DeviceRepository( private val context: Context, @@ -216,14 +217,39 @@ class DeviceRepository( } catch (_: Throwable) { } } + private suspend fun isDeviceRegisteredLocally(): Boolean { + return context.dataStore.data + .map { preferences -> preferences[Key.DeviceRegistered] == true } + .firstOrNull() ?: false + } + + private suspend fun setDeviceRegisteredLocally(registered: Boolean) { + context.dataStore.edit { preferences -> + preferences[Key.DeviceRegistered] = registered + } + } + private suspend fun callRegisterDevice(device: Device) { try { - val isRegistered = gemApiClient.isDeviceRegistered(device.id) - if (!isRegistered) { - gemApiClient.registerDevice(device) - return + val locallyRegistered = isDeviceRegisteredLocally() + if (!locallyRegistered) { + val isRegistered = gemApiClient.isDeviceRegistered(device.id) + if (!isRegistered) { + gemApiClient.registerDevice(device) + setDeviceRegisteredLocally(true) + return + } + setDeviceRegisteredLocally(true) } - val remoteDeviceInfo = gemApiClient.getDevice(device.id) ?: return + val remoteDeviceInfo = try { + gemApiClient.getDevice(device.id) + } catch (e: HttpException) { + if (e.code() == 404) { + setDeviceRegisteredLocally(false) + return callRegisterDevice(device) + } + return + } ?: return if (remoteDeviceInfo.hasChanges(device)) { val subVersion = max(device.subscriptionsVersion, remoteDeviceInfo.subscriptionsVersion) + 1 setSubscriptionVersion(subVersion) @@ -354,6 +380,7 @@ class DeviceRepository( private object Key { val PushEnabled = booleanPreferencesKey("push_enabled") val SupportId = stringPreferencesKey("support-id") + val DeviceRegistered = booleanPreferencesKey("device_registered") } companion object { From bf8f67cc3867146959555b1ca2a0aa3c5d406c27 Mon Sep 17 00:00:00 2001 From: gemcoder21 <104884878+gemcoder21@users.noreply.github.com> Date: Fri, 30 Jan 2026 02:46:31 +0000 Subject: [PATCH 3/4] Refactor referral and rewards APIs to use device and wallet IDs Updated referral and rewards coordinator interfaces and implementations to require deviceId and walletId parameters. Modified GemApiClient endpoints to use device and wallet IDs in paths, and adjusted related data classes and repository logic accordingly. This change improves API consistency and aligns with backend requirements for device- and wallet-scoped operations. --- .../referral/CreateReferralImpl.kt | 8 +++-- .../coordinates/referral/GetRewardsImpl.kt | 6 ++-- .../data/coordinates/referral/RedeemImpl.kt | 7 ++-- .../referral/UseReferralCodeImpl.kt | 6 ++-- .../repositoreis/device/DeviceRepository.kt | 6 ++-- .../data/services/gemapi/GemApiClient.kt | 33 ++++++++++++------- .../referral/viewmodels/ReferralViewModel.kt | 22 ++++++------- .../referral/coordinators/CreateReferral.kt | 4 +-- .../referral/coordinators/GetRewards.kt | 4 +-- .../referral/coordinators/Redeem.kt | 4 +-- .../referral/coordinators/UseReferralCode.kt | 4 +-- .../com/wallet/core/primitives/Support.kt | 5 +++ 12 files changed, 64 insertions(+), 45 deletions(-) diff --git a/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/referral/CreateReferralImpl.kt b/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/referral/CreateReferralImpl.kt index 8edf7154c..282a8b274 100644 --- a/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/referral/CreateReferralImpl.kt +++ b/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/referral/CreateReferralImpl.kt @@ -21,12 +21,14 @@ class CreateReferralImpl( ) : CreateReferral { - override suspend fun createReferral(code: String, wallet: Wallet): Rewards { + override suspend fun createReferral(code: String, wallet: Wallet, deviceId: String): Rewards { val account = wallet.getAccount(Chain.referralChain) ?: throw ReferralError.BadWallet val authPayload = getAuthPayload.getAuthPayload(wallet, account.chain) return try { gemApiClient.createReferral( - AuthenticatedRequest( + deviceId = deviceId, + walletId = wallet.id, + body = AuthenticatedRequest( auth = authPayload, data = ReferralCode( code = code @@ -41,4 +43,4 @@ class CreateReferralImpl( throw err } } -} \ No newline at end of file +} diff --git a/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/referral/GetRewardsImpl.kt b/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/referral/GetRewardsImpl.kt index 7111b24c8..e4311e6ff 100644 --- a/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/referral/GetRewardsImpl.kt +++ b/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/referral/GetRewardsImpl.kt @@ -8,11 +8,11 @@ import com.wallet.core.primitives.Rewards class GetRewardsImpl( private val gemApiClient: GemApiClient, ) : GetRewards { - override suspend fun getRewards(address: String): Rewards { - val response = gemApiClient.getRewards(address) + override suspend fun getRewards(deviceId: String, walletId: String): Rewards { + val response = gemApiClient.getRewards(deviceId, walletId) if (response.code == null) { throw ReferralError.NotCreated } return response } -} \ No newline at end of file +} diff --git a/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/referral/RedeemImpl.kt b/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/referral/RedeemImpl.kt index 2bfd20435..455c95f0f 100644 --- a/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/referral/RedeemImpl.kt +++ b/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/referral/RedeemImpl.kt @@ -29,7 +29,7 @@ class RedeemImpl( private val assetsRepository: AssetsRepository, ) : Redeem { - override suspend fun redeem(wallet: Wallet, rewards: Rewards, option: RewardRedemptionOption): RedemptionResult { + override suspend fun redeem(wallet: Wallet, rewards: Rewards, option: RewardRedemptionOption, deviceId: String): RedemptionResult { val account = wallet.getAccount(Chain.referralChain) ?: throw ReferralError.BadWallet val authPayload = getAuthPayload.getAuthPayload(wallet, account.chain) if (rewards.points < option.points) { @@ -37,7 +37,8 @@ class RedeemImpl( } return try { val result = gemApiClient.redeem( - address = account.address, + deviceId = deviceId, + walletId = wallet.id, request = AuthenticatedRequest( auth = authPayload, data = RedemptionRequest(option.id) @@ -59,4 +60,4 @@ class RedeemImpl( } } -} \ No newline at end of file +} diff --git a/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/referral/UseReferralCodeImpl.kt b/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/referral/UseReferralCodeImpl.kt index 1fb8d67d2..f151dde49 100644 --- a/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/referral/UseReferralCodeImpl.kt +++ b/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/referral/UseReferralCodeImpl.kt @@ -20,11 +20,13 @@ class UseReferralCodeImpl( ) : UseReferralCode { - override suspend fun useReferralCode(code: String, wallet: Wallet): Boolean { + override suspend fun useReferralCode(code: String, wallet: Wallet, deviceId: String): Boolean { val account = wallet.getAccount(Chain.referralChain) ?: throw ReferralError.BadWallet val auth = getAuthPayload.getAuthPayload(wallet, account.chain) return try { gemApiClient.useReferralCode( + deviceId = deviceId, + walletId = wallet.id, body = AuthenticatedRequest( auth = auth, data = ReferralCode(code) @@ -39,4 +41,4 @@ class UseReferralCodeImpl( throw err } } -} \ No newline at end of file +} diff --git a/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/device/DeviceRepository.kt b/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/device/DeviceRepository.kt index 563f853c5..2aac6f5f4 100644 --- a/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/device/DeviceRepository.kt +++ b/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/device/DeviceRepository.kt @@ -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 @@ -208,9 +208,9 @@ 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 } diff --git a/data/services/remote-gem/src/main/kotlin/com/gemwallet/android/data/services/gemapi/GemApiClient.kt b/data/services/remote-gem/src/main/kotlin/com/gemwallet/android/data/services/gemapi/GemApiClient.kt index 679a07005..3be717d5b 100644 --- a/data/services/remote-gem/src/main/kotlin/com/gemwallet/android/data/services/gemapi/GemApiClient.kt +++ b/data/services/remote-gem/src/main/kotlin/com/gemwallet/android/data/services/gemapi/GemApiClient.kt @@ -18,7 +18,8 @@ import com.wallet.core.primitives.FiatQuoteUrlRequest import com.wallet.core.primitives.FiatQuotes import com.wallet.core.primitives.NFTData import com.wallet.core.primitives.NameRecord -import com.wallet.core.primitives.NewSupportDevice +import com.wallet.core.primitives.ReferralLeaderboard +import com.wallet.core.primitives.RewardRedemptionOption import com.wallet.core.primitives.PriceAlert import com.wallet.core.primitives.RedemptionRequest import com.wallet.core.primitives.RedemptionResult @@ -29,6 +30,7 @@ import com.wallet.core.primitives.ScanTransaction import com.wallet.core.primitives.ScanTransactionPayload import com.wallet.core.primitives.Subscription import com.wallet.core.primitives.SupportDevice +import com.wallet.core.primitives.SupportDeviceRequest import com.wallet.core.primitives.WalletSubscription import com.wallet.core.primitives.WalletSubscriptionChains import retrofit2.http.Body @@ -90,8 +92,8 @@ interface GemApiClient { @GET("/v1/devices/{device_id}/is_registered") suspend fun isDeviceRegistered(@Path("device_id") deviceId: String): Boolean - @POST("/v1/support/add_device") - suspend fun registerSupport(@Body request: NewSupportDevice): SupportDevice + @POST("/v1/devices/{device_id}/support") + suspend fun registerSupport(@Path("device_id") deviceId: String, @Body request: SupportDeviceRequest): SupportDevice @PUT("/v1/devices/{device_id}") suspend fun updateDevice(@Path("device_id") deviceId: String, @Body request: Device): Device @@ -154,15 +156,24 @@ interface GemApiClient { @GET("/v1/devices/{device_id}/auth/nonce") suspend fun getAuthNonce(@Path("device_id")deviceId: String): AuthNonce - @GET("/v1/rewards/{address}") - suspend fun getRewards(@Path("address") address: String): Rewards + @GET("/v1/devices/{device_id}/wallets/{wallet_id}/rewards") + suspend fun getRewards(@Path("device_id") deviceId: String, @Path("wallet_id") walletId: String): Rewards - @POST("/v1/rewards/referrals/create") - suspend fun createReferral(@Body body: AuthenticatedRequest): Rewards + @GET("/v1/devices/{device_id}/wallets/{wallet_id}/rewards/events") + suspend fun getRewardsEvents(@Path("device_id") deviceId: String, @Path("wallet_id") walletId: String): List - @POST("/v1/rewards/referrals/use") - suspend fun useReferralCode(@Body body: AuthenticatedRequest): List + @GET("/v1/devices/{device_id}/rewards/leaderboard") + suspend fun getRewardsLeaderboard(@Path("device_id") deviceId: String): ReferralLeaderboard - @POST("/v1/rewards/{address}/redeem") - suspend fun redeem(@Path("address") address: String, @Body request: AuthenticatedRequest): RedemptionResult + @GET("/v1/devices/{device_id}/rewards/redemptions/{code}") + suspend fun getRedemptionOption(@Path("device_id") deviceId: String, @Path("code") code: String): RewardRedemptionOption + + @POST("/v1/devices/{device_id}/wallets/{wallet_id}/rewards/referrals/create") + suspend fun createReferral(@Path("device_id") deviceId: String, @Path("wallet_id") walletId: String, @Body body: AuthenticatedRequest): Rewards + + @POST("/v1/devices/{device_id}/wallets/{wallet_id}/rewards/referrals/use") + suspend fun useReferralCode(@Path("device_id") deviceId: String, @Path("wallet_id") walletId: String, @Body body: AuthenticatedRequest): List + + @POST("/v1/devices/{device_id}/wallets/{wallet_id}/rewards/redeem") + suspend fun redeem(@Path("device_id") deviceId: String, @Path("wallet_id") walletId: String, @Body request: AuthenticatedRequest): RedemptionResult } \ No newline at end of file diff --git a/features/referral/viewmodels/src/main/kotlin/com/gemwallet/features/referral/viewmodels/ReferralViewModel.kt b/features/referral/viewmodels/src/main/kotlin/com/gemwallet/features/referral/viewmodels/ReferralViewModel.kt index a867f0e40..ccac7200b 100644 --- a/features/referral/viewmodels/src/main/kotlin/com/gemwallet/features/referral/viewmodels/ReferralViewModel.kt +++ b/features/referral/viewmodels/src/main/kotlin/com/gemwallet/features/referral/viewmodels/ReferralViewModel.kt @@ -7,11 +7,11 @@ 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.repositoreis.session.SessionRepository import com.gemwallet.android.data.repositoreis.wallets.WalletsRepository import com.gemwallet.android.ext.getAccount import com.gemwallet.android.ext.referralChain -import com.wallet.core.primitives.Account import com.wallet.core.primitives.Chain import com.wallet.core.primitives.RewardRedemptionOption import com.wallet.core.primitives.Rewards @@ -41,6 +41,7 @@ class ReferralViewModel @Inject constructor( private val redeem: Redeem, private val useReferralCode: UseReferralCode, private val createReferral: CreateReferral, + private val getDeviceId: GetDeviceId, private val savedStateHandle: SavedStateHandle, ) : ViewModel() { @@ -74,10 +75,7 @@ class ReferralViewModel @Inject constructor( } }.stateIn(viewModelScope, SharingStarted.Eagerly, null) - private val referralAccount = currentWallet.filterNotNull().mapLatest { wallet -> - wallet.getAccount(Chain.referralChain) ?: return@mapLatest null - } - .filterNotNull() + private val referralWallet = currentWallet.filterNotNull() .onEach { sync(it, SyncType.Init) } .stateIn(viewModelScope, SharingStarted.Eagerly, null) @@ -86,13 +84,13 @@ class ReferralViewModel @Inject constructor( } fun sync() { - sync(referralAccount.value ?: return, SyncType.Refresh) + sync(referralWallet.value ?: return, SyncType.Refresh) } - private fun sync(account: Account, type: SyncType) = viewModelScope.launch(Dispatchers.IO) { + private fun sync(wallet: Wallet, type: SyncType) = viewModelScope.launch(Dispatchers.IO) { inSync.update { type } val rewards = try { - getRewards.getRewards(account.address) + getRewards.getRewards(getDeviceId.getDeviceId(), wallet.id) } catch (_: Exception) { null } finally { @@ -104,7 +102,7 @@ class ReferralViewModel @Inject constructor( fun createReferral(username: String, callback: (Exception?) -> Unit) = viewModelScope.launch(Dispatchers.IO) { val rewards = try { val wallet = currentWallet.value ?: return@launch - val response = createReferral.createReferral(username, wallet) + val response = createReferral.createReferral(username, wallet, getDeviceId.getDeviceId()) callback(null) response } catch (err: Exception) { @@ -117,7 +115,7 @@ class ReferralViewModel @Inject constructor( fun useCode(code: String, callback: (Exception?) -> Unit) = viewModelScope.launch(Dispatchers.IO) { try { val wallet = currentWallet.value ?: return@launch - useReferralCode.useReferralCode(code, wallet) + useReferralCode.useReferralCode(code, wallet, getDeviceId.getDeviceId()) callback(null) } catch (err: Exception) { callback(err) @@ -129,7 +127,7 @@ class ReferralViewModel @Inject constructor( val rewards = rewards.value ?: return viewModelScope.launch(Dispatchers.IO) { try { - redeem.redeem(wallet, rewards, option) + redeem.redeem(wallet, rewards, option, getDeviceId.getDeviceId()) sync() withContext(Dispatchers.Main) { callback(null) @@ -145,4 +143,4 @@ class ReferralViewModel @Inject constructor( fun cancelCode() { savedStateHandle["code"] = null } -} \ No newline at end of file +} diff --git a/gemcore/src/main/kotlin/com/gemwallet/android/application/referral/coordinators/CreateReferral.kt b/gemcore/src/main/kotlin/com/gemwallet/android/application/referral/coordinators/CreateReferral.kt index b7d4d9f86..9ef02a7f0 100644 --- a/gemcore/src/main/kotlin/com/gemwallet/android/application/referral/coordinators/CreateReferral.kt +++ b/gemcore/src/main/kotlin/com/gemwallet/android/application/referral/coordinators/CreateReferral.kt @@ -4,5 +4,5 @@ import com.wallet.core.primitives.Rewards import com.wallet.core.primitives.Wallet interface CreateReferral { - suspend fun createReferral(code: String, wallet: Wallet): Rewards -} \ No newline at end of file + suspend fun createReferral(code: String, wallet: Wallet, deviceId: String): Rewards +} diff --git a/gemcore/src/main/kotlin/com/gemwallet/android/application/referral/coordinators/GetRewards.kt b/gemcore/src/main/kotlin/com/gemwallet/android/application/referral/coordinators/GetRewards.kt index 4e851cf4b..077cc7d09 100644 --- a/gemcore/src/main/kotlin/com/gemwallet/android/application/referral/coordinators/GetRewards.kt +++ b/gemcore/src/main/kotlin/com/gemwallet/android/application/referral/coordinators/GetRewards.kt @@ -3,5 +3,5 @@ package com.gemwallet.android.application.referral.coordinators import com.wallet.core.primitives.Rewards interface GetRewards { - suspend fun getRewards(address: String): Rewards -} \ No newline at end of file + suspend fun getRewards(deviceId: String, walletId: String): Rewards +} diff --git a/gemcore/src/main/kotlin/com/gemwallet/android/application/referral/coordinators/Redeem.kt b/gemcore/src/main/kotlin/com/gemwallet/android/application/referral/coordinators/Redeem.kt index 7e190c9c8..985f76711 100644 --- a/gemcore/src/main/kotlin/com/gemwallet/android/application/referral/coordinators/Redeem.kt +++ b/gemcore/src/main/kotlin/com/gemwallet/android/application/referral/coordinators/Redeem.kt @@ -6,5 +6,5 @@ import com.wallet.core.primitives.Rewards import com.wallet.core.primitives.Wallet interface Redeem { - suspend fun redeem(wallet: Wallet, rewards: Rewards, option: RewardRedemptionOption): RedemptionResult -} \ No newline at end of file + suspend fun redeem(wallet: Wallet, rewards: Rewards, option: RewardRedemptionOption, deviceId: String): RedemptionResult +} diff --git a/gemcore/src/main/kotlin/com/gemwallet/android/application/referral/coordinators/UseReferralCode.kt b/gemcore/src/main/kotlin/com/gemwallet/android/application/referral/coordinators/UseReferralCode.kt index 1f56223af..3a6eb3364 100644 --- a/gemcore/src/main/kotlin/com/gemwallet/android/application/referral/coordinators/UseReferralCode.kt +++ b/gemcore/src/main/kotlin/com/gemwallet/android/application/referral/coordinators/UseReferralCode.kt @@ -3,5 +3,5 @@ package com.gemwallet.android.application.referral.coordinators import com.wallet.core.primitives.Wallet interface UseReferralCode { - suspend fun useReferralCode(code: String, wallet: Wallet): Boolean -} \ No newline at end of file + suspend fun useReferralCode(code: String, wallet: Wallet, deviceId: String): Boolean +} diff --git a/gemcore/src/main/kotlin/com/wallet/core/primitives/Support.kt b/gemcore/src/main/kotlin/com/wallet/core/primitives/Support.kt index a7f852f63..44181ec9b 100644 --- a/gemcore/src/main/kotlin/com/wallet/core/primitives/Support.kt +++ b/gemcore/src/main/kotlin/com/wallet/core/primitives/Support.kt @@ -19,3 +19,8 @@ data class SupportDevice ( val unread: Int ) +@Serializable +data class SupportDeviceRequest ( + val supportDeviceId: String +) + From a0b67a24317019777063c187c406ec62310dba31 Mon Sep 17 00:00:00 2001 From: furenster <123000419+furenster@users.noreply.github.com> Date: Thu, 29 Jan 2026 20:53:57 -0800 Subject: [PATCH 4/4] Fixes --- .../data/coordinates/di/ReferralModule.kt | 11 ++- .../perpetuals/GetPerpetualBalancesImpl.kt | 2 + .../referral/CreateReferralImpl.kt | 6 +- .../coordinates/referral/GetRewardsImpl.kt | 6 +- .../data/coordinates/referral/RedeemImpl.kt | 6 +- .../referral/UseReferralCodeImpl.kt | 6 +- .../repositoreis/assets/AssetsRepository.kt | 2 +- .../repositoreis/device/DeviceRepository.kt | 75 +++++++++---------- .../data/repositoreis/nft/NftRepository.kt | 2 +- .../referral/viewmodels/ReferralViewModel.kt | 17 ++--- .../referral/coordinators/CreateReferral.kt | 2 +- .../referral/coordinators/GetRewards.kt | 2 +- .../referral/coordinators/Redeem.kt | 2 +- .../referral/coordinators/UseReferralCode.kt | 2 +- 14 files changed, 74 insertions(+), 67 deletions(-) diff --git a/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/di/ReferralModule.kt b/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/di/ReferralModule.kt index dc3fa9242..8e39b3d49 100644 --- a/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/di/ReferralModule.kt +++ b/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/di/ReferralModule.kt @@ -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 @@ -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 ) } @@ -38,9 +41,11 @@ object ReferralModule { @Singleton fun provideGetRewards( gemApiClient: GemApiClient, + getDeviceId: GetDeviceId, ): GetRewards { return GetRewardsImpl( gemApiClient = gemApiClient, + getDeviceId = getDeviceId, ) } @@ -49,6 +54,7 @@ object ReferralModule { fun provideRedeem( sessionRepository: SessionRepository, gemApiClient: GemApiClient, + getDeviceId: GetDeviceId, getAuthPayload: GetAuthPayload, tokensRepository: TokensRepository, assetsRepository: AssetsRepository, @@ -56,6 +62,7 @@ object ReferralModule { return RedeemImpl( sessionRepository = sessionRepository, gemApiClient = gemApiClient, + getDeviceId = getDeviceId, getAuthPayload = getAuthPayload, tokensRepository = tokensRepository, assetsRepository = assetsRepository, @@ -66,10 +73,12 @@ object ReferralModule { @Singleton fun provideUseReferralCode( gemApiClient: GemApiClient, + getDeviceId: GetDeviceId, getAuthPayload: GetAuthPayload ): UseReferralCode { return UseReferralCodeImpl( gemApiClient = gemApiClient, + getDeviceId = getDeviceId, getAuthPayload = getAuthPayload ) } diff --git a/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/perpetuals/GetPerpetualBalancesImpl.kt b/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/perpetuals/GetPerpetualBalancesImpl.kt index 1cb4639f5..b240352db 100644 --- a/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/perpetuals/GetPerpetualBalancesImpl.kt +++ b/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/perpetuals/GetPerpetualBalancesImpl.kt @@ -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 @@ -19,6 +20,7 @@ class GetPerpetualBalancesImpl( override fun getPerpetualBalance(): Flow { return sessionRepository.session().map { it?.wallet?.accounts?.map { it.address } ?: emptyList()} + .filter { it.isNotEmpty() } .flatMapLatest { perpetualRepository.getBalances(it) } .map { items -> items.fold( diff --git a/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/referral/CreateReferralImpl.kt b/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/referral/CreateReferralImpl.kt index 282a8b274..ea336b0be 100644 --- a/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/referral/CreateReferralImpl.kt +++ b/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/referral/CreateReferralImpl.kt @@ -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 @@ -17,16 +18,17 @@ import retrofit2.HttpException class CreateReferralImpl( private val gemApiClient: GemApiClient, + private val getDeviceId: GetDeviceId, private val getAuthPayload: GetAuthPayload ) : CreateReferral { - override suspend fun createReferral(code: String, wallet: Wallet, deviceId: String): Rewards { + override suspend fun createReferral(code: String, wallet: Wallet): Rewards { val account = wallet.getAccount(Chain.referralChain) ?: throw ReferralError.BadWallet val authPayload = getAuthPayload.getAuthPayload(wallet, account.chain) return try { gemApiClient.createReferral( - deviceId = deviceId, + deviceId = getDeviceId.getDeviceId(), walletId = wallet.id, body = AuthenticatedRequest( auth = authPayload, diff --git a/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/referral/GetRewardsImpl.kt b/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/referral/GetRewardsImpl.kt index e4311e6ff..cc87f3cb6 100644 --- a/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/referral/GetRewardsImpl.kt +++ b/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/referral/GetRewardsImpl.kt @@ -1,15 +1,17 @@ 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(deviceId: String, walletId: String): Rewards { - val response = gemApiClient.getRewards(deviceId, walletId) + override suspend fun getRewards(walletId: String): Rewards { + val response = gemApiClient.getRewards(getDeviceId.getDeviceId(), walletId) if (response.code == null) { throw ReferralError.NotCreated } diff --git a/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/referral/RedeemImpl.kt b/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/referral/RedeemImpl.kt index 455c95f0f..09a363643 100644 --- a/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/referral/RedeemImpl.kt +++ b/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/referral/RedeemImpl.kt @@ -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 @@ -24,12 +25,13 @@ 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, ) : Redeem { - override suspend fun redeem(wallet: Wallet, rewards: Rewards, option: RewardRedemptionOption, deviceId: String): RedemptionResult { + override suspend fun redeem(wallet: Wallet, rewards: Rewards, option: RewardRedemptionOption): RedemptionResult { val account = wallet.getAccount(Chain.referralChain) ?: throw ReferralError.BadWallet val authPayload = getAuthPayload.getAuthPayload(wallet, account.chain) if (rewards.points < option.points) { @@ -37,7 +39,7 @@ class RedeemImpl( } return try { val result = gemApiClient.redeem( - deviceId = deviceId, + deviceId = getDeviceId.getDeviceId(), walletId = wallet.id, request = AuthenticatedRequest( auth = authPayload, diff --git a/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/referral/UseReferralCodeImpl.kt b/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/referral/UseReferralCodeImpl.kt index f151dde49..284aa957f 100644 --- a/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/referral/UseReferralCodeImpl.kt +++ b/data/coordinators/src/main/kotlin/com/gemwallet/android/data/coordinates/referral/UseReferralCodeImpl.kt @@ -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 @@ -16,16 +17,17 @@ import retrofit2.HttpException class UseReferralCodeImpl( private val gemApiClient: GemApiClient, + private val getDeviceId: GetDeviceId, private val getAuthPayload: GetAuthPayload, ) : UseReferralCode { - override suspend fun useReferralCode(code: String, wallet: Wallet, deviceId: String): Boolean { + override suspend fun useReferralCode(code: String, wallet: Wallet): Boolean { val account = wallet.getAccount(Chain.referralChain) ?: throw ReferralError.BadWallet val auth = getAuthPayload.getAuthPayload(wallet, account.chain) return try { gemApiClient.useReferralCode( - deviceId = deviceId, + deviceId = getDeviceId.getDeviceId(), walletId = wallet.id, body = AuthenticatedRequest( auth = auth, diff --git a/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/assets/AssetsRepository.kt b/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/assets/AssetsRepository.kt index b0145f657..ebe5a26bc 100644 --- a/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/assets/AssetsRepository.kt +++ b/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/assets/AssetsRepository.kt @@ -310,7 +310,7 @@ class AssetsRepository @Inject constructor( } val availableAssetsId = try { - gemApi.getAssets(getDeviceId.getDeviceId(), wallet.id) + gemApi.getAssets(deviceId = getDeviceId.getDeviceId(), walletId = wallet.id) } catch (_: Throwable) { return@launch } diff --git a/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/device/DeviceRepository.kt b/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/device/DeviceRepository.kt index 2aac6f5f4..c4ac73982 100644 --- a/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/device/DeviceRepository.kt +++ b/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/device/DeviceRepository.kt @@ -45,7 +45,6 @@ import java.util.Locale import java.util.UUID import kotlin.collections.contains import kotlin.math.max -import retrofit2.HttpException class DeviceRepository( private val context: Context, @@ -108,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) } } @@ -217,50 +213,47 @@ class DeviceRepository( } catch (_: Throwable) { } } - private suspend fun isDeviceRegisteredLocally(): Boolean { - return context.dataStore.data - .map { preferences -> preferences[Key.DeviceRegistered] == true } - .firstOrNull() ?: false - } + private suspend fun handlePushToken(pushToken: String, device: Device) { + val device = device.copy(token = pushToken) - private suspend fun setDeviceRegisteredLocally(registered: Boolean) { - context.dataStore.edit { preferences -> - preferences[Key.DeviceRegistered] = registered + if (isDeviceRegistered(device.id)) { + updateDevice(device) + } else { + registerDevice(device) } } - private suspend fun callRegisterDevice(device: 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 { - val locallyRegistered = isDeviceRegisteredLocally() - if (!locallyRegistered) { - val isRegistered = gemApiClient.isDeviceRegistered(device.id) - if (!isRegistered) { - gemApiClient.registerDevice(device) - setDeviceRegisteredLocally(true) - return - } - setDeviceRegisteredLocally(true) - } - val remoteDeviceInfo = try { - gemApiClient.getDevice(device.id) - } catch (e: HttpException) { - if (e.code() == 404) { - setDeviceRegisteredLocally(false) - return callRegisterDevice(device) - } - return - } ?: return - if (remoteDeviceInfo.hasChanges(device)) { - val subVersion = max(device.subscriptionsVersion, remoteDeviceInfo.subscriptionsVersion) + 1 - setSubscriptionVersion(subVersion) + val remote = gemApiClient.getDevice(device.id) + + if (remote?.hasChanges(device) == true) { + val subscriptionsVersion = max(device.subscriptionsVersion, remote.subscriptionsVersion) + 1 + setSubscriptionVersion(subscriptionsVersion) gemApiClient.updateDevice( - device.id, - device.copy(subscriptionsVersion = subVersion) + 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 { return try { gemApiClient.getSubscriptions(deviceId) ?: throw Exception() diff --git a/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/nft/NftRepository.kt b/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/nft/NftRepository.kt index c31dcbba9..dcd1c6f75 100644 --- a/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/nft/NftRepository.kt +++ b/data/repositories/src/main/kotlin/com/gemwallet/android/data/repositoreis/nft/NftRepository.kt @@ -34,7 +34,7 @@ class NftRepository( override suspend fun loadNFT(wallet: Wallet) { val deviceId = getDeviceId.getDeviceId() - val response = gemApiClient.getNFTs(deviceId, wallet.id) + val response = gemApiClient.getNFTs(deviceId = deviceId, walletId = wallet.id) val collections = response.map { DbNFTCollection( id = it.collection.id, diff --git a/features/referral/viewmodels/src/main/kotlin/com/gemwallet/features/referral/viewmodels/ReferralViewModel.kt b/features/referral/viewmodels/src/main/kotlin/com/gemwallet/features/referral/viewmodels/ReferralViewModel.kt index ccac7200b..912606078 100644 --- a/features/referral/viewmodels/src/main/kotlin/com/gemwallet/features/referral/viewmodels/ReferralViewModel.kt +++ b/features/referral/viewmodels/src/main/kotlin/com/gemwallet/features/referral/viewmodels/ReferralViewModel.kt @@ -7,12 +7,8 @@ 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.repositoreis.session.SessionRepository import com.gemwallet.android.data.repositoreis.wallets.WalletsRepository -import com.gemwallet.android.ext.getAccount -import com.gemwallet.android.ext.referralChain -import com.wallet.core.primitives.Chain import com.wallet.core.primitives.RewardRedemptionOption import com.wallet.core.primitives.Rewards import com.wallet.core.primitives.Wallet @@ -35,13 +31,12 @@ import javax.inject.Inject @OptIn(ExperimentalCoroutinesApi::class) @HiltViewModel class ReferralViewModel @Inject constructor( - private val sessionRepository: SessionRepository, - private val walletsRepository: WalletsRepository, + sessionRepository: SessionRepository, + walletsRepository: WalletsRepository, private val getRewards: GetRewards, private val redeem: Redeem, private val useReferralCode: UseReferralCode, private val createReferral: CreateReferral, - private val getDeviceId: GetDeviceId, private val savedStateHandle: SavedStateHandle, ) : ViewModel() { @@ -90,7 +85,7 @@ class ReferralViewModel @Inject constructor( private fun sync(wallet: Wallet, type: SyncType) = viewModelScope.launch(Dispatchers.IO) { inSync.update { type } val rewards = try { - getRewards.getRewards(getDeviceId.getDeviceId(), wallet.id) + getRewards.getRewards(wallet.id) } catch (_: Exception) { null } finally { @@ -102,7 +97,7 @@ class ReferralViewModel @Inject constructor( fun createReferral(username: String, callback: (Exception?) -> Unit) = viewModelScope.launch(Dispatchers.IO) { val rewards = try { val wallet = currentWallet.value ?: return@launch - val response = createReferral.createReferral(username, wallet, getDeviceId.getDeviceId()) + val response = createReferral.createReferral(username, wallet) callback(null) response } catch (err: Exception) { @@ -115,7 +110,7 @@ class ReferralViewModel @Inject constructor( fun useCode(code: String, callback: (Exception?) -> Unit) = viewModelScope.launch(Dispatchers.IO) { try { val wallet = currentWallet.value ?: return@launch - useReferralCode.useReferralCode(code, wallet, getDeviceId.getDeviceId()) + useReferralCode.useReferralCode(code, wallet) callback(null) } catch (err: Exception) { callback(err) @@ -127,7 +122,7 @@ class ReferralViewModel @Inject constructor( val rewards = rewards.value ?: return viewModelScope.launch(Dispatchers.IO) { try { - redeem.redeem(wallet, rewards, option, getDeviceId.getDeviceId()) + redeem.redeem(wallet, rewards, option) sync() withContext(Dispatchers.Main) { callback(null) diff --git a/gemcore/src/main/kotlin/com/gemwallet/android/application/referral/coordinators/CreateReferral.kt b/gemcore/src/main/kotlin/com/gemwallet/android/application/referral/coordinators/CreateReferral.kt index 9ef02a7f0..a6e34e153 100644 --- a/gemcore/src/main/kotlin/com/gemwallet/android/application/referral/coordinators/CreateReferral.kt +++ b/gemcore/src/main/kotlin/com/gemwallet/android/application/referral/coordinators/CreateReferral.kt @@ -4,5 +4,5 @@ import com.wallet.core.primitives.Rewards import com.wallet.core.primitives.Wallet interface CreateReferral { - suspend fun createReferral(code: String, wallet: Wallet, deviceId: String): Rewards + suspend fun createReferral(code: String, wallet: Wallet): Rewards } diff --git a/gemcore/src/main/kotlin/com/gemwallet/android/application/referral/coordinators/GetRewards.kt b/gemcore/src/main/kotlin/com/gemwallet/android/application/referral/coordinators/GetRewards.kt index 077cc7d09..e42abb22f 100644 --- a/gemcore/src/main/kotlin/com/gemwallet/android/application/referral/coordinators/GetRewards.kt +++ b/gemcore/src/main/kotlin/com/gemwallet/android/application/referral/coordinators/GetRewards.kt @@ -3,5 +3,5 @@ package com.gemwallet.android.application.referral.coordinators import com.wallet.core.primitives.Rewards interface GetRewards { - suspend fun getRewards(deviceId: String, walletId: String): Rewards + suspend fun getRewards(walletId: String): Rewards } diff --git a/gemcore/src/main/kotlin/com/gemwallet/android/application/referral/coordinators/Redeem.kt b/gemcore/src/main/kotlin/com/gemwallet/android/application/referral/coordinators/Redeem.kt index 985f76711..33371154f 100644 --- a/gemcore/src/main/kotlin/com/gemwallet/android/application/referral/coordinators/Redeem.kt +++ b/gemcore/src/main/kotlin/com/gemwallet/android/application/referral/coordinators/Redeem.kt @@ -6,5 +6,5 @@ import com.wallet.core.primitives.Rewards import com.wallet.core.primitives.Wallet interface Redeem { - suspend fun redeem(wallet: Wallet, rewards: Rewards, option: RewardRedemptionOption, deviceId: String): RedemptionResult + suspend fun redeem(wallet: Wallet, rewards: Rewards, option: RewardRedemptionOption): RedemptionResult } diff --git a/gemcore/src/main/kotlin/com/gemwallet/android/application/referral/coordinators/UseReferralCode.kt b/gemcore/src/main/kotlin/com/gemwallet/android/application/referral/coordinators/UseReferralCode.kt index 3a6eb3364..f76ed28e6 100644 --- a/gemcore/src/main/kotlin/com/gemwallet/android/application/referral/coordinators/UseReferralCode.kt +++ b/gemcore/src/main/kotlin/com/gemwallet/android/application/referral/coordinators/UseReferralCode.kt @@ -3,5 +3,5 @@ package com.gemwallet.android.application.referral.coordinators import com.wallet.core.primitives.Wallet interface UseReferralCode { - suspend fun useReferralCode(code: String, wallet: Wallet, deviceId: String): Boolean + suspend fun useReferralCode(code: String, wallet: Wallet): Boolean }