Skip to content

JaredDoge/LiveDataBus

Repository files navigation

LiveDataBus

增強UnPeek-LiveData,將其包裝成觀察者模式的事件總線

Getting started

  1. Add it in your root build.gradle at the end of repositories:
     allprojects {
       repositories {
         // ...
         maven { url 'https://jitpack.io' }
       }
     }
  2. Add the dependency
    dependencies {
             implementation 'com.github.JaredDoge:LiveDataBus:{latest_version}'
      }

Usage

observe

LiveDataBus.get<String>(key).observe(lifecycleOwner){
            print(it)
}

當你的lifecycleOwner使用fragment時,可以調用擴充方法toViewLife()將其轉為viewLifecycleOwner

setValue

LiveDataBus.get<String>(key).setValue(msg)

SingleLiveData

當然,你可以單獨使用SingleLiveData,以確保訊息來源是可靠的。

例如在viewModel中:

class MainViewModel : ViewModel() {

    val showToast: SingleLiveData<String> = SingleLiveData<String>()
    
    fun show(){
        showToast.setValue("testMsg")
    }
}

Activity

mainViewModel.showToast.observe(this){
    //確保訊息來源是來自MainViewModel的showToast
    Toast.makeText(this, it, Toast.LENGTH_SHORT).show()
}  

Dynamic Proxy

除了使用.get(key)取得總線外,還可以使用.of()的方式,避免key打錯的問題。 首先先建立一個interface

interface

interface SampleEvent {

    //Int為總線的資料型態      
    fun message(): SingleLiveData<Int>
    
    //可以建立多個總線
    //fun message2(): SingleLiveData<String>
    //fun message3(): StringLvieData<MyJavaBean>
    //....
}

因為使用動態代理的關係,所以必須是interface

observe

LiveDataBus.of<SampleEvent>().message().observe(this){
  //it is Int
}

setValue

LiveDataBus.of<SampleEvent>().message().setValue(123)

About

增強UnPeek-LiveData,將其包裝成觀察者模式的事件總線

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages