Skip to content

Commit

Permalink
Koin as DI
Browse files Browse the repository at this point in the history
Signed-off-by: Mohsen <[email protected]>
  • Loading branch information
mohsenoid committed Dec 4, 2023
1 parent 1f341bf commit 33d3470
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 90 deletions.
2 changes: 1 addition & 1 deletion .idea/.name

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

4 changes: 3 additions & 1 deletion .idea/artifacts/composeApp_desktop.xml

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

4 changes: 4 additions & 0 deletions .idea/detekt.xml

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

15 changes: 14 additions & 1 deletion composeApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ kotlin {
listOf(
iosX64(),
iosArm64(),
iosSimulatorArm64()
iosSimulatorArm64(),
).forEach { iosTarget ->
iosTarget.binaries.framework {
baseName = "ComposeApp"
Expand All @@ -37,22 +37,31 @@ kotlin {
implementation(libs.compose.ui)
implementation(libs.compose.ui.tooling.preview)
implementation(libs.androidx.activity.compose)

implementation(libs.koin.core)
implementation(libs.koin.android)

implementation(libs.ktor.client.okhttp)
}

desktopMain.dependencies {
implementation(compose.desktop.currentOs)
implementation(libs.ktor.client.okhttp)
}

iosMain.dependencies {
implementation(libs.ktor.client.darwin)
}

commonMain.dependencies {
implementation(compose.runtime)
implementation(compose.foundation)
implementation(compose.material3)
@OptIn(ExperimentalComposeLibrary::class)
implementation(compose.components.resources)

implementation(libs.koin.core)

implementation(libs.ktor.client.core)
implementation(libs.ktor.client.content.negotiation)
implementation(libs.ktor.serialization.kotlinx.json)
Expand All @@ -62,6 +71,10 @@ kotlin {

implementation(libs.kamel)
}

commonTest.dependencies {
implementation(libs.koin.test)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package episodes.data

import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.request.get
import io.ktor.client.request.parameter
import io.ktor.serialization.kotlinx.json.json

class EpisodesRemoteDataSource {

private val httpClient = HttpClient {
install(ContentNegotiation) {
json()
}
}

suspend fun fetchEpisodes(page: Int): Response =
httpClient
.get("https://rickandmortyapi.com/api/episode") {
parameter("page", page)
}
.body<Response>()

fun close() {
httpClient.close()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package episodes.data

import episodes.domain.Episode
import episodes.domain.EpisodesRepository

internal class EpisodesRepositoryImpl(
private val remote: EpisodesRemoteDataSource,
) : EpisodesRepository {

override suspend fun getEpisodes(page: Int): List<Episode> {
return remote.fetchEpisodes(page).results.map {
Episode(
id = it.id,
name = it.name,
airDate = it.airDate,
episode = it.episode,
characters = it.characters,
created = it.created,
)
}
}

override fun close() {
remote.close()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package episodes.domain

interface EpisodesRepository {
suspend fun getEpisodes(page: Int): List<Episode>
fun close()
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
package episodes.presentation

import dev.icerock.moko.mvvm.viewmodel.ViewModel
import episodes.data.Response
import episodes.data.Result
import episodes.domain.Episode
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.request.get
import io.ktor.client.request.parameter
import io.ktor.serialization.kotlinx.json.json
import episodes.domain.EpisodesRepository
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update
Expand All @@ -20,41 +12,20 @@ class EpisodesViewModel : ViewModel() {
private val _uiState = MutableStateFlow(EpisodesUiState())
val uiState: StateFlow<EpisodesUiState> by ::_uiState

private val httpClient = HttpClient() {
install(ContentNegotiation) {
json()
}
}
private val repository: EpisodesRepository = TODO()

fun updateEpisodes() {
viewModelScope.launch {
val episodes = getEpisodes()
val episodes = repository.getEpisodes(1)
_uiState.update { currentState ->
currentState.copy(episodes = episodes.results.map { it.toEpisode() })
currentState.copy(episodes = episodes)
}
}
}

private suspend fun getEpisodes(): Response =
httpClient
.get("https://rickandmortyapi.com/api/episode") {
parameter("page", 1)
}
.body<Response>()

private fun Result.toEpisode() =
Episode(
id = id,
name = name,
airDate = airDate,
episode = episode,
characters = characters,
created = created
)

override fun onCleared() {
super.onCleared()
httpClient.close()
repository.close()
}

fun onEpisodeClicked(episodeId: Int) {
Expand Down
110 changes: 57 additions & 53 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,53 +1,57 @@
[versions]
compose = "1.5.4"
compose-plugin = "1.5.11"
compose-compiler = "1.5.5"
agp = "8.1.4"
android-minSdk = "24"
android-compileSdk = "34"
android-targetSdk = "34"
androidx-activityCompose = "1.8.1"
androidx-core-ktx = "1.12.0"
androidx-appcompat = "1.6.1"
androidx-material = "1.10.0"
androidx-constraintlayout = "2.1.4"
androidx-test-junit = "1.1.5"
androidx-espresso-core = "3.5.1"
kotlin = "1.9.21"
kotlinx-couroutines = "1.7.3"
junit = "4.13.2"
ktor = "2.3.6"
moko-mvvm = "0.16.1"
kamel = "0.8.3"

[libraries]
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
kotlin-test-junit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "kotlin" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
compose-ui = { module = "androidx.compose.ui:ui", version.ref = "compose" }
compose-ui-tooling = { module = "androidx.compose.ui:ui-tooling", version.ref = "compose" }
compose-ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview", version.ref = "compose" }
compose-foundation = { module = "androidx.compose.foundation:foundation", version.ref = "compose" }
compose-material3 = { module = "androidx.compose.material3:material3", version.ref = "compose" }
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "androidx-core-ktx" }
androidx-test-junit = { group = "androidx.test.ext", name = "junit", version.ref = "androidx-test-junit" }
androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "androidx-espresso-core" }
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "androidx-appcompat" }
androidx-material3 = { group = "com.google.android.material", name = "material", version.ref = "androidx-material" }
androidx-constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "androidx-constraintlayout" }
androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "androidx-activityCompose" }
ktor-client-content-negotiation = { module = "io.ktor:ktor-client-content-negotiation", version.ref = "ktor" }
ktor-serialization-kotlinx-json = { module = "io.ktor:ktor-serialization-kotlinx-json", version.ref = "ktor" }
ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor" }
ktor-client-okhttp = { module = "io.ktor:ktor-client-okhttp", version.ref = "ktor" }
ktor-client-darwin = { module = "io.ktor:ktor-client-darwin", version.ref = "ktor" }
moko-mvvm-core = { module = "dev.icerock.moko:mvvm-core", version.ref = "moko-mvvm" }
moko-mvvm-compose = { module = "dev.icerock.moko:mvvm-compose", version.ref = "moko-mvvm" }
kamel = { module = "media.kamel:kamel-image", version.ref = "kamel" }

[plugins]
jetbrainsCompose = { id = "org.jetbrains.compose", version.ref = "compose-plugin" }
androidApplication = { id = "com.android.application", version.ref = "agp" }
androidLibrary = { id = "com.android.library", version.ref = "agp" }
kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
kotlinXSerialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
[versions]
compose = "1.5.4"
compose-plugin = "1.5.11"
compose-compiler = "1.5.5"
agp = "8.1.4"
android-minSdk = "24"
android-compileSdk = "34"
android-targetSdk = "34"
androidx-activityCompose = "1.8.1"
androidx-core-ktx = "1.12.0"
androidx-appcompat = "1.6.1"
androidx-material = "1.10.0"
androidx-constraintlayout = "2.1.4"
androidx-test-junit = "1.1.5"
androidx-espresso-core = "3.5.1"
kotlin = "1.9.21"
kotlinx-couroutines = "1.7.3"
junit = "4.13.2"
koin = "3.2.0"
ktor = "2.3.6"
moko-mvvm = "0.16.1"
kamel = "0.8.3"

[libraries]
kotlin-test = { module = "org.jetbrains.kotlin", name = "kotlin-test", version.ref = "kotlin" }
kotlin-test-junit = { module = "org.jetbrains.kotlin", name = "kotlin-test-junit", version.ref = "kotlin" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
compose-ui = { module = "androidx.compose.ui", name = "ui", version.ref = "compose" }
compose-ui-tooling = { module = "androidx.compose.ui", name = "ui-tooling", version.ref = "compose" }
compose-ui-tooling-preview = { module = "androidx.compose.ui", name = "ui-tooling-preview", version.ref = "compose" }
compose-foundation = { module = "androidx.compose.foundation", name = "foundation", version.ref = "compose" }
compose-material3 = { module = "androidx.compose.material3", name = "material3", version.ref = "compose" }
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "androidx-core-ktx" }
androidx-test-junit = { group = "androidx.test.ext", name = "junit", version.ref = "androidx-test-junit" }
androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "androidx-espresso-core" }
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "androidx-appcompat" }
androidx-material3 = { group = "com.google.android.material", name = "material", version.ref = "androidx-material" }
androidx-constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "androidx-constraintlayout" }
androidx-activity-compose = { module = "androidx.activity", name = "activity-compose", version.ref = "androidx-activityCompose" }
koin-core = { module = "io.insert-koin", name = "koin-core", version.ref = "koin" }
koin-test = { module = "io.insert-koin", name = "koin-test", version.ref = "koin" }
koin-android = { module = "io.insert-koin", name = "koin-android", version.ref = "koin" }
ktor-client-content-negotiation = { module = "io.ktor", name = "ktor-client-content-negotiation", version.ref = "ktor" }
ktor-serialization-kotlinx-json = { module = "io.ktor", name = "ktor-serialization-kotlinx-json", version.ref = "ktor" }
ktor-client-core = { module = "io.ktor", name = "ktor-client-core", version.ref = "ktor" }
ktor-client-okhttp = { module = "io.ktor", name = "ktor-client-okhttp", version.ref = "ktor" }
ktor-client-darwin = { module = "io.ktor", name = "ktor-client-darwin", version.ref = "ktor" }
moko-mvvm-core = { module = "dev.icerock.moko", name = "mvvm-core", version.ref = "moko-mvvm" }
moko-mvvm-compose = { module = "dev.icerock.moko", name = "mvvm-compose", version.ref = "moko-mvvm" }
kamel = { module = "media.kamel", name = "kamel-image", version.ref = "kamel" }

[plugins]
jetbrainsCompose = { id = "org.jetbrains.compose", version.ref = "compose-plugin" }
androidApplication = { id = "com.android.application", version.ref = "agp" }
androidLibrary = { id = "com.android.library", version.ref = "agp" }
kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
kotlinXSerialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }

0 comments on commit 33d3470

Please sign in to comment.