Skip to content

Commit

Permalink
feat: use adapters instead of reflection for json bus stop deserializ…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
balexei committed Nov 11, 2024
1 parent e2d9ce2 commit b7fcfab
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ dependencies {
annotationProcessor(libs.androidx.room.compiler)
ksp(libs.androidx.room.compiler)
implementation(libs.moshi.core)
implementation(libs.moshi.kotlin)
ksp(libs.moshi.kotlin.codegen)
implementation(libs.okhttp)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package io.github.balexei.vitrasaparada.data.source.network

import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import com.squareup.moshi.adapter
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
Expand All @@ -11,18 +10,17 @@ import okhttp3.Request
class BusStopNetworkDataSource : NetworkDataSource {
private val stopsUrl = "https://datos.vigo.org/data/transporte/paradas.json"
private val client = OkHttpClient()
private val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
private val moshi = Moshi.Builder().build()

@OptIn(ExperimentalStdlibApi::class)
override suspend fun loadBusStops(): List<NetworkBusStop> = withContext(Dispatchers.IO) {
val request = Request.Builder().url(stopsUrl).build()
try {
client.newCall(request).execute().use { response ->
if (!response.isSuccessful) {
throw RuntimeException("Unexpected code $response")
}
val type = Types.newParameterizedType(List::class.java, NetworkBusStop::class.java)
val adapter = moshi.adapter<List<NetworkBusStop>>(type)

val adapter = moshi.adapter<List<NetworkBusStop>>()
response.body?.string()?.let { json ->
val busStops = adapter.fromJson(json)
busStops ?: emptyList()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.github.balexei.vitrasaparada.data.source.network

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass

@JsonClass(generateAdapter = true)
data class NetworkBusStop(
@Json(name = "id") val id: Int,
@Json(name = "nombre") val stopName: String,
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ androidx-room-runtime = { module = "androidx.room:room-runtime", version.ref = "
androidx-room-ktx = { module = "androidx.room:room-ktx", version.ref = "androidxRoom" }
androidx-room-compiler = { module = "androidx.room:room-compiler", version.ref = "androidxRoom" }
moshi-core = { group = "com.squareup.moshi", name = "moshi", version.ref = "moshi" }
moshi-kotlin = { group = "com.squareup.moshi", name = "moshi-kotlin", version.ref = "moshi" }
moshi-kotlin-codegen = { group = "com.squareup.moshi", name = "moshi-kotlin-codegen", version.ref = "moshi" }
okhttp = { group = "com.squareup.okhttp3", name = "okhttp", version.ref = "okhttp" }
timber = { group = "com.jakewharton.timber", name = "timber", version.ref = "timber" }
play-services-location = { group = "com.google.android.gms", name = "play-services-location", version.ref = "location" }
Expand Down

0 comments on commit b7fcfab

Please sign in to comment.