Skip to content

Commit

Permalink
Allow to bindOut a flow
Browse files Browse the repository at this point in the history
  • Loading branch information
AChep committed Feb 27, 2021
1 parent a77a92f commit 918226c
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 6 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,16 @@ val binding = viewLifecycleOwner.bindIn(flow) {
button.setOnCheckedChangeListener { _, isChecked ->
observer(isChecked)
}
fun() {
button.setOnCheckedChangeListener(null)
}
},
pipe = {
flow.value = it
},
)
```
for some of the views there's already a `bindOut` function
for some of the views there's already a `bindOut` function.
```kotlin
val flow = MutableStateFlow<Boolean>(false)
val binding = viewLifecycleOwner.bindIn(flow) {
Expand All @@ -92,6 +95,16 @@ val binding = viewLifecycleOwner.bindIn(flow) {
}
```

You may also want to pass `Flow` to the `bindOut` function.
```kotlin
val flow = MutableStateFlow<Boolean>(false)
val binding = viewLifecycleOwner.bindIn(flow) {
button.isChecked = it
}.bindOut(button.checked()) {
flow.value = it
}
```

Report a bug or request a feature
----------------
Before creating a new issue please make sure that same or similar issue is not already created by checking [open issues][2] and [closed issues][3] *(please note that there might be multiple pages)*. If your issue is already there, don't create a new one, but leave a comment under already existing one.
Expand Down
2 changes: 2 additions & 0 deletions library/src/main/java/com/artemchep/bindin/BindIn.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ private inline fun <T> LifecycleOwner._bindIn(
},
)
return InBinding(
lifecycleOwner = this,
minimumLifecycleState = minimumLifecycleState,
data = inBindingData,
unbind = registration,
)
Expand Down
3 changes: 3 additions & 0 deletions library/src/main/java/com/artemchep/bindin/BindInLiveData.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.artemchep.bindin

import androidx.annotation.UiThread
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
Expand All @@ -17,6 +18,8 @@ fun <T> LifecycleOwner.bindIn(
}

val inBinding = InBinding(
lifecycleOwner = this,
minimumLifecycleState = Lifecycle.State.STARTED,
data = inBindingData,
) {
liveData.removeObserver(observer)
Expand Down
46 changes: 43 additions & 3 deletions library/src/main/java/com/artemchep/bindin/BindOut.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,56 @@
package com.artemchep.bindin

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect

fun <T> InBinding<T>.bindOut(
observe: (callback: (T) -> Unit) -> Unit,
observe: (callback: (T) -> Unit) -> () -> Unit,
/** A pipe to pass data to... */
pipe: (T) -> Unit,
) {
observe { value ->
): InBinding<T> {
val unbindObserve = observe { value ->
if (data.eq(value)) {
return@observe
}

data.outValue = value
pipe(value)
}
return copy(
unbind = {
unbind()
unbindObserve()
}
)
}

fun <T> InBinding<T>.bindOut(
flow: Flow<T>,
/** A pipe to pass data to... */
pipe: (T) -> Unit,
): InBinding<T> {
val unbindBlock = lifecycleOwner.bindBlock(minimumLifecycleState = minimumLifecycleState) {
flow.collect {
_pipe(it, pipe)
}
}
return copy(
unbind = {
unbind()
unbindBlock()
}
)
}

private fun <T> InBinding<T>._pipe(
value: T,
/** A pipe to pass data to... */
pipe: (T) -> Unit,
) {
if (data.eq(value)) {
return
}

data.outValue = value
pipe(value)
}
7 changes: 6 additions & 1 deletion library/src/main/java/com/artemchep/bindin/Model.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.artemchep.bindin

class InBinding<T>(
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner

data class InBinding<T>(
val lifecycleOwner: LifecycleOwner,
val minimumLifecycleState: Lifecycle.State,
val data: InBindingData<T>,
val unbind: () -> Unit,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ fun InBinding<Boolean>.bindOut(
view.setOnCheckedChangeListener { _, isChecked ->
observer(isChecked)
}

fun() {
view.setOnCheckedChangeListener(null)
}
},
pipe = pipe
)
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ fun InBinding<String>.bindOut(
pipe: (String) -> Unit,
) = bindOut(
observe = { observer ->
editText.addTextChangedListener {
val listener = editText.addTextChangedListener {
observer(it?.toString().orEmpty())
}

fun() {
editText.removeTextChangedListener(listener)
}
},
pipe = pipe
)

0 comments on commit 918226c

Please sign in to comment.