Android Clean Architecture Using Kotlin

Google+ Pinterest LinkedIn Tumblr +
Spread the love

 

Clean Architecture

Clean Architectre

Clean architecture Application is extremely maintainable, because of interchangeable implementations it would be easy for you to change every aspect of your app: from UI and simple data processing to DB and API frameworks.

Now let’s describe more about Kotlin and Rx-java.

Kotlin

 

Kotlin features extremely simplify android development not just by allowing you to use smaller constructions of language, but maintaining semantically right functions in your app. This will allow you to think less about implementation details and more about the whole app structure. Functional part of Kotlin will reduce side effects of your code and make it easier to test and understand.

RXJava

RX operators that RXJava provides easy to use stuff for working with data streams and threads. But in my opinion, the main purpose of RxJava is to have universal structures for data flow in an application.

Clean Architecture:



Data layer

Data layer is represented by Repository interfaces. Each repository works with a single, well-defined aspect of application (users, messages, events inbox, etc).

Domain layer

Domain layer’s element is UseCase. UseCase’s role is to combine data layer elements to perform a combination of data operations.

Presentation layer

Presentation layer consists of 2 elements: a passive View and a Presenter with presentation logic. View is completely passive, so it just calls presenter functions when any UI action is performed, and Presenter is deciding what actions must be performed on one or another UI event. 

Calls stack

That was a quick dive into layers. Now let’s go through calls stack when user presses confirm Button.

Firstly, button click listener in view is called.

confirmButton.onClick { 
  presenter.onConfirmButtonClicked()
}

Then view calls the presenter’s method. In presenter we validate user input with ValidationUtils.isValidEmail(view.getEmailInput()) and show user validation error message if input is not valid. We setup observable to perform its emissions and notifications on a specified thread with observeOn() operator. If we don’t need to perform operations on the certain thread we’re letting UseCase or Repository to setup thread that observable will work on.

changeUserEmailUseCase.changeUserEmail(view.getEmailInput()) .observeOn(AndroidSchedulers.mainThread()) 

Finally, we subscribe to item emissions, specifying ‘happy’ and ‘error’ cases.

...
.subscribe(
 { //Email is successfully changed },
 { //Error while changing email }
)

UseCase itself will use implementation of repository to perform email changing operation.

appUserRepository.changeUserEmail(email)
    .map { DomainMapper.fromDataUserToDomain(it) }

It also maps elements to the understandable by UI layer type, by reducing amount of outer layer dependencies or adding fields that might be needed to display content .

The repository will perform email changing operation with DB, Firebase or API, cache results in local memory and distribute new info to subscribers through RxJava subjects.

Links

Kotlin language docs & tutorials
RxJava and RxAndroid

 

 

Share.

About Author

Leave A Reply