Skip to content

Instantly share code, notes, and snippets.

View CyxouD's full-sized avatar

Dmitriy Zasukha CyxouD

  • Ukraine, Dnipro
View GitHub Profile
@CyxouD
CyxouD / Model-View-Presenter Communication.kt
Last active April 8, 2019 12:18
Examples of tests for: 1) testing MVP architecture (Model-View-Presenter Communication.kt) - tests communication between view and presenter and mocking repository behavior; 2) Repository (Repository.kt) - tests how data from local data source and remote data source is received and processed; 3) UI (UI (Integration).kt) - tests UI behavior
//imports are missing
class CardsListPresenterTest {
private lateinit var presenter: CardsListPresenter
private val view = mock<CardsListContact.View>()
private val repository = mock<Repository>()
private val scheduleProvider = ImmediateSchedulerProvider()
// ... some other tests
@Test
@CyxouD
CyxouD / AddEditCardContract.kt
Last active April 8, 2019 11:58
Model-View-Presenter+Repository architecture example
//contract which declare action of Presenter and MVP View
interface AddEditCardContract {
interface Presenter : BasePresenter {
fun getCreditCardLogo(creditCardNumber: String)
fun validateCreditCardNumber(creditCardNumber: String)
fun validateCreditCardHolder(creditCardHolder: String)
fun validateCreditCardExpiryDate(creditExpiryDate: String)
fun validateCreditCardCVV(creditCVV: String)
fun validateCreditCardTypeAndPriority(creditCardType: String, creditCardPriority: String)
fun saveCreditCard(number: String,
@CyxouD
CyxouD / AddEditCardContract.kt
Created July 19, 2018 14:57
Contract (interface) which declare action of Presenter and MVP View. In this case AddEditCardFragment will implement AddEditCardContract.View interface and AddEditCardPresenter will implement AddEditCardContract.Presenter interface
interface AddEditCardContract {
interface Presenter : BasePresenter {
fun getCreditCardLogo(creditCardNumber: String)
fun validateCreditCardNumber(creditCardNumber: String)
fun validateCreditCardHolder(creditCardHolder: String)
fun validateCreditCardExpiryDate(creditExpiryDate: String)
fun validateCreditCardCVV(creditCVV: String)
fun validateCreditCardTypeAndPriority(creditCardType: String, creditCardPriority: String)
fun saveCreditCard(number: String,
holderName: String,
@CyxouD
CyxouD / AddEditCardFragment.kt
Created July 19, 2018 14:59
Fragment which is MVP View which can show credit card logo, show that there is no credit card logo, show credit card number was validated successfully etc (everything defined in AddEditCardContract.kt)
//MVP View
class AddEditCardFragment : BaseFragment<AddEditCardContract.Presenter>(), AddEditCardContract.View {
// ...Android lifecycle methods
override fun setPresenter(presenter: AddEditCardContract.Presenter) {
mPresenter = presenter
}
override fun showCreditCardLogo(creditCardEnum: CreditCardEnum) {
@CyxouD
CyxouD / AddEditCardPresenter.kt
Created July 19, 2018 15:01
Organize between with View (view: AddEditCardContract.View) and Model (repository: DataSource). Performs logic which is not View-dependant and process data from Model before passing it to View
class AddEditCardPresenter(override val repository: DataSource,
val view: AddEditCardContract.View,
override val schedulerProvider: BaseSchedulerProvider,
override val mCompositeDisposable: CompositeDisposable = CompositeDisposable()) : AddEditCardContract.Presenter {
init {
view.setPresenter(this)
}
override fun subscribe() {
}
@CyxouD
CyxouD / LocalDataSource.kt
Created July 19, 2018 15:04
This file is responsible for handling of saving data locally in Realm Database, Shared preferences, Android Secure storage, retrieving data and passing it with RxJava Observables
open class LocalDataSource private constructor() : DataSource, AnkoLogger {
private val authorizeTokenAlias = "authorizeTokenAlias"
private val conversationIdAlias = "conversationIdAlias"
companion object {
private var INSTANCE: LocalDataSource? = null
fun getInstance(): LocalDataSource {
if (INSTANCE == null) {
@CyxouD
CyxouD / RemoteDataSource.kt
Created July 19, 2018 15:05
This file is responsible for handling network request using Retrofit and pass result with RxJava Observables
open class RemoteDataSource private constructor() : DataSource {
private val service by lazy {
RetrofitService.create()
}
companion object {
private var INSTANCE: RemoteDataSource? = null
@CyxouD
CyxouD / Repository.kt
Created July 19, 2018 15:07
This file is responsible for organizing communication between local and remote data sources (storages) and passing result with RxJava Observables
open class Repository private constructor(
remoteDataSource: DataSource,
localDataSource: DataSource)
: DataSource {
private val mRemoteDataSource = remoteDataSource
private val mLocalDataSource = localDataSource
companion object {
private var INSTANCE: Repository? = null
@CyxouD
CyxouD / RetrofitService.kt
Created July 19, 2018 15:09
Interface, which declare PUT, GET, POST etc methods, which Retrofit (Http network client) will use to create network requests
interface RetrofitService {
companion object {
fun create(): DevGetVoilaService {
val clientBuilder = OkHttpClient.Builder().apply {
val loggingInterceptor = HttpLoggingInterceptor()
loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
addInterceptor(loggingInterceptor)
addInterceptor(AuthenticationInterceptor())
addInterceptor(AuditInterceptor())
}
@CyxouD
CyxouD / gist:7eaf69ccff3c0745c3a32fec4e92c065
Created August 16, 2018 07:07
Make all libraries to use same support libs version (26.1.0 in this case) - place it in your root build.gradle (I placed in build script)
subprojects {
project.configurations.all {
resolutionStrategy.eachDependency { details ->
if (details.requested.group == 'com.android.support'
&& !details.requested.name.contains('multidex') ) {
details.useVersion "26.1.0"
}
}
}
}