Skip to content

Instantly share code, notes, and snippets.

View wellingtoncosta's full-sized avatar

Wellington Costa wellingtoncosta

View GitHub Profile
suspend fun doSomeHeavyOperation() {
// do some non blocking operation
}
fun main() {
GlobalScope.launch {
val payments = fetchLastPayments()
processPayments(payments)
}
}
class FragmentTwo : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_fragment_two, container, false).apply {
this.back_to_fragment_one.setOnClickListener {
RxBus.instance.publish(MAIN_ACTIVITY_SUBJECT, FRAGMENT_ONE_TAG)
}
class FragmentOne : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_fragment_one, container, false).apply {
this.go_to_fragment_two.setOnClickListener {
RxBus.instance.publish(MAIN_ACTIVITY_SUBJECT, FRAGMENT_TWO_TAG)
}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
observeEvents()
RxBus.instance.publish(MAIN_ACTIVITY_SUBJECT, FRAGMENT_ONE_TAG)
}
private fun observeEvents() {
class RxBus private constructor() {
private val subjects = HashMap<String, PublishSubject<Any>>()
private val subscriptions = HashMap<Any, CompositeDisposable>()
private fun subject(key: String) = subjects.getOrPut(key) { PublishSubject.create() }
private fun disposable(key: Any) = subscriptions.getOrPut(key) { CompositeDisposable() }
fun subscribe(subject: String, owner: Any, action: (Any) -> Unit) {
class FragmentOne : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_fragment_one, container, false).apply {
this.go_to_fragment_two.setOnClickListener {
RxBus.instance.publish(FRAGMENT_TWO_TAG)
}
class FragmentTwo : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_fragment_two, container, false).apply {
this.back_to_fragment_one.setOnClickListener {
RxBus.instance.publish(FRAGMENT_ONE_TAG)
}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
observeEvents()
RxBus.instance.publish(FRAGMENT_ONE_TAG)
}
private fun observeEvents() {
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.FragmentTwo">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar_fragment_two"