-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reintroduce separate activity and fragment bind(), because LifecycleO…
…wner receiver is pretty broad, but leave simplified api for fragment
- Loading branch information
Showing
3 changed files
with
108 additions
and
68 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
jetmvi/src/main/kotlin/io/github/solrudev/jetmvi/BindActivityJetView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package io.github.solrudev.jetmvi | ||
|
||
import androidx.activity.ComponentActivity | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
/** | ||
* Launches lifecycle-aware collection of the [Flow] of [JetState] which will re-render view each time new state is | ||
* emitted. It also accounts for [JetView.trackedState]. | ||
* | ||
* Binding by using [jetViewModels] (or [activityJetViewModels]) delegate is preferred to manually calling this | ||
* function, because it automatically manages binding lifecycle. Don't use this function together with aforementioned | ||
* delegates to avoid duplicate binding. | ||
* | ||
* This function must be called in activity's `onCreate()`. | ||
* | ||
* @param jetView a view to bind UI state flow to, parent [JetView] for [derivedViews]. | ||
* @param derivedViews views derived from [jetView]. Created with [derivedView] delegate. | ||
* @return [Job] of the flow collection. | ||
*/ | ||
public fun <S : JetState, V> Flow<S>.bind(jetView: V, vararg derivedViews: JetView<S>): Job | ||
where V : JetView<S>, | ||
V : ComponentActivity { | ||
return bind(jetView, derivedViews, jetView.lifecycle) | ||
} | ||
|
||
/** | ||
* Launches lifecycle-aware collection of the [Flow] of [JetState] which will re-render _only_ derived views each time | ||
* new state is emitted. It also accounts for [JetView.trackedState]. | ||
* | ||
* Binding by using [jetViewModels] (or [activityJetViewModels]) delegate is preferred to manually calling this | ||
* function, because it automatically manages binding lifecycle. Don't use this function together with aforementioned | ||
* delegates to avoid duplicate binding. | ||
* | ||
* This function must be called in activity's `onCreate()`. | ||
* | ||
* @param parentView parent [JetView]. | ||
* @param derivedViews views derived from [parentView]. Created with [derivedView] delegate. | ||
* @return [Job] of the flow collection. | ||
*/ | ||
public fun <S : JetState, V> Flow<S>.bindDerived(parentView: V, vararg derivedViews: JetView<S>): Job | ||
where V : JetView<S>, | ||
V : ComponentActivity { | ||
return bind(parentView, derivedViews, parentView.lifecycle, bindParent = false) | ||
} |
63 changes: 63 additions & 0 deletions
63
jetmvi/src/main/kotlin/io/github/solrudev/jetmvi/BindFragmentJetView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package io.github.solrudev.jetmvi | ||
|
||
import androidx.fragment.app.Fragment | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
/** | ||
* Launches lifecycle-aware collection of the [Flow] of [JetState] which will re-render view each time new state is | ||
* emitted. It also accounts for [JetView.trackedState]. | ||
* | ||
* Binding by using [jetViewModels] (or [activityJetViewModels]) delegate is preferred to manually calling this | ||
* function, because it automatically manages binding lifecycle. Don't use this function together with aforementioned | ||
* delegates to avoid duplicate binding. | ||
* | ||
* **For fragment with a view:** | ||
* | ||
* If fragment has a view, this function must be called in fragment's `onViewCreated()`. | ||
* | ||
* **For fragment without a view:** | ||
* | ||
* If fragment doesn't have a view, this function must be called in fragment's `onCreate()`. | ||
* | ||
* @param jetView a view to bind UI state flow to, parent [JetView] for [derivedViews]. | ||
* @param derivedViews views derived from [jetView]. Created with [derivedView] delegate. | ||
* @return [Job] of the flow collection. | ||
*/ | ||
public fun <S : JetState, V> Flow<S>.bind(jetView: V, vararg derivedViews: JetView<S>): Job | ||
where V : JetView<S>, | ||
V : Fragment { | ||
if (jetView.view != null) { | ||
return bind(jetView, derivedViews, jetView.viewLifecycleOwner.lifecycle) | ||
} | ||
return bind(jetView, derivedViews, jetView.lifecycle) | ||
} | ||
|
||
/** | ||
* Launches lifecycle-aware collection of the [Flow] of [JetState] which will re-render _only_ derived views each time | ||
* new state is emitted. It also accounts for [JetView.trackedState]. | ||
* | ||
* Binding by using [jetViewModels] (or [activityJetViewModels]) delegate is preferred to manually calling this | ||
* function, because it automatically manages binding lifecycle. Don't use this function together with aforementioned | ||
* delegates to avoid duplicate binding. | ||
* | ||
* **For fragment with a view:** | ||
* | ||
* If fragment has a view, this function must be called in fragment's `onViewCreated()`. | ||
* | ||
* **For fragment without a view:** | ||
* | ||
* If fragment doesn't have a view, this function must be called in fragment's `onCreate()`. | ||
* | ||
* @param jetView a view to bind UI state flow to, parent [JetView] for [derivedViews]. | ||
* @param derivedViews views derived from [jetView]. Created with [derivedView] delegate. | ||
* @return [Job] of the flow collection. | ||
*/ | ||
public fun <S : JetState, V> Flow<S>.bindDerived(jetView: V, vararg derivedViews: JetView<S>): Job | ||
where V : JetView<S>, | ||
V : Fragment { | ||
if (jetView.view != null) { | ||
return bind(jetView, derivedViews, jetView.viewLifecycleOwner.lifecycle, bindParent = false) | ||
} | ||
return bind(jetView, derivedViews, jetView.lifecycle, bindParent = false) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters