Skip to content

Commit ae024b7

Browse files
refactor: migrate proto-datastore to kotlinx serialization JSON-datastore
1 parent a5f6fd2 commit ae024b7

17 files changed

Lines changed: 211 additions & 207 deletions

app/build.gradle.kts

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ plugins {
66
id("com.android.application") version "9.2.0"
77
id("org.jetbrains.kotlin.plugin.compose") version "2.3.21"
88
id("org.jetbrains.kotlin.plugin.serialization") version "2.3.21"
9-
id("com.google.protobuf") version "0.10.0"
109
id("org.jlleitschuh.gradle.ktlint") version "14.2.0"
1110
}
1211

@@ -55,11 +54,12 @@ tasks.withType<KotlinCompile> {
5554
dependencies {
5655
// Basic android dependencies
5756
implementation("androidx.core:core-ktx:1.17.0")
58-
implementation("androidx.datastore:datastore:1.2.0")
59-
implementation("com.google.protobuf:protobuf-javalite:4.33.5")
6057
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.10.0")
6158
implementation("androidx.core:core-splashscreen:1.2.0")
6259

60+
// DataStore
61+
implementation("androidx.datastore:datastore:1.2.1")
62+
6363
// Jetpack Compose
6464
// Adding the Bill of Materials synchronizes dependencies in the androidx.compose namespace
6565
// You can remove the library version in your dependency declarations
@@ -88,22 +88,6 @@ dependencies {
8888
androidTestImplementation("androidx.test.espresso:espresso-core:3.7.0")
8989
}
9090

91-
protobuf {
92-
protoc {
93-
artifact = "com.google.protobuf:protoc:3.25.0"
94-
}
95-
96-
generateProtoTasks {
97-
all().forEach { task ->
98-
task.builtins {
99-
create("java") {
100-
option("lite")
101-
}
102-
}
103-
}
104-
}
105-
}
106-
10791
ktlint {
10892
version = "1.8.0"
10993
ignoreFailures = false

app/src/main/java/org/bitcoindevkit/devkitwallet/data/UserPreferencesSerializer.kt

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

app/src/main/java/org/bitcoindevkit/devkitwallet/data/WalletConfigs.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package org.bitcoindevkit.devkitwallet.data
77

88
import org.bitcoindevkit.Descriptor
99
import org.bitcoindevkit.Network
10+
import org.bitcoindevkit.devkitwallet.data.datastore.ActiveWalletScriptType
1011

1112
data class NewWalletConfig(
1213
val name: String,
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2021-2026 thunderbiscuit and contributors.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the ./LICENSE file.
4+
*/
5+
6+
package org.bitcoindevkit.devkitwallet.data.datastore
7+
8+
import androidx.datastore.core.CorruptionException
9+
import androidx.datastore.core.Serializer
10+
import kotlinx.serialization.Serializable
11+
import kotlinx.serialization.SerializationException
12+
import kotlinx.serialization.json.Json
13+
import java.io.InputStream
14+
import java.io.OutputStream
15+
16+
@Serializable
17+
data class AppSettings(
18+
val darkTheme: Boolean = true,
19+
val introDone: Boolean = false,
20+
)
21+
22+
object AppSettingsSerializer : Serializer<AppSettings> {
23+
override val defaultValue = AppSettings()
24+
25+
override suspend fun readFrom(input: InputStream): AppSettings {
26+
try {
27+
return Json.decodeFromString(input.readBytes().decodeToString())
28+
} catch (e: SerializationException) {
29+
throw CorruptionException("Cannot read AppSettings.", e)
30+
}
31+
}
32+
33+
override suspend fun writeTo(t: AppSettings, output: OutputStream) {
34+
output.write(Json.encodeToString(t).encodeToByteArray())
35+
}
36+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.bitcoindevkit.devkitwallet.data.datastore
2+
3+
import androidx.datastore.core.CorruptionException
4+
import androidx.datastore.core.Serializer
5+
import kotlinx.serialization.Serializable
6+
import kotlinx.serialization.SerializationException
7+
import kotlinx.serialization.json.Json
8+
import java.io.InputStream
9+
import java.io.OutputStream
10+
11+
@Serializable
12+
enum class ActiveWalletNetwork {
13+
TESTNET,
14+
SIGNET,
15+
REGTEST,
16+
}
17+
18+
@Serializable
19+
enum class ActiveWalletScriptType {
20+
P2WPKH,
21+
P2TR,
22+
UNKNOWN,
23+
}
24+
25+
@Serializable
26+
data class StoredWallet(
27+
val id: String,
28+
val name: String,
29+
val network: ActiveWalletNetwork,
30+
val scriptType: ActiveWalletScriptType,
31+
val descriptor: String,
32+
val changeDescriptor: String,
33+
val recoveryPhrase: String = "",
34+
val fullScanCompleted: Boolean = false,
35+
)
36+
37+
@Serializable
38+
data class WalletData(
39+
val wallets: List<StoredWallet> = emptyList(),
40+
// network config fields go here alongside wallets
41+
)
42+
43+
object WalletDataSerializer : Serializer<WalletData> {
44+
override val defaultValue = WalletData()
45+
46+
override suspend fun readFrom(input: InputStream): WalletData {
47+
try {
48+
return Json.decodeFromString(input.readBytes().decodeToString())
49+
} catch (e: SerializationException) {
50+
throw CorruptionException("Cannot read AppSettings.", e)
51+
}
52+
}
53+
54+
override suspend fun writeTo(t: WalletData, output: OutputStream) {
55+
output.write(Json.encodeToString(t).encodeToByteArray())
56+
}
57+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2021-2026 thunderbiscuit and contributors.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the ./LICENSE file.
4+
*/
5+
6+
package org.bitcoindevkit.devkitwallet.domain
7+
8+
import androidx.datastore.core.DataStore
9+
import kotlinx.coroutines.flow.first
10+
import org.bitcoindevkit.devkitwallet.data.datastore.AppSettings
11+
12+
class AppSettingsRepository(private val store: DataStore<AppSettings>) {
13+
suspend fun fetchDarkTheme() = store.data.first().darkTheme
14+
15+
suspend fun setDarkTheme(isDark: Boolean) = store.updateData { it.copy(darkTheme = isDark) }
16+
17+
suspend fun fetchIntroDone() = store.data.first().introDone
18+
19+
suspend fun setIntroDone() = store.updateData { it.copy(introDone = true) }
20+
}

app/src/main/java/org/bitcoindevkit/devkitwallet/domain/UserPreferencesRepository.kt

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

0 commit comments

Comments
 (0)