Skip to content

Convenient extensions built on top of the Android ViewModel Extensions library to enable easy ViewModel creation and Dependency Injection.

License

Notifications You must be signed in to change notification settings

robotsquidward/lazyviewmodels

Repository files navigation

😴 Lazy ViewModels

Convenient extensions built on top of the Android ViewModel Extensions library to enable easy ViewModel creation and Dependency Injection.

🛠️ Usage

These extensions create an Android ViewModel scoped to an Activity.

Basic Usage

When in an Activity (ComponentActivity):

private val viewModel: MyViewModel by lazyViewModels {
    MyViewModel(repo = MyRepository())  
}

When in a Fragment, create an Activity-scoped ViewModel with:

private val viewModel: MyViewModel by lazyActivityViewModels {
    MyViewModel(repo = MyRepository())  
}

SavedStateHandle

To create a ViewModel that utilizes SavedStateHandle via the constructor, use the Saved State versions of these extensions.

When in an Activity (ComponentActivity):

private val viewModel: MyViewModel by lazySavedStateViewModels { handle: SavedStateHandle ->
    MyViewModel(
        repo = MyRepository(),
        savedStateHandle = handle
    )
}

When in a Fragment, create an Activity-scoped View Model with:

private val viewModel: MyViewModel by lazySavedStateActivityViewModels { handle: SavedStateHandle ->
    MyViewModel(
        repo = MyRepository(),
        savedStateHandle = handle
    )
}

Additionally, if you prefer to customize the SavedStateRegistryOwner that provides your SavedStateHandle, you can pass one in:

private val viewModel: MyViewModel by lazySavedStateActivityViewModels(this.requireActivity()) { handle: SavedStateHandle ->
    MyViewModel(
        repo = MyRepository(),
        savedStateHandle = handle
    )
}

Check out my post on Android ViewModel Dependency Injection with extensions to learn more.

📦 Install

Packages are hosted in GitHub Packages. Click here for Lazy ViewModels packages.

GitHub Packages requires you to authenticate to GitHub to install packages. You can see an example setting up auth for this repo in the root build.gradle. In your allProjects.repositories block

maven {
    name = "GitHubPackages"
    url = uri("https://maven.pkg.github.com/robotsquidward/lazyviewmodels")
    credentials {
        username = 'your username'
        password = 'your personal access token'
    }
}

Generate a personal access token with the permission for Reading from GitHub Packages and include it with your GitHub username. Then, include the dependency as normal

dependencies {
    implementation 'dev.ajkueterman:lazyviewmodels:<version>'
}

📬 Contact

This project is solely maintained by AJ (robotsquidward).

If you need to raise an issue or question about this library, please create an issue and tag it with a relevant label.

✏️ Contributing

To get started, please fork the repo and checkout a new branch. You can then build the library code locally with the Gradle wrapper

./gradlew :lazyviewmodels:build

The library code is in the /lazyviewmodels directory. Feel free to commit code to the :app wrapper application to support changes to the library.

See more in CONTRIBUTING

⚖️ License

This library is licensed under the MIT License

About

Convenient extensions built on top of the Android ViewModel Extensions library to enable easy ViewModel creation and Dependency Injection.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages