SlideShare una empresa de Scribd logo
1 de 64
Descargar para leer sin conexión
VIPER ON ANDROID
Gabriel B. Zandavalle
Thiago “Fred” Porciúncula
• Clean Architecture
• What is VIPER
• Cases
• Code samples
• Cons e Pros
• Architecture Components
• Questions
CLEAN ARCHITECTURE | WHAT IT IS?
https://8thlight.com/blog/uncle-bob/2012/08/13/the-clean-architecture.html
CLEAN ARCHITECTURE | WHAT IT IS?
VIPER
CLEAN ARCHITECTURE | VIPER
VIPER IS AN APPLICATION OF CLEAN ARCHITECTURE
TO IOS APPS (AND NOW ANDROID!)
V - View
I - Interactor
P - Presenter
E - Entity
R - Routing
VIPER | WHAT DOES IT MEAN?
VIEW
Displays what it is told to by the Presenter and relays
user input back to the Presenter.
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).
INTERACTOR
Contains the business logic as specified by a use case.
ENTITY
Contains basic model objects used by the Interactor.
ROUTING
Contains navigation logic for describing which
screens are shown in which order.
VIPER | MODULE
WHY VIPER ON ANDROID?
CASES | UBER
https://eng.uber.com/new-rider-app/
CASES | UBER
CASES | BRAGI
http://luboganev.github.io/blog/clean-architecture-pt1/
CASES | COURSERA
https://news.realm.io/news/360andev-richa-khandelwal-effective-
android-architecture-patterns-java/
CASES | COURSERA
CASES | COURSERA
CASES | COURSERA
View Presenter Interactor
Router
Dagger
Entity
VIPER | MODULE
HomeActivity
HomePresenterOutput
HomePresenter HomeInteractor
HomePresenterInput
HomeInteractorOutput
HomeInteractorInput
class HomeContracts {



interface HomePresenterInput {

fun viewLoaded()

}

interface HomeInteractorInput {

fun loadMovies()

}


interface HomeInteractorOutput {

fun moviesLoaded(items: List<Movie>)

}


interface HomePresenterOutput {

fun showMovies(items: List<Movie>)

}

}
VIPER | CONTRACTS
class HomeContracts {



interface HomePresenterInput {

fun viewLoaded()

}

interface HomeInteractorInput {

fun loadMovies()

}


interface HomeInteractorOutput {

fun moviesLoaded(items: List<Movie>)

}


interface HomePresenterOutput {

fun showMovies(items: List<Movie>)

}

}
VIPER | CONTRACTS
HomeActivity
HomePresenterOutpu
HomePresenter HomeInteractor
HomePresenterInput
HomeInteractorOutpu
HomeInteractorInput
class HomeActivity: AppCompatActivity(), HomeContracts.HomePresenterOutput {



@Inject

lateinit var homePresenterInput: HomeContracts.HomePresenterInput



override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_home)
injectDependencies()



homePresenterInput.viewLoaded()

}



override fun showMovies(items: List<Movie>) {

moviesRecyclerView.adapter = MovieAdapter(items)

moviesRecyclerView.layoutManager = LinearLayoutManager(this)

}

}
VIPER | VIEW
class HomeActivity: AppCompatActivity(), HomeContracts.HomePresenterOutput {



@Inject

lateinit var homePresenterInput: HomeContracts.HomePresenterInput



override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_home)
injectDependencies()



homePresenterInput.viewLoaded()

}



override fun showMovies(items: List<Movie>) {

moviesRecyclerView.adapter = MovieAdapter(items)

moviesRecyclerView.layoutManager = LinearLayoutManager(this)

}

}
VIPER | VIEW
HomeActivity
HomePresenterOutpu
HomePresenter HomeInteractor
HomePresenterInput
HomeInteractorOutpu
HomeInteractorInput
viewLoaded()
showMovies()
VIPER | PRESENTER
class HomePresenter(
private val homePresenterOutput: HomePresenterOutput,
private val homeInteractorInput: HomeInteractorInput) :

HomePresenterInput, HomeInteractorOutput {



override fun viewLoaded() {

homeInteractorInput.loadMovies()

}



override fun moviesLoaded(items: List<Movie>) {

homePresenterOutput.showMovies(items)

}

}
VIPER | PRESENTER
class HomePresenter(
private val homePresenterOutput: HomePresenterOutput,
private val homeInteractorInput: HomeInteractorInput) :

HomePresenterInput, HomeInteractorOutput {



override fun viewLoaded() {

homeInteractorInput.loadMovies()

}



override fun moviesLoaded(items: List<Movie>) {

homePresenterOutput.showMovies(items)

}

}
HomeActivity
HomePresenterOutpu
HomePresenter HomeInteractor
HomePresenterInput
HomeInteractorOutpu
HomeInteractorInput
viewLoaded() loadMovies()
moviesLoaded()showMovies()
VIPER | INTERACTOR
class HomeInteractor(private val moviesApi: MoviesAPI) : HomeInteractorInput {



lateinit var homeInteractorOutput: HomeInteractorOutput



override fun loadMovies() {

moviesApi.getList("1", "api_key").subscribe ({

homeInteractorOutput.moviesLoaded(it.items)

}, {

Log.e(TAG, "Error loading movies", it)

})

}

}
VIPER | INTERACTOR
class HomeInteractor(private val moviesApi: MoviesAPI) : HomeInteractorInput {



lateinit var homeInteractorOutput: HomeInteractorOutput



override fun loadMovies() {

moviesApi.getList("1", "api_key").subscribe ({

homeInteractorOutput.moviesLoaded(it.items)

}, {

Log.e(TAG, "Error loading movies", it)

})

}

}
HomeActivity
HomePresenterOutpu
HomePresenter HomeInteractor
HomePresenterInput
HomeInteractorOutpu
HomeInteractorInput
loadMovies()
moviesLoaded()
VIPER | ENTITY
data class Movie(

val id: String = "",

val posterPath: String = "",

val title: String = "",

val overview: String = "",

val releaseDate : String = ""

)

}
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)

}

}

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) }

}
VIPER | TESTS
@Test

fun shouldLoadMoviesWhenViewIsLoaded() {

homePresenter.viewLoaded()

verify(homeInteractorInput).loadMovies()

}



@Test

fun shouldShowMoviesWhenMoviesAreLoaded() {

homePresenter.moviesLoaded(movies)

verify(homePresenterOutput).showMovies(movies)

}
VIPER | MODULES
VIPER | CONS
VIPER | CONS
• Overkill for small projects
VIPER | CONS
• Overkill for small projects
• Overhead for inexperienced teams and risk of
mixing it with different strategies (MVP/MVC/
MVVM)
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
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
VIPER | PROS
VIPER | PROS
• Improved responsibility balance
VIPER | PROS
• Improved responsibility balance
• Well-defined contracts
VIPER | PROS
• Improved responsibility balance
• Well-defined contracts
• Smaller classes and methods
VIPER | PROS
• Improved responsibility balance
• Well-defined contracts
• Smaller classes and methods
• Easier to test
VIPER | PROS
• Improved responsibility balance
• Well-defined contracts
• Smaller classes and methods
• Easier to test
• Easier to maintain and add new features
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
ANDROID ARCHITECTURE
COMPONENTES
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
View
Presenter?
Interactor
Model
SECTION | PRESENTER vs VIEWMODEL
SECTION | PRESENTER vs VIEWMODEL
• Both are responsible for preparing the data for
the UI
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
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
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
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."
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."
https://github.com/arctouch-gabrielzandavalle/tmdbviper
OBRIGADO
We are hiring !

Más contenido relacionado

Similar a Viper on Android

iPOJO - The Simple Life - Richard Hall, Visiting Assistant Professor at Tufts...
iPOJO - The Simple Life - Richard Hall, Visiting Assistant Professor at Tufts...iPOJO - The Simple Life - Richard Hall, Visiting Assistant Professor at Tufts...
iPOJO - The Simple Life - Richard Hall, Visiting Assistant Professor at Tufts...
mfrancis
 
Google Developer Group(GDG) DevFest Event 2012 Android talk
Google Developer Group(GDG) DevFest Event 2012 Android talkGoogle Developer Group(GDG) DevFest Event 2012 Android talk
Google Developer Group(GDG) DevFest Event 2012 Android talk
Imam Raza
 
Philip Shurpik "Architecting React Native app"
Philip Shurpik "Architecting React Native app"Philip Shurpik "Architecting React Native app"
Philip Shurpik "Architecting React Native app"
Fwdays
 

Similar a Viper on Android (20)

Oracle JavaScript Extension Toolkit Web Components Bring Agility to App Devel...
Oracle JavaScript Extension Toolkit Web Components Bring Agility to App Devel...Oracle JavaScript Extension Toolkit Web Components Bring Agility to App Devel...
Oracle JavaScript Extension Toolkit Web Components Bring Agility to App Devel...
 
Spring Cloud Function & Project riff #jsug
Spring Cloud Function & Project riff #jsugSpring Cloud Function & Project riff #jsug
Spring Cloud Function & Project riff #jsug
 
Building maintainable app
Building maintainable appBuilding maintainable app
Building maintainable app
 
Android Widget
Android WidgetAndroid Widget
Android Widget
 
Building maintainable app #droidconzg
Building maintainable app #droidconzgBuilding maintainable app #droidconzg
Building maintainable app #droidconzg
 
iPOJO - The Simple Life - Richard Hall, Visiting Assistant Professor at Tufts...
iPOJO - The Simple Life - Richard Hall, Visiting Assistant Professor at Tufts...iPOJO - The Simple Life - Richard Hall, Visiting Assistant Professor at Tufts...
iPOJO - The Simple Life - Richard Hall, Visiting Assistant Professor at Tufts...
 
Innovation Generation - The Mobile Meetup: Android Best Practices
Innovation Generation - The Mobile Meetup: Android Best PracticesInnovation Generation - The Mobile Meetup: Android Best Practices
Innovation Generation - The Mobile Meetup: Android Best Practices
 
Model View Presenter
Model View Presenter Model View Presenter
Model View Presenter
 
Serverless Single Page Apps with React and Redux at ItCamp 2017
Serverless Single Page Apps with React and Redux at ItCamp 2017Serverless Single Page Apps with React and Redux at ItCamp 2017
Serverless Single Page Apps with React and Redux at ItCamp 2017
 
Thoughts on Component Resuse
Thoughts on Component ResuseThoughts on Component Resuse
Thoughts on Component Resuse
 
Acercándonos a la Programación Funcional a través de la Arquitectura Hexag...
Acercándonos a la Programación Funcional a través de la Arquitectura Hexag...Acercándonos a la Programación Funcional a través de la Arquitectura Hexag...
Acercándonos a la Programación Funcional a través de la Arquitectura Hexag...
 
Unity and Azure Mobile Services using Prime31 plugin
Unity and Azure Mobile Services using Prime31 pluginUnity and Azure Mobile Services using Prime31 plugin
Unity and Azure Mobile Services using Prime31 plugin
 
Red Hat JBoss BRMS and BPMS Workbench and Rich Client Technology
Red Hat JBoss BRMS and BPMS Workbench and Rich Client TechnologyRed Hat JBoss BRMS and BPMS Workbench and Rich Client Technology
Red Hat JBoss BRMS and BPMS Workbench and Rich Client Technology
 
Advanced #6 clean architecture
Advanced #6  clean architectureAdvanced #6  clean architecture
Advanced #6 clean architecture
 
Google Developer Group(GDG) DevFest Event 2012 Android talk
Google Developer Group(GDG) DevFest Event 2012 Android talkGoogle Developer Group(GDG) DevFest Event 2012 Android talk
Google Developer Group(GDG) DevFest Event 2012 Android talk
 
The fundamental problems of GUI applications and why people choose React
The fundamental problems of GUI applications and why people choose ReactThe fundamental problems of GUI applications and why people choose React
The fundamental problems of GUI applications and why people choose React
 
From Containerization to Modularity
From Containerization to ModularityFrom Containerization to Modularity
From Containerization to Modularity
 
springtraning-7024840-phpapp01.pdf
springtraning-7024840-phpapp01.pdfspringtraning-7024840-phpapp01.pdf
springtraning-7024840-phpapp01.pdf
 
Philip Shurpik "Architecting React Native app"
Philip Shurpik "Architecting React Native app"Philip Shurpik "Architecting React Native app"
Philip Shurpik "Architecting React Native app"
 
Introduction to angular with a simple but complete project
Introduction to angular with a simple but complete projectIntroduction to angular with a simple but complete project
Introduction to angular with a simple but complete project
 

Último

CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
anilsa9823
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Último (20)

CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 

Viper on Android

  • 1. VIPER ON ANDROID Gabriel B. Zandavalle Thiago “Fred” Porciúncula
  • 2. • Clean Architecture • What is VIPER • Cases • Code samples • Cons e Pros • Architecture Components • Questions
  • 3. CLEAN ARCHITECTURE | WHAT IT IS? https://8thlight.com/blog/uncle-bob/2012/08/13/the-clean-architecture.html
  • 4. CLEAN ARCHITECTURE | WHAT IT IS?
  • 5.
  • 6.
  • 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).
  • 12. INTERACTOR Contains the business logic as specified by a use case.
  • 13. ENTITY Contains basic model objects used by the Interactor.
  • 14. ROUTING Contains navigation logic for describing which screens are shown in which order.
  • 16. WHY VIPER ON ANDROID?
  • 25. VIPER | MODULE HomeActivity HomePresenterOutput HomePresenter HomeInteractor HomePresenterInput HomeInteractorOutput HomeInteractorInput
  • 26. class HomeContracts {
 
 interface HomePresenterInput {
 fun viewLoaded()
 }
 interface HomeInteractorInput {
 fun loadMovies()
 } 
 interface HomeInteractorOutput {
 fun moviesLoaded(items: List<Movie>)
 } 
 interface HomePresenterOutput {
 fun showMovies(items: List<Movie>)
 }
 } VIPER | CONTRACTS
  • 27. class HomeContracts {
 
 interface HomePresenterInput {
 fun viewLoaded()
 }
 interface HomeInteractorInput {
 fun loadMovies()
 } 
 interface HomeInteractorOutput {
 fun moviesLoaded(items: List<Movie>)
 } 
 interface HomePresenterOutput {
 fun showMovies(items: List<Movie>)
 }
 } VIPER | CONTRACTS HomeActivity HomePresenterOutpu HomePresenter HomeInteractor HomePresenterInput HomeInteractorOutpu HomeInteractorInput
  • 28. class HomeActivity: AppCompatActivity(), HomeContracts.HomePresenterOutput {
 
 @Inject
 lateinit var homePresenterInput: HomeContracts.HomePresenterInput
 
 override fun onCreate(savedInstanceState: Bundle?) {
 super.onCreate(savedInstanceState)
 setContentView(R.layout.activity_home) injectDependencies()
 
 homePresenterInput.viewLoaded()
 }
 
 override fun showMovies(items: List<Movie>) {
 moviesRecyclerView.adapter = MovieAdapter(items)
 moviesRecyclerView.layoutManager = LinearLayoutManager(this)
 }
 } VIPER | VIEW
  • 29. class HomeActivity: AppCompatActivity(), HomeContracts.HomePresenterOutput {
 
 @Inject
 lateinit var homePresenterInput: HomeContracts.HomePresenterInput
 
 override fun onCreate(savedInstanceState: Bundle?) {
 super.onCreate(savedInstanceState)
 setContentView(R.layout.activity_home) injectDependencies()
 
 homePresenterInput.viewLoaded()
 }
 
 override fun showMovies(items: List<Movie>) {
 moviesRecyclerView.adapter = MovieAdapter(items)
 moviesRecyclerView.layoutManager = LinearLayoutManager(this)
 }
 } VIPER | VIEW HomeActivity HomePresenterOutpu HomePresenter HomeInteractor HomePresenterInput HomeInteractorOutpu HomeInteractorInput viewLoaded() showMovies()
  • 30. VIPER | PRESENTER class HomePresenter( private val homePresenterOutput: HomePresenterOutput, private val homeInteractorInput: HomeInteractorInput) :
 HomePresenterInput, HomeInteractorOutput {
 
 override fun viewLoaded() {
 homeInteractorInput.loadMovies()
 }
 
 override fun moviesLoaded(items: List<Movie>) {
 homePresenterOutput.showMovies(items)
 }
 }
  • 31. VIPER | PRESENTER class HomePresenter( private val homePresenterOutput: HomePresenterOutput, private val homeInteractorInput: HomeInteractorInput) :
 HomePresenterInput, HomeInteractorOutput {
 
 override fun viewLoaded() {
 homeInteractorInput.loadMovies()
 }
 
 override fun moviesLoaded(items: List<Movie>) {
 homePresenterOutput.showMovies(items)
 }
 } HomeActivity HomePresenterOutpu HomePresenter HomeInteractor HomePresenterInput HomeInteractorOutpu HomeInteractorInput viewLoaded() loadMovies() moviesLoaded()showMovies()
  • 32. VIPER | INTERACTOR class HomeInteractor(private val moviesApi: MoviesAPI) : HomeInteractorInput {
 
 lateinit var homeInteractorOutput: HomeInteractorOutput
 
 override fun loadMovies() {
 moviesApi.getList("1", "api_key").subscribe ({
 homeInteractorOutput.moviesLoaded(it.items)
 }, {
 Log.e(TAG, "Error loading movies", it)
 })
 }
 }
  • 33. VIPER | INTERACTOR class HomeInteractor(private val moviesApi: MoviesAPI) : HomeInteractorInput {
 
 lateinit var homeInteractorOutput: HomeInteractorOutput
 
 override fun loadMovies() {
 moviesApi.getList("1", "api_key").subscribe ({
 homeInteractorOutput.moviesLoaded(it.items)
 }, {
 Log.e(TAG, "Error loading movies", it)
 })
 }
 } HomeActivity HomePresenterOutpu HomePresenter HomeInteractor HomePresenterInput HomeInteractorOutpu HomeInteractorInput loadMovies() moviesLoaded()
  • 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)
 }
  • 40. VIPER | CONS • Overkill for small projects
  • 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
  • 45. VIPER | PROS • Improved responsibility balance
  • 46. VIPER | PROS • Improved responsibility balance • Well-defined contracts
  • 47. VIPER | PROS • Improved responsibility balance • Well-defined contracts • Smaller classes and methods
  • 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
  • 53.
  • 55. SECTION | PRESENTER vs VIEWMODEL
  • 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."