8. CLEAN ARCHITECTURE | VIPER
VIPER IS AN APPLICATION OF CLEAN ARCHITECTURE
TO IOS APPS (AND NOW ANDROID!)
9. V - View
I - Interactor
P - Presenter
E - Entity
R - Routing
VIPER | WHAT DOES IT MEAN?
10. VIEW
Displays what it is told to by the Presenter and relays
user input back to the Presenter.
11. PRESENTER
Contains view logic for preparing content for display (as
received from the Interactor) and for reacting to user
inputs (by requesting new data from the Interactor).
34. VIPER | ENTITY
data class Movie(
val id: String = "",
val posterPath: String = "",
val title: String = "",
val overview: String = "",
val releaseDate : String = ""
)
}
35. VIPER | ROUTER
class HomeRouter(private val context: Context) {
fun navigateToDetail(id: String) {
val intent = Intent(context, DetailActivity::class.java)
intent.putExtra(
DetailActivity.EXTRA_SELECTED_MOVIE_ID, id)
context.startActivity(intent)
}
}
36. VIPER | TESTS
@Mock
lateinit var homePresenterOutput: HomeContracts.HomePresenterOutput
@Mock
lateinit var homeInteractorInput: HomeContracts.HomeInteractorInput
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
homePresenter = HomePresenter(homePresenterOutput, homeInteractorInput)
homePresenter.setPresenterOutput(homePresenterOutput)
given(homeInteractorInput.loadMovies()).will { homePresenter.moviesLoaded(movies) }
}
37. VIPER | TESTS
@Test
fun shouldLoadMoviesWhenViewIsLoaded() {
homePresenter.viewLoaded()
verify(homeInteractorInput).loadMovies()
}
@Test
fun shouldShowMoviesWhenMoviesAreLoaded() {
homePresenter.moviesLoaded(movies)
verify(homePresenterOutput).showMovies(movies)
}
41. VIPER | CONS
• Overkill for small projects
• Overhead for inexperienced teams and risk of
mixing it with different strategies (MVP/MVC/
MVVM)
42. VIPER | CONS
• Overkill for small projects
• Overhead for inexperienced teams and risk of
mixing it with different strategies (MVP/MVC/
MVVM)
• Tedious modules creation without code generators
43. VIPER | CONS
• Overkill for small projects
• Overhead for inexperienced teams and risk of
mixing it with different strategies (MVP/MVC/
MVVM)
• Tedious modules creation without code generators
• Might not be a perfect fit for any kind of project
48. VIPER | PROS
• Improved responsibility balance
• Well-defined contracts
• Smaller classes and methods
• Easier to test
49. VIPER | PROS
• Improved responsibility balance
• Well-defined contracts
• Smaller classes and methods
• Easier to test
• Easier to maintain and add new features
50. VIPER | PROS
• Improved responsibility balance
• Well-defined contracts
• Smaller classes and methods
• Easier to test
• Easier to maintain and add new features
• Possibility to use the same architecture between
iOS and Android projects
52. SECTION | ARCHITECTURE COMPONENTS
"The most important thing you
should focus on is the separation of
concerns in your app.”
https://developer.android.com/topic/libraries/architecture/guide.html
56. SECTION | PRESENTER vs VIEWMODEL
• Both are responsible for preparing the data for
the UI
57. SECTION | PRESENTER vs VIEWMODEL
• Both are responsible for preparing the data for
the UI
• The Presenter has a reference to the view, while
ViewModel doesn’t
58. SECTION | PRESENTER vs VIEWMODEL
• Both are responsible for preparing the data for
the UI
• The Presenter has a reference to the view, while
ViewModel doesn’t
• The ViewModel enables data binding
59. SECTION | PRESENTER vs VIEWMODEL
• Both are responsible for preparing the data for
the UI
• The Presenter has a reference to the view, while
ViewModel doesn’t
• The ViewModel enables data binding
• The ViewModel provides observable data to the
view
60. SECTION | ARCHITECTURE COMPONENTS
"If your UI is complex, consider creating a
Presenter class to handle UI modifications.
This is usually overkill, but might make
your UIs easier to test."
61.
62. VIPER | CONCLUSION
"It is impossible to have
one way of writing apps
that will be the best for
every scenario. (…) If you
already have a good way
of writing Android apps,
you don't need to change."