Skip to content

Instantly share code, notes, and snippets.

@dmersiyanov
Last active July 14, 2020 08:45
Show Gist options
  • Save dmersiyanov/0c83e8640726f3996213e002f1e9604b to your computer and use it in GitHub Desktop.
Save dmersiyanov/0c83e8640726f3996213e002f1e9604b to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btnEmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Emit" />
<Button
android:id="@+id/btnConsume1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Consume 1" />
<Button
android:id="@+id/btnConsume2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Consume 2" />
<Button
android:id="@+id/btnClose"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Close channel" />
</LinearLayout>
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.InternalCoroutinesApi
import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.ConflatedBroadcastChannel
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
@ExperimentalCoroutinesApi
@FlowPreview
class MainActivity : AppCompatActivity() {
private val publishChannel = BroadcastChannel<Int>(Channel.BUFFERED)
private val behaviourChannel = ConflatedBroadcastChannel<Int>()
private val tag = MainActivity::class.java.simpleName
private var emitValue = 1
@InternalCoroutinesApi
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnEmit.setOnClickListener {
lifecycleScope.launch(Dispatchers.Default) {
if (!publishChannel.isClosedForSend) {
publishChannel.send(emitValue++)
} else {
log("channel closed")
}
}
}
btnConsume1.setOnClickListener {
lifecycleScope.launch {
consumeFirst()
}
}
btnConsume2.setOnClickListener {
lifecycleScope.launch {
consumeSecond()
}
}
btnClose.setOnClickListener {
publishChannel.close()
}
}
private fun consumeFirst() {
publishChannel
.asFlow()
.onEach {
log("consumeFirst value received:", it)
}
.launchIn(lifecycleScope)
}
private fun consumeSecond() {
publishChannel
.asFlow()
.onEach {
log("consumeSecond value received:", it)
}
.launchIn(this.lifecycleScope)
}
private fun log(message: String, arg: Any? = null) {
Log.e(tag, "$message $arg")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment