-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
136 additions
and
2 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
wallet_app/android/app/src/main/java/com/example/walletapp/CoinViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.example.walletapp | ||
|
||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import kotlinx.coroutines.launch | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.State | ||
import com.example.walletapp.data.repository.CoinRepository | ||
|
||
class CoinViewModel : ViewModel() { | ||
private val repository = CoinRepository() | ||
|
||
private val _prices = mutableStateOf<Map<String, Double>>(emptyMap()) // Token -> USD price | ||
val prices: State<Map<String, Double>> = _prices | ||
|
||
private val _errorMessage = mutableStateOf("") | ||
val errorMessage: State<String> = _errorMessage | ||
|
||
fun getTokenPrices(ids: String, | ||
vsCurrencies: String) { | ||
viewModelScope.launch { | ||
try { | ||
// Fetch prices for starknet and bitcoin in USD | ||
val response = repository.getTokenPrices(ids,vsCurrencies) | ||
if (response.isSuccessful) { | ||
response.body()?.let { priceMap -> | ||
val parsedPrices = mutableMapOf<String, Double>() | ||
priceMap.forEach { (token, currencyMap) -> | ||
parsedPrices[token] = currencyMap["usd"] ?: 0.0 | ||
} | ||
_prices.value = parsedPrices | ||
} ?: run { | ||
_errorMessage.value = "No data available." | ||
} | ||
} else { | ||
_errorMessage.value = "Error: ${response.code()} ${response.message()}" | ||
} | ||
} catch (e: Exception) { | ||
_errorMessage.value = "Exception: ${e.localizedMessage}" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
wallet_app/android/app/src/main/java/com/example/walletapp/data/datasource/CoinGeckoApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.example.walletapp.data.datasource | ||
|
||
import retrofit2.Response | ||
import retrofit2.http.GET | ||
import retrofit2.http.Headers | ||
import retrofit2.http.Query | ||
|
||
interface CoinGeckoApi { | ||
@Headers( | ||
"accept: application/json", | ||
"x-cg-demo-api-key: CG-mRdWfNFoZnKVan4GNdTrhZjL" | ||
) | ||
@GET("simple/price") | ||
suspend fun getTokenPrices( | ||
@Query("ids") ids: String, // Comma-separated token IDs | ||
@Query("vs_currencies") vsCurrencies: String // Comma-separated currency codes | ||
): Response<Map<String, Map<String, Double>>> | ||
} |
17 changes: 17 additions & 0 deletions
17
wallet_app/android/app/src/main/java/com/example/walletapp/data/repository/CoinRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.example.walletapp.data.repository | ||
|
||
import com.example.walletapp.di.RetrofitClient | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import retrofit2.Response | ||
|
||
class CoinRepository { | ||
suspend fun getTokenPrices( | ||
ids: String, | ||
vsCurrencies: String | ||
): Response<Map<String, Map<String, Double>>> { | ||
return withContext(Dispatchers.IO) { | ||
RetrofitClient.apiService.getTokenPrices(ids, vsCurrencies) | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
wallet_app/android/app/src/main/java/com/example/walletapp/di/RetrofitClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.example.walletapp.di | ||
|
||
import com.example.walletapp.data.datasource.CoinGeckoApi | ||
|
||
import retrofit2.Retrofit | ||
import retrofit2.converter.gson.GsonConverterFactory | ||
|
||
object RetrofitClient { | ||
private const val BASE_URL = "https://api.coingecko.com/api/v3/" | ||
|
||
val apiService: CoinGeckoApi by lazy { | ||
Retrofit.Builder() | ||
.baseUrl(BASE_URL) | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
.build() | ||
.create(CoinGeckoApi::class.java) | ||
} | ||
} |