ReactiveCircus / FlowBinding

Kotlin Coroutines Flow binding APIs for Android's platform and unbundled UI widgets, inspired by RxBinding.
https://reactivecircus.github.io/FlowBinding/
Apache License 2.0
901 stars 42 forks source link

Does FlowBinding already supports clicks inside dialog #116

Open mochadwi opened 3 years ago

mochadwi commented 3 years ago

is it currently supported? *I'm unable to find the related docs in readme, might've missed that

currently we're manually using callbackFlow to achieve this

ychescale9 commented 3 years ago

We don't have dialog bindings in the flowbinding-android library. We do however have bindings for MaterialDatePickers:

// MaterialDatePicker
fun <S> MaterialDatePicker<S>.cancels(): Flow<Unit>
fun <S> MaterialDatePicker<S>.dismisses(): Flow<Unit>
fun <S> MaterialDatePicker<S>.negativeButtonClicks(): Flow<Unit>
fun <S> MaterialDatePicker<S>.positiveButtonClicks(): Flow<S>

Is this something you like to see in the platform bindings (flowbinding-android)?

mochadwi commented 3 years ago

currently we're using BottomSheetDialogFragment to be precise but the MaterialDatePicker is interesting, might take a look at your implementation code for our references~

mochadwi commented 3 years ago

FYI, previously we're trying with this approach:

private val dialogBinding by viewBinding(DialogInboxBinding::inflate) // using https://github.com/kirich1409/ViewBindingPropertyDelegate

private val updateStateIntent: Flow<UpdateInboxIntent>
        get() = dialogBinding.btnUpdate.clicks {} // View.clicks from FlowBinding

EDITED: but the clicks event never triggers it, unless we're using manual approach something like this:

// OurActivity.kt
typealias UpdateClickListener = (Param1) -> Unit
var updateClickListener: UpdateClickListener? = null

private val updateStateIntent: Flow<UpdateInboxIntent>
        get() = updatesClicks.map { param1 -> }

// somewhere in the code

dialogBinding.btnUpdate.setOnClickListener {
  updateClickListener?.invoke(param1) // manually calls this
}
// extensions.kt
val OurActivity.updatesClicks: Flow<Param1>
    get() = callbackFlow {
        val listener: UpdateClickListener = { param1 ->
            safeOffer(param1)
            Unit
        }
        updateClickListener = listener
        awaitClose { updateClickListener = null }
    }.conflate().debounce(200)

@ychescale9

ychescale9 commented 3 years ago

I've never needed this myself but happy to accept a PR if you have a generic implementation 😃

mochadwi commented 3 years ago

We've edited the previous comments with codes example, or do u have any suggestion for simpler approach rather than manually creating callbackFlow or are we missing something?

ychescale9 commented 3 years ago

How are you collection the flow? do you have a single collector in your activity or do you have another one in your BottomSheetDialogFragment?

Each fragment and activity has its own LifecycleScope so sharing can be tricky. In this case does it make sense to communicate between your dialog and the host activity (assuming that's what you're trying to achieve) with a SharedFlow which is injected to both?

mochadwi commented 3 years ago

before typing comment reply, is it okay to continue discussion here or should I use discussion in this repo? @ychescale9

ychescale9 commented 3 years ago

Sure let's try out discussion!