GithubHelp home page GithubHelp logo

bpelogia / mvvmarchitecture Goto Github PK

View Code? Open in Web Editor NEW

This project forked from agustarc/mvvmarchitecture

0.0 1.0 0.0 197 KB

An example Android app using Retrofit, Room, LiveData, RxJava2, Paging, Koin and the MVVM pattern with the databinding

License: Apache License 2.0

Kotlin 100.00%

mvvmarchitecture's Introduction

MVVMArchitecture

License Platform

My way to MVVM pattern using RxJava, LiveData, Room, Paging with the Android databinding

What is MVVM pattern?

MVVM is an architectural pattern that was created to simplify user interface programming. Google appears to be encouraging the use of MVVM for data binding. In fact, the Architecture Components of its Data Binding Library are modeled on the MVVM pattern.


Samples

Activity Layout File

    <data>
        <variable name="vm" type="com.leopold.mvvm.viewmodel.search.SearchViewModel"/>
    </data>
    
    <androidx.appcompat.widget.AppCompatImageButton
        android:id="@+id/search_button"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:src="@drawable/ic_search"
        android:background="?attr/selectableItemBackgroundBorderless"
        android:onClick="@{() -> vm.search()}"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/search_edit"/>

Set Up the ViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val binding = DataBindingUtil.setContentView<ActivitySearchBinding>(this, R.layout.activity_search)
        binding.vm = getViewModel()
        binding.setLifecycleOwner(this)
    }

RecyclerView Adapter Item Layout File

    <data>
        <variable name="item" type="com.leopold.mvvm.data.remote.domain.Repository"/>
        <variable name="vm" type="com.leopold.mvvm.viewmodel.search.SearchViewModel"/>
    </data>
    
    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/repository_item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="6dp"
        android:text="@{item.name}"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"/>

DataBinding in ViewHolder

    override fun onBindViewHolder(holder: RepositoryViewHolder, position: Int) {
        holder.binding.item = items[position]
        holder.binding.vm = vm
    }
    
    class RepositoryViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val binding: LayoutRepositoryItemBinding = LayoutRepositoryItemBinding.bind(view)
    }

Libraries

Android Jetpack

  • DataBinding Declaratively bind observable data to UI elements.

  • Lifecycle Create a UI that automatically responds to lifecycle events.

  • LiveData Build data objects that notify views when the underlying database changes.

  • ViewModel Store UI-related data that isn't destroyed on app rotations. Easily schedule asynchronous tasks for optimal execution.

  • Room Access your app's SQLite database with in-app objects and compile-time checks.

  • Paging Makes it easier for you to load data gradually and gracefully within your app's RecyclerView.

HTTP

  • Retrofit2 Type-safe HTTP client for Android and Java by Square, Inc.

  • OkHttp An HTTP+HTTP/2 client for Android and Java applications.

ReactiveX

  • RxJava2 A library for composing asynchronous and event-based programs using observable sequences for the Java VM

  • RxAndroid RxJava bindings for Android

DI

  • Koin A pragmatic lightweight dependency injection framework for Kotlin developers.

References


License

Copyright 2018 Leopold Baik

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.

mvvmarchitecture's People

Contributors

agustarc avatar

Watchers

James Cloos 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.