Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Architecting Your Apps

Architecting Your Apps

App architecture design is a crucial consideration for ensuring that your app is robust, testable and maintainable. Common architecture patterns used in android development include MVC(Model-View-Controller), MVP(Model-View-Presenter) and MVVM(Model-View-ViewModel). In this session, we will look at how to use MVVM to create production-quality android apps.

Beatrice Kinya

November 06, 2021
Tweet

More Decks by Beatrice Kinya

Other Decks in Programming

Transcript

  1. Introduction ➔ A simple android app: ◆ Will have a

    screen(s) where the users interact with the app. ◆ Could be connecting to an api, to fetch or send data e.g. TMDB API. ◆ Could be saving data locally in the app db.
  2. App Architecture: Principles ➔ Goal: Build robust , maintainable, testable

    production-quality apps. ➔ Separation of concerns between business logic and UI layer code. ◆ Let the UI classes, Activities or Fragments, contain logic that handles UI interactions, i.e displaying data to user, receiving user events etc.. • You can avoid many life-cycle related problems. • Adaptability to change. • Testable code.
  3. Principles cont’ ➔ Drive UI from a Model, preferably a

    persistence model. ◆ Your app continues to work in cases of no network or users network is flaky. ◆ Models are unaffected by apps lifecycle. This way the user does not lose data in case OS destroys your app to clear up resources
  4. Android Architectures ➔ Commonly used architectures patterns in Android development

    include: ◆ MVC: Model View Controller ◆ MVP: Model View Presenter ◆ MVVM: Model View ViewModel
  5. MVC ➔ It suggests splitting the app code into 3

    components: ◆ Model. This component manages application data. • It contains the business logic and rules of the application. • It handles network and database API. ◆ View. This is the UI layer. • It provides visualization of data stored in the models. • It offers an interface for users to interact with the app
  6. MVC cont’ ➔ Controller. This is the logic layer. ◆

    it establishes relationship between View and Model. ◆ It gets notified of the user behavior and updates Model as required.
  7. MVVM ➔ Model. ◆ Represents the data layer ➔ View

    . ◆ Represents UI layer ➔ ViewModel.
  8. ViewModel ➔ It contains data specific to a UI component,

    such as a fragment or an activity. ➔ It contains data handling logic to communicate with the model. ➔ It is not affected by configuration changes such as screen rotation.
  9. Livedata ➔ It is an observable data holder class. ◆

    It can notify other components such as activities or fragments of data changes ➔ It is lifecycle aware, meaning it respects lifecycle of other components such as activities and fragments. ◆ This way Livedata only updates app components that are in active state. A component is considered to be in active state if its life cycle is in STARTED or RESUMED state.
  10. Repository ➔ ViewModel delegates updating model, i.e. saving data, fetching

    data, updating data to the repository module. ➔ It knows where to get the data from. ➔ It know which API calls to make when data is updated. ➔ Repositories are mediators between different data sources such as web services, caches, and persistent models
  11. Resources ➔ Guide To App architecture ➔ Repository Pattern ➔

    Github Project Further Reading ➔ Clean Architecture in Android