Skip to content

Commit eccb001

Browse files
committed
Added onReactiveStateAttachedTo(parent)
1 parent bf88dd5 commit eccb001

File tree

8 files changed

+26
-5
lines changed

8 files changed

+26
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 5.13.0
4+
5+
* Added `OnReactiveStateAttachedTo.onReactiveStateAttachedTo(parent)`, so the ViewModel can finish its initialization and do additional checks against the parent (which can be the UI or a parent ViewModel).
6+
37
## 5.12.0
48

59
* Fixed `by childReactiveState` to call `onReactiveStateAttached(child)` (the arguments were inverted).

reactivestate-compose/src/composeMain/kotlin/com/ensody/reactivestate/compose/ViewModelExt.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import androidx.lifecycle.viewmodel.compose.viewModel
1414
import com.ensody.reactivestate.ErrorEvents
1515
import com.ensody.reactivestate.ExperimentalReactiveStateApi
1616
import com.ensody.reactivestate.InMemoryStateFlowStore
17+
import com.ensody.reactivestate.OnReactiveStateAttached
18+
import com.ensody.reactivestate.OnReactiveStateAttachedTo
1719
import com.ensody.reactivestate.ReactiveState
1820
import com.ensody.reactivestate.ReactiveStateContext
1921
import com.ensody.reactivestate.ReactiveViewModel
@@ -55,6 +57,8 @@ public inline fun <reified E : ErrorEvents, reified VM : ReactiveState<E>> E.rea
5557
observeLoadingEffect(viewModel)
5658
LaunchedEffect(this, viewModel.eventNotifier) {
5759
viewModel.eventNotifier.handleEvents(this@reactiveState)
60+
(this@reactiveState as? OnReactiveStateAttached)?.onReactiveStateAttached(viewModel)
61+
(viewModel as? OnReactiveStateAttachedTo)?.onReactiveStateAttachedTo(this@reactiveState)
5862
}
5963
return viewModel
6064
}

reactivestate-core/src/commonMain/kotlin/com/ensody/reactivestate/OnReactiveStateAttached.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ package com.ensody.reactivestate
77
* method in every UI screen (e.g. on Android you could have a `BaseFragment` implementing this interface).
88
*/
99
public interface OnReactiveStateAttached {
10-
public fun onReactiveStateAttached(reactiveState: ReactiveState<out ErrorEvents>)
10+
public fun onReactiveStateAttached(child: ReactiveState<out ErrorEvents>)
1111
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.ensody.reactivestate
2+
3+
/**
4+
* Implement this interface to get notified when your [ReactiveState] is attached its parent (screen or ReactiveState).
5+
*
6+
* This can be useful e.g. to check if the parent
7+
*/
8+
public interface OnReactiveStateAttachedTo {
9+
public fun onReactiveStateAttachedTo(parent: Any)
10+
}

reactivestate-core/src/commonMain/kotlin/com/ensody/reactivestate/ReactiveState.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public fun <E : ErrorEvents, P : ReactiveState<out E>, RS : ReactiveState<E>> P.
105105
eventNotifier.emitAll(child.eventNotifier)
106106
}
107107
(this as? OnReactiveStateAttached)?.onReactiveStateAttached(child)
108+
(child as? OnReactiveStateAttachedTo)?.onReactiveStateAttachedTo(this)
108109
return WrapperProperty(child)
109110
}
110111

reactivestate-core/src/commonMain/kotlin/com/ensody/reactivestate/ReactiveViewModel.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ package com.ensody.reactivestate
1313
public abstract class ReactiveViewModel(
1414
/** This is `open` to allow overriding with a more specific type. */
1515
public open val context: ReactiveViewModelContext,
16-
) : BaseReactiveState<ErrorEvents>(context.scope) {
17-
init {
16+
) : BaseReactiveState<ErrorEvents>(context.scope), OnReactiveStateAttachedTo {
17+
override fun onReactiveStateAttachedTo(parent: Any) {
1818
context.preInit.trigger(this)
1919
}
2020
}

reactivestate/src/androidMain/kotlin/com/ensody/reactivestate/android/ReactiveViewModelExt.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import androidx.lifecycle.viewModelScope
1010
import com.ensody.reactivestate.ErrorEvents
1111
import com.ensody.reactivestate.NamespacedStateFlowStore
1212
import com.ensody.reactivestate.OnReactiveStateAttached
13+
import com.ensody.reactivestate.OnReactiveStateAttachedTo
1314
import com.ensody.reactivestate.ReactiveState
1415
import com.ensody.reactivestate.StateFlowStore
1516
import kotlinx.coroutines.CoroutineScope
@@ -48,6 +49,7 @@ public fun <E : ErrorEvents> Lazy<ReactiveState<E>>.attachLazyReactiveState(
4849
}
4950
owner.launchOnceStateAtLeast(Lifecycle.State.CREATED) {
5051
(owner as? OnReactiveStateAttached)?.onReactiveStateAttached(value)
52+
(value as? OnReactiveStateAttachedTo)?.onReactiveStateAttachedTo(owner)
5153
}
5254
}
5355

reactivestate/src/androidUnitTest/kotlin/com/ensody/reactivestate/android/TestFragment.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ internal class TestFragment : Fragment(), ErrorEvents, OnReactiveStateAttached {
4848
errors.add(error)
4949
}
5050

51-
override fun onReactiveStateAttached(reactiveState: ReactiveState<out ErrorEvents>) {
52-
attachedReactiveStates.add(reactiveState)
51+
override fun onReactiveStateAttached(child: ReactiveState<out ErrorEvents>) {
52+
attachedReactiveStates.add(child)
5353
}
5454
}

0 commit comments

Comments
 (0)