Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
FarshadTahmasbi committed May 30, 2021
2 parents 1f54ac1 + f45b32a commit 6330bfb
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
# Vita [ ![Download](https://api.bintray.com/packages/farshad-tmb/Vita/vita/images/download.svg) ](https://bintray.com/farshad-tmb/Vita/vita/_latestVersion)
An extension for android ViewModel
# Vita

![Maven Central](https://img.shields.io/maven-central/v/com.androidisland.arch/vita.svg?label=Maven%20Central)
![Apache License, Version 2.0, January 2004](https://img.shields.io/github/license/apache/maven.svg?label=License)


<p align="center">
<img width="600" src="images/vita_typo.png">
</p>


As we know we need a LifeCycleOwner (e.g Fragment or FragmentActivity) to create ViewModels, when the owner is at the end of its lifecycle the ViewModel will be cleared as well, Sometimes you need to share the ViewModel between multiple owners, By default we can only share ViewModel of an activity between its fragments for now, nothing more...
**Vita** is a light and simple library that helps you to share ViewModel between fragments and even activities! Also you can create ViewModels that are available all over the application! Cool, right!?

**Vita** is a light and simple library that helps you to share ViewModel between fragments and activities, even you can create ViewModels in application scope
As we know we need a LifeCycleOwner (e.g Fragment or FragmentActivity) to create ViewModels, when the owner is at the end of its lifecycle the ViewModel will be cleared as well, Sometimes you need to share the ViewModel between multiple owners, By default we can only share ViewModel of an activity between its fragments for now, nothing more...

## What Vita does:
- Creates ViewModels with **Single Owner**:
This is the default ViewModel behavior that already have, The ViewModels created in this way are only available to the owner.
This is the default ViewModel behavior that already has, The ViewModels created in this way are only available to the owner.

<br/>
<p align="center">
Expand All @@ -28,7 +31,7 @@ As we know we need a LifeCycleOwner (e.g Fragment or FragmentActivity) to create
</p>

- Creates ViewModels with **No Owner**:
The ViewModels has no owner, they are available in the application scope and stay alive until the user closes the application
The ViewModels have no owner, they are available in the application scope and stay alive until the user closes the application

<br/>
<p align="center">
Expand All @@ -40,11 +43,20 @@ As we know we need a LifeCycleOwner (e.g Fragment or FragmentActivity) to create
Make sure your project includes jcenter in its repositories and add this to build.gradle in app module

dependencies {
implementation 'com.androidisland.arch:vita:0.1.0'
implementation 'com.androidisland.arch:vita:$latest_version'
}

## How to use

First start **Vita** in application class:

class App : Application() {
override fun onCreate() {
super.onCreate()
startVita()
}
}

There is an extension value named **vita** that gives you access to a singleton object of Vita everywhere, Just pass your desired VitaOwner and get the ViewModel you want:

`val myViewModel = vita.with(VitaOwner.Multiple(this)).getViewModel<MyViewModel>()`
Expand All @@ -55,6 +67,7 @@ Also you can pass a function as factory like this:
MyViewModelWithFactory(initData)
}`

### ⭐️ Don't forget to give it a star if you liked it!

## License

Expand Down

0 comments on commit 6330bfb

Please sign in to comment.