mirror of
https://github.com/owenlejeune/TVTime.git
synced 2025-11-13 23:32:46 -05:00
add koin, retrofit and setup retrofit boilerplate
This commit is contained in:
@@ -22,6 +22,8 @@ android {
|
||||
vectorDrawables {
|
||||
useSupportLibrary true
|
||||
}
|
||||
|
||||
buildConfigField("String", "TMDB_ApiKey", TMDB_ApiKey)
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
@@ -53,14 +55,25 @@ android {
|
||||
dependencies {
|
||||
|
||||
implementation Dependencies.AndroidX.ktxCore
|
||||
|
||||
implementation Dependencies.Compose.ui
|
||||
implementation Dependencies.Compose.material3
|
||||
implementation Dependencies.Compose.material
|
||||
implementation Dependencies.Compose.uiToolingPreview
|
||||
implementation Dependencies.Lifecycle.runtime
|
||||
implementation Dependencies.Compose.activity
|
||||
implementation Dependencies.Compose.accompanistSystemUi
|
||||
implementation Dependencies.Compose.navigation
|
||||
|
||||
implementation Dependencies.Lifecycle.runtime
|
||||
|
||||
implementation Dependencies.Network.retrofit
|
||||
implementation Dependencies.Network.retrofitGson
|
||||
implementation Dependencies.Network.gson
|
||||
implementation Dependencies.Network.stetho
|
||||
implementation Dependencies.Network.stethoOkHttp
|
||||
|
||||
implementation Dependencies.DI.koin
|
||||
|
||||
testImplementation Dependencies.Testing.junit
|
||||
androidTestImplementation Dependencies.Testing.androidXJunit
|
||||
androidTestImplementation Dependencies.Testing.espressoCore
|
||||
|
||||
@@ -2,7 +2,10 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.owenlejeune.tvtime">
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
|
||||
<application
|
||||
android:name=".TvTimeApplication"
|
||||
android:allowBackup="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.owenlejeune.tvtime
|
||||
|
||||
import android.app.Application
|
||||
import com.facebook.stetho.Stetho
|
||||
import com.owenlejeune.tvtime.di.modules.networkModule
|
||||
import org.koin.android.ext.koin.androidContext
|
||||
import org.koin.android.ext.koin.androidLogger
|
||||
import org.koin.core.context.startKoin
|
||||
import org.koin.core.logger.Level
|
||||
|
||||
class TvTimeApplication: Application() {
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
|
||||
// start koin
|
||||
startKoin {
|
||||
androidLogger(
|
||||
if (BuildConfig.DEBUG) Level.ERROR else Level.NONE
|
||||
)
|
||||
androidContext(this@TvTimeApplication)
|
||||
modules(networkModule)
|
||||
}
|
||||
|
||||
if (BuildConfig.DEBUG) {
|
||||
Stetho.initializeWithDefaults(this)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
32
app/src/main/java/com/owenlejeune/tvtime/api/Client.kt
Normal file
32
app/src/main/java/com/owenlejeune/tvtime/api/Client.kt
Normal file
@@ -0,0 +1,32 @@
|
||||
package com.owenlejeune.tvtime.api
|
||||
|
||||
import okhttp3.Interceptor
|
||||
import org.koin.core.component.KoinComponent
|
||||
import org.koin.core.component.inject
|
||||
import retrofit2.Retrofit
|
||||
|
||||
class Client(baseUrl: String): KoinComponent {
|
||||
|
||||
private val converter: Converter by inject()
|
||||
private val client: HttpClient by inject()
|
||||
|
||||
private var retrofit: Retrofit = Retrofit.Builder()
|
||||
.baseUrl(baseUrl)
|
||||
.client(client.httpClient)
|
||||
.addConverterFactory(converter.get())
|
||||
.build()
|
||||
|
||||
fun <T> create(service: Class<T>): T {
|
||||
return retrofit.create(service)
|
||||
}
|
||||
|
||||
fun addInterceptor(interceptor: Interceptor) {
|
||||
val iClient = client.httpClient.newBuilder()
|
||||
.addInterceptor(interceptor)
|
||||
.build()
|
||||
retrofit = retrofit.newBuilder()
|
||||
.client(iClient)
|
||||
.build()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.owenlejeune.tvtime.api
|
||||
|
||||
import retrofit2.Converter
|
||||
|
||||
interface Converter {
|
||||
|
||||
fun get(): Converter.Factory
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.owenlejeune.tvtime.api
|
||||
|
||||
import com.facebook.stetho.okhttp3.StethoInterceptor
|
||||
import okhttp3.OkHttpClient
|
||||
|
||||
class DebugHttpClient: HttpClient {
|
||||
override val httpClient: OkHttpClient = OkHttpClient.Builder()
|
||||
.addNetworkInterceptor(StethoInterceptor())
|
||||
.build()
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.owenlejeune.tvtime.api
|
||||
|
||||
import com.google.gson.GsonBuilder
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
|
||||
class GsonConverter: Converter {
|
||||
override fun get(): retrofit2.Converter.Factory {
|
||||
val gson = GsonBuilder()
|
||||
.create()
|
||||
|
||||
return GsonConverterFactory.create(gson)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.owenlejeune.tvtime.api
|
||||
|
||||
import okhttp3.OkHttpClient
|
||||
|
||||
interface HttpClient {
|
||||
val httpClient: OkHttpClient
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.owenlejeune.tvtime.api
|
||||
|
||||
import okhttp3.OkHttpClient
|
||||
|
||||
class ProdHttpClient: HttpClient {
|
||||
override val httpClient: OkHttpClient = OkHttpClient.Builder().build()
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.owenlejeune.tvtime.di.modules
|
||||
|
||||
import com.owenlejeune.tvtime.BuildConfig
|
||||
import com.owenlejeune.tvtime.api.*
|
||||
import org.koin.dsl.module
|
||||
|
||||
val networkModule = module {
|
||||
single { if (BuildConfig.DEBUG) DebugHttpClient() else ProdHttpClient() }
|
||||
single<Converter> { GsonConverter() }
|
||||
single { (baseUrl: String) -> Client(baseUrl) }
|
||||
}
|
||||
Reference in New Issue
Block a user