ScopedViewModel

JitPack version number

Android library to provide ViewModels based on a scope extending the functionality of Fragment KTX.

View the API documentation.

Including the library

Add jitpack.io to your repositories in your projects build.gradle file.

allprojects {
    repositories {
	    ...
    maven { url 'https://jitpack.io' }
    }
}

Add ScopedViewModel to your dependencies of your module.

dependencies {
  implementation 'uk.co.conjure:ScopedViewModel:TAG'
}

Code Sample

Consider the following ViewModel which you want to share between a Fragment and it’s child Fragment

interface ChildViewModel {
    val sharedData: LiveData<Int>
}

class ParentViewModel : ViewModel(), ChildViewModel {
    override val sharedData: MutableLiveData<Int> = MutableLiveData(120)
}

ParentFragment

Create a scoped ViewModel

class ParentFragment : Fragment() {

  private val parentViewModel: ParentViewModel by createViewModelScope()
  
  //...
}

ChildFragment

Retrieve the ViewModel from the parent Fragment

class ChildFragment : Fragment() {

  private val childViewModel: ChildViewModel by scopedInterface()
  
  //...
}
Conjure Logo

Copyright ©2023 Conjure Ltd.
All product names and trademarks are the property of their respective owners. Reg. England & Wales 6897070.