Add build type params, fix network module structure
This commit is contained in:
@@ -29,6 +29,9 @@ android {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
buildFeatures {
|
||||||
|
buildConfig = true
|
||||||
|
}
|
||||||
compileOptions {
|
compileOptions {
|
||||||
sourceCompatibility = JavaVersion.VERSION_17
|
sourceCompatibility = JavaVersion.VERSION_17
|
||||||
targetCompatibility = JavaVersion.VERSION_17
|
targetCompatibility = JavaVersion.VERSION_17
|
||||||
@@ -51,6 +54,7 @@ dependencies {
|
|||||||
implementation(libs.okhttp)
|
implementation(libs.okhttp)
|
||||||
implementation(libs.retrofit)
|
implementation(libs.retrofit)
|
||||||
|
|
||||||
|
implementation(project(":core:common"))
|
||||||
implementation(project(":core:network"))
|
implementation(project(":core:network"))
|
||||||
implementation(project(":core:database"))
|
implementation(project(":core:database"))
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package ru.fincode.tsudesk.di
|
||||||
|
|
||||||
|
import dagger.Module
|
||||||
|
import dagger.Provides
|
||||||
|
import dagger.hilt.InstallIn
|
||||||
|
import dagger.hilt.components.SingletonComponent
|
||||||
|
import ru.fincode.tsudesk.BuildConfig
|
||||||
|
import ru.fincode.tsudesk.core.common.config.AppConfig
|
||||||
|
import javax.inject.Singleton
|
||||||
|
|
||||||
|
@Module
|
||||||
|
@InstallIn(SingletonComponent::class)
|
||||||
|
object AppConfigModule {
|
||||||
|
const val BASE_URL = "https://tulsu.ru/schedule/queries/"
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
@Singleton
|
||||||
|
fun provideAppConfig(): AppConfig =
|
||||||
|
AppConfig(
|
||||||
|
isDebug = BuildConfig.DEBUG,
|
||||||
|
baseUrl = BASE_URL,
|
||||||
|
networkTimeoutSec = 30L
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
|
||||||
|
|
||||||
</manifest>
|
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package ru.fincode.tsudesk.core.common.config
|
||||||
|
|
||||||
|
data class AppConfig(
|
||||||
|
val isDebug: Boolean,
|
||||||
|
val baseUrl: String,
|
||||||
|
val networkTimeoutSec: Long
|
||||||
|
)
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
|
||||||
|
|
||||||
</manifest>
|
|
||||||
@@ -48,4 +48,6 @@ dependencies {
|
|||||||
api(libs.moshi)
|
api(libs.moshi)
|
||||||
api(libs.moshiKotlin)
|
api(libs.moshiKotlin)
|
||||||
api(libs.retrofitMoshi)
|
api(libs.retrofitMoshi)
|
||||||
|
|
||||||
|
implementation(project(":core:common"))
|
||||||
}
|
}
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
|
||||||
|
|
||||||
</manifest>
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
package ru.fincode.tsudesk.core.network
|
package ru.fincode.tsudesk.core.network
|
||||||
|
|
||||||
object Constants {
|
object Constants {
|
||||||
const val BASE_URL = "https://tulsu.ru/schedule/queries/"
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package ru.fincode.tsudesk.core.network
|
||||||
|
|
||||||
|
import okhttp3.OkHttpClient
|
||||||
|
import ru.fincode.tsudesk.core.common.config.AppConfig
|
||||||
|
import ru.fincode.tsudesk.core.network.interceptor.DebugInterceptor
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
|
import javax.inject.Inject
|
||||||
|
import javax.inject.Singleton
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
class OkHttpClientFactory @Inject constructor(
|
||||||
|
private val appConfig: AppConfig,
|
||||||
|
private val debugInterceptor: DebugInterceptor
|
||||||
|
) {
|
||||||
|
|
||||||
|
fun create(): OkHttpClient {
|
||||||
|
val builder = OkHttpClient.Builder()
|
||||||
|
.connectTimeout(appConfig.networkTimeoutSec, TimeUnit.SECONDS)
|
||||||
|
.readTimeout(appConfig.networkTimeoutSec, TimeUnit.SECONDS)
|
||||||
|
.writeTimeout(appConfig.networkTimeoutSec, TimeUnit.SECONDS)
|
||||||
|
.retryOnConnectionFailure(true)
|
||||||
|
|
||||||
|
if (appConfig.isDebug) {
|
||||||
|
builder.addInterceptor(debugInterceptor)
|
||||||
|
}
|
||||||
|
|
||||||
|
return builder.build()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,21 +1,21 @@
|
|||||||
package ru.fincode.tsudesk.core.network
|
package ru.fincode.tsudesk.core.network
|
||||||
|
|
||||||
import com.squareup.moshi.Moshi
|
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
import retrofit2.Retrofit
|
import retrofit2.Retrofit
|
||||||
import retrofit2.converter.moshi.MoshiConverterFactory
|
import retrofit2.converter.moshi.MoshiConverterFactory
|
||||||
import javax.inject.Inject
|
import com.squareup.moshi.Moshi
|
||||||
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
|
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
|
||||||
|
import javax.inject.Inject
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
class RetrofitProvider @Inject constructor() {
|
class RetrofitFactory @Inject constructor() {
|
||||||
|
|
||||||
private val moshi: Moshi = Moshi.Builder()
|
private val moshi: Moshi = Moshi.Builder()
|
||||||
.add(KotlinJsonAdapterFactory())
|
.add(KotlinJsonAdapterFactory())
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
fun execute(baseUrl: String, client: OkHttpClient): Retrofit =
|
fun create(baseUrl: String, client: OkHttpClient): Retrofit =
|
||||||
Retrofit.Builder()
|
Retrofit.Builder()
|
||||||
.baseUrl(baseUrl)
|
.baseUrl(baseUrl)
|
||||||
.client(client)
|
.client(client)
|
||||||
@@ -5,26 +5,15 @@ import dagger.Provides
|
|||||||
import dagger.hilt.InstallIn
|
import dagger.hilt.InstallIn
|
||||||
import dagger.hilt.components.SingletonComponent
|
import dagger.hilt.components.SingletonComponent
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
import ru.fincode.tsudesk.core.network.interceptor.DebugInterceptor
|
import ru.fincode.tsudesk.core.network.OkHttpClientFactory
|
||||||
import java.util.concurrent.TimeUnit
|
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
|
||||||
@Module
|
@Module
|
||||||
@InstallIn(SingletonComponent::class)
|
@InstallIn(SingletonComponent::class)
|
||||||
object OkHttpModule {
|
object OkHttpModule {
|
||||||
|
|
||||||
private const val TIMEOUT = 30L
|
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
fun provideOkHttpClient(
|
fun provideOkHttpClient(factory: OkHttpClientFactory): OkHttpClient =
|
||||||
): OkHttpClient {
|
factory.create()
|
||||||
return OkHttpClient.Builder()
|
|
||||||
.connectTimeout(TIMEOUT, TimeUnit.SECONDS)
|
|
||||||
.readTimeout(TIMEOUT, TimeUnit.SECONDS)
|
|
||||||
.writeTimeout(TIMEOUT, TimeUnit.SECONDS)
|
|
||||||
.retryOnConnectionFailure(true)
|
|
||||||
.addInterceptor(DebugInterceptor())
|
|
||||||
.build()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ import dagger.hilt.InstallIn
|
|||||||
import dagger.hilt.components.SingletonComponent
|
import dagger.hilt.components.SingletonComponent
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
import retrofit2.Retrofit
|
import retrofit2.Retrofit
|
||||||
import ru.fincode.tsudesk.core.network.Constants
|
import ru.fincode.tsudesk.core.common.config.AppConfig
|
||||||
import ru.fincode.tsudesk.core.network.RetrofitProvider
|
import ru.fincode.tsudesk.core.network.RetrofitFactory
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
|
||||||
@Module
|
@Module
|
||||||
@@ -17,8 +17,12 @@ object RetrofitModule {
|
|||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
fun provideRetrofit(
|
fun provideRetrofit(
|
||||||
provider: RetrofitProvider,
|
factory: RetrofitFactory,
|
||||||
client: OkHttpClient
|
client: OkHttpClient,
|
||||||
|
appConfig: AppConfig
|
||||||
): Retrofit =
|
): Retrofit =
|
||||||
provider.execute(Constants.BASE_URL, client)
|
factory.create(
|
||||||
|
baseUrl = appConfig.baseUrl,
|
||||||
|
client = client
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,17 +3,19 @@ package ru.fincode.tsudesk.core.network.interceptor
|
|||||||
import android.util.Log
|
import android.util.Log
|
||||||
import okhttp3.Interceptor
|
import okhttp3.Interceptor
|
||||||
import okhttp3.Response
|
import okhttp3.Response
|
||||||
|
import javax.inject.Inject
|
||||||
|
import javax.inject.Singleton
|
||||||
|
|
||||||
private const val TAG = "NETWORK_DEBUG"
|
private const val TAG = "NETWORK_DEBUG"
|
||||||
|
|
||||||
class DebugInterceptor : Interceptor {
|
@Singleton
|
||||||
|
class DebugInterceptor @Inject constructor() : Interceptor {
|
||||||
|
|
||||||
override fun intercept(chain: Interceptor.Chain): Response {
|
override fun intercept(chain: Interceptor.Chain): Response {
|
||||||
val request = chain.request()
|
val request = chain.request()
|
||||||
|
|
||||||
Log.d(TAG, "URL: ${request.url}")
|
Log.d(TAG, "URL: ${request.url}")
|
||||||
Log.d(TAG, "Method: ${request.method}")
|
Log.d(TAG, "Method: ${request.method}")
|
||||||
Log.d(TAG, "Headers: ${request.headers}")
|
|
||||||
|
|
||||||
return chain.proceed(request)
|
return chain.proceed(request)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
|
||||||
|
|
||||||
</manifest>
|
|
||||||
Reference in New Issue
Block a user