FlowBinding icon indicating copy to clipboard operation
FlowBinding copied to clipboard

Does FlowBinding already supports clicks inside dialog

Open mochadwi opened this issue 5 years ago • 8 comments

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

mochadwi avatar Dec 11 '20 02:12 mochadwi

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)?

ychescale9 avatar Dec 11 '20 02:12 ychescale9

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 avatar Dec 11 '20 03:12 mochadwi

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

mochadwi avatar Dec 11 '20 03:12 mochadwi

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

ychescale9 avatar Dec 11 '20 03:12 ychescale9

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?

mochadwi avatar Dec 11 '20 03:12 mochadwi

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?

ychescale9 avatar Dec 11 '20 05:12 ychescale9

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

mochadwi avatar Dec 11 '20 06:12 mochadwi

Sure let's try out discussion!

ychescale9 avatar Dec 11 '20 07:12 ychescale9