Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ce59307

Browse files
authored
Merge pull request #129 from arkivanov/update-samples
Updated store factories in samples
2 parents 2c9080a + fe94d9d commit ce59307

File tree

32 files changed

+559
-583
lines changed

32 files changed

+559
-583
lines changed

sample/coroutines/shared/build.gradle.kts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,24 @@ android {
2525
}
2626

2727
kotlin {
28-
targets
29-
.filterIsInstance<KotlinNativeTarget>()
30-
.filter { it.konanTarget.family == Family.IOS }
31-
.forEach { target ->
32-
target.binaries.framework {
33-
baseName = "Todo"
28+
if ("XCODE_VERSION_MAJOR" in System.getenv().keys) {
29+
targets
30+
.filterIsInstance<KotlinNativeTarget>()
31+
.filter { it.konanTarget.family == Family.IOS }
32+
.forEach { target ->
33+
target.binaries.framework {
34+
baseName = "Todo"
3435

35-
export(project(":sample:database"))
36-
export(deps.essenty.lifecycle)
37-
export(deps.essenty.instanceKeeper)
38-
export(project(":mvikotlin"))
39-
export(project(":mvikotlin-main"))
40-
export(project(":mvikotlin-logging"))
41-
export(project(":mvikotlin-timetravel"))
36+
export(project(":sample:database"))
37+
export(deps.essenty.lifecycle)
38+
export(deps.essenty.instanceKeeper)
39+
export(project(":mvikotlin"))
40+
export(project(":mvikotlin-main"))
41+
export(project(":mvikotlin-logging"))
42+
export(project(":mvikotlin-timetravel"))
43+
}
4244
}
43-
}
45+
}
4446

4547
setupSourceSets {
4648
val darwin by bundle()

sample/coroutines/shared/src/commonMain/kotlin/com/arkivanov/mvikotlin/sample/coroutines/shared/details/DetailsController.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import com.arkivanov.mvikotlin.extensions.coroutines.labels
1010
import com.arkivanov.mvikotlin.extensions.coroutines.states
1111
import com.arkivanov.mvikotlin.sample.coroutines.shared.TodoDispatchers
1212
import com.arkivanov.mvikotlin.sample.coroutines.shared.details.store.DetailsStore.Label
13-
import com.arkivanov.mvikotlin.sample.coroutines.shared.details.store.DetailsStoreFactory
13+
import com.arkivanov.mvikotlin.sample.coroutines.shared.details.store.detailsStore
1414
import com.arkivanov.mvikotlin.sample.database.TodoDatabase
1515
import com.arkivanov.mvikotlin.sample.database.TodoItem
1616
import kotlinx.coroutines.flow.map
@@ -27,13 +27,12 @@ class DetailsController(
2727
) {
2828

2929
private val detailsStore =
30-
DetailsStoreFactory(
31-
storeFactory = storeFactory,
30+
storeFactory.detailsStore(
3231
database = database,
3332
mainContext = dispatchers.main,
3433
ioContext = dispatchers.io,
3534
itemId = itemId,
36-
).create()
35+
)
3736

3837
init {
3938
lifecycle.doOnDestroy(detailsStore::dispose)

sample/coroutines/shared/src/commonMain/kotlin/com/arkivanov/mvikotlin/sample/coroutines/shared/details/store/DetailsStore.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ internal interface DetailsStore : Store<Intent, State, Label> {
1212
// Serializable only for exporting events in Time Travel, no need otherwise.
1313
sealed class Intent : JvmSerializable {
1414
data class SetText(val text: String) : Intent()
15-
object ToggleDone : Intent()
16-
object Delete : Intent()
15+
data object ToggleDone : Intent()
16+
data object Delete : Intent()
1717
}
1818

1919
data class State(
Lines changed: 57 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@file:OptIn(ExperimentalMviKotlinApi::class)
2+
13
package com.arkivanov.mvikotlin.sample.coroutines.shared.details.store
24

35
import com.arkivanov.mvikotlin.core.store.SimpleBootstrapper
@@ -16,72 +18,68 @@ import kotlinx.coroutines.launch
1618
import kotlinx.coroutines.withContext
1719
import kotlin.coroutines.CoroutineContext
1820

19-
@OptIn(ExperimentalMviKotlinApi::class)
20-
internal class DetailsStoreFactory(
21-
private val storeFactory: StoreFactory,
22-
private val database: TodoDatabase,
23-
private val mainContext: CoroutineContext,
24-
private val ioContext: CoroutineContext,
25-
private val itemId: String,
26-
) {
27-
28-
fun create(): DetailsStore =
29-
object : DetailsStore, Store<Intent, State, Label> by storeFactory.create<Intent, Unit, Msg, State, Label>(
30-
name = "TodoDetailsStore",
31-
initialState = State(),
32-
bootstrapper = SimpleBootstrapper(Unit),
33-
executorFactory = coroutineExecutorFactory(mainContext) {
34-
onAction<Unit> {
35-
launch {
36-
val item: TodoItem? = withContext(ioContext) { database.get(itemId) }
37-
dispatch(item?.data?.let(Msg::Loaded) ?: Msg.Finished)
38-
}
39-
}
21+
internal fun StoreFactory.detailsStore(
22+
database: TodoDatabase,
23+
mainContext: CoroutineContext,
24+
ioContext: CoroutineContext,
25+
itemId: String,
26+
): DetailsStore =
27+
object : DetailsStore, Store<Intent, State, Label> by create<Intent, Unit, Msg, State, Label>(
28+
name = "TodoDetailsStore",
29+
initialState = State(),
30+
bootstrapper = SimpleBootstrapper(Unit),
31+
executorFactory = coroutineExecutorFactory(mainContext) {
32+
fun ExecutorScope.save() {
33+
val data = state().data ?: return
34+
publish(Label.Changed(itemId, data))
4035

41-
onIntent<Intent.SetText> {
42-
dispatch(Msg.TextChanged(it.text))
43-
save()
36+
launch(ioContext) {
37+
database.save(itemId, data)
4438
}
39+
}
4540

46-
onIntent<Intent.ToggleDone> {
47-
dispatch(Msg.DoneToggled)
48-
save()
41+
onAction<Unit> {
42+
launch {
43+
val item: TodoItem? = withContext(ioContext) { database.get(itemId) }
44+
dispatch(item?.data?.let(Msg::Loaded) ?: Msg.Finished)
4945
}
46+
}
5047

51-
onIntent<Intent.Delete> {
52-
publish(Label.Deleted(itemId))
53-
54-
launch {
55-
withContext(ioContext) { database.delete(itemId) }
56-
dispatch(Msg.Finished)
57-
}
58-
}
59-
},
60-
reducer = { msg ->
61-
when (msg) {
62-
is Msg.Loaded -> copy(data = msg.data)
63-
is Msg.Finished -> copy(isFinished = true)
64-
is Msg.TextChanged -> copy(data = data?.copy(text = msg.text))
65-
is Msg.DoneToggled -> copy(data = data?.copy(isDone = !data.isDone))
66-
}
67-
},
68-
) {}
48+
onIntent<Intent.SetText> {
49+
dispatch(Msg.TextChanged(it.text))
50+
save()
51+
}
6952

53+
onIntent<Intent.ToggleDone> {
54+
dispatch(Msg.DoneToggled)
55+
save()
56+
}
7057

71-
// Serializable only for exporting events in Time Travel, no need otherwise.
72-
private sealed class Msg : JvmSerializable {
73-
data class Loaded(val data: TodoItem.Data) : Msg()
74-
object Finished : Msg()
75-
data class TextChanged(val text: String) : Msg()
76-
object DoneToggled : Msg()
77-
}
58+
onIntent<Intent.Delete> {
59+
publish(Label.Deleted(itemId))
7860

79-
private fun CoroutineExecutorScope<State, *, *, Label>.save() {
80-
val data = state().data ?: return
81-
publish(Label.Changed(itemId, data))
61+
launch {
62+
withContext(ioContext) { database.delete(itemId) }
63+
dispatch(Msg.Finished)
64+
}
65+
}
66+
},
67+
reducer = { msg ->
68+
when (msg) {
69+
is Msg.Loaded -> copy(data = msg.data)
70+
is Msg.Finished -> copy(isFinished = true)
71+
is Msg.TextChanged -> copy(data = data?.copy(text = msg.text))
72+
is Msg.DoneToggled -> copy(data = data?.copy(isDone = !data.isDone))
73+
}
74+
},
75+
) {}
8276

83-
launch(ioContext) {
84-
database.save(itemId, data)
85-
}
86-
}
77+
// Serializable only for exporting events in Time Travel, no need otherwise.
78+
private sealed class Msg : JvmSerializable {
79+
data class Loaded(val data: TodoItem.Data) : Msg()
80+
data object Finished : Msg()
81+
data class TextChanged(val text: String) : Msg()
82+
data object DoneToggled : Msg()
8783
}
84+
85+
private typealias ExecutorScope = CoroutineExecutorScope<State, Msg, Unit, Label>

sample/coroutines/shared/src/commonMain/kotlin/com/arkivanov/mvikotlin/sample/coroutines/shared/main/MainController.kt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import com.arkivanov.mvikotlin.extensions.coroutines.labels
1111
import com.arkivanov.mvikotlin.extensions.coroutines.states
1212
import com.arkivanov.mvikotlin.sample.coroutines.shared.TodoDispatchers
1313
import com.arkivanov.mvikotlin.sample.coroutines.shared.main.MainView.Event
14-
import com.arkivanov.mvikotlin.sample.coroutines.shared.main.store.AddStoreFactory
15-
import com.arkivanov.mvikotlin.sample.coroutines.shared.main.store.ListStore
16-
import com.arkivanov.mvikotlin.sample.coroutines.shared.main.store.ListStoreFactory
14+
import com.arkivanov.mvikotlin.sample.coroutines.shared.main.store.add.addStore
15+
import com.arkivanov.mvikotlin.sample.coroutines.shared.main.store.list.ListStore
16+
import com.arkivanov.mvikotlin.sample.coroutines.shared.main.store.list.listStore
1717
import com.arkivanov.mvikotlin.sample.database.TodoDatabase
1818
import com.arkivanov.mvikotlin.sample.database.TodoItem
1919
import kotlinx.coroutines.flow.combine
@@ -30,22 +30,20 @@ class MainController(
3030

3131
private val listStore =
3232
instanceKeeper.getStore {
33-
ListStoreFactory(
34-
storeFactory = storeFactory,
33+
storeFactory.listStore(
3534
database = database,
3635
mainContext = dispatchers.main,
3736
ioContext = dispatchers.io,
38-
).create()
37+
)
3938
}
4039

4140
private val addStore =
4241
instanceKeeper.getStore {
43-
AddStoreFactory(
44-
storeFactory = storeFactory,
42+
storeFactory.addStore(
4543
database = database,
4644
mainContext = dispatchers.main,
4745
ioContext = dispatchers.io,
48-
).create()
46+
)
4947
}
5048

5149
init {

sample/coroutines/shared/src/commonMain/kotlin/com/arkivanov/mvikotlin/sample/coroutines/shared/main/Mappers.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package com.arkivanov.mvikotlin.sample.coroutines.shared.main
33
import com.arkivanov.mvikotlin.sample.database.TodoItem
44
import com.arkivanov.mvikotlin.sample.coroutines.shared.main.MainView.Event
55
import com.arkivanov.mvikotlin.sample.coroutines.shared.main.MainView.Model
6-
import com.arkivanov.mvikotlin.sample.coroutines.shared.main.store.AddStore
7-
import com.arkivanov.mvikotlin.sample.coroutines.shared.main.store.ListStore
6+
import com.arkivanov.mvikotlin.sample.coroutines.shared.main.store.add.AddStore
7+
import com.arkivanov.mvikotlin.sample.coroutines.shared.main.store.list.ListStore
88

99
internal val statesToModel: (ListStore.State, AddStore.State) -> Model =
1010
{ listState, addState ->

sample/coroutines/shared/src/commonMain/kotlin/com/arkivanov/mvikotlin/sample/coroutines/shared/main/store/AddStoreFactory.kt

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

0 commit comments

Comments
 (0)