All Projects → inloop → Androidviewmodel

inloop / Androidviewmodel

Licence: apache-2.0
Separating data and state handling from Fragments or Activities without lots of boilerplate-code.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Androidviewmodel

Newandroidarchitecture Component Github
Sample project based on the new Android Component Architecture
Stars: ✭ 229 (-72.21%)
Mutual labels:  architecture, mvvm
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 (-8.62%)
Mutual labels:  architecture, mvvm
iOS-Clean-Architecture-Example
An iOS app designed using clean architecture and MVVM.
Stars: ✭ 50 (-93.93%)
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 (-79.25%)
Mutual labels:  architecture, mvvm
Ribs
Uber's cross-platform mobile architecture framework.
Stars: ✭ 6,641 (+705.95%)
Mutual labels:  architecture, mvvm
Rxpm
Reactive implementation of Presentation Model pattern in Android
Stars: ✭ 176 (-78.64%)
Mutual labels:  architecture, mvvm
Cleanarchitecturerxswift
Example of Clean Architecture of iOS app using RxSwift
Stars: ✭ 3,256 (+295.15%)
Mutual labels:  architecture, mvvm
Mvvmarchitecture
MVVM 框架,采用 Kotlin+Jetpack,可自由配置功能,欢迎 star,fork,issue
Stars: ✭ 159 (-80.7%)
Mutual labels:  architecture, mvvm
Awesome Ios Architecture
🏯 Better ways to structure iOS apps
Stars: ✭ 4,451 (+440.17%)
Mutual labels:  architecture, mvvm
Wanandroid
🏄 基于Architecture Components dependencies (Lifecycles,LiveData,ViewModel,Room)构建的WanAndroid开源项目。 你值得拥有的MVVM快速开发框架:https://github.com/jenly1314/MVVMFrame
Stars: ✭ 410 (-50.24%)
Mutual labels:  architecture, mvvm
Remvvm
ReMVVM is an application architecture concept, marriage of Unidirectional Data Flow (Redux) with MVVM.
Stars: ✭ 168 (-79.61%)
Mutual labels:  architecture, mvvm
Viabus Architecture
让 Android 开发可以像流水线一样高效的,职责分离架构 ⚡ 不同于 MVP 的配置解耦,也不能和 似是而非 的 MVVM - Clean 同日而语。VIABUS 是世界范围内首个明确提出,通过职责分离,来真正实现 UI 和 业务并行开发的 Android 项目级开发架构和设计模式理念。
Stars: ✭ 485 (-41.14%)
Mutual labels:  architecture, mvvm
Mmlpx
🐘 mobx model layer paradigm
Stars: ✭ 164 (-80.1%)
Mutual labels:  architecture, mvvm
Transport Eta
Twitch streamed 🎥playground repo, README speaks to you.
Stars: ✭ 223 (-72.94%)
Mutual labels:  architecture, mvvm
Clean Architecture Swiftui
SwiftUI sample app using Clean Architecture. Examples of working with CoreData persistence, networking, dependency injection, unit testing, and more.
Stars: ✭ 2,925 (+254.98%)
Mutual labels:  architecture, mvvm
ReMVVM
ReMVVM is an application architecture concept, marriage of Unidirectional Data Flow (Redux) with MVVM.
Stars: ✭ 180 (-78.16%)
Mutual labels:  architecture, mvvm
Restapimvvm
App that interacts with a Rest Api. Architecture is MVVM.
Stars: ✭ 130 (-84.22%)
Mutual labels:  architecture, mvvm
Cocktailapp
Cocktails Android App with Clean Architecture, MVVM , Retrofit, Coroutines, Navigation Components , Room, Dagger Hilt, Cache Strategy and Coroutines Flow
Stars: ✭ 128 (-84.47%)
Mutual labels:  architecture, mvvm
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.
Stars: ✭ 308 (-62.62%)
Mutual labels:  architecture, mvvm
Coordinator Mvvm Rx Example
Example of MVVM-C architecture implemented with RxSwift
Stars: ✭ 469 (-43.08%)
Mutual labels:  architecture, mvvm

AndroidViewModel

Important notice: Deprecated

This library served it's purpose for over 3 years. We believe that Google's Android Architecture Components are the preferred setup now for new projects. INLOOPX is dedicated to continue maintaining this library (no deadline on support end). So rest assured that your existing projects don't need be migrated from AndroidViewModel because of this deprecation. We are only stopping new feature development and don't recommend using it for new projects.

Separating data and state handling from Fragments or Activities without lots of boilerplate-code. Reducing them to simple dumb views.

Basic idea behind this library. An instance of a ViewModel class is assigned to your Fragment or Activity during the first creation and is kept during it's life cycle, even between display orientation changes. The ViewModel instance is removed after the Fragment or Activity is completely gone (finished, popped from backstack, replaced without keeping it in backstack).

You can execute asynchronous tasks in this ViewModel instance and this class is not destroyed during orientation change. All data handling and state logic should be placed inside this class. The Fragment or Activity is just a "dumb" view.

How to implement

  1. Create an interface for your View by extending IView. We will call it IUserListView for this example.

     public interface IUserListView extends IView {
          public void showUsers(List<User> users);
     }
    
    
  2. Create your ViewModel class by extending AbstractViewModel. For example:

    public class UserListViewModel extends AbstractViewModel<IUserListView> {
       ....
    }
    
  3. Each Fragment or Activity that you would like to associate with a ViewModel will need either to extend ViewModelActivityBase/ViewModelBaseFragment or copy the implementation from these classes to your base activity/fragment class (in case you can't inherit directly). For example:

    public class UserListFragment extends ViewModelBaseFragment<IUserListView, UserListViewModel> 
       implements IUserListView {
       
    }
    
  4. Also each Fragment or Activity has to call setModelView() after the View (Fragment/Activity) was created and initialized. This is usually on the end of onViewCreated (or onCreate in case of an Activity)

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
         super.onViewCreated(view, savedInstanceState);
         ButterKnife.inject(this, view);
         setModelView(this);
    }
    

How to use

You can forward user interaction from the View into the ViewModel simply by calling:

getViewModel().onDeleteUserClicked(userId);

The same goes for the opposite direction, when your asynchronous operation in the ViewModel finished and you would like to forward data to the View to show a list for example:

getViewOptional().showUsers(userList);

The getViewOptional() method will never return null. It will return a dummy implementation in case the View is null at the moment (e.g. Fragment already destroyed, or between orientation change). You can also check if the View is not null in case you need to:

if (getView() != null) {
    getView().showUsers(userList);
}

Your Fragment argument Bundle and Activity intent Bundle is forwarded to the ViewModel's onCreate method, which you can override to read the initial arguments for the ViewModel.

public void onCreate(Bundle arguments, Bundle savedInstanceState) {
   long userId = arguments.getInt("user_id", -1);
}

Data binding support

Data binding is supported - extend ViewModelBaseBindingFragment.java instead of ViewModelBaseFragment and implement getViewModelBindingConfig() in your Fragment.

@Override
public ViewModelBindingConfig getViewModelBindingConfig() {
   return new ViewModelBindingConfig(R.layout.fragment_sample_binding, requireActivity());
}

That's it. You can then directly use ObservableField in your ViewModels. See example.

Special handling for FragmentStatePagerAdapter

The Android implementation of FragmentStatePagerAdapter is removing Fragments and storing their state. This is in contrast with FragmentPagerAdapter where the Fragments are just detached but not removed. We should be also removing ViewModels and storing their state to be consistent with this behaviour.

Use ViewModelStatePagerAdapter instead of the default FragmentStatePagerAdapter. This class is only overriding the destroyItem() method and making sure that ViewModel is removed. The state is stored/restored automatically. You can also use the standard FragmentStatePagerAdapter - in that case ViewModels will be kept in memory and removed only when you leave the screen (Activity finished or Fragment removed).

How does it work?

A unique global ID is generated for the first time your Fragment or Activity is shown. This ID is passed on during orientation changes. Opening another instance of the same Fragment or Activity will result in a different ID. The ID is unique screen identifier. A ViewModel class is created and bound to this ID. The corresponding ViewModel instance is attached to your Fragment or Activity after an orientation change or if you return to the fragment in the back stack. The ViewModel is discarded once the Fragment/Activity is not reachable anymore (activity is finished or fragment permanently removed).

Download

compile 'eu.inloop:androidviewmodel:1.4.0'

Android Studio Template

For faster creating new screens, you can use Android Studio Template

Android Studio Template Window

Install template

Manually:

Copy the template folder to Android Studio templates folder (/Applications/Android Studio.app/Contents/plugins/android/lib/templates/others on Mac)

Automatically:

Run the following command to download and install the template automatically (Mac only)

curl -o androidviewmodel.zip -Lk https://github.com/inloop/AndroidViewModel/archive/master.zip && unzip androidviewmodel.zip && cp -af AndroidViewModel-master/template/AVM_Inloop/. "/Applications/Android Studio.app/Contents/plugins/android/lib/templates/other/AVM_Inloop" && rm -r AndroidViewModel-master && rm androidviewmodel.zip

Don't forget to restart the Android Studio.

Usage

In the Android Studio right click inside the Projet window and select File > New > AndroidViewModel Inloop > AVM Fragment

Android Studio New Template

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