Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/FarshadTahmasbi/Vita
Browse files Browse the repository at this point in the history
  • Loading branch information
FarshadTahmasbi committed Aug 20, 2019
2 parents 516f654 + 2599721 commit c81c5e9
Showing 1 changed file with 44 additions and 5 deletions.
49 changes: 44 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Vita
# 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_typo_.png)
<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...
Expand All @@ -14,21 +16,58 @@ As we know we need a LifeCycleOwner (e.g Fragment or FragmentActivity) to create

<br/>
<p align="center">
<img src="single_owner_diagram.png">
<img src="images/single_owner_diagram.png">
</p>

- Creates ViewModels with **Multiple Owners**:
The ViewModels are shared between multiple owners and stay alive while at least one owner is alive

<br/>
<p align="center">
<img src="multiple_owner_diagram.png">
<img src="images/multiple_owner_diagram.png">
</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

<br/>
<p align="center">
<img src="no_owner_diagram.png">
<img src="images/no_owner_diagram.png">
</p>

## Gradle setup

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'
}

## How to use

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>()`

Also you can pass a function as factory like this:

`val myViewModelWithFactory = vita.with(VitaOwner.Multiple(this)).getViewModel(){
MyViewModelWithFactory(initData)
}`


## License

Copyright 2019 Farshad Tahmasbi

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

0 comments on commit c81c5e9

Please sign in to comment.