Giter Site home page Giter Site logo

ulaserdegor / android-viewmodelbinding Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jakubkinst/android-viewmodelbinding

0.0 2.0 0.0 484 KB

A lightweight library aiming to speed up Android app development by leveraging the new Android Data Binding together with the Model-View-ViewModel design pattern.

FreeMarker 12.89% Java 87.11%

android-viewmodelbinding's Introduction

Android ViewModelBinding

Build Status

Intro

A lightweight library aiming to speed up Android app development by leveraging the new Android Data Binding and taking the best from the Model-View-ViewModel design pattern.

Why should I use it?

  1. Data Binding Android Data Binding is great and if you're not, you should start using it today.
  2. You don't need to care about screen rotation (configuration change) at all. Most of the screen lifecycle is moved to ViewModel where the lifecycle is dramatically easier to understand and to use. The ViewModel instance outlives it's Activity/Fragment during configuration change so no more hassle with onSaveInstanceState() or using retained Fragments.
  3. ViewModel as the only variable in the layout ViewModel serves as the data provider in layout's binding as well as handler for click or other methods common fro Data Binding. With a construct like android:onClick="@{viewModel.onClickedPlayButton}" you will never have to set an OnClickListener anymore. Also, each ViewModel extends BaseObservable so you have a choice between using BaseObservable approach or ObservableField approach within the DataBinding. (see Data Binding Guide)

How does it work?

The framework extensively uses Java Generics to provide a type-safe link between Activity/Fragment and ViewModel and its binding.

ViewModel instances are stored in a global static Map and reattached automatically to corresponding Activity/Fragment. When there is no need for the ViewModel anymore (Activity finished) the instance is destroyed.

ViewModel Lifecycle

ViewModel Lifecycle Diagram

Installation

compile 'cz.kinst.jakub:viewmodelbinding:0.8.1'

Don't forget to enable Data Binding in your module:

android {
	dataBinding {
		enabled = true;
	}
}

Usage

Activity

MainActivity.java

public class MainActivity extends ViewModelActivity<ActivityMainBinding, MainViewModel> {

	@Override
	public ViewModelBindingConfig getViewModelBindingConfig() {
		return new ViewModelBindingConfig(R.layout.activity_main, MainViewModel.class);
	}
	
	// handle Activity related stuff here - Options menu, Toolbar, Window config, etc.
}

activity_main.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android"
		xmlns:tools="http://schemas.android.com/tools">

	<data>

		<variable
			name="viewModel"
			type="cz.kinst.jakub.sample.viewmodelbinding.MainViewModel"/>
	</data>

	<LinearLayout
		android:layout_width="match_parent"
		android:layout_height="match_parent"
		android:orientation="vertical">

		<EditText
			android:id="@+id/name_edit_text"
			android:layout_width="match_parent"
			android:layout_height="wrap_content"
			android:hint="@string/hint_enter_your_name"/>

		<Button
			android:layout_width="match_parent"
			android:layout_height="wrap_content"
			android:onClick="@{viewModel.onClickGreetButton}"
			android:text="Greet"/>

		<TextView
			android:layout_width="match_parent"
			android:layout_height="wrap_content"
			android:gravity="center"
			android:text="@{viewModel.name != null &amp;&amp; !viewModel.name.empty ? @string/hello(viewModel.name) : ``}"
			tools:text="@string/hello"/>
	</LinearLayout>
</layout>

ViewModel

MainViewModel.java

public class MainViewModel extends ViewModel<ActivityMainBinding> {

	public final ObservableField<String> name = new ObservableField<>();

	@Override
	public void onViewModelCreated() {
		super.onViewModelCreated();
		// Do API calls etc.
	}

	@Override
	public void onViewAttached(boolean firstAttachment) {
		super.onViewAttached(firstAttachment);
		// manipulate with the view
	}

	public void onClickGreetButton(View v) {
		name.set(getBinding().nameEditText.getText().toString());
	}
}

Real usage

For a more complex example of using this approach, see Weather 2.0 project.

Android Studio New Screen Template

To deploy new screens even faster, use the included Android Studio Template (revision 2)

Android Studio Template

Usage

  1. Copy the template folder to Android Studio templates folder (/Applications/Android Studio.app/Contents/plugins/android/lib/templates/ on Mac) OR run the following command to download and install the template automatically

     curl -o viewmodelbinding.zip -Lk https://github.com/jakubkinst/Android-ViewModelBinding/archive/master.zip && unzip viewmodelbinding.zip && cp -af Android-ViewModelBinding-master/extras/AndroidStudioTemplate/templates/. "/Applications/Android Studio.app/Contents/plugins/android/lib/templates/" && rm -r Android-ViewModelBinding-master && rm viewmodelbinding.zip
    
  2. Restart Android Studio

  3. Use File>New>ViewModelBinding>ViewModelBinding Screen action to add a new screen

Changelog

v0.8.1 (Feb 3, 2016)

  • Added runOnUiThread(), postDelayed() and getRootView() methods to ViewModel
  • Added ViewModelDialogFragment
  • getBinding() is now public in ViewModel

v0.8 (Jan 19, 2016)

  • ViewModelConfig can be created without BR.viewModel as long as the name ov the binding variable is viewModel
  • Added onViewModelCreated() callback in ViewModel
  • BREAKING Renamed onModelRemoved() to onViewModelDestroyed() callback in ViewModel
  • Added getResources() convenience method to ViewModel

Authors

The library was inspired by a great AndroidViewModel library by Inloop

License

Copyright 2015 Jakub Kinst & Stepan Sanda

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.

android-viewmodelbinding's People

Contributors

jakubkinst avatar stepansanda avatar

Watchers

 avatar  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.