MVP

for

Listing Templates

-nihal111
Nihal Singh

Why?

  • To showcase my work (more technical depth)
  • To share knowledge about existence of a feature in our codebase that follows MVP pattern (the re-architectured "right" way)

Overview

  • The Listing Templates Feature
  • How did I make it?
  • Some MVP Tips
  • Recreating a Screen Together
  • Android Studio walkthrough

Demo Video

Listing Templates

How did I do it?

Step 1: Understand the SPEC

Understanding the spec and being able to picture the flow of the feature is crucial for efficient implementation.

Step 2: Sketch out the flow

Have a design/flow-diagram to work with.

Make a rough one by hand if you don't get one from the designer. It helps.

Design Blueprint

Step 3: Sign the contract

Once the first two steps are done, it's time to create a Contract for the ViewProxy, Presenter and Model layers.

This does not have to be final. It just serves as a great starting point and allows you to get some clarity.

Make an architectural design doc, using the template.

Step 3: Sign the contract

The doc should essentially contain the following-

 

  • Breakdown/Decomposition of the feature in ViewProxy, Model and Presenter layers
  • (Having a draft contract for each screen works well)
  • List of classes with brief distribution of responsibilities.

Step 4: Dive into implementation

Once you've signed the Contract,

it's time to commit.

Keep writing tests as you go, or at least keep thinking about how you'll write them. This should keep the code testable.

MVP tips

Activity

 

Keep the activities lean

 

Delegate lifecycle events to the Presenter

 

To call super methods in the Activity, use a custom interface, that gets called by the presenter

MVP tips

Presenter

It's a good idea to have the Presenter extend from the LifecycleAwarePresenter (com.kouzoh.mercari.common.presenter)

The lifecycle (FragmentActivity.getLifecycle()) can be passed to the Presenter during construction

This allows for clearing the disposables at the right lifecycle events.

MVP tips

ViewProxy

The ViewProxy should be initialized inside the Activity and passed on to the presenter during it's construction.

The ViewProxy can take in the activity as parameter during construction. This allows for obtaining the context and finding views by ID.

Let's do it together!

Template Create and Edit Screen

TemplateCreateEditActivity

SPEC Breakdown

​Create Screen

  • Set Toolbar Title and Button text
  • Setup onClickListener for the Create button
  • On Click of the Create Button-
    • If entered title and description are non-empty, make the API request to create a new template
    • Else show a dialog alert

SPEC Breakdown

Edit Screen

  • Set Toolbar Title and Button text
  • Fill fields with selected template
  • Setup onClickListener for the Edit button
  • On Click of the Edit Button-
    • If entered title and description are non-empty, make the API request to edit the opened template
    • Else show a dialog alert

Set Toolbar Title and Button text

When?

onCreate

How?

Activity

Presenter

ViewProxy

Activity delegates onCreate to Presenter

Presenter calls view to update UI, based on intent received

Depends on?

intent.getStringExtra(S.MODE)

Set onClickListeners for the Button

When?

onCreate

How?

Activity

Presenter

ViewProxy

Activity delegates onCreate to Presenter

Presenter defines the onClickListener and passes it to the ViewProxy

Depends on?

intent.getStringExtra(S.MODE)

What does the contract look like?

Presenter

  • onCreate
     
  • createTemplate
  • editTemplate

Repository

  • createTemplate(title, description): Observable
     
  • editTemplate(id, title, description): Observable

ViewProxy

  • setTitleForCreate()
  • setTitleForEdit()
  • setTextForSaveButton()
  • setOnClickListenerForSave
    Button(listener)

     
  • fillFields(title, description)
     
  • getTitleText()
  • getDescriptionText()
     
  • displayErrorMessage()

Android Studio

MVP for Listing Templates

By Nihal Singh

MVP for Listing Templates

  • 981