Skip to content

Commit

Permalink
adding kotlin serialization
Browse files Browse the repository at this point in the history
Signed-off-by: Kelvin Bush <kelybush@gmail.com>
  • Loading branch information
kelvinbush committed Feb 4, 2022
1 parent 6ea8edb commit ce14942
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 31 deletions.
15 changes: 6 additions & 9 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {
id 'dagger.hilt.android.plugin'
id 'com.google.gms.google-services'
id 'kotlin-parcelize'
id 'kotlinx-serialization'
}

android {
Expand Down Expand Up @@ -68,16 +69,12 @@ dependencies {
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
implementation "com.google.accompanist:accompanist-insets:0.18.0"

//retrofit
implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
implementation "com.squareup.moshi:moshi-kotlin:$moshi_version"
kapt "com.squareup.moshi:moshi-kotlin-codegen:$moshi_version"
implementation "com.squareup.retrofit2:converter-moshi:$retrofit_version"
/*implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation("com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:0.8.0")*/
// Retrofit && KotlinX Serialization
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation("com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:0.8.0")
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.2"

// KotlinX Serialization
// implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.0"

//hilt
implementation("com.google.dagger:hilt-android:$hilt_version")
Expand Down
35 changes: 16 additions & 19 deletions app/src/main/java/com/kelvinbush/nectar/di/NetworkModule.kt
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
package com.kelvinbush.nectar.di

import android.content.Context
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import com.kelvinbush.nectar.data.AuthInterceptor
import com.kelvinbush.nectar.data.remote.FruityApi
import com.kelvinbush.nectar.util.Constants.BASE_URL
import com.kelvinbush.nectar.util.DefaultIfNullFactory
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
import okhttp3.MediaType
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import java.util.concurrent.TimeUnit
import javax.inject.Singleton


@ExperimentalSerializationApi
@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {
Expand All @@ -35,22 +36,18 @@ object NetworkModule {

@Provides
@Singleton
fun provideMoshi(): Moshi =
Moshi.Builder()
.add(DefaultIfNullFactory())
.addLast(KotlinJsonAdapterFactory())
fun provideRetrofitInstance(okHttpClient: OkHttpClient): Retrofit {
val contentType = MediaType.get("application/json")
val json = Json {
ignoreUnknownKeys = true
isLenient = true
}
return Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(json.asConverterFactory(contentType))
.build()

@Provides
@Singleton
fun provideRetrofitInstance(
okHttpClient: OkHttpClient,
moshi: Moshi,
): Retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(MoshiConverterFactory.create(moshi).asLenient())
.build()
}


@Provides
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package com.kelvinbush.nectar.domain.model

import kotlinx.serialization.Serializable

@Serializable
data class CartProduct(
val id: String,
val quantity: Int,
val product: Product
)

@Serializable
data class Product(
val name: String,
val imageUrl: String,
val price: Double,
)


@Serializable
data class CartItemList(
val cartItems: List<CartProduct>? = emptyList(),
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.kelvinbush.nectar.domain.model

import kotlinx.serialization.Serializable

@Serializable
data class NetworkProduct(
val id: String,
val name: String,
Expand All @@ -11,20 +13,20 @@ data class NetworkProduct(
val inventory: ProductInventory,
)


@Serializable
data class ProductCategory(
val id: String,
val name: String,
val description: String,
)


@Serializable
data class ProductInventory(
val id: String,
val quantity: Int,
)


@Serializable
data class AllNetworkProducts(
val result: List<NetworkProduct>? = emptyList(),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.kelvinbush.nectar.navigation

/*data class DetailsNavArguments(
val navArguments: List<>
)*/
12 changes: 12 additions & 0 deletions app/src/main/java/com/kelvinbush/nectar/navigation/NavGraph.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ import androidx.compose.ui.unit.dp
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.navArgument
import coil.annotation.ExperimentalCoilApi
import com.kelvinbush.nectar.R
import com.kelvinbush.nectar.domain.model.ProductDetail
Expand All @@ -42,6 +44,15 @@ import com.kelvinbush.nectar.util.Constants.DETAIL_ARGUMENT_KEY
@ExperimentalMaterialApi
@Composable
fun SetUpNavGraph(navController: NavHostController) {

val detailNavArguments = listOf(
navArgument("name") { type = NavType.StringType },
navArgument("price") { type = NavType.FloatType },
navArgument("imageUrl") { type = NavType.StringType },
navArgument("description") { type = NavType.StringType },
navArgument("id") { type = NavType.StringType }
)

Scaffold(
bottomBar = { MyBottomNav(navController = navController) }
) {
Expand Down Expand Up @@ -71,6 +82,7 @@ fun SetUpNavGraph(navController: NavHostController) {
}
}


@Composable
fun MyBottomNav(navController: NavHostController) {
val bottomItems = listOf(
Expand Down

0 comments on commit ce14942

Please sign in to comment.