Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MainActivity: AppCompatActivity(){
- var mainMessage: Disposable?=null
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- setSupportActionBar(toolbar)
- getSupportActionBar()?.hide();
- view_pager.adapter = TabsAdapter(supportFragmentManager)
- tab_layout.setupWithViewPager(view_pager)
- }
- public override fun onStart() {
- super.onStart()
- mainMessage = MainEventBus.INSTANCE.toObserverable().subscribe({
- println("MESSAGE SENT FROM MAINACTIVITY")
- println(it)
- })
- }
- public override fun onStop() {
- mainMessage?.dispose()
- super.onStop()
- }
- override fun onResume() {
- super.onResume()
- val testEvent = MainEventBus.Companion.KeyValue(key = "testkey", value = "keyvalue")
- MainEventBus.INSTANCE.send(testEvent)
- weatherRequests()
- }
- private fun weatherRequests(){
- "http://api.openweathermap.org/data/2.5/weather?q=Austin,us&appid=abd532567a4d169ee04f6597584ba084".httpGet().responseJson { _, _, result ->
- //do something with response
- result.fold(success = { json ->
- println("success from json request to weatherAPI")
- val weatherArray = result.get().obj().getJSONArray("weather").get(0) as JSONObject
- println("value of main in weatherArray:" + weatherArray["main"])
- println("value of description in weatherArray:" + weatherArray["description"])
- println("value of icon in weatherArray: " + weatherArray["icon"])
- val mainWeatherEvent = MainEventBus.Companion.KeyValue(key="mainWeather", value = weatherArray["main"].toString())
- val descriptionWeatherEvent = MainEventBus.Companion.KeyValue(key="descriptionWeather", value = weatherArray["description"].toString())
- val weatherurl = "http://openweathermap.org/img/w/"+weatherArray["icon"].toString()+".png"
- val iconWeatherEvent = MainEventBus.Companion.KeyValue(key="iconWeather", value = weatherurl)
- println("BEFORE SENDING EVENTBUS ITEMS")
- MainEventBus.INSTANCE.send(mainWeatherEvent)
- MainEventBus.INSTANCE.send(descriptionWeatherEvent)
- MainEventBus.INSTANCE.send(iconWeatherEvent)
- println("AFTER SENDING EVENTBUS ITEMS")
- }, failure = { error ->
- Log.e("qdp error", error.toString())
- })
- }
- Log.d("aftertag","after fuel Request")
- }
- }
- I/System.out: INSIDE EVENTBUS
- I/System.out: VALUE OF KEYVALUE
- I/System.out: KeyValue(key=testkey, value=keyvalue)
- I/System.out: MESSAGE SENT FROM MAINACTIVITY
- I/System.out: KeyValue(key=testkey, value=keyvalue)
- enum class MainEventBus {
- INSTANCE;
- companion object {
- data class KeyValue(var key: String, var value: String)
- }
- private val bus = PublishSubject.create<KeyValue>() // the actual publisher handling all of the events
- fun send(keyValue: KeyValue) {
- println("INSIDE EVENTBUS")
- println("VALUE OF KEYVALUE")
- println(keyValue)
- bus.onNext(keyValue)
- // bus.onNext({key, message}) // the message being sent to all subscribers
- }
- fun toObserverable(): Observable<KeyValue> {
- return bus // return the publisher itself as an observable to subscribe to
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement