Skip to content

Commit 855a806

Browse files
authored
Merge pull request #55 from yumemi-inc/refactoring
refactor: improve API consistency and usability
2 parents f7b5eb3 + 3ef23f9 commit 855a806

File tree

7 files changed

+58
-11
lines changed

7 files changed

+58
-11
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ viewStore.handle<CounterEvent> { event ->
548548
549549
### Mock for preview and testing
550550
551-
Create an instance of ViewStore using the `mock()` with target *State*.
551+
Create an instance of ViewStore using the `viewStore()` with target *State*.
552552
You can statically create a ViewStore instance without a *Store* instance.
553553
554554
```kt
@@ -557,7 +557,7 @@ You can statically create a ViewStore instance without a *Store* instance.
557557
fun LoadingPreview() {
558558
MyApplicationTheme {
559559
YourComposable(
560-
viewStore = ViewStore.mock(
560+
viewStore = viewStore(
561561
state = CounterState.Loading,
562562
),
563563
)

tart-compose/src/commonMain/kotlin/io/yumemi/tart/compose/ViewStore.kt

+15-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import kotlinx.coroutines.flow.filter
2626

2727
@Suppress("unused")
2828
@Stable
29-
class ViewStore<S : State, A : Action, E : Event> private constructor(
29+
class ViewStore<S : State, A : Action, E : Event> internal constructor(
3030
val state: S,
3131
val dispatch: (action: A) -> Unit,
3232
val eventFlow: Flow<E>,
@@ -46,7 +46,7 @@ class ViewStore<S : State, A : Action, E : Event> private constructor(
4646
if (state is S2) {
4747
block(
4848
remember(state) {
49-
create(
49+
viewStore(
5050
state = state,
5151
dispatch = dispatch,
5252
eventFlow = eventFlow,
@@ -66,6 +66,7 @@ class ViewStore<S : State, A : Action, E : Event> private constructor(
6666
}
6767

6868
companion object {
69+
@Deprecated("Use viewStore() function instead")
6970
fun <S : State, A : Action, E : Event> create(state: S, dispatch: (action: A) -> Unit, eventFlow: Flow<E>): ViewStore<S, A, E> {
7071
return ViewStore(
7172
state = state,
@@ -74,6 +75,7 @@ class ViewStore<S : State, A : Action, E : Event> private constructor(
7475
)
7576
}
7677

78+
@Deprecated("Use viewStore() function instead")
7779
fun <S : State, A : Action, E : Event> mock(state: S): ViewStore<S, A, E> {
7880
return ViewStore(
7981
state = state,
@@ -84,6 +86,14 @@ class ViewStore<S : State, A : Action, E : Event> private constructor(
8486
}
8587
}
8688

89+
fun <S : State, A : Action, E : Event> viewStore(state: S, dispatch: (action: A) -> Unit = {}, eventFlow: Flow<E> = emptyFlow()): ViewStore<S, A, E> {
90+
return ViewStore(
91+
state = state,
92+
dispatch = dispatch,
93+
eventFlow = eventFlow,
94+
)
95+
}
96+
8797
@Suppress("unused")
8898
@Composable
8999
fun <S : State, A : Action, E : Event> rememberViewStore(store: Store<S, A, E>, autoDispose: Boolean = false): ViewStore<S, A, E> {
@@ -100,7 +110,7 @@ fun <S : State, A : Action, E : Event> rememberViewStore(store: Store<S, A, E>,
100110
}
101111

102112
return remember(state) {
103-
ViewStore.create(
113+
ViewStore(
104114
state = state,
105115
dispatch = rememberStore::dispatch,
106116
eventFlow = rememberStore.event,
@@ -133,7 +143,7 @@ fun <S : State, A : Action, E : Event> rememberViewStore(saver: Saver<S?, out An
133143
}
134144

135145
return remember(state) {
136-
ViewStore.create(
146+
ViewStore(
137147
state = state,
138148
dispatch = store::dispatch,
139149
eventFlow = store.event,
@@ -194,7 +204,7 @@ fun <S : State, A : Action, E : Event> rememberViewStoreSaveable(
194204
}
195205

196206
return remember(state) {
197-
ViewStore.create(
207+
ViewStore(
198208
state = state,
199209
dispatch = store::dispatch,
200210
eventFlow = store.event,

tart-core/src/commonMain/kotlin/io/yumemi/tart/core/StateSaver.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ interface StateSaver<S : State> {
66
}
77

88
@Suppress("unused")
9-
fun <S : State> StateSaver(
9+
fun <S : State> stateSaver(
1010
save: (state: S) -> Unit,
1111
restore: () -> S?,
1212
): StateSaver<S> {

tart-core/src/commonMain/kotlin/io/yumemi/tart/core/Store.kt

+14-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ interface Store<S : State, A : Action, E : Event> {
3535
)
3636

3737
companion object {
38-
@Suppress("unused")
38+
@Deprecated("Use store() function instead")
3939
fun <S : State, A : Action, E : Event> mock(state: S): Store<S, A, E> {
4040
return object : Store<S, A, E> {
4141
override val state: StateFlow<S> = MutableStateFlow(state)
@@ -49,3 +49,16 @@ interface Store<S : State, A : Action, E : Event> {
4949
}
5050
}
5151
}
52+
53+
@Suppress("unused")
54+
fun <S : State, A : Action, E : Event> store(state: S): Store<S, A, E> {
55+
return object : Store<S, A, E> {
56+
override val state: StateFlow<S> = MutableStateFlow(state)
57+
override val event: Flow<E> = emptyFlow()
58+
override val currentState: S = state
59+
override fun dispatch(action: A) {}
60+
override fun collectState(skipInitialState: Boolean, startStore: Boolean, state: (state: S) -> Unit) {}
61+
override fun collectEvent(event: (event: E) -> Unit) {}
62+
override fun dispose() {}
63+
}
64+
}

tart-logging/src/commonMain/kotlin/io/yumemi/tart/logging/TartLogger.kt tart-logging/src/commonMain/kotlin/io/yumemi/tart/logging/DefaultLogger.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package io.yumemi.tart.logging
33
import co.touchlab.kermit.Severity
44
import co.touchlab.kermit.Logger.Companion as Kermit
55

6-
class TartLogger : Logger {
6+
class DefaultLogger : Logger {
77
override suspend fun log(severity: Logger.Severity, tag: String, throwable: Throwable?, message: String) {
88
if (isDisabled) return
99
Kermit.log(

tart-logging/src/commonMain/kotlin/io/yumemi/tart/logging/LoggingMiddleware.kt

+14-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import kotlin.coroutines.CoroutineContext
1313

1414
@Suppress("unused")
1515
open class LoggingMiddleware<S : State, A : Action, E : Event>(
16-
private val logger: Logger = TartLogger(),
16+
private val logger: Logger = DefaultLogger(),
1717
private val tag: String = "Tart",
1818
private val severity: Logger.Severity = Logger.Severity.Debug,
1919
) : Middleware<S, A, E> {
@@ -46,3 +46,16 @@ open class LoggingMiddleware<S : State, A : Action, E : Event>(
4646
}
4747
}
4848
}
49+
50+
@Suppress("unused")
51+
fun <S : State, A : Action, E : Event> defaultLoggingMiddleware(
52+
logger: Logger = DefaultLogger(),
53+
tag: String = "Tart",
54+
severity: Logger.Severity = Logger.Severity.Debug,
55+
): LoggingMiddleware<S, A, E> {
56+
return object : LoggingMiddleware<S, A, E>(
57+
logger = logger,
58+
tag = tag,
59+
severity = severity,
60+
) {}
61+
}

tart-message/src/commonMain/kotlin/io/yumemi/tart/message/MessageMiddleware.kt

+11
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,14 @@ abstract class MessageMiddleware<S : State, A : Action, E : Event> : Middleware<
5252
suspend fun send(message: Message) {
5353
MessageHub.send(message = message)
5454
}
55+
56+
@Suppress("unused")
57+
fun <S : State, A : Action, E : Event> messageMiddleware(
58+
receive: suspend (message: Message, dispatch: (action: A) -> Unit) -> Unit,
59+
): Middleware<S, A, E> {
60+
return object : MessageMiddleware<S, A, E>() {
61+
override suspend fun receive(message: Message, dispatch: (action: A) -> Unit) {
62+
receive.invoke(message, dispatch)
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)