Giter VIP home page Giter VIP logo

mvvm-template's Introduction

Android-Studio-MVVM-DataBinding-Dagger-Template-Google-Architecture (Java & Kotlin)

This is an Android Studio template for MVVM; the template is inspired by google samples/ android architecture

Important: The main goal of this template is to speed up the development process using android MVVM template. This is just a template and basic code, so feel free to make changes according to your needs. It also shows how to create a set of files using template files in Android Studio. This template was successfully tested with Android 3.1.1.

Demo

Create MVVM template

Code Quality

This project integrates a combination of unit tests, functional test and code analysis tools.

Tests

To run unit tests on your machine:

./gradlew test

To run functional tests on connected devices:

./gradlew connectedAndroidTest

Note: For Android Studio to use syntax highlighting for Automated tests and Unit tests you must switch the Build Variant to the desired mode.

Code Analysis tools

The following code analysis tools are set up on this project:

  • PMD: It finds common programming flaws like unused variables, empty catch blocks, unnecessary object creation, and so forth. See this project's PMD ruleset.
./gradlew pmd
  • Findbugs: This tool uses static analysis to find bugs in Java code. Unlike PMD, it uses compiled Java bytecode instead of source code.
./gradlew findbugs
./gradlew checkstyle

The check task

To ensure that your code is valid and stable use check:

./gradlew check

This will run all the code analysis tools and unit tests in the following order:

Check Diagram

Installation

For Mac:

  • If you have a standard Android Studio installation:

Just run the install script at the root of this repository:

./install.sh
  • Manual installation:

Just copy directory MVVMBasic to $ANDROID_STUDIO_FOLDER$/Contents/plugins/android/lib/templates/activities/

For Windows:

Just copy directory MVVMBasic to $ANDROID_STUDIO_FOLDER$\plugins\android\lib\templates\activities\

How to use

1. Create Base classes

The main idea of the base classes is to have common methods that share across the Activities, Navigator, ViewModel and Module written down in one place.

Code Basic

  • BaseActivity
abstract class BaseActivity<T : ViewDataBinding, V : BaseViewModel<*>> : AppCompatActivity(), BaseFragment.Callback {

    var viewDataBinding: T? = null
        private set
    private var mViewModel: V? = null

    ...
    @get:LayoutRes
    abstract val layoutId: Int
    
	...    
    private fun performDataBinding() {
        viewDataBinding = DataBindingUtil.setContentView(this, layoutId)
        this.mViewModel = if (mViewModel == null) viewModel else mViewModel
        viewDataBinding!!.setVariable(bindingVariable, mViewModel)
        viewDataBinding!!.executePendingBindings()
    }

    fun performDependencyInjection() {
        AndroidInjection.inject(this)
    }
}
  • BaseFragment
public abstract V getViewModel();
abstract class BaseFragment<T : ViewDataBinding, V : BaseViewModel<*>> : Fragment() {

    var baseActivity: BaseActivity<*, *>? = null
        private set
    var viewDataBinding: T? = null
        private set
    private var mViewModel: V? = null
    private var mRootView: View? = null
    
    @get:LayoutRes
    abstract val layoutId: Int
    
    private fun performDependencyInjection() {
        AndroidSupportInjection.inject(this)
    }

    interface Callback {

        fun onFragmentAttached()

        fun onFragmentDetached(tag: String)
    }
}

  • BaseViewModel
abstract class BaseViewModel<N>(val dataManager: DataManager,
                                val schedulerProvider: SchedulerProvider) : ViewModel() {
    val isLoading = ObservableBoolean(false)
    var navigator: N? = null
    val compositeDisposable: CompositeDisposable

    init {
        this.compositeDisposable = CompositeDisposable()
    }

    fun setIsLoading(isLoading: Boolean) {
        this.isLoading.set(isLoading)
    }

    override fun onCleared() {
        compositeDisposable.dispose()
        super.onCleared()
    }
}


  • BaseViewHolder for RecycleView.Adapter
abstract class BaseViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    abstract fun onBind(position: Int)
}


2. Create Activity MVVM template

Create MVVM template

Support

If you've found an error in this sample, please file an issue to https://github.com Patches are encouraged, and may be submitted by forking this project and submitting a pull request through GitHub.

License

License

Copyright 2017 Bach

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.

mvvm-template's People

Contributors

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