Skip to content

Commit b01ce34

Browse files
committed
Setup multiplatform settings with KMP and theme
Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
1 parent c34bb67 commit b01ce34

8 files changed

Lines changed: 89 additions & 19 deletions

File tree

composeApp/build.gradle.kts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,22 @@ kotlin {
4949
implementation(libs.jetbrains.lifecycle.viewmodel)
5050

5151
// Koin
52-
implementation(project.dependencies.platform(libs.koin.bom))
5352
api(libs.koin.annotations)
5453
implementation(libs.koin.core)
5554
implementation(libs.koin.compose)
5655
implementation(libs.koin.viewmodel)
56+
57+
// Settings
58+
implementation(libs.russhwolf.settings)
5759
}
5860
commonTest.dependencies {
5961
implementation(libs.kotlin.test)
60-
61-
// Koin
62-
implementation(project.dependencies.platform(libs.koin.bom))
6362
implementation(libs.koin.test)
6463
}
6564
androidMain.dependencies {
6665
implementation(compose.preview)
6766
implementation(libs.androidx.activity)
67+
implementation(libs.androidx.preference)
6868
}
6969
jvmMain.dependencies {
7070
implementation(compose.desktop.currentOs)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2026 NewPipe e.V. <https://newpipe-ev.de>
3+
* SPDX-License-Identifier: GPL-3.0-or-later
4+
*/
5+
6+
package net.newpipe.app.module
7+
8+
import androidx.preference.PreferenceManager
9+
import com.russhwolf.settings.Settings
10+
import com.russhwolf.settings.SharedPreferencesSettings
11+
import org.koin.android.ext.koin.androidContext
12+
import org.koin.dsl.module
13+
14+
actual val platformModule = module {
15+
single<Settings> {
16+
SharedPreferencesSettings(PreferenceManager.getDefaultSharedPreferences(androidContext()))
17+
}
18+
}

composeApp/src/commonMain/kotlin/net/newpipe/app/App.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
package net.newpipe.app
77

88
import androidx.compose.runtime.Composable
9-
import net.newpipe.app.module.appModules
9+
import net.newpipe.app.module.platformModule
1010
import net.newpipe.app.theme.AppTheme
1111
import org.koin.compose.KoinMultiplatformApplication
1212
import org.koin.core.annotation.KoinExperimentalAPI
@@ -17,7 +17,7 @@ import org.koin.dsl.koinConfiguration
1717
fun App() {
1818
KoinMultiplatformApplication(
1919
config = koinConfiguration {
20-
modules(appModules)
20+
modules(platformModule)
2121
}
2222
) {
2323
AppTheme {

composeApp/src/commonMain/kotlin/net/newpipe/app/module/Koin.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55

66
package net.newpipe.app.module
77

8-
import org.koin.dsl.module
8+
import org.koin.core.module.Module
99

10-
val appModules = module {
11-
12-
}
10+
/**
11+
* Contains platform specific module; See actual implementation for more details
12+
*/
13+
expect val platformModule: Module

composeApp/src/commonMain/kotlin/net/newpipe/app/theme/Theme.kt

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import androidx.compose.material3.MaterialTheme
1111
import androidx.compose.material3.darkColorScheme
1212
import androidx.compose.material3.lightColorScheme
1313
import androidx.compose.runtime.Composable
14+
import androidx.compose.ui.graphics.Color
15+
import com.russhwolf.settings.Settings
16+
import org.koin.compose.koinInject
1417

1518
private val lightScheme = lightColorScheme(
1619
primary = primaryLight,
@@ -88,12 +91,25 @@ private val darkScheme = darkColorScheme(
8891
surfaceContainerHighest = surfaceContainerHighestDark,
8992
)
9093

94+
private val blackScheme = darkScheme.copy(surface = Color.Black)
95+
9196
@Composable
92-
fun AppTheme(useDarkTheme: Boolean = isSystemInDarkTheme(), content: @Composable () -> Unit) {
97+
fun AppTheme(
98+
useDarkTheme: Boolean = isSystemInDarkTheme(),
99+
settings: Settings = koinInject(),
100+
content: @Composable () -> Unit
101+
) {
102+
val nightScheme = when(settings.getString("night_theme", "dark_theme")) {
103+
"black_theme" -> blackScheme
104+
else -> darkScheme
105+
}
106+
93107
MaterialTheme(
94-
colorScheme = when {
95-
!useDarkTheme -> lightScheme
96-
else -> darkScheme
108+
colorScheme = when(settings.getString("theme", "auto_device_theme")) {
109+
"light_theme" -> lightScheme
110+
"dark_theme" -> darkScheme
111+
"black_theme" -> blackScheme
112+
else -> if (!useDarkTheme) lightScheme else nightScheme
97113
},
98114
content = content
99115
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2026 NewPipe e.V. <https://newpipe-ev.de>
3+
* SPDX-License-Identifier: GPL-3.0-or-later
4+
*/
5+
6+
package net.newpipe.app.module
7+
8+
import com.russhwolf.settings.NSUserDefaultsSettings
9+
import com.russhwolf.settings.Settings
10+
import org.koin.dsl.module
11+
import platform.Foundation.NSUserDefaults
12+
13+
actual val platformModule = module {
14+
single<Settings> {
15+
NSUserDefaultsSettings(NSUserDefaults())
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2026 NewPipe e.V. <https://newpipe-ev.de>
3+
* SPDX-License-Identifier: GPL-3.0-or-later
4+
*/
5+
6+
package net.newpipe.app.module
7+
8+
import com.russhwolf.settings.PreferencesSettings
9+
import com.russhwolf.settings.Settings
10+
import org.koin.dsl.module
11+
import java.util.prefs.Preferences
12+
13+
actual val platformModule = module {
14+
single<Settings> {
15+
PreferencesSettings(Preferences.userRoot())
16+
}
17+
}

gradle/libs.versions.toml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ runner = "1.7.0"
5252
rxandroid = "3.0.2"
5353
rxbinding = "4.0.0"
5454
rxjava = "3.1.12"
55+
settings = "1.3.0"
5556
sonarqube = "7.2.1.6560"
5657
statesaver = "1.4.1" # TODO: Drop because it is deprecated and incompatible with KSP2
5758
stetho = "1.6.0"
@@ -120,12 +121,11 @@ jetbrains-lifecycle-viewmodel = { module = "org.jetbrains.androidx.lifecycle:lif
120121
jsoup = { module = "org.jsoup:jsoup", version.ref = "jsoup" }
121122
junit = { module = "junit:junit", version.ref = "junit" }
122123
koin-annotations = { module = "io.insert-koin:koin-annotations", version.ref = "koin-annotations" }
123-
koin-bom = { module = "io.insert-koin:koin-bom", version.ref = "koin-bom" }
124124
koin-compiler = { module = "io.insert-koin:koin-ksp-compiler", version.ref = "koin-annotations" }
125-
koin-compose = { module = "io.insert-koin:koin-compose" }
126-
koin-core = { module = "io.insert-koin:koin-core" }
127-
koin-test = { module = "io.insert-koin:koin-test" }
128-
koin-viewmodel = { module = "io.insert-koin:koin-compose-viewmodel" }
125+
koin-compose = { module = "io.insert-koin:koin-compose", version.ref = "koin-bom" }
126+
koin-core = { module = "io.insert-koin:koin-core", version.ref = "koin-bom" }
127+
koin-test = { module = "io.insert-koin:koin-test", version.ref = "koin-bom" }
128+
koin-viewmodel = { module = "io.insert-koin:koin-compose-viewmodel", version.ref = "koin-bom" }
129129
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
130130
lisawray-groupie-core = { module = "com.github.lisawray.groupie:groupie", version.ref = "groupie" }
131131
lisawray-groupie-viewbinding = { module = "com.github.lisawray.groupie:groupie-viewbinding", version.ref = "groupie" }
@@ -141,6 +141,7 @@ pinterest-ktlint = { module = "com.pinterest.ktlint:ktlint-cli", version.ref = "
141141
puppycrawl-checkstyle = { module = "com.puppycrawl.tools:checkstyle", version.ref = "checkstyle" }
142142
reactivex-rxandroid = { module = "io.reactivex.rxjava3:rxandroid", version.ref = "rxandroid" }
143143
reactivex-rxjava = { module = "io.reactivex.rxjava3:rxjava", version.ref = "rxjava" }
144+
russhwolf-settings = { module = "com.russhwolf:multiplatform-settings", version.ref = "settings" }
144145
squareup-leakcanary-core = { module = "com.squareup.leakcanary:leakcanary-android-core", version.ref = "leakcanary" }
145146
squareup-leakcanary-plumber = { module = "com.squareup.leakcanary:plumber-android", version.ref = "leakcanary" }
146147
squareup-leakcanary-watcher = { module = "com.squareup.leakcanary:leakcanary-object-watcher-android", version.ref = "leakcanary" }

0 commit comments

Comments
 (0)