All Projects → jakubkinst → Android Viewmodelbinding

jakubkinst / Android Viewmodelbinding

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.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Android Viewmodelbinding

Ios Clean Architecture Mvvm
Template iOS app using Clean Architecture and MVVM. Includes DIContainer, FlowCoordinator, DTO, Response Caching and one of the views in SwiftUI
Stars: ✭ 753 (+144.48%)
Mutual labels:  architecture, mvvm, viewmodel
iOS-Clean-Architecture-Example
An iOS app designed using clean architecture and MVVM.
Stars: ✭ 50 (-83.77%)
Mutual labels:  architecture, mvvm, viewmodel
Android Mvvm Architecture
A basic sample android application to understand MVVM in a very simple way.
Stars: ✭ 129 (-58.12%)
Mutual labels:  architecture, mvvm, viewmodel
Wanandroid
🏄 基于Architecture Components dependencies (Lifecycles,LiveData,ViewModel,Room)构建的WanAndroid开源项目。 你值得拥有的MVVM快速开发框架:https://github.com/jenly1314/MVVMFrame
Stars: ✭ 410 (+33.12%)
Mutual labels:  architecture, mvvm, viewmodel
Restapimvvm
App that interacts with a Rest Api. Architecture is MVVM.
Stars: ✭ 130 (-57.79%)
Mutual labels:  architecture, mvvm, viewmodel
Newandroidarchitecture Component Github
Sample project based on the new Android Component Architecture
Stars: ✭ 229 (-25.65%)
Mutual labels:  architecture, mvvm
Witch-Android
View-data binding library for Android.
Stars: ✭ 25 (-91.88%)
Mutual labels:  binding, viewmodel
PlayAndroid
✌️✊👋玩安卓Mvvm组件化客户端,整合Jetpack组件DataBinding、ViewModel以及LiveData;屏幕适配✔️状态栏沉浸式✔️黑夜模式✔️,无数据、加载失败状态页;骨架屏、Koin依赖注入等
Stars: ✭ 193 (-37.34%)
Mutual labels:  mvvm, viewmodel
Paging-3-Sample
This app is created as a sample app which loads movies from Tmdb api and uses Paging 3 library to show it in a Recycler view.
Stars: ✭ 96 (-68.83%)
Mutual labels:  mvvm, viewmodel
Remvvm
ReMVVM is an application architecture concept, marriage of Unidirectional Data Flow (Redux) with MVVM.
Stars: ✭ 168 (-45.45%)
Mutual labels:  architecture, mvvm
MvvmScarletToolkit
MvvmScarletToolkit is a personal project and framework to speed up the development process of xaml based applications using the viewmodel first approach
Stars: ✭ 23 (-92.53%)
Mutual labels:  mvvm, viewmodel
CoMvvmHelper
android mvvm 基础框架,适合日常快速开发。有需要添加的内容或者发现问题可以提 issue。
Stars: ✭ 26 (-91.56%)
Mutual labels:  mvvm, viewmodel
Transport Eta
Twitch streamed 🎥playground repo, README speaks to you.
Stars: ✭ 223 (-27.6%)
Mutual labels:  architecture, mvvm
Rxpm
Reactive implementation of Presentation Model pattern in Android
Stars: ✭ 176 (-42.86%)
Mutual labels:  architecture, mvvm
Local Db Cache Retrofit Rest Api Mvvm
App that interacts with a REST API using Retrofit. There is a local db cache and architecture is MVVM
Stars: ✭ 171 (-44.48%)
Mutual labels:  architecture, mvvm
ReMVVM
ReMVVM is an application architecture concept, marriage of Unidirectional Data Flow (Redux) with MVVM.
Stars: ✭ 180 (-41.56%)
Mutual labels:  architecture, mvvm
Cleanarchitecturerxswift
Example of Clean Architecture of iOS app using RxSwift
Stars: ✭ 3,256 (+957.14%)
Mutual labels:  architecture, mvvm
DeezerClone
This Application using Dagger Hilt, Coroutines, Flow, Jetpack (Room, ViewModel, LiveData),Navigation based on MVVM architecture.
Stars: ✭ 81 (-73.7%)
Mutual labels:  mvvm, viewmodel
StackOverFlowApi
working with Stack OverFlow Api
Stars: ✭ 24 (-92.21%)
Mutual labels:  mvvm, viewmodel
modern-android
Modern Android Project Skeleton
Stars: ✭ 17 (-94.48%)
Mutual labels:  mvvm, viewmodel

Android ViewModelBinding 2.0

Build Status Android Arsenal Download

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:2.0.0'

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

android {
	dataBinding.enabled = true
}

Usage

Activity/Fragment

MainActivity.java

public class MainActivity extends ViewModelActivity<ActivityMainBinding, MainViewModel> {

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		setupViewModel(R.layout.activity_main, MainViewModel.class);
		super.onCreate(savedInstanceState);
	}
	
	// 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" xmlns:app="http://schemas.android.com/apk/res-auto">

	<data>

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

	<LinearLayout
		android:layout_width="match_parent"
		android:layout_height="match_parent"
		android:padding="@dimen/activity_padding"
		android:orientation="vertical">

		<android.support.design.widget.TextInputLayout
			android:layout_width="match_parent"
			android:layout_height="wrap_content">

			<EditText
				android:layout_width="match_parent"
				android:layout_height="wrap_content"
				android:text="@={viewModel.name}"
				android:inputType="textPersonName|textCapWords"
				android:hint="@string/hint_enter_your_name" />
		</android.support.design.widget.TextInputLayout>


		<FrameLayout
			android:layout_width="match_parent"
			android:layout_height="0dp"
			android:layout_weight="1"
			android:animateLayoutChanges="true">

			<TextView
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				android:layout_gravity="center"
				android:textAppearance="@style/Base.TextAppearance.AppCompat.Headline"
				android:textColor="@color/colorPrimary"
				android:text="@{@string/hello(viewModel.name)}"
				app:show="@{viewModel.name != null &amp;&amp; !viewModel.name.empty}"
				tools:text="@string/hello" />
		</FrameLayout>


		<Button
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:layout_gravity="center"
			android:onClick="@{() -> viewModel.showDialog()}"
			android:text="@string/button_dialog_fragment"
			style="@style/Widget.AppCompat.Button.Colored" />
	</LinearLayout>
</layout>

ViewModel

MainViewModel.java

public class MainViewModel extends ViewModel {

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

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

v2.0.0 (Mar 8, 2017)

  • Activity result delivered to ViewModel automatically
  • ViewModel is not tied to binding (layout) anymore
  • ViewModel has getApplicationContext() which returns Context at all times (even if View is not attached at the moment)
  • Included couple of handy BindingAdapters (app:show, app:hide, app:invisible)
  • New way of configuring Activity/Fragment (call setupViewModel() before super.onCreate())
  • Added onViewModelInitialized() callback to Activity/Fragment to be able to setup ViewModel before onViewModelCreated() is called (example: feeding ViewModel with Extras/Arguments - see ArgumentDialogFragment in sample)
  • Optional automatic binding of Activity/Fragment into layout file next to the ViewModel instance (add variable of name view and appropriate type)
  • [ALPHA] Added simple permission handling (PermissionManager) to ViewModel - see sample

v0.9.4 (Jul 18, 2016)

  • ViewInterface now has to implement startActivityForResult()

v0.9.2 (Jun 28, 2016)

  • Tasks added by runOnUiThread() are performed after onViewAttached() method is called when there are some left in the queue

v0.9 (Jun 21, 2016)

  • Added safe handling of Runnables in runOnUiThread() - if the ViewModel is not attached to an Activity/Fragment at the time, the Runnable will be executed once it is attached again
  • Added getString() method taking formatting arguments to ViewModel
  • Added RetrofitCallViewModel extension for handling Retrofit calls (see the source)
  • Updated dependencies (support library versions, targetSdkVersion, etc.)

v0.8.3 (Mar 10, 2016)

  • Improved internal generics - more type-safety across the library

v0.8.2 (Feb 26, 2016)

  • Added isRunning() method to ViewModel telling if Activity/Fragment is in RUNNING state (in between onResume() and onPause())

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

Contributors

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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].