Skip to content

Commit

Permalink
reintroduce separate activity and fragment bind(), because LifecycleO…
Browse files Browse the repository at this point in the history
…wner receiver is pretty broad, but leave simplified api for fragment
  • Loading branch information
solrudev committed Nov 10, 2022
1 parent c2e51fd commit 512682b
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 68 deletions.
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)
}
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)
}
68 changes: 0 additions & 68 deletions jetmvi/src/main/kotlin/io/github/solrudev/jetmvi/BindJetView.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package io.github.solrudev.jetmvi

import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.coroutineScope
import androidx.lifecycle.repeatOnLifecycle
import kotlinx.coroutines.Job
Expand All @@ -12,72 +10,6 @@ import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch

/**
* 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 activity:**
*
* This function must be called in activity's `onCreate()`.
*
* **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 : LifecycleOwner {
if (jetView is Fragment && 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 activity:**
*
* This function must be called in activity's `onCreate()`.
*
* **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 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 : LifecycleOwner {
if (parentView is Fragment && parentView.view != null) {
return bind(parentView, derivedViews, parentView.viewLifecycleOwner.lifecycle, bindParent = false)
}
return bind(parentView, derivedViews, parentView.lifecycle, bindParent = false)
}

/**
* Launches views rendering of this [JetState] flow with the given [lifecycle].
*/
Expand Down

0 comments on commit 512682b

Please sign in to comment.