Skip to content

Commit 5dee36c

Browse files
authored
Mark mutable*Source as ReadOnlyComposable (#372)
1 parent c5f9ed1 commit 5dee36c

File tree

8 files changed

+110
-64
lines changed

8 files changed

+110
-64
lines changed

vice-sources/api/android/vice-sources.api

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,15 @@ public abstract interface class com/eygraber/vice/sources/MutableViceSource : co
101101
}
102102

103103
public final class com/eygraber/vice/sources/MutableViceSourceKt {
104-
public static final fun mutableDoubleSource (DZ)Lcom/eygraber/vice/sources/MutableViceSource;
105-
public static synthetic fun mutableDoubleSource$default (DZILjava/lang/Object;)Lcom/eygraber/vice/sources/MutableViceSource;
106-
public static final fun mutableFloatSource (FZ)Lcom/eygraber/vice/sources/MutableViceSource;
107-
public static synthetic fun mutableFloatSource$default (FZILjava/lang/Object;)Lcom/eygraber/vice/sources/MutableViceSource;
108-
public static final fun mutableIntSource (IZ)Lcom/eygraber/vice/sources/MutableViceSource;
109-
public static synthetic fun mutableIntSource$default (IZILjava/lang/Object;)Lcom/eygraber/vice/sources/MutableViceSource;
110-
public static final fun mutableLongSource (JZ)Lcom/eygraber/vice/sources/MutableViceSource;
111-
public static synthetic fun mutableLongSource$default (JZILjava/lang/Object;)Lcom/eygraber/vice/sources/MutableViceSource;
104+
public static final fun mutableDoubleSource (D)Lcom/eygraber/vice/sources/MutableViceSource;
105+
public static final fun mutableFloatSource (F)Lcom/eygraber/vice/sources/MutableViceSource;
106+
public static final fun mutableIntSource (I)Lcom/eygraber/vice/sources/MutableViceSource;
107+
public static final fun mutableLongSource (J)Lcom/eygraber/vice/sources/MutableViceSource;
112108
public static final fun mutableViceSource (Ljava/lang/Object;)Lcom/eygraber/vice/sources/MutableViceSource;
109+
public static final fun saveableMutableDoubleSource (D)Lcom/eygraber/vice/sources/MutableViceSource;
110+
public static final fun saveableMutableFloatSource (F)Lcom/eygraber/vice/sources/MutableViceSource;
111+
public static final fun saveableMutableIntSource (I)Lcom/eygraber/vice/sources/MutableViceSource;
112+
public static final fun saveableMutableLongSource (J)Lcom/eygraber/vice/sources/MutableViceSource;
113113
public static final fun saveableMutableViceSource (Ljava/lang/Object;Landroidx/compose/runtime/saveable/Saver;)Lcom/eygraber/vice/sources/MutableViceSource;
114114
public static synthetic fun saveableMutableViceSource$default (Ljava/lang/Object;Landroidx/compose/runtime/saveable/Saver;ILjava/lang/Object;)Lcom/eygraber/vice/sources/MutableViceSource;
115115
}

vice-sources/api/jvm/vice-sources.api

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,15 @@ public abstract interface class com/eygraber/vice/sources/MutableViceSource : co
101101
}
102102

103103
public final class com/eygraber/vice/sources/MutableViceSourceKt {
104-
public static final fun mutableDoubleSource (DZ)Lcom/eygraber/vice/sources/MutableViceSource;
105-
public static synthetic fun mutableDoubleSource$default (DZILjava/lang/Object;)Lcom/eygraber/vice/sources/MutableViceSource;
106-
public static final fun mutableFloatSource (FZ)Lcom/eygraber/vice/sources/MutableViceSource;
107-
public static synthetic fun mutableFloatSource$default (FZILjava/lang/Object;)Lcom/eygraber/vice/sources/MutableViceSource;
108-
public static final fun mutableIntSource (IZ)Lcom/eygraber/vice/sources/MutableViceSource;
109-
public static synthetic fun mutableIntSource$default (IZILjava/lang/Object;)Lcom/eygraber/vice/sources/MutableViceSource;
110-
public static final fun mutableLongSource (JZ)Lcom/eygraber/vice/sources/MutableViceSource;
111-
public static synthetic fun mutableLongSource$default (JZILjava/lang/Object;)Lcom/eygraber/vice/sources/MutableViceSource;
104+
public static final fun mutableDoubleSource (D)Lcom/eygraber/vice/sources/MutableViceSource;
105+
public static final fun mutableFloatSource (F)Lcom/eygraber/vice/sources/MutableViceSource;
106+
public static final fun mutableIntSource (I)Lcom/eygraber/vice/sources/MutableViceSource;
107+
public static final fun mutableLongSource (J)Lcom/eygraber/vice/sources/MutableViceSource;
112108
public static final fun mutableViceSource (Ljava/lang/Object;)Lcom/eygraber/vice/sources/MutableViceSource;
109+
public static final fun saveableMutableDoubleSource (D)Lcom/eygraber/vice/sources/MutableViceSource;
110+
public static final fun saveableMutableFloatSource (F)Lcom/eygraber/vice/sources/MutableViceSource;
111+
public static final fun saveableMutableIntSource (I)Lcom/eygraber/vice/sources/MutableViceSource;
112+
public static final fun saveableMutableLongSource (J)Lcom/eygraber/vice/sources/MutableViceSource;
113113
public static final fun saveableMutableViceSource (Ljava/lang/Object;Landroidx/compose/runtime/saveable/Saver;)Lcom/eygraber/vice/sources/MutableViceSource;
114114
public static synthetic fun saveableMutableViceSource$default (Ljava/lang/Object;Landroidx/compose/runtime/saveable/Saver;ILjava/lang/Object;)Lcom/eygraber/vice/sources/MutableViceSource;
115115
}

vice-sources/api/vice-sources.klib.api

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,14 @@ final fun com.eygraber.vice.sources/com_eygraber_vice_sources_MutableStateSource
190190
final fun com.eygraber.vice.sources/com_eygraber_vice_sources_SaveableMutableStateSource$stableprop_getter(): kotlin/Int // com.eygraber.vice.sources/com_eygraber_vice_sources_SaveableMutableStateSource$stableprop_getter|com_eygraber_vice_sources_SaveableMutableStateSource$stableprop_getter(){}[0]
191191
final fun com.eygraber.vice.sources/com_eygraber_vice_sources_SerializableMutableStateSource$stableprop_getter(): kotlin/Int // com.eygraber.vice.sources/com_eygraber_vice_sources_SerializableMutableStateSource$stableprop_getter|com_eygraber_vice_sources_SerializableMutableStateSource$stableprop_getter(){}[0]
192192
final fun com.eygraber.vice.sources/com_eygraber_vice_sources_StateFlowSource$stableprop_getter(): kotlin/Int // com.eygraber.vice.sources/com_eygraber_vice_sources_StateFlowSource$stableprop_getter|com_eygraber_vice_sources_StateFlowSource$stableprop_getter(){}[0]
193-
final fun com.eygraber.vice.sources/mutableDoubleSource(kotlin/Double, kotlin/Boolean = ...): com.eygraber.vice.sources/MutableViceSource<kotlin/Double> // com.eygraber.vice.sources/mutableDoubleSource|mutableDoubleSource(kotlin.Double;kotlin.Boolean){}[0]
194-
final fun com.eygraber.vice.sources/mutableFloatSource(kotlin/Float, kotlin/Boolean = ...): com.eygraber.vice.sources/MutableViceSource<kotlin/Float> // com.eygraber.vice.sources/mutableFloatSource|mutableFloatSource(kotlin.Float;kotlin.Boolean){}[0]
195-
final fun com.eygraber.vice.sources/mutableIntSource(kotlin/Int, kotlin/Boolean = ...): com.eygraber.vice.sources/MutableViceSource<kotlin/Int> // com.eygraber.vice.sources/mutableIntSource|mutableIntSource(kotlin.Int;kotlin.Boolean){}[0]
196-
final fun com.eygraber.vice.sources/mutableLongSource(kotlin/Long, kotlin/Boolean = ...): com.eygraber.vice.sources/MutableViceSource<kotlin/Long> // com.eygraber.vice.sources/mutableLongSource|mutableLongSource(kotlin.Long;kotlin.Boolean){}[0]
193+
final fun com.eygraber.vice.sources/mutableDoubleSource(kotlin/Double): com.eygraber.vice.sources/MutableViceSource<kotlin/Double> // com.eygraber.vice.sources/mutableDoubleSource|mutableDoubleSource(kotlin.Double){}[0]
194+
final fun com.eygraber.vice.sources/mutableFloatSource(kotlin/Float): com.eygraber.vice.sources/MutableViceSource<kotlin/Float> // com.eygraber.vice.sources/mutableFloatSource|mutableFloatSource(kotlin.Float){}[0]
195+
final fun com.eygraber.vice.sources/mutableIntSource(kotlin/Int): com.eygraber.vice.sources/MutableViceSource<kotlin/Int> // com.eygraber.vice.sources/mutableIntSource|mutableIntSource(kotlin.Int){}[0]
196+
final fun com.eygraber.vice.sources/mutableLongSource(kotlin/Long): com.eygraber.vice.sources/MutableViceSource<kotlin/Long> // com.eygraber.vice.sources/mutableLongSource|mutableLongSource(kotlin.Long){}[0]
197+
final fun com.eygraber.vice.sources/saveableMutableDoubleSource(kotlin/Double): com.eygraber.vice.sources/MutableViceSource<kotlin/Double> // com.eygraber.vice.sources/saveableMutableDoubleSource|saveableMutableDoubleSource(kotlin.Double){}[0]
198+
final fun com.eygraber.vice.sources/saveableMutableFloatSource(kotlin/Float): com.eygraber.vice.sources/MutableViceSource<kotlin/Float> // com.eygraber.vice.sources/saveableMutableFloatSource|saveableMutableFloatSource(kotlin.Float){}[0]
199+
final fun com.eygraber.vice.sources/saveableMutableIntSource(kotlin/Int): com.eygraber.vice.sources/MutableViceSource<kotlin/Int> // com.eygraber.vice.sources/saveableMutableIntSource|saveableMutableIntSource(kotlin.Int){}[0]
200+
final fun com.eygraber.vice.sources/saveableMutableLongSource(kotlin/Long): com.eygraber.vice.sources/MutableViceSource<kotlin/Long> // com.eygraber.vice.sources/saveableMutableLongSource|saveableMutableLongSource(kotlin.Long){}[0]
197201
final inline fun <#A: kotlin/Any?, #B: kotlin/Any?> (com.eygraber.vice.loadable/ViceLoadable<#A>).com.eygraber.vice.loadable/map(kotlin/Function1<#A, com.eygraber.vice.loadable/ViceLoadable<#B>>): com.eygraber.vice.loadable/ViceLoadable<#B> // com.eygraber.vice.loadable/map|[email protected]<0:0>(kotlin.Function1<0:0,com.eygraber.vice.loadable.ViceLoadable<0:1>>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
198202
final inline fun <#A: kotlin/Any?, #B: kotlin/Any?> (com.eygraber.vice.loadable/ViceLoadable<#A>).com.eygraber.vice.loadable/mapValue(kotlin/Function1<#A, #B>): com.eygraber.vice.loadable/ViceLoadable<#B> // com.eygraber.vice.loadable/mapValue|[email protected]<0:0>(kotlin.Function1<0:0,0:1>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
199203
final inline fun <#A: reified kotlin/Any> com.eygraber.vice.sources/serializableMutableViceSource(#A, kotlinx.serialization/KSerializer<#A> = ...): com.eygraber.vice.sources/MutableViceSource<#A> // com.eygraber.vice.sources/serializableMutableViceSource|serializableMutableViceSource(0:0;kotlinx.serialization.KSerializer<0:0>){0§<kotlin.Any>}[0]

vice-sources/src/commonMain/kotlin/com/eygraber/vice/sources/MutableViceSource.kt

Lines changed: 74 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.eygraber.vice.sources
22

33
import androidx.compose.runtime.Composable
4+
import androidx.compose.runtime.ReadOnlyComposable
45
import androidx.compose.runtime.getValue
56
import androidx.compose.runtime.mutableDoubleStateOf
67
import androidx.compose.runtime.mutableFloatStateOf
@@ -30,6 +31,7 @@ public fun <T> mutableViceSource(
3031
}
3132

3233
@Composable
34+
@ReadOnlyComposable
3335
override fun currentState(): T = state
3436
}
3537

@@ -77,7 +79,20 @@ public inline fun <reified T : Any> serializableMutableViceSource(
7779

7880
public fun mutableDoubleSource(
7981
initial: Double,
80-
isSaveable: Boolean = false,
82+
): MutableViceSource<Double> = object : MutableViceSource<Double> {
83+
private var state by mutableDoubleStateOf(initial)
84+
85+
override fun update(value: Double) {
86+
state = value
87+
}
88+
89+
@Composable
90+
@ReadOnlyComposable
91+
override fun currentState(): Double = state
92+
}
93+
94+
public fun saveableMutableDoubleSource(
95+
initial: Double,
8196
): MutableViceSource<Double> = object : MutableViceSource<Double> {
8297
private var state = mutableDoubleStateOf(initial)
8398

@@ -86,19 +101,28 @@ public fun mutableDoubleSource(
86101
}
87102

88103
@Composable
89-
override fun currentState(): Double =
90-
if(isSaveable) {
91-
state = rememberSaveable { state }
92-
state.doubleValue
93-
}
94-
else {
95-
state.doubleValue
96-
}
104+
override fun currentState(): Double {
105+
state = rememberSaveable { state }
106+
return state.doubleValue
107+
}
97108
}
98109

99110
public fun mutableFloatSource(
100111
initial: Float,
101-
isSaveable: Boolean = false,
112+
): MutableViceSource<Float> = object : MutableViceSource<Float> {
113+
private var state by mutableFloatStateOf(initial)
114+
115+
override fun update(value: Float) {
116+
state = value
117+
}
118+
119+
@Composable
120+
@ReadOnlyComposable
121+
override fun currentState(): Float = state
122+
}
123+
124+
public fun saveableMutableFloatSource(
125+
initial: Float,
102126
): MutableViceSource<Float> = object : MutableViceSource<Float> {
103127
private var state = mutableFloatStateOf(initial)
104128

@@ -107,19 +131,28 @@ public fun mutableFloatSource(
107131
}
108132

109133
@Composable
110-
override fun currentState(): Float =
111-
if(isSaveable) {
112-
state = rememberSaveable { state }
113-
state.floatValue
114-
}
115-
else {
116-
state.floatValue
117-
}
134+
override fun currentState(): Float {
135+
state = rememberSaveable { state }
136+
return state.floatValue
137+
}
118138
}
119139

120140
public fun mutableIntSource(
121141
initial: Int,
122-
isSaveable: Boolean = false,
142+
): MutableViceSource<Int> = object : MutableViceSource<Int> {
143+
private var state by mutableIntStateOf(initial)
144+
145+
override fun update(value: Int) {
146+
state = value
147+
}
148+
149+
@Composable
150+
@ReadOnlyComposable
151+
override fun currentState(): Int = state
152+
}
153+
154+
public fun saveableMutableIntSource(
155+
initial: Int,
123156
): MutableViceSource<Int> = object : MutableViceSource<Int> {
124157
private var state = mutableIntStateOf(initial)
125158

@@ -128,19 +161,28 @@ public fun mutableIntSource(
128161
}
129162

130163
@Composable
131-
override fun currentState(): Int =
132-
if(isSaveable) {
133-
state = rememberSaveable { state }
134-
state.intValue
135-
}
136-
else {
137-
state.intValue
138-
}
164+
override fun currentState(): Int {
165+
state = rememberSaveable { state }
166+
return state.intValue
167+
}
139168
}
140169

141170
public fun mutableLongSource(
142171
initial: Long,
143-
isSaveable: Boolean = false,
172+
): MutableViceSource<Long> = object : MutableViceSource<Long> {
173+
private var state by mutableLongStateOf(initial)
174+
175+
override fun update(value: Long) {
176+
state = value
177+
}
178+
179+
@Composable
180+
@ReadOnlyComposable
181+
override fun currentState(): Long = state
182+
}
183+
184+
public fun saveableMutableLongSource(
185+
initial: Long,
144186
): MutableViceSource<Long> = object : MutableViceSource<Long> {
145187
private var state = mutableLongStateOf(initial)
146188

@@ -149,12 +191,8 @@ public fun mutableLongSource(
149191
}
150192

151193
@Composable
152-
override fun currentState(): Long =
153-
if(isSaveable) {
154-
state = rememberSaveable { state }
155-
state.longValue
156-
}
157-
else {
158-
state.longValue
159-
}
194+
override fun currentState(): Long {
195+
state = rememberSaveable { state }
196+
return state.longValue
197+
}
160198
}

vice-sources/src/jvmTest/kotlin/com/eygraber/vice/sources/mutable/source/MutableDoubleViceSourceTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import androidx.compose.ui.test.ExperimentalTestApi
77
import androidx.compose.ui.test.runComposeUiTest
88
import com.eygraber.vice.sources.ViceStateRestorationTester
99
import com.eygraber.vice.sources.mutableDoubleSource
10+
import com.eygraber.vice.sources.saveableMutableDoubleSource
1011
import kotlin.test.Test
1112
import kotlin.test.assertContentEquals
1213
import kotlin.test.assertEquals
@@ -88,11 +89,11 @@ class MutableDoubleViceSourceTest {
8889

8990
@Test
9091
fun `test that saveable mutable double source is retained across recreation`() = runComposeUiTest {
91-
var source = mutableDoubleSource(1.0, isSaveable = true)
92+
var source = saveableMutableDoubleSource(1.0)
9293
val values = mutableListOf<Double>()
9394
val restorationTester = ViceStateRestorationTester(
9495
composeTest = this,
95-
onDisposedAction = { source = mutableDoubleSource(1.0, isSaveable = true) },
96+
onDisposedAction = { source = saveableMutableDoubleSource(1.0) },
9697
)
9798
val disposedValues = mutableListOf<Boolean>()
9899

vice-sources/src/jvmTest/kotlin/com/eygraber/vice/sources/mutable/source/MutableFloatViceSourceTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import androidx.compose.ui.test.ExperimentalTestApi
77
import androidx.compose.ui.test.runComposeUiTest
88
import com.eygraber.vice.sources.ViceStateRestorationTester
99
import com.eygraber.vice.sources.mutableFloatSource
10+
import com.eygraber.vice.sources.saveableMutableFloatSource
1011
import kotlin.test.Test
1112
import kotlin.test.assertContentEquals
1213
import kotlin.test.assertEquals
@@ -88,11 +89,11 @@ class MutableFloatViceSourceTest {
8889

8990
@Test
9091
fun `test that saveable mutable float source is retained across recreation`() = runComposeUiTest {
91-
var source = mutableFloatSource(1F, isSaveable = true)
92+
var source = saveableMutableFloatSource(1F)
9293
val values = mutableListOf<Float>()
9394
val restorationTester = ViceStateRestorationTester(
9495
composeTest = this,
95-
onDisposedAction = { source = mutableFloatSource(1F, isSaveable = true) },
96+
onDisposedAction = { source = saveableMutableFloatSource(1F) },
9697
)
9798
val disposedValues = mutableListOf<Boolean>()
9899

vice-sources/src/jvmTest/kotlin/com/eygraber/vice/sources/mutable/source/MutableIntViceSourceTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import androidx.compose.ui.test.ExperimentalTestApi
77
import androidx.compose.ui.test.runComposeUiTest
88
import com.eygraber.vice.sources.ViceStateRestorationTester
99
import com.eygraber.vice.sources.mutableIntSource
10+
import com.eygraber.vice.sources.saveableMutableIntSource
1011
import kotlin.test.Test
1112
import kotlin.test.assertContentEquals
1213
import kotlin.test.assertEquals
@@ -88,11 +89,11 @@ class MutableIntViceSourceTest {
8889

8990
@Test
9091
fun `test that saveable mutable int source is retained across recreation`() = runComposeUiTest {
91-
var source = mutableIntSource(1, isSaveable = true)
92+
var source = saveableMutableIntSource(1)
9293
val values = mutableListOf<Int>()
9394
val restorationTester = ViceStateRestorationTester(
9495
composeTest = this,
95-
onDisposedAction = { source = mutableIntSource(1, isSaveable = true) },
96+
onDisposedAction = { source = saveableMutableIntSource(1) },
9697
)
9798
val disposedValues = mutableListOf<Boolean>()
9899

vice-sources/src/jvmTest/kotlin/com/eygraber/vice/sources/mutable/source/MutableLongViceSourceTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import androidx.compose.ui.test.ExperimentalTestApi
77
import androidx.compose.ui.test.runComposeUiTest
88
import com.eygraber.vice.sources.ViceStateRestorationTester
99
import com.eygraber.vice.sources.mutableLongSource
10+
import com.eygraber.vice.sources.saveableMutableLongSource
1011
import kotlin.test.Test
1112
import kotlin.test.assertContentEquals
1213
import kotlin.test.assertEquals
@@ -88,11 +89,11 @@ class MutableLongViceSourceTest {
8889

8990
@Test
9091
fun `test that saveable mutable long source is retained across recreation`() = runComposeUiTest {
91-
var source = mutableLongSource(1L, isSaveable = true)
92+
var source = saveableMutableLongSource(1L)
9293
val values = mutableListOf<Long>()
9394
val restorationTester = ViceStateRestorationTester(
9495
composeTest = this,
95-
onDisposedAction = { source = mutableLongSource(1L, isSaveable = true) },
96+
onDisposedAction = { source = saveableMutableLongSource(1L) },
9697
)
9798
val disposedValues = mutableListOf<Boolean>()
9899

0 commit comments

Comments
 (0)