Skip to content
This repository has been archived by the owner on Aug 26, 2024. It is now read-only.
/ DiMvRx Public archive
forked from airbnb/mavericks

DiMvRx: Make MvRx easier to work with Dagger2

License

Notifications You must be signed in to change notification settings

luozejiaqun/DiMvRx

 
 

Repository files navigation

中文文档

DiMvRx

Simplify MvRx, make it quite convenient to work with Dagger2.
Changes include:

  1. Remove all the reflections of MvRx when debugMode set to false.
  2. Don NOT need to extend from BaseMvRxActivity and BaseMvRxFragment.
  3. You can provide your own ViewModelProvider.Factory when create ViewModels. With the help of Dagger2, you can implement ViewModelProvider.Factory quite simple.
  4. You can share ViewModel through parent Fragment which might be helpful when you use ViewPager.
  5. No need to implement MvRxViewModelFactory in ViewModel.
  6. No need to use State's secondary constructor to initialize the state.

This is what it looks like:

data class HelloWorldState(val title: String = "Hello World") : MvRxState

class HelloWorldViewModel() : MyBaseMvRxViewModel<HelloWorldState>(HelloWorldState()) {
    fun getMoreExcited() = setState { copy(title = "$title!") }
}

class HelloWorldFragment : BaseFragment() {
    private val viewModel: HelloWorldViewModel by fragmentViewModel()

    override fun EpoxyController.buildModels() = withState(viewModel) { state ->
        header {
            title(state.title)
        }
        basicRow { 
            onClick { viewModel.getMoreExcited() }
        }
    }
}

abstract class BaseFragment : Fragment(), MvRxView, ViewModelFactoryOwner {
    //ViewModelFactory is a class implements ViewModelProvider.Factory
    @Inject override lateinit var viewModelFactory: ViewModelFactory
    
    override fun onCreate(savedInstanceState: Bundle?) {
        //inject viewModelFactory with dagger
        appComponent.inject(this)
        super.onCreate(savedInstanceState)
    }
}

See the sample for more infomation.

Installation

Just copy the code and make it your own way. This library is just a sample to show how I integate MvRx with Dagger2. You can find your way.

About

DiMvRx: Make MvRx easier to work with Dagger2

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Kotlin 99.9%
  • Java 0.1%