Skip to content

Commit 6f3c2c8

Browse files
committed
refactor datastore implementation using expect/actual
1 parent 852e2f3 commit 6f3c2c8

10 files changed

Lines changed: 50 additions & 49 deletions

File tree

composeApp/build.gradle.kts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ kotlin {
2121
}
2222

2323
jvm("desktop")
24-
24+
2525
sourceSets {
2626
val desktopMain by getting
2727

@@ -126,7 +126,8 @@ compose.desktop {
126126
mainClass = "com.sakethh.linkora.MainKt"
127127

128128
nativeDistributions {
129-
targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb,
129+
targetFormats(
130+
TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb,
130131
TargetFormat.AppImage, TargetFormat.Rpm, TargetFormat.Pkg, TargetFormat.Exe
131132
)
132133
packageName = "Linkora"
@@ -146,7 +147,8 @@ compose.desktop {
146147
linux {
147148
this.iconFile.set(project.file("src/desktopMain/resources/logo.png"))
148149
}
149-
150+
modules("jdk.unsupported")
151+
modules("jdk.unsupported.desktop")
150152
}
151153
}
152154
}

composeApp/src/androidMain/kotlin/com/sakethh/Expected.android.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import androidx.compose.ui.platform.LocalConfiguration
1515
import androidx.compose.ui.text.font.FontFamily
1616
import androidx.core.app.NotificationCompat
1717
import androidx.core.content.ContextCompat
18+
import androidx.datastore.core.DataStore
19+
import androidx.datastore.preferences.core.PreferenceDataStoreFactory
20+
import androidx.datastore.preferences.core.Preferences
1821
import androidx.datastore.preferences.core.longPreferencesKey
1922
import androidx.datastore.preferences.core.stringPreferencesKey
2023
import androidx.work.Constraints
@@ -30,6 +33,7 @@ import com.sakethh.linkora.RefreshAllLinksWorker
3033
import com.sakethh.linkora.common.Localization
3134
import com.sakethh.linkora.common.preferences.AppPreferenceType
3235
import com.sakethh.linkora.common.preferences.AppPreferences
36+
import com.sakethh.linkora.common.utils.Constants
3337
import com.sakethh.linkora.common.utils.getLocalizedString
3438
import com.sakethh.linkora.common.utils.isNull
3539
import com.sakethh.linkora.data.local.LocalDatabase
@@ -50,6 +54,7 @@ import kotlinx.coroutines.flow.Flow
5054
import kotlinx.coroutines.flow.channelFlow
5155
import kotlinx.coroutines.flow.collectLatest
5256
import kotlinx.coroutines.launch
57+
import okio.Path.Companion.toPath
5358
import java.io.File
5459
import java.text.DateFormat
5560
import java.util.Date
@@ -237,4 +242,10 @@ actual class DataSyncingNotificationService actual constructor() {
237242
notificationManager.cancelAll()
238243
}
239244

240-
}
245+
}
246+
247+
actual val linkoraDataStore: DataStore<Preferences> = PreferenceDataStoreFactory.createWithPath(
248+
produceFile = {
249+
LinkoraApp.getContext().applicationContext.filesDir.resolve(Constants.DATA_STORE_NAME).absolutePath.toPath()
250+
}
251+
)

composeApp/src/androidMain/kotlin/com/sakethh/linkora/LinkoraApp.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ import androidx.sqlite.execSQL
1414
import com.sakethh.linkora.common.DependencyContainer
1515
import com.sakethh.linkora.common.Localization
1616
import com.sakethh.linkora.common.preferences.AppPreferences
17-
import com.sakethh.linkora.data.local.LinkoraDataStoreName
1817
import com.sakethh.linkora.data.local.LocalDatabase
19-
import com.sakethh.linkora.data.local.createDataStore
2018
import com.sakethh.linkora.ui.utils.linkoraLog
2119
import kotlinx.coroutines.Dispatchers
2220
import java.time.Instant
@@ -36,9 +34,6 @@ class LinkoraApp : Application() {
3634
override fun onCreate() {
3735
super.onCreate()
3836
LinkoraApp.applicationContext = this.applicationContext
39-
DependencyContainer.dataStorePref = createDataStore {
40-
applicationContext.filesDir.resolve(LinkoraDataStoreName).absolutePath
41-
}
4237
localDatabase = buildLocalDatabase()
4338
AppPreferences.readAll(DependencyContainer.preferencesRepo.value)
4439
Localization.loadLocalizedStrings(

composeApp/src/commonMain/kotlin/com/sakethh/Expected.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package com.sakethh
22

33
import androidx.compose.runtime.Composable
44
import androidx.compose.ui.text.font.FontFamily
5+
import androidx.datastore.core.DataStore
6+
import androidx.datastore.preferences.core.Preferences
57
import com.sakethh.linkora.data.local.LocalDatabase
68
import com.sakethh.linkora.domain.ExportFileType
79
import com.sakethh.linkora.domain.ImportFileType
@@ -19,6 +21,7 @@ expect val BUILD_FLAVOUR: String
1921
expect val platform: @Composable () -> Platform
2022

2123
expect val localDatabase: LocalDatabase?
24+
expect val linkoraDataStore: DataStore<Preferences>
2225

2326
expect val poppinsFontFamily: FontFamily
2427

composeApp/src/commonMain/kotlin/com/sakethh/linkora/common/DependencyContainer.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.sakethh.linkora.common
22

3-
import androidx.datastore.core.DataStore
4-
import androidx.datastore.preferences.core.Preferences
53
import com.sakethh.linkora.common.network.Network
64
import com.sakethh.linkora.common.network.repository.NetworkRepoImpl
75
import com.sakethh.linkora.common.preferences.AppPreferences
@@ -18,13 +16,13 @@ import com.sakethh.linkora.data.remote.repository.RemoteFoldersRepoImpl
1816
import com.sakethh.linkora.data.remote.repository.RemoteLinksRepoImpl
1917
import com.sakethh.linkora.data.remote.repository.RemotePanelsRepoImpl
2018
import com.sakethh.linkora.data.remote.repository.RemoteSyncRepoImpl
19+
import com.sakethh.linkoraDataStore
2120
import com.sakethh.localDatabase
2221

2322
object DependencyContainer {
24-
lateinit var dataStorePref: DataStore<Preferences>
2523

2624
val preferencesRepo = lazy {
27-
PreferencesImpl(dataStorePref)
25+
PreferencesImpl(linkoraDataStore)
2826
}
2927

3028
val localizationRepo = lazy {

composeApp/src/commonMain/kotlin/com/sakethh/linkora/common/utils/Constants.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ object Constants {
1515
const val DEFAULT_APP_LANGUAGE_NAME = "English"
1616
const val DEFAULT_USER_AGENT = "Twitterbot/1.0"
1717
const val EXPORT_SCHEMA_VERSION = 12
18+
19+
const val DATA_STORE_NAME = "linkoraDataStore.preferences_pb"
1820
}
1921

2022
object LinkType {

composeApp/src/commonMain/kotlin/com/sakethh/linkora/data/local/DataStore.kt

Lines changed: 0 additions & 15 deletions
This file was deleted.

composeApp/src/desktopMain/kotlin/com/sakethh/Expected.desktop.kt

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@ package com.sakethh
22

33
import androidx.compose.runtime.Composable
44
import androidx.compose.ui.text.font.FontFamily
5+
import androidx.datastore.core.DataStore
6+
import androidx.datastore.preferences.core.PreferenceDataStoreFactory
7+
import androidx.datastore.preferences.core.Preferences
58
import androidx.room.Room
69
import androidx.sqlite.driver.bundled.BundledSQLiteDriver
710
import com.sakethh.linkora.RefreshAllLinksService
811
import com.sakethh.linkora.common.Localization
12+
import com.sakethh.linkora.common.utils.Constants
913
import com.sakethh.linkora.common.utils.getLocalizedString
1014
import com.sakethh.linkora.common.utils.ifNot
1115
import com.sakethh.linkora.common.utils.isNotNull
@@ -23,6 +27,7 @@ import kotlinx.coroutines.Dispatchers
2327
import kotlinx.coroutines.flow.Flow
2428
import kotlinx.coroutines.flow.emptyFlow
2529
import kotlinx.coroutines.withContext
30+
import okio.Path.Companion.toPath
2631
import java.awt.FileDialog
2732
import java.awt.Frame
2833
import java.io.File
@@ -36,17 +41,22 @@ actual val BUILD_FLAVOUR: String = "desktop"
3641
actual val platform: @Composable () -> Platform = {
3742
Platform.Desktop
3843
}
39-
actual val localDatabase: LocalDatabase? =
40-
File(System.getProperty("user.home").run {
41-
val appDataDir = File(this, ".linkora")
42-
if (appDataDir.exists().not()) {
43-
appDataDir.mkdirs()
44-
}
45-
appDataDir
46-
}, "${LocalDatabase.NAME}.db").run {
47-
Room.databaseBuilder<LocalDatabase>(name = this.absolutePath).setDriver(BundledSQLiteDriver()).build()
44+
private val linkoraSpecificFolder = System.getProperty("user.home").run {
45+
val appDataDir = File(this, ".linkora")
46+
if (appDataDir.exists().not()) {
47+
appDataDir.mkdirs()
4848
}
49+
appDataDir
50+
}
4951

52+
actual val localDatabase: LocalDatabase? =
53+
File(linkoraSpecificFolder, "${LocalDatabase.NAME}.db").run {
54+
Room.databaseBuilder<LocalDatabase>(name = this.absolutePath)
55+
.setDriver(BundledSQLiteDriver()).build()
56+
}
57+
actual val linkoraDataStore: DataStore<Preferences> = PreferenceDataStoreFactory.createWithPath {
58+
linkoraSpecificFolder.resolve(Constants.DATA_STORE_NAME).absolutePath.toPath()
59+
}
5060
actual val poppinsFontFamily: FontFamily = com.sakethh.linkora.ui.theme.poppinsFontFamily
5161
actual val showDynamicThemingOption: Boolean = false
5262

composeApp/src/desktopMain/kotlin/com/sakethh/linkora/main.kt

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ import com.sakethh.linkora.common.preferences.AppPreferences
4343
import com.sakethh.linkora.common.utils.getLocalizedString
4444
import com.sakethh.linkora.common.utils.inDoubleQuotes
4545
import com.sakethh.linkora.common.utils.rememberLocalizedString
46-
import com.sakethh.linkora.data.local.LinkoraDataStoreName
47-
import com.sakethh.linkora.data.local.createDataStore
4846
import com.sakethh.linkora.domain.LinkoraPlaceHolder
4947
import com.sakethh.linkora.domain.Platform
5048
import com.sakethh.linkora.ui.App
@@ -58,10 +56,6 @@ import com.sakethh.linkora.ui.utils.UIEvent
5856
import com.sakethh.linkora.ui.utils.UIEvent.pushUIEvent
5957

6058
fun main() {
61-
val dataStorePref = createDataStore {
62-
LinkoraDataStoreName
63-
}
64-
DependencyContainer.dataStorePref = dataStorePref
6559
AppPreferences.readAll(DependencyContainer.preferencesRepo.value)
6660
Localization.loadLocalizedStrings(
6761
AppPreferences.preferredAppLanguageCode.value
@@ -90,13 +84,14 @@ fun main() {
9084
if (AppPreferences.shouldUseForceDarkTheme.value) DarkColors else LightColors
9185
}
9286
) {
93-
Scaffold(topBar = {
94-
if (AppPreferences.useLinkoraTopDecoratorOnDesktop.value) {
95-
WindowDraggableArea {
96-
TopDecorator(windowState)
87+
Scaffold(
88+
topBar = {
89+
if (AppPreferences.useLinkoraTopDecoratorOnDesktop.value) {
90+
WindowDraggableArea {
91+
TopDecorator(windowState)
92+
}
9793
}
98-
}
99-
},
94+
},
10095
modifier = Modifier.border(
10196
0.5.dp,
10297
MaterialTheme.colorScheme.outline.copy(0.25f)

gradlew

100644100755
File mode changed.

0 commit comments

Comments
 (0)