Search code examples
androidkotlinkotlinx.coroutines

Turning listeners into kotlin coroutine channels


I have several functions that I want to use to do pipelines with Channels. The main one is globalLayouts, where I create a Channel from the framework listener:

fun View.globalLayouts(): ReceiveChannel<View> =
    Channel<View>().apply {
        val view = this@globalLayouts

        val listener = ViewTreeObserver.OnGlobalLayoutListener {
            offer(view)
        }

        invokeOnClose {
            viewTreeObserver.removeOnGlobalLayoutListener(listener)
        }

        viewTreeObserver.addOnGlobalLayoutListener(listener)
    }

@UseExperimental(InternalCoroutinesApi::class)
fun <E> ReceiveChannel<E>.distinctUntilChanged(context: CoroutineContext = Dispatchers.Unconfined): ReceiveChannel<E> =
    GlobalScope.produce(context, onCompletion = consumes()) {
        var last: Any? = Any()

        consumeEach {
            if (it != last) {
                send(it)
                last = it
            }
        }
    }

fun View.keyboardVisibility(): ReceiveChannel<KeyboardVisibility> {
    val rect = Rect()

    return globalLayouts()
        .map {
            getWindowVisibleDisplayFrame(rect)

            when (rect.height()) {
                height -> KeyboardVisibility.HIDDEN
                else -> KeyboardVisibility.SHOWN
            }
        }
        .distinctUntilChanged()
}

I have a CoroutineScope called alive:

val ControllerLifecycle.alive: CoroutineScope
    get() {
        val scope = MainScope()

        addLifecycleListener(object : Controller.LifecycleListener() {
            override fun preDestroyView(controller: Controller, view: View) {
                removeLifecycleListener(this)
                scope.cancel()
            }
        })

        return scope
    }

then I do:

alive.launch {
    root.keyboardVisibility().consumeEach {
        appbar.setExpanded(it == KeyboardVisibility.HIDDEN)
    }
}

This code starts working just fine, but I get

kotlinx.coroutines.JobCancellationException: Job was cancelled; job=JobImpl{Cancelled}@811031f

once my alive scope is destroyed. Right after invokeOnClose is called in globalLayouts. What am I doing wrong and how do I debug this?


Solution

  • Figured it out - the code works fine, but

    viewTreeObserver.removeOnGlobalLayoutListener(listener)
    

    is bugged for CoordinatorLayout.