Skip to content

Commit

Permalink
Fixes anr when initializing ProviderAssetManager (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
percivalruiz-bitcoin authored Jan 17, 2025
1 parent 13eb8b5 commit f9425be
Show file tree
Hide file tree
Showing 11 changed files with 269 additions and 162 deletions.
2 changes: 2 additions & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ dependencies {
//Mockk
testImplementation "io.mockk:mockk:1.12.0"
androidTestImplementation "io.mockk:mockk-android:1.12.0"

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.1'
}

repositories {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.bitcoin.assetconverter.mappings

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonNames

@Serializable
data class BanxaAssetMapping(
val assets: List<Assets>
): AssetMapping() {
@OptIn(ExperimentalSerializationApi::class)
@Serializable
data class Assets (
@JsonNames("compound_key") val compoundKey: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.bitcoin.assetconverter.mappings

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonNames

@Serializable
data class MoonpayAssetMapping(
val assets: List<Assets>
): AssetMapping() {
@OptIn(ExperimentalSerializationApi::class)
@Serializable
data class Assets (
@JsonNames("currency_code") val currencyCode: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.bitcoin.assetconverter.mappings

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonNames

@Serializable
data class SimplexAssetMapping(
val assets: List<Assets>
): AssetMapping() {

@OptIn(ExperimentalSerializationApi::class)
@Serializable
data class Assets (
@JsonNames("currency_code") val currencyCode: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.bitcoin.assetconverter.models

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonNames

@Serializable
data class ProviderAssets(val assets: List<Assets>) {

@OptIn(ExperimentalSerializationApi::class)
@Serializable
data class Assets(
@JsonNames("compound_key", "currency_code") val providerKey: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,30 @@ package com.bitcoin.assetconverter.providers

import android.content.Context
import com.bitcoin.assetconverter.models.ProviderAssets
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromStream

class ProviderAssetManager(context: Context){
class ProviderAssetManager(private val context: Context) {

val assets: MutableMap<ProviderType, List<ProviderAssets.Assets>> = mutableMapOf()
init {
val json = Json { ignoreUnknownKeys = true }
ProviderType.values().forEach {
val assetManager = context.assets
assetManager.open(it.resource).use { inputStream ->
private val assets: MutableMap<ProviderType, List<ProviderAssets.Assets>> = mutableMapOf()
private val json = Json { ignoreUnknownKeys = true }

@OptIn(ExperimentalSerializationApi::class)
private fun loadAssets(provider: ProviderType): List<ProviderAssets.Assets> {
return try {
context.assets.open(provider.resource).use { inputStream ->
val assetMapping: ProviderAssets = json.decodeFromStream(inputStream)
assets[it] = assetMapping.assets
assetMapping.assets
}
} catch (e: Exception) {
emptyList()
}
}

fun getAssets(provider: ProviderType): List<ProviderAssets.Assets>{
return assets[provider] ?: listOf()
fun getAssets(provider: ProviderType): List<ProviderAssets.Assets> {
return assets.getOrPut(provider) { loadAssets(provider) }
}

}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.8.10'
ext.kotlin_version = '2.1.0'

repositories {
google()
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
7 changes: 4 additions & 3 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#Wed Oct 12 10:15:37 JST 2022
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit f9425be

Please sign in to comment.