Skip to content

Commit

Permalink
migrate from voyager screenmodel to androidx-viewmodel - #141
Browse files Browse the repository at this point in the history
  • Loading branch information
joelkanyi committed Sep 14, 2024
1 parent 3239d77 commit a0ea40f
Show file tree
Hide file tree
Showing 27 changed files with 461 additions and 377 deletions.
19 changes: 13 additions & 6 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
androidDriver = "2.0.2"
kotlin = "2.0.0"
compose = "1.6.10"
koinCore = "3.5.6"
koinAndroid = "3.5.6"

koin = "3.5.6"
koinCompose = "3.6.0-wasm-alpha2"
koinComposeMultiplatform = "1.2.0-alpha3"

gradle = "8.5.0"
sqlDelight = " 2.0.2"
voyager = "1.0.0"
multiplatformSettings = "1.1.1"
koin-compose = "1.1.5"
core = "1.13.1"
jna = "5.14.0"
koalaplotCore = "0.5.4"
Expand All @@ -23,15 +25,20 @@ toast4j = "0.2.0"
compose-activity = "1.9.0"
spotless = "6.25.0"
accompanist-systemUIController = "0.34.0"
androidx-lifecycle = "2.8.0"

[libraries]
android-driver = { module = "app.cash.sqldelight:android-driver", version.ref = "androidDriver" }
core = { module = "androidx.core:core", version.ref = "core" }
coroutines-extensions = { module = "app.cash.sqldelight:coroutines-extensions", version.ref = "androidDriver" }
jna = { module = "net.java.dev.jna:jna", version.ref = "jna" }
koalaplot-core = { module = "io.github.koalaplot:koalaplot-core", version.ref = "koalaplotCore" }
koin-core = { module = "io.insert-koin:koin-core", version.ref = "koinCore" }
koin-compose = { module = "io.insert-koin:koin-compose", version.ref = "koin-compose"}

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

kotlinx-coroutines-swing = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-swing", version.ref = "kotlinxCoroutinesSwing" }
kotlinX-serializationJson = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "kotlinxSerializationJson" }
material3-window-size-multiplatform = { module = "dev.chrisbanes.material3:material3-window-size-class-multiplatform", version.ref = "material3WindowSizeClassMultiplatform" }
Expand All @@ -44,7 +51,6 @@ multiplatformSettings-noArg = { module = "com.russhwolf:multiplatform-settings-n
multiplatformSettings-coroutines = { module = "com.russhwolf:multiplatform-settings-coroutines", version.ref = "multiplatformSettings" }

compose-activity = { module = "androidx.activity:activity-compose", version.ref = "compose-activity" }
koin-android = { module = "io.insert-koin:koin-android", version.ref = "koinAndroid" }
sqlite-driver = { module = "app.cash.sqldelight:sqlite-driver", version.ref = "androidDriver" }
toast4j = { module = "de.mobanisto:toast4j", version.ref = "toast4j" }

Expand All @@ -59,6 +65,7 @@ core-library-desugaring = { module = "com.android.tools:desugar_jdk_libs", versi
accompanist-systemUIController = { module = "com.google.accompanist:accompanist-systemuicontroller", version.ref = "accompanist-systemUIController" }

stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" }
androidx-lifecycle-viewmodel-compose = { module = "org.jetbrains.androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "androidx-lifecycle" }

[plugins]
compose-multiplatform = { id = "org.jetbrains.compose", version.ref = "compose" }
Expand Down
7 changes: 6 additions & 1 deletion shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ kotlin {
sourceSets {
commonMain.dependencies {
api(libs.koin.core)
api(libs.koin.compose)
implementation(libs.koin.compose)

implementation(compose.material3)
implementation(compose.material)
Expand Down Expand Up @@ -105,6 +105,8 @@ kotlin {
implementation(libs.koalaplot.core)

implementation(libs.stdlib)

implementation(libs.androidx.lifecycle.viewmodel.compose)
}

androidMain.dependencies {
Expand All @@ -113,6 +115,9 @@ kotlin {
implementation(libs.accompanist.systemUIController)
implementation(libs.core)
implementation(libs.compose.activity)

implementation(libs.koin.android)
implementation(libs.koin.androidx.compose)
}

jvmMain.dependencies {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2024 Joel Kanyi.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.joelkanyi.focusbloom.di

import org.koin.androidx.viewmodel.dsl.viewModelOf
import org.koin.dsl.module
import com.joelkanyi.focusbloom.feature.home.HomeViewModel
import com.joelkanyi.focusbloom.feature.addtask.AddTaskViewModel
import com.joelkanyi.focusbloom.feature.calendar.CalendarViewModel
import com.joelkanyi.focusbloom.feature.onboarding.OnboardingViewModel
import com.joelkanyi.focusbloom.feature.settings.SettingsViewModel
import com.joelkanyi.focusbloom.feature.statistics.StatisticsViewModel
import com.joelkanyi.focusbloom.feature.taskprogress.TaskProgressViewModel
import com.joelkanyi.focusbloom.main.MainViewModel

actual val viewModelModule = module {
viewModelOf(::HomeViewModel)
viewModelOf(::AddTaskViewModel)
viewModelOf(::CalendarViewModel)
viewModelOf(::OnboardingViewModel)
viewModelOf(::SettingsViewModel)
viewModelOf(::StatisticsViewModel)
viewModelOf(::TaskProgressViewModel)
viewModelOf(::MainViewModel)
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ import androidx.compose.ui.Modifier
import cafe.adriel.voyager.navigator.CurrentScreen
import cafe.adriel.voyager.navigator.Navigator
import com.joelkanyi.focusbloom.core.presentation.theme.FocusBloomTheme
import com.joelkanyi.focusbloom.core.utils.koinViewModel
import com.joelkanyi.focusbloom.feature.onboarding.OnboardingScreen
import com.joelkanyi.focusbloom.main.MainScreen
import com.joelkanyi.focusbloom.main.MainViewModel
import com.joelkanyi.focusbloom.main.OnBoardingState
import com.joelkanyi.focusbloom.platform.StatusBarColors
import org.koin.compose.KoinContext
import org.koin.compose.koinInject

@Composable
fun FocusBloomApp(
mainViewModel: MainViewModel = koinInject(),
mainViewModel: MainViewModel = koinViewModel(),
) {
val darkTheme = when (mainViewModel.appTheme.collectAsState().value) {
1 -> true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import androidx.compose.ui.text.capitalize
import androidx.compose.ui.text.intl.Locale
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.Dp
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewmodel.compose.viewModel
import com.joelkanyi.focusbloom.core.domain.model.SessionType
import com.joelkanyi.focusbloom.core.domain.model.Task
import com.joelkanyi.focusbloom.core.domain.model.taskTypes
Expand All @@ -41,6 +43,7 @@ import kotlinx.datetime.plus
import kotlinx.datetime.toInstant
import kotlinx.datetime.toLocalDateTime
import org.jetbrains.compose.resources.DrawableResource
import org.koin.compose.currentKoinScope
import kotlin.jvm.JvmInline

@Composable
Expand Down Expand Up @@ -608,3 +611,11 @@ fun Int.formattedNumber(): String {
}
}"
}

@Composable
inline fun <reified T : ViewModel> koinViewModel(): T {
val scope = currentKoinScope()
return viewModel {
scope.get<T>()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ import com.joelkanyi.focusbloom.core.data.repository.tasks.TasksRepositoryImpl
import com.joelkanyi.focusbloom.core.domain.repository.settings.SettingsRepository
import com.joelkanyi.focusbloom.core.domain.repository.tasks.TasksRepository
import com.joelkanyi.focusbloom.database.BloomDatabase
import com.joelkanyi.focusbloom.feature.addtask.AddTaskScreenModel
import com.joelkanyi.focusbloom.feature.calendar.CalendarScreenModel
import com.joelkanyi.focusbloom.feature.home.HomeScreenModel
import com.joelkanyi.focusbloom.feature.onboarding.OnboadingViewModel
import com.joelkanyi.focusbloom.feature.settings.SettingsScreenModel
import com.joelkanyi.focusbloom.feature.statistics.StatisticsScreenModel
import com.joelkanyi.focusbloom.feature.taskprogress.TaskProgressScreenModel
import com.joelkanyi.focusbloom.main.MainViewModel
import com.joelkanyi.focusbloom.platform.DatabaseDriverFactory
import database.TaskEntity
import org.koin.core.module.Module
Expand Down Expand Up @@ -82,59 +74,6 @@ fun commonModule() = module {
bloomDatabase = get(),
)
}

/**
* ViewModels
*/
single<SettingsScreenModel> {
SettingsScreenModel(
settingsRepository = get(),
)
}
single<AddTaskScreenModel> {
AddTaskScreenModel(
settingsRepository = get(),
tasksRepository = get(),
)
}
single<HomeScreenModel> {
HomeScreenModel(
tasksRepository = get(),
settingsRepository = get(),
)
}
single<StatisticsScreenModel> {
StatisticsScreenModel(
tasksRepository = get(),
settingsRepository = get(),
)
}
single<CalendarScreenModel> {
CalendarScreenModel(
tasksRepository = get(),
settingsRepository = get(),
)
}

single<TaskProgressScreenModel> {
TaskProgressScreenModel(
settingsRepository = get(),
tasksRepository = get(),
notificationManager = get(),
)
}

single<MainViewModel> {
MainViewModel(
settingsRepository = get(),
)
}

single<OnboadingViewModel> {
OnboadingViewModel(
settingsRepository = get(),
)
}
}

expect fun platformModule(): Module
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class KoinInit {
listOf(
platformModule(),
commonModule(),
viewModelModule,
),
)
appDeclaration()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2024 Joel Kanyi.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.joelkanyi.focusbloom.di

import org.koin.core.module.Module

expect val viewModelModule: Module
Loading

0 comments on commit a0ea40f

Please sign in to comment.