Skip to content

Commit 16f359b

Browse files
authored
Merge pull request #92 from joelkanyi/fix/saver-crash
fix: introduces a custom Saver that correctly saves and restores the state of the signature.
2 parents 3246d5d + 29ce34f commit 16f359b

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

.idea/deploymentTargetSelector.xml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sain/build.gradle.kts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
@file:Suppress("DEPRECATION")
1818

19+
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
1920
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
21+
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSetTree
2022

2123
plugins {
2224
alias(libs.plugins.multiplatform)
@@ -39,6 +41,14 @@ kotlin {
3941
jvmTarget.set(JvmTarget.JVM_1_8)
4042
}
4143
publishLibraryVariants("release")
44+
45+
@OptIn(ExperimentalKotlinGradlePluginApi::class)
46+
instrumentedTestVariant.sourceSetTree.set(KotlinSourceSetTree.test)
47+
48+
dependencies {
49+
androidTestImplementation("androidx.compose.ui:ui-test-junit4-android:1.8.2")
50+
debugImplementation("androidx.compose.ui:ui-test-manifest:1.8.2")
51+
}
4252
}
4353

4454
jvm()
@@ -63,8 +73,17 @@ kotlin {
6373
implementation(compose.foundation)
6474
}
6575

76+
// Adds common test dependencies
6677
commonTest.dependencies {
67-
implementation(libs.kotlin.test)
78+
implementation(kotlin("test"))
79+
80+
@OptIn(org.jetbrains.compose.ExperimentalComposeLibrary::class)
81+
implementation(compose.uiTest)
82+
}
83+
84+
// Adds the desktop test dependency
85+
jvmTest.dependencies {
86+
implementation(compose.desktop.currentOs)
6887
}
6988
}
7089
}
@@ -75,6 +94,7 @@ android {
7594
compileSdk = libs.versions.android.compileSdk.get().toInt()
7695
defaultConfig {
7796
minSdk = libs.versions.android.minSdk.get().toInt()
97+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
7898
}
7999
compileOptions {
80100
sourceCompatibility = JavaVersion.VERSION_1_8

sain/src/commonMain/kotlin/io/github/joelkanyi/sain/Sain.kt

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import androidx.compose.runtime.saveable.Saver
3535
import androidx.compose.runtime.saveable.rememberSaveable
3636
import androidx.compose.ui.Modifier
3737
import androidx.compose.ui.draw.drawWithContent
38+
import androidx.compose.ui.geometry.Offset
3839
import androidx.compose.ui.graphics.Color
3940
import androidx.compose.ui.graphics.ImageBitmap
4041
import androidx.compose.ui.input.pointer.pointerInput
@@ -288,14 +289,25 @@ public class SignatureState {
288289
}
289290

290291
public companion object {
291-
public val Saver: Saver<SignatureState, *> = Saver(
292-
save = {
293-
it.signatureLines to it.signature
292+
public val Saver: Saver<SignatureState, Any> = Saver(
293+
save = { state ->
294+
// Store only values that are Parcelable/Serializable
295+
state.signatureLines.map { line ->
296+
listOf(line.start.x, line.start.y, line.end.x, line.end.y)
297+
}
294298
},
295-
restore = {
299+
restore = { saved ->
300+
@Suppress("UNCHECKED_CAST")
301+
val points = saved as List<List<Float>>
296302
SignatureState().apply {
297-
_signatureLines.addAll(it.first)
298-
_signature.value = it.second
303+
_signatureLines.addAll(
304+
points.map {
305+
SignatureLine(
306+
start = Offset(it[0], it[1]),
307+
end = Offset(it[2], it[3]),
308+
)
309+
},
310+
)
299311
}
300312
},
301313
)

0 commit comments

Comments
 (0)