This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
GlobalScope.launch { | |
val payments = fetchLastPayments() | |
processPayments(payments) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
suspend fun doSomeHeavyOperation() { | |
// do some non blocking operation | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RxBus private constructor() { | |
private val subject = PublishSubject.create<Any>() | |
fun publish(message: Any) { | |
subject.onNext(message) | |
} | |
fun subscribe(action: (Any) -> Unit): Disposable { | |
return subject.subscribe { action(it) } |