All Projects → RobertApikyan → LifecycleMvp

RobertApikyan / LifecycleMvp

Licence: Apache-2.0 license
No description or website provided.

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to LifecycleMvp

Jetpack Mvvm Scaffold
人生苦短,让脚手架为你节省时间。(目前作为《最佳实践》项目的 Dev 版来优先更新)
Stars: ✭ 239 (+1095%)
Mutual labels:  viewmodel, lifecycle, livedata
GithubApp-android-architecture
Let's learn a deep look at the Android architecture
Stars: ✭ 16 (-20%)
Mutual labels:  mvp, viewmodel, livedata
Readhubclient
Readhub客户端
Stars: ✭ 44 (+120%)
Mutual labels:  viewmodel, lifecycle, livedata
Mvvmarms
Android MVVM Architecture Components based on MVPArms and Android Architecture Components.
Stars: ✭ 425 (+2025%)
Mutual labels:  viewmodel, lifecycle, livedata
Aachulk
️🔥️🔥️🔥AACHulk是以Google的ViewModel+DataBinding+LiveData+Lifecycles框架为基础, 结合Okhttp+Retrofit+BaseRecyclerViewAdapterHelper+SmartRefreshLayout+ARouter打造的一款快速MVVM开发框架
Stars: ✭ 109 (+445%)
Mutual labels:  viewmodel, lifecycle, livedata
Wanandroid
Jetpack MVVM For Wanandroid 最佳实践 !
Stars: ✭ 1,004 (+4920%)
Mutual labels:  viewmodel, lifecycle, livedata
Jetpackmvvm
🐔🏀一个Jetpack结合MVVM的快速开发框架,基于MVVM模式集成谷歌官方推荐的JetPack组件库:LiveData、ViewModel、Lifecycle、Navigation组件 使用Kotlin语言,添加大量拓展函数,简化代码 加入Retrofit网络请求,协程,帮你简化各种操作,让你快速开发项目
Stars: ✭ 1,100 (+5400%)
Mutual labels:  viewmodel, lifecycle, livedata
AndriodDevelopLibrary
Lifecycle->ViewModel+LiveData+Repository
Stars: ✭ 16 (-20%)
Mutual labels:  viewmodel, lifecycle, livedata
Archcomp
使用Dagger,LiveData,ViewModel,Rxjava ,Retrofit等搭建App业务模块组件化框架。并处理多个系统提供数据导致的返回数据格式不一致,More ,please readme
Stars: ✭ 127 (+535%)
Mutual labels:  mvp, viewmodel, livedata
Aacomponents
基于google Android Architecture Components 封装实现组件式MVP快速开发框架
Stars: ✭ 66 (+230%)
Mutual labels:  viewmodel, lifecycle, livedata
Android Jetpack Demo
🔥 快速入门Android Jetpack以及相关Kotlin、RxJava、MVVM等主流技术,独立构架App的基础技能
Stars: ✭ 335 (+1575%)
Mutual labels:  viewmodel, lifecycle, livedata
Androidbaseframemvvm
Android 组件化 MVVM 框架 基于 Jetpack + Kotlin
Stars: ✭ 169 (+745%)
Mutual labels:  viewmodel, lifecycle, livedata
CoMvvmHelper
android mvvm 基础框架,适合日常快速开发。有需要添加的内容或者发现问题可以提 issue。
Stars: ✭ 26 (+30%)
Mutual labels:  viewmodel, lifecycle, livedata
Mvvmframe
🏰 MVVMFrame for Android 是一个基于Google官方推出的Architecture Components dependencies(现在叫JetPack){ Lifecycle,LiveData,ViewModel,Room } 构建的快速开发框架。有了MVVMFrame的加持,从此构建一个MVVM模式的项目变得快捷简单。
Stars: ✭ 218 (+990%)
Mutual labels:  viewmodel, lifecycle, livedata
MockAppMVVM
A sample app structure using the MVVM architecture using Retrofit, Dagger2, LiveData, RxJava, ViewModel and Room.
Stars: ✭ 14 (-30%)
Mutual labels:  viewmodel, lifecycle, livedata
Tdcapp
Sample app which access the TDC (The Developer's Conference) REST API.
Stars: ✭ 55 (+175%)
Mutual labels:  viewmodel, lifecycle, livedata
TeamManagerApp
A sample app structure using the MVVM architecture LiveData, RxJava, ViewModel, Room and the Navigation Arch Components.
Stars: ✭ 36 (+80%)
Mutual labels:  viewmodel, lifecycle, livedata
Easychatandroidclient
EasyChat是一个开源的社交类的App。主要包含消息、好友、群组等相关的IM核心功能。部分界面参照了QQ、微信等相关社交APP。EasyChat APP整体采用MVVM模式,基于JetPack(Lifecycle,LiveData,ViewModel,Room)构建
Stars: ✭ 64 (+220%)
Mutual labels:  viewmodel, lifecycle, livedata
Saber
🏄 帮助你快速使用Android的LiveData与ViewModel,已支持SavedState
Stars: ✭ 143 (+615%)
Mutual labels:  viewmodel, lifecycle, livedata
Relax
☘☘Relax 基于Kotlin语言编写的一套组件化框架,不紧整体组件化、内部也高度组件化🎋你可配置MVP、MVVM的开发模式、也可以配置所需要的业务组件🍁🍁
Stars: ✭ 253 (+1165%)
Mutual labels:  mvp, lifecycle, livedata

N|Solid

MinSDK 14+

License

Download

Gradle

Add to project level build.gradle

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Add dependency to app module level build.gradle

dependencies {
    implementation 'com.github.RobertApikyan:LifecycleMvp:1.1.1'
}

Maven

<repositories>
	<repository>
	<id>jitpack.io</id>
	<url>https://jitpack.io</url>
	</repository>
</repositories>

Add dependency

<dependency>
	<groupId>com.github.RobertApikyan</groupId>
	<artifactId>LifecycleMvp</artifactId>
	<version>1.1.1</version>
</dependency>

LifecycleMvp

It's convenient to write android applications with MVP architecture, because it's simple and lightweight.

LifecycleMvp is implementation of AbstractMvp with Android Arcitecture Components. AbstractMvp framework solves a number of issues related with classic MVP implementation. Read more about AbstractMvp here.

Let's try it on a ColorApp

Here we have a ColorApp. It's main point is to display different colors when the user is tapping on the screen. It has only one activity, and for that activity we gonna create ColorView interface and ColorPresenter class.

Step 1. Creating ColorView and ColorPresenter.
// View
// inherit from LifecycleView
interface ColorView : LifecycleView { 
    fun setNewColor(color: Int) // Set the color value as a view background
}
// Presenter
// inherit from LifecyclePresenter and define View generic type as a ColorView
class ColorPresenter : LifecyclePresenter<ColorView>() { 
    ...
}
Step 2. Implement ColorView interface by ColorActivity
class ColorActivity : AppCompatActivity(), ColorView {
  ...
  fun setNewColor(color:Int){
    mBackgroundView.setBackgroundColor(color)
  }
  ...
}
Step 3. Receive presenter instance in ColorActivity

Here we receiving presenter instance by calling LifecycleMvp.from(this, ::ColorPresenter) where "this"- is ColorView implementation, ::ColorPresenter is Presenter's constructor's reference. Alternatively it can be writen like this LifecycleMvp.from(this) { ColorPresenter() }

...
override fun onCreate(savedInstanceState: Bundle?) {
        ...
        // Receiving presenter instance 
        presenter = LifecycleMvp.from(this, ::ColorPresenter)
        mColorChangeView.setOnClickListener {
           presenter.onColorViewClick()
        }
}
...
The Important think here is that our presenter is lifecycle persistence. After configuration change, such as rotation we gonna receive the same presenter instance.
Step 4. Let's define our ColorPresenter
 class ColorPresenter : LifecyclePresenter<ColorView>() { 
    // Here we hold current display color
    private var currentColor = -1
    
    // This method called when user clicks on mColorBackground
    fun onColorViewClick() {
        // set new color value
        currentColor = ColorGenerator.generateColor()
        // call to change UI color
        onColorChanged()
    }
    
    // This method opens the view scope and send viewAction to view
    private fun onColorChanged() = view {
        setNewColor(currentColor) // UI color will be changed
    }
}

Here new think is view{ } method, which receives lambda(V.() -> Unit) ViewAction as an argument This viewAction will be invoked by LifecycleMvp framework.

Note.

It's impossible to come up with NullPointerException while trying to access view instance at the time when view is detached from presenter and there is no need to make nullability check every time before accessing view instance.

Done. Full example of ColorApp is here

UNDER THE HOOD

Lets understand what is happening when we call view { ... } or viewImmediate { ... } methods. N|Solid

  1. First when we call view { setNewColor(currentColor) } new ViewAction instance is created, and passed to ViewActionDispatcherLiveData.

  2. ViewActionDispatcherLiveData is IViewActionDispatcher(from AbstactMvp) implementation with LiveData from Android arcitecture components. It holds viewActions and dispatch them to ViewActionObserver.

  3. ViewActionObserver receives viewActions and invoke them, with passing the view instance.

  4. After ViewActionObserver invokes viewAction, setNewColor(color:Int) method will be called inside ColorActivity.

view { ... } and viewImmediate { ... }

When viewAction is created via view { ... } method, ViewActionDispatcherLiveData will cache the viewActions if view is detached, and send them when view will become attached again. If viewAction is created via viewImmediate{ ... } method, it will be send it only if view is attached, otherwise viewAction will be lost. This method can be used only after presenter's onCreate() lifecycle method call. This method is calling by AbstactMvp framework (Later more detail about LifecyclePresenter lifecycle). This methods can be called from worker thread, but the execution of viewAction will be automatically switched to the main thread.

It's comfortable to use view { ... } and viewImmediate { ... } methods with different type of expressions in kotlin language such as if or when.

// this methods are defined inside presenter
...
// using view { ... } with IF
fun onComplete(items:List<Item>) {
    if(items.isEmpty()) view {
        showEmptyResult()
    } else view {
        showItems(items)
    }
}
// using view { ... } with WHEN
fun onComplete(genre:FilmGenres) = when (genre) {
            FilmGenres.HORROR -> view { 
                showHorrors()
            }
            FilmGenres.COMEDY -> view { 
                showComedy()
            }
            FilmGenres.ROMANCE -> view {
                showRomance()
            }
        }
...

LifecyclePresenter's Lifecycle

LifecyclePresenter's has five lifecycle methods.

  1. First one is onCreate(), which is the initial stating point for presenter. As we know this method is called by AbstactMvp framework, when presenter instance is created, since LifecyclePresenter is Activity lifecycle persistence, it will be called only once. onCreate() is called when AbstactMvp framework finish binding all components together. (more about AbstractMvp components here). Only onCreate() method is related with presenter's lifecycle, upcoming methods are bounded with viewController lifecycle.

  2. onViewAttach() This method is called with ViewController's onCreate()

  3. onViewStart() This method is called with ViewController's onStart()

  4. onViewStop() This method is called with ViewController's onStop()

  5. onViewDetach() This method is called with ViewController's onDestroy()

LifecycleMvpFactory Class

AbstactMvp framework uses Mvp.Factory<V,P> factory, in order to get all components instances. LifecycleMvpFactory implement Mvp.Factory<V,P> interface and provides all necessary lifecycle components. If you need to change some component implementation you can inherit from LifecycleMvpFactory class and override component provider method that you want to change.

Summary

LifecycleMvp library is AbstractMvp implementation with LiveData, ViewModels and Lifecycle from Android Architecture Components.

View Robert Apikyan profile on LinkedIn

License

Copyright 2018 Robert Apikyan

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