Skip to content

Commit

Permalink
Added Koin for DI
Browse files Browse the repository at this point in the history
  • Loading branch information
AshuTyagi16 committed Jan 1, 2024
1 parent 381c4d1 commit f8f76e7
Show file tree
Hide file tree
Showing 23 changed files with 241 additions and 31 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,22 @@ android {

dependencies {

// Shared Module
implementation(project(":shared"))

// Shared Core Network Module
implementation(project(":shared:core-network"))

// Core Ktx
implementation(libs.core.ktx)

// Lifecycle Ktx
implementation(libs.lifecycle.runtime.ktx)

// Koin
implementation(libs.koin)
implementation(libs.koin.android)

//Compose
implementation(libs.activity.compose)
implementation(platform(libs.compose.bom))
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:tools="http:https://schemas.android.com/tools">

<application
android:name=".SpotifyApp"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
Expand Down
21 changes: 21 additions & 0 deletions app/src/main/java/com/spotify/app/kmp/SpotifyApp.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.spotify.app.kmp

import android.app.Application
import com.spotify.app.core_network.shared.impl.HttpEngineProviderAndroid
import com.spotify.app.shared.di.getSharedModules
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.core.context.startKoin

class SpotifyApp : Application() {

override fun onCreate() {
super.onCreate()
startKoin {
androidContext(this@SpotifyApp)
androidLogger()
modules(getSharedModules(httpEngineProvider = HttpEngineProviderAndroid()))
}
}

}
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ kotlin.code.style=official
# Enables namespacing of each library's R class so that its R class includes only the
# resources declared in the library itself and none from the library's dependencies,
# thereby reducing the size of the R class for that library
android.nonTransitiveRClass=true
android.nonTransitiveRClass=true
buildkonfig.flavor=debug
4 changes: 4 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ kermit = "2.0.2"
ktor = "2.3.7"
prferences = "1.1.1"
build-konfig = "0.15.1"
koin = "3.5.3"

[libraries]
## Core-Ktx
Expand Down Expand Up @@ -62,6 +63,9 @@ coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", ve
touchlab-kermit = { module = "co.touchlab:kermit", version.ref = "kermit" }
build-konfig = { module = "com.codingfeline.buildkonfig:buildkonfig-gradle-plugin", version.ref = "build-konfig" }

koin = { module = "io.insert-koin:koin-core", version.ref = "koin"}
koin-android = { module = "io.insert-koin:koin-android", version.ref = "koin"}

## Testing
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
Expand Down
12 changes: 11 additions & 1 deletion shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,17 @@ kotlin {

sourceSets {
commonMain.dependencies {
//put your multiplatform dependencies here
// Koin
implementation(libs.koin)

// Shared Core Network Module
implementation(project(":shared:core-network"))

// Shared Core Preferences Module
implementation(project(":shared:core-preferences"))

// Shared Core Logger Module
implementation(project(":shared:core-logger"))
}
commonTest.dependencies {
implementation(libs.kotlin.test)
Expand Down
32 changes: 31 additions & 1 deletion shared/core-logger/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import com.codingfeline.buildkonfig.compiler.FieldSpec.Type.BOOLEAN

@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
plugins {
alias(libs.plugins.kotlin.multiplatform)
alias(libs.plugins.com.android.library)
id(libs.plugins.build.konfig.get().toString())
}

kotlin {
Expand All @@ -26,18 +29,45 @@ kotlin {

sourceSets {
commonMain.dependencies {
// Kermit
implementation(libs.touchlab.kermit)

// Koim
implementation(libs.koin)
}
commonTest.dependencies {
implementation(libs.kotlin.test)
}
}
}

val modulePackageName = "com.spotify.app.core_logger.shared"

buildkonfig {
packageName = modulePackageName
exposeObjectWithName = "CoreLoggerBuildKonfig"

defaultConfigs {
buildConfigField(BOOLEAN, "DEBUG", "true")
}

defaultConfigs("debug") {
buildConfigField(BOOLEAN, "DEBUG", "true")
}

defaultConfigs("release") {
buildConfigField(BOOLEAN, "DEBUG", "false")
}
}

android {
namespace = "com.spotify.app.core_logger.shared"
namespace = modulePackageName
compileSdk = libs.versions.compileSdkVersion.get().toInt()
defaultConfig {
minSdk = libs.versions.minSdkVersion.get().toInt()
}
}

task("testClasses").doLast {
println("This is a dummy testClasses task")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.spotify.app.core_logger.shared.impl.di

import com.spotify.app.core_logger.shared.CoreLoggerBuildKonfig
import com.spotify.app.core_logger.shared.api.LoggerApi
import com.spotify.app.core_logger.shared.impl.LoggerApiImpl
import org.koin.dsl.module

val loggerModule = module {
single<LoggerApi> {
LoggerApiImpl(shouldEnableLogs = CoreLoggerBuildKonfig.DEBUG)
}
}
13 changes: 13 additions & 0 deletions shared/core-network/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import com.codingfeline.buildkonfig.compiler.FieldSpec.Type.BOOLEAN
import com.codingfeline.buildkonfig.compiler.FieldSpec.Type.STRING

@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
Expand Down Expand Up @@ -40,6 +41,9 @@ kotlin {
// Ktor
implementation(libs.bundles.ktor.common)

// Koin
implementation(libs.koin)

// Core-Logger Module
implementation(project(":shared:core-logger"))

Expand Down Expand Up @@ -73,6 +77,15 @@ buildkonfig {
defaultConfigs {
buildConfigField(STRING, "BASE_URL", "api.spotify.com")
buildConfigField(STRING, "BASE_URL_AUTH", "accounts.spotify.com")
buildConfigField(BOOLEAN, "DEBUG", "true")
}

defaultConfigs("debug") {
buildConfigField(BOOLEAN, "DEBUG", "true")
}

defaultConfigs("release") {
buildConfigField(BOOLEAN, "DEBUG", "false")
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.spotify.app.core_network.shared.impl

import io.ktor.client.engine.HttpClientEngine
import io.ktor.client.engine.okhttp.OkHttp

class HttpEngineProviderAndroid : HttpEngineProvider {

override fun clientEngine(shouldEnableLogging: Boolean): HttpClientEngine {
return OkHttp.create()
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.spotify.app.core_network.shared.api

import com.spotify.app.core_network.shared.impl.HttpEngineProvider
import io.ktor.client.HttpClient


interface HttpClientApi {
fun getHttpClient(httpEngineProvider: HttpEngineProvider): HttpClient
fun getHttpClient(): HttpClient

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.spotify.app.core_network.shared.impl
import com.spotify.app.core_logger.shared.api.LoggerApi
import com.spotify.app.core_network.shared.CoreNetworkBuildKonfig
import com.spotify.app.core_network.shared.api.HttpClientApi
import com.spotify.app.core_preferences.shared.impl.util.PreferenceUtil
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.plugins.HttpTimeout
Expand All @@ -19,6 +18,7 @@ import io.ktor.client.plugins.observer.ResponseObserver
import com.spotify.app.core_network.shared.impl.util.NetworkConstants.Endpoints
import com.spotify.app.core_network.shared.impl.model.RefreshTokenRequest
import com.spotify.app.core_network.shared.impl.model.RefreshTokenResponse
import com.spotify.app.core_preferences.shared.api.PreferenceUtilApi
import io.ktor.client.request.host
import io.ktor.client.request.post
import io.ktor.client.request.setBody
Expand All @@ -34,8 +34,9 @@ import kotlinx.serialization.json.Json

class HttpClientApiImpl(
private val shouldEnableLogging: Boolean,
private val httpEngineProvider: HttpEngineProvider,
private val loggerApi: LoggerApi,
private val preferenceUtil: PreferenceUtil
private val preferenceUtilApi: PreferenceUtilApi,
) : HttpClientApi {

companion object {
Expand All @@ -56,7 +57,7 @@ class HttpClientApiImpl(
}
}

override fun getHttpClient(httpEngineProvider: HttpEngineProvider): HttpClient {
override fun getHttpClient(): HttpClient {
return HttpClient(httpEngineProvider.clientEngine(shouldEnableLogging)) {
expectSuccess = true

Expand All @@ -74,8 +75,8 @@ class HttpClientApiImpl(
bearer {
loadTokens {
BearerTokens(
accessToken = preferenceUtil.getAccessToken()!!,
refreshToken = preferenceUtil.getRefreshToken()!!
accessToken = preferenceUtilApi.getAccessToken()!!,
refreshToken = preferenceUtilApi.getRefreshToken()!!
)
}
refreshTokens {
Expand All @@ -86,7 +87,7 @@ class HttpClientApiImpl(
setBody(
RefreshTokenRequest(
grantType = "",
refreshToken = preferenceUtil.getRefreshToken()!!,
refreshToken = preferenceUtilApi.getRefreshToken()!!,
clientId = ""
)
)
Expand All @@ -97,8 +98,8 @@ class HttpClientApiImpl(
val refreshToken = refreshTokenResponse.refreshToken

runBlocking {
preferenceUtil.setAccessToken(accessToken)
preferenceUtil.setRefreshToken(refreshToken)
preferenceUtilApi.setAccessToken(accessToken)
preferenceUtilApi.setRefreshToken(refreshToken)
}

BearerTokens(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.spotify.app.core_network.shared.impl.di

import com.spotify.app.core_network.shared.CoreNetworkBuildKonfig
import com.spotify.app.core_network.shared.impl.HttpClientApiImpl
import com.spotify.app.core_network.shared.impl.HttpEngineProvider
import org.koin.dsl.module

fun createNetworkModule(httpEngineProvider: HttpEngineProvider) = module {
single {
HttpClientApiImpl(
shouldEnableLogging = CoreNetworkBuildKonfig.DEBUG,
httpEngineProvider = httpEngineProvider,
loggerApi = get(),
preferenceUtilApi = get()
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.spotify.app.core_network.shared.impl

import io.ktor.client.engine.HttpClientEngine
import io.ktor.client.engine.darwin.Darwin

class HttpEngineProviderIOS : HttpEngineProvider {
override fun clientEngine(shouldEnableLogging: Boolean): HttpClientEngine = Darwin.create()

}
1 change: 1 addition & 0 deletions shared/core-preferences/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ kotlin {
sourceSets {
commonMain.dependencies {
implementation(libs.bundles.multiplatform.preferences)
implementation(libs.koin)
}
commonTest.dependencies {
implementation(libs.kotlin.test)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.spotify.app.core_preferences.shared.api
interface PreferenceUtilApi {
suspend fun getAccessToken(): String?

suspend fun setAccessToken(token: String)

suspend fun getRefreshToken(): String?

suspend fun setRefreshToken(token: String)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.spotify.app.core_preferences.shared.impl.di

import com.russhwolf.settings.ExperimentalSettingsApi
import com.russhwolf.settings.Settings
import com.russhwolf.settings.coroutines.toSuspendSettings
import com.spotify.app.core_preferences.shared.api.PreferenceApi
import com.spotify.app.core_preferences.shared.api.PreferenceUtilApi
import com.spotify.app.core_preferences.shared.impl.PreferenceApiImpl
import com.spotify.app.core_preferences.shared.impl.util.PreferenceUtilImpl
import org.koin.dsl.module

@OptIn(ExperimentalSettingsApi::class)
val preferencesModule = module {
single<PreferenceApi> {
PreferenceApiImpl(suspendSettings = Settings().toSuspendSettings())
}
single<PreferenceUtilApi> {
PreferenceUtilImpl(get())
}
}

This file was deleted.

Loading

0 comments on commit f8f76e7

Please sign in to comment.