MVP + Kotlin + Retrofit2 + Dagger2 + Coroutines + Anko + Kotlin-Android-Extensions + RX-java + Mockk + Espresso + Junit5
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
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.
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.
// 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'
@GET("topstories/v2/home.json") fun fetchNews(): Call<NewsModel>
async
we create new coroutine and returns its future result as an implementation of [Deferred].launch
allow us to start a coroutine in background and keep working in the meantime.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)) } }
Single responsibility principle
SOLID (object-oriented design)
, so don't break this concept byCopyright [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.