• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ahmedeltaher/Android-MVP-Architecture: MVP + Kotlin + Retrofit2 + Dagger2 + Coro ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

ahmedeltaher/Android-MVP-Architecture

开源软件地址(OpenSource Url):

https://github.com/ahmedeltaher/Android-MVP-Architecture

开源编程语言(OpenSource Language):

Kotlin 91.6%

开源软件介绍(OpenSource Introduction):

Model–view–presenter (MVP)

Android Arsenal kotlin coroutines ANKO Mockk Junit5 Espresso Dagger 2 Kotlin-Android-Extensions MVVM MVP

androkotlinandroid-mvp-flow

Model–view–presenter (MVP) is a derivation of the model–view–controller (MVC) architectural pattern which mostly used for building user interfaces. In MVP, the presenter assumes the functionality of the “middle-man”. In MVP, all presentation logic is pushed to the presenter. Check here for MVVM

What is Coroutines ?

Coroutines :
Is light wight threads for asynchronous programming, Coroutines not only open the doors to
asynchronous programming, but also provide a wealth of other possibilities such as concurrency, actors, etc.

Coroutines VS RXJava

They're different tools with different strengths. Like a tank and a cannon, they have a lot of overlap but are more or less desirable under different circumstances.
- Coroutines Is light wight threads for asynchronous programming.
- RX-Kotlin/RX-Java is functional reactive programming, its core pattern relay on
observer design pattern, so you can use it to handle user interaction with UI while you
still using coroutines as main core for background work.

How does Coroutines concept work ?

  • Kotlin coroutine is a way of doing things asynchronously in a sequential manner. Creating a coroutine is a lot cheaper vs creating a thread.

When I can choose Coroutines or RX-Kotlin to do some behaviour ?

  • Coroutines : When we have concurrent tasks , like you would fetch data from Remote connections
    , database , any background processes , sure you can use RX in such cases too, but it looks like
    you use a tank to kill ant.
  • RX-Kotlin : When you would to handle stream of UI actions like : user scrolling , clicks ,
    update UI upon some events .....ect .

What is the Coroutines benefits?

  • Writing an asynchronous code is sequential manner.
  • Costing of create coroutines are much cheaper to crate threads.
  • Don't be over engineered to use observable pattern, when no need to use it.
  • parent coroutine can automatically manage the life cycle of its child coroutines for you.

Handle Retrofit with Coroutines

8399

  • Add Coroutines to your gradle file
// Add Coroutines  
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.2'  
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2'  
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.3.2'  
// Add Retrofit2  
implementation 'com.squareup.retrofit2:retrofit:2.6.2'  
implementation 'com.squareup.retrofit2:converter-gson:2.6.2'  
implementation 'com.squareup.okhttp3:okhttp:4.2.2'  
  • Make Retrofit Calls.
@GET("topstories/v2/home.json")  
fun fetchNews(): Call<NewsModel>  
  • With async we create new coroutine and returns its future result as an implementation of [Deferred].
  • The coroutine builder called launch allow us to start a coroutine in background and keep working in the meantime.
  • so async will run in background then return its promised result to parent coroutine which
    created by launch.
  • when we get a result, it is up to us to do handle the result.
launch {  
  try {  
        val serviceResponse: Data? = withContext(Dispatchers.IO) { dataRepository.requestNews()  
       }  
  if (serviceResponse?.code == Error.SUCCESS_CODE) {  
            val data = serviceResponse.data  
            callback.onSuccess(data as NewsModel)  
        } else {  
            callback.onFail(serviceResponse?.error)  
        }  
    } catch (e: Exception) {  
            callback.onFail(Error(e))  
    }  
}  

Keep your code clean according to MVP

  • yes , RXAndroid is easy , powerful , but you should know in which MVP
    layer you will put it .
  • for observables which will emit data stream , it has to be in your
    data layer , and don't inform those observables any thing else like
    in which thread those will consume , cause it is another
    responsibility , and according to Single responsibility principle
    in SOLID (object-oriented design) , so don't break this concept by
    mixing every thing due to RXAndroid ease .

MVP


LICENSE

Copyright [2016] [Ahmed Eltaher]

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.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap