Giter VIP home page Giter VIP logo

vita's Introduction

Vita Download

Share ViewModels EVERYWHERE !

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!?

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 has, The ViewModels created in this way are only available to the owner.

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

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

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

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

Also you can pass a function as factory like this:

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

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

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.    

vita's People

Contributors

farshadtahmasbi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.