diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 3eaba7dcb..497da6b64 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -127,7 +127,7 @@ okhttp-bom = { group = "com.squareup.okhttp3", name = "okhttp-bom", version.ref okhttp-core = { group = "com.squareup.okhttp3", name = "okhttp" } okhttp-logging = { group = "com.squareup.okhttp3", name = "logging-interceptor" } retrofit-core = { group = "com.squareup.retrofit2", name = "retrofit", version.ref = "retrofit" } -retrofit-converter-gson = { group = "com.squareup.retrofit2", name = "converter-gson", version.ref = "retrofit" } +retrofit-converter-kotlinx-serialization = { group = "com.squareup.retrofit2", name = "converter-kotlinx-serialization", version.ref = "retrofit" } coil-kt = { group = "io.coil-kt", name = "coil", version.ref = "coil" } coil-kt-compose = { group = "io.coil-kt", name = "coil-compose", version.ref = "coil" } coil-kt-svg = { group = "io.coil-kt", name = "coil-svg", version.ref = "coil" } diff --git a/network/build.gradle.kts b/network/build.gradle.kts index e0afdeb80..8f21aa59e 100644 --- a/network/build.gradle.kts +++ b/network/build.gradle.kts @@ -46,7 +46,7 @@ dependencies { // ... Retrofit implementation(libs.retrofit.core) - implementation(libs.retrofit.converter.gson) + implementation(libs.retrofit.converter.kotlinx.serialization) // ... Coil implementation(libs.coil.kt) diff --git a/network/src/main/kotlin/dev/atick/network/di/retrofit/ConverterModule.kt b/network/src/main/kotlin/dev/atick/network/di/retrofit/ConverterModule.kt new file mode 100644 index 000000000..2040893c9 --- /dev/null +++ b/network/src/main/kotlin/dev/atick/network/di/retrofit/ConverterModule.kt @@ -0,0 +1,34 @@ +package dev.atick.network.di.retrofit + +import dagger.Module +import dagger.Provides +import dagger.hilt.InstallIn +import dagger.hilt.components.SingletonComponent +import kotlinx.serialization.json.Json +import okhttp3.MediaType.Companion.toMediaType +import retrofit2.Converter +import retrofit2.converter.kotlinx.serialization.asConverterFactory +import javax.inject.Singleton + +/** + * Module for providing [Converter.Factory]. + */ +@Module +@InstallIn(SingletonComponent::class) +object ConverterModule { + /** + * Provides kotlinx.serialization [Converter.Factory]. + * + * @return [Converter.Factory]. + */ + @Provides + @Singleton + fun provideConverter(): Converter.Factory { + val json = Json { + ignoreUnknownKeys = true + } + return json.asConverterFactory( + "application/json; charset=UTF8".toMediaType(), + ) + } +} \ No newline at end of file diff --git a/network/src/main/kotlin/dev/atick/network/di/retrofit/GsonConverterModule.kt b/network/src/main/kotlin/dev/atick/network/di/retrofit/GsonConverterModule.kt deleted file mode 100644 index 13b665ec9..000000000 --- a/network/src/main/kotlin/dev/atick/network/di/retrofit/GsonConverterModule.kt +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2023 Atick Faisal - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package dev.atick.network.di.retrofit - -import dagger.Module -import dagger.Provides -import dagger.hilt.InstallIn -import dagger.hilt.components.SingletonComponent -import retrofit2.converter.gson.GsonConverterFactory -import javax.inject.Singleton - -/** - * Module for providing [GsonConverterFactory]. - */ -@Module -@InstallIn(SingletonComponent::class) -object GsonConverterModule { - - /** - * Provides [GsonConverterFactory]. - * - * @return [GsonConverterFactory]. - */ - @Singleton - @Provides - fun provideGsonConverterFactory(): GsonConverterFactory { - return GsonConverterFactory.create() - } -} diff --git a/network/src/main/kotlin/dev/atick/network/di/retrofit/RetrofitModule.kt b/network/src/main/kotlin/dev/atick/network/di/retrofit/RetrofitModule.kt index e3c6aeed0..9b7a05335 100644 --- a/network/src/main/kotlin/dev/atick/network/di/retrofit/RetrofitModule.kt +++ b/network/src/main/kotlin/dev/atick/network/di/retrofit/RetrofitModule.kt @@ -23,8 +23,8 @@ import dagger.hilt.components.SingletonComponent import dev.atick.network.BuildConfig import dev.atick.network.di.okhttp.OkHttpClientModule import okhttp3.OkHttpClient +import retrofit2.Converter import retrofit2.Retrofit -import retrofit2.converter.gson.GsonConverterFactory import javax.inject.Singleton /** @@ -41,14 +41,14 @@ object RetrofitModule { /** * Provides [Retrofit]. * - * @param converterFactory [GsonConverterFactory]. + * @param converterFactory [Converter.Factory]. * @param okHttpClient [OkHttpClient]. * @return [Retrofit]. */ @Singleton @Provides fun provideRetrofitClient( - converterFactory: GsonConverterFactory, + converterFactory: Converter.Factory, okHttpClient: OkHttpClient, ): Retrofit { return Retrofit.Builder() diff --git a/network/src/main/kotlin/dev/atick/network/models/NetworkPost.kt b/network/src/main/kotlin/dev/atick/network/models/NetworkPost.kt index b80514f65..20295d586 100644 --- a/network/src/main/kotlin/dev/atick/network/models/NetworkPost.kt +++ b/network/src/main/kotlin/dev/atick/network/models/NetworkPost.kt @@ -16,6 +16,8 @@ package dev.atick.network.models +import kotlinx.serialization.Serializable + /** * Data class representing a network post retrieved from a remote source. * @@ -24,6 +26,7 @@ package dev.atick.network.models * @property url The URL associated with the network post. * @property thumbnailUrl The URL of the thumbnail image associated with the network post. */ +@Serializable data class NetworkPost( val id: Int, val title: String,