Skip to content

Commit 512682b

Browse files
committed
reintroduce separate activity and fragment bind(), because LifecycleOwner receiver is pretty broad, but leave simplified api for fragment
1 parent c2e51fd commit 512682b

File tree

3 files changed

+108
-68
lines changed

3 files changed

+108
-68
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package io.github.solrudev.jetmvi
2+
3+
import androidx.activity.ComponentActivity
4+
import kotlinx.coroutines.Job
5+
import kotlinx.coroutines.flow.Flow
6+
7+
/**
8+
* Launches lifecycle-aware collection of the [Flow] of [JetState] which will re-render view each time new state is
9+
* emitted. It also accounts for [JetView.trackedState].
10+
*
11+
* Binding by using [jetViewModels] (or [activityJetViewModels]) delegate is preferred to manually calling this
12+
* function, because it automatically manages binding lifecycle. Don't use this function together with aforementioned
13+
* delegates to avoid duplicate binding.
14+
*
15+
* This function must be called in activity's `onCreate()`.
16+
*
17+
* @param jetView a view to bind UI state flow to, parent [JetView] for [derivedViews].
18+
* @param derivedViews views derived from [jetView]. Created with [derivedView] delegate.
19+
* @return [Job] of the flow collection.
20+
*/
21+
public fun <S : JetState, V> Flow<S>.bind(jetView: V, vararg derivedViews: JetView<S>): Job
22+
where V : JetView<S>,
23+
V : ComponentActivity {
24+
return bind(jetView, derivedViews, jetView.lifecycle)
25+
}
26+
27+
/**
28+
* Launches lifecycle-aware collection of the [Flow] of [JetState] which will re-render _only_ derived views each time
29+
* new state is emitted. It also accounts for [JetView.trackedState].
30+
*
31+
* Binding by using [jetViewModels] (or [activityJetViewModels]) delegate is preferred to manually calling this
32+
* function, because it automatically manages binding lifecycle. Don't use this function together with aforementioned
33+
* delegates to avoid duplicate binding.
34+
*
35+
* This function must be called in activity's `onCreate()`.
36+
*
37+
* @param parentView parent [JetView].
38+
* @param derivedViews views derived from [parentView]. Created with [derivedView] delegate.
39+
* @return [Job] of the flow collection.
40+
*/
41+
public fun <S : JetState, V> Flow<S>.bindDerived(parentView: V, vararg derivedViews: JetView<S>): Job
42+
where V : JetView<S>,
43+
V : ComponentActivity {
44+
return bind(parentView, derivedViews, parentView.lifecycle, bindParent = false)
45+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package io.github.solrudev.jetmvi
2+
3+
import androidx.fragment.app.Fragment
4+
import kotlinx.coroutines.Job
5+
import kotlinx.coroutines.flow.Flow
6+
7+
/**
8+
* Launches lifecycle-aware collection of the [Flow] of [JetState] which will re-render view each time new state is
9+
* emitted. It also accounts for [JetView.trackedState].
10+
*
11+
* Binding by using [jetViewModels] (or [activityJetViewModels]) delegate is preferred to manually calling this
12+
* function, because it automatically manages binding lifecycle. Don't use this function together with aforementioned
13+
* delegates to avoid duplicate binding.
14+
*
15+
* **For fragment with a view:**
16+
*
17+
* If fragment has a view, this function must be called in fragment's `onViewCreated()`.
18+
*
19+
* **For fragment without a view:**
20+
*
21+
* If fragment doesn't have a view, this function must be called in fragment's `onCreate()`.
22+
*
23+
* @param jetView a view to bind UI state flow to, parent [JetView] for [derivedViews].
24+
* @param derivedViews views derived from [jetView]. Created with [derivedView] delegate.
25+
* @return [Job] of the flow collection.
26+
*/
27+
public fun <S : JetState, V> Flow<S>.bind(jetView: V, vararg derivedViews: JetView<S>): Job
28+
where V : JetView<S>,
29+
V : Fragment {
30+
if (jetView.view != null) {
31+
return bind(jetView, derivedViews, jetView.viewLifecycleOwner.lifecycle)
32+
}
33+
return bind(jetView, derivedViews, jetView.lifecycle)
34+
}
35+
36+
/**
37+
* Launches lifecycle-aware collection of the [Flow] of [JetState] which will re-render _only_ derived views each time
38+
* new state is emitted. It also accounts for [JetView.trackedState].
39+
*
40+
* Binding by using [jetViewModels] (or [activityJetViewModels]) delegate is preferred to manually calling this
41+
* function, because it automatically manages binding lifecycle. Don't use this function together with aforementioned
42+
* delegates to avoid duplicate binding.
43+
*
44+
* **For fragment with a view:**
45+
*
46+
* If fragment has a view, this function must be called in fragment's `onViewCreated()`.
47+
*
48+
* **For fragment without a view:**
49+
*
50+
* If fragment doesn't have a view, this function must be called in fragment's `onCreate()`.
51+
*
52+
* @param jetView a view to bind UI state flow to, parent [JetView] for [derivedViews].
53+
* @param derivedViews views derived from [jetView]. Created with [derivedView] delegate.
54+
* @return [Job] of the flow collection.
55+
*/
56+
public fun <S : JetState, V> Flow<S>.bindDerived(jetView: V, vararg derivedViews: JetView<S>): Job
57+
where V : JetView<S>,
58+
V : Fragment {
59+
if (jetView.view != null) {
60+
return bind(jetView, derivedViews, jetView.viewLifecycleOwner.lifecycle, bindParent = false)
61+
}
62+
return bind(jetView, derivedViews, jetView.lifecycle, bindParent = false)
63+
}

jetmvi/src/main/kotlin/io/github/solrudev/jetmvi/BindJetView.kt

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package io.github.solrudev.jetmvi
22

3-
import androidx.fragment.app.Fragment
43
import androidx.lifecycle.Lifecycle
5-
import androidx.lifecycle.LifecycleOwner
64
import androidx.lifecycle.coroutineScope
75
import androidx.lifecycle.repeatOnLifecycle
86
import kotlinx.coroutines.Job
@@ -12,72 +10,6 @@ import kotlinx.coroutines.flow.launchIn
1210
import kotlinx.coroutines.flow.onEach
1311
import kotlinx.coroutines.launch
1412

15-
/**
16-
* Launches lifecycle-aware collection of the [Flow] of [JetState] which will re-render view each time new state is
17-
* emitted. It also accounts for [JetView.trackedState].
18-
*
19-
* Binding by using [jetViewModels] (or [activityJetViewModels]) delegate is preferred to manually calling this
20-
* function, because it automatically manages binding lifecycle. Don't use this function together with aforementioned
21-
* delegates to avoid duplicate binding.
22-
*
23-
* **For activity:**
24-
*
25-
* This function must be called in activity's `onCreate()`.
26-
*
27-
* **For fragment with a view:**
28-
*
29-
* If fragment has a view, this function must be called in fragment's `onViewCreated()`.
30-
*
31-
* **For fragment without a view:**
32-
*
33-
* If fragment doesn't have a view, this function must be called in fragment's `onCreate()`.
34-
*
35-
* @param jetView a view to bind UI state flow to, parent [JetView] for [derivedViews].
36-
* @param derivedViews views derived from [jetView]. Created with [derivedView] delegate.
37-
* @return [Job] of the flow collection.
38-
*/
39-
public fun <S : JetState, V> Flow<S>.bind(jetView: V, vararg derivedViews: JetView<S>): Job
40-
where V : JetView<S>,
41-
V : LifecycleOwner {
42-
if (jetView is Fragment && jetView.view != null) {
43-
return bind(jetView, derivedViews, jetView.viewLifecycleOwner.lifecycle)
44-
}
45-
return bind(jetView, derivedViews, jetView.lifecycle)
46-
}
47-
48-
/**
49-
* Launches lifecycle-aware collection of the [Flow] of [JetState] which will re-render _only_ derived views each time
50-
* new state is emitted. It also accounts for [JetView.trackedState].
51-
*
52-
* Binding by using [jetViewModels] (or [activityJetViewModels]) delegate is preferred to manually calling this
53-
* function, because it automatically manages binding lifecycle. Don't use this function together with aforementioned
54-
* delegates to avoid duplicate binding.
55-
*
56-
* **For activity:**
57-
*
58-
* This function must be called in activity's `onCreate()`.
59-
*
60-
* **For fragment with a view:**
61-
*
62-
* If fragment has a view, this function must be called in fragment's `onViewCreated()`.
63-
*
64-
* **For fragment without a view:**
65-
*
66-
* If fragment doesn't have a view, this function must be called in fragment's `onCreate()`.
67-
*
68-
* @param parentView parent [JetView].
69-
* @param derivedViews views derived from [parentView]. Created with [derivedView] delegate.
70-
* @return [Job] of the flow collection.
71-
*/
72-
public fun <S : JetState, V> Flow<S>.bindDerived(parentView: V, vararg derivedViews: JetView<S>): Job
73-
where V : JetView<S>,
74-
V : LifecycleOwner {
75-
if (parentView is Fragment && parentView.view != null) {
76-
return bind(parentView, derivedViews, parentView.viewLifecycleOwner.lifecycle, bindParent = false)
77-
}
78-
return bind(parentView, derivedViews, parentView.lifecycle, bindParent = false)
79-
}
80-
8113
/**
8214
* Launches views rendering of this [JetState] flow with the given [lifecycle].
8315
*/

0 commit comments

Comments
 (0)