From 8660717b523e11ceb9bf5f6fdf1a694280ce4865 Mon Sep 17 00:00:00 2001 From: E-D-W-I-N Date: Thu, 13 May 2021 12:43:46 +0700 Subject: [PATCH] Code review [#25] P1 - Retrofit creator. --- .idea/runConfigurations.xml | 10 ++++++++++ app/build.gradle | 4 ---- .../java/com/edwin/weatherapp/di/AppModule.kt | 19 ++++++++---------- .../com/edwin/data/network/RetrofitClient.kt | 20 +++++++++++++++++++ 4 files changed, 38 insertions(+), 15 deletions(-) create mode 100644 .idea/runConfigurations.xml create mode 100644 data/src/main/java/com/edwin/data/network/RetrofitClient.kt diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..797acea --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index b419f9d..a6008b8 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -57,10 +57,6 @@ dependencies { // Koin implementation "io.insert-koin:koin-android:$koin_version" - // Retrofit + GSON - implementation "com.squareup.retrofit2:retrofit:$retrofit_version" - implementation "com.squareup.retrofit2:converter-gson:$retrofit_version" - // Location implementation "com.google.android.gms:play-services-location:$playServicesLocation_version" diff --git a/app/src/main/java/com/edwin/weatherapp/di/AppModule.kt b/app/src/main/java/com/edwin/weatherapp/di/AppModule.kt index 369eb55..cd5bcd6 100644 --- a/app/src/main/java/com/edwin/weatherapp/di/AppModule.kt +++ b/app/src/main/java/com/edwin/weatherapp/di/AppModule.kt @@ -3,7 +3,7 @@ package com.edwin.weatherapp.di import android.location.Geocoder import com.edwin.data.device.GetLocationDataSource import com.edwin.data.network.GeocoderDataSource -import com.edwin.data.network.WeatherApiDataSource +import com.edwin.data.network.RetrofitClient import com.edwin.data.repository.WeatherRepositoryImpl import com.edwin.domain.WeatherRepository import com.edwin.domain.usecase.GetAddressFromGeocoderUseCase @@ -15,17 +15,8 @@ import com.google.android.gms.location.LocationServices import org.koin.android.ext.koin.androidContext import org.koin.androidx.viewmodel.dsl.viewModel import org.koin.dsl.module -import retrofit2.Retrofit -import retrofit2.converter.gson.GsonConverterFactory val dataModule = module { - // Retrofit API - single { - Retrofit.Builder() - .baseUrl("https://api.openweathermap.org/data/2.5/") - .addConverterFactory(GsonConverterFactory.create()) - .build().create(WeatherApiDataSource::class.java) - } // FusedLocationProvider single { LocationServices.getFusedLocationProviderClient(androidContext()) } @@ -40,7 +31,13 @@ val dataModule = module { single { GeocoderDataSource(get()) } // WeatherRepository - single { WeatherRepositoryImpl(get(), get(), get()) } + single { + WeatherRepositoryImpl( + RetrofitClient.weatherDataSource, + get(), + get() + ) + } } val useCaseModule = module { diff --git a/data/src/main/java/com/edwin/data/network/RetrofitClient.kt b/data/src/main/java/com/edwin/data/network/RetrofitClient.kt new file mode 100644 index 0000000..b116fb2 --- /dev/null +++ b/data/src/main/java/com/edwin/data/network/RetrofitClient.kt @@ -0,0 +1,20 @@ +package com.edwin.data.network + +import retrofit2.Retrofit +import retrofit2.converter.gson.GsonConverterFactory + +object RetrofitClient { + + private const val BASE_URL = "https://api.openweathermap.org/data/2.5/" + + private val retrofit: Retrofit by lazy { + Retrofit.Builder() + .addConverterFactory(GsonConverterFactory.create()) + .baseUrl(BASE_URL) + .build() + } + + val weatherDataSource: WeatherApiDataSource by lazy { + retrofit.create(WeatherApiDataSource::class.java) + } +} \ No newline at end of file