Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Android MVP

No description
by

Diego Marcher

on 5 June 2017

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Android MVP

Presenter
View
MVC vs MVP
Interfaz con la lógica de negocio

Única interfaz con manejador de DB

Construye loader (en caso de haberlo)

Se comunica con el presenter a través de eventos (Otto)
Model
Orquestador entre Model y View

Obtiene datos del modelo y los entrega a la vista.

Permite
mockear
vista y modelo en tests (referencias)
¿Qué es MVP?
Model-View-Presenter

Patrón de arquitectura

Variante de MVC
MVP

TDD (Test-driven development)

FDD (Feature-driven development)
Presenter First
Sin lógica de negocio

Contiene los widgets

Controla eventos sobre widgets

Evento del usuario -> notificación al presenter (Bus events)
¿Por qué MVP?
Eliminar
activities
incomprensibles, intesteables y difíciles de mantener.

Separar presentación de lógica

Ideal para GUI
Activity
Registra listeners (ej.: sensores, giroscopio, acelerómetro)

Instancia de presenter

Forwarding de eventos al presenter

Notifica (en caso necesario) al presenter de
callbacks
como onPause y onResume.
Android MVP + Presenter First
Gracias
Diego Marcher
public class
MainModel {

private final
DemoRepository
repository
;


public

MainModel
(DemoRepository repository) {

this
.
repository
= repository
;
}


public
Loader<Cursor>
getMainLoader
(Activity activity) {

return

repository
.getMainLoader(activity
,

false
)
;
}


public void
someAsyncTask(
final int
id) {

new
AsyncTask<Void
,
Integer
,
Boolean>() {

@Override

protected
Boolean
doInBackground
(Void... voids) {

/** Server Call **/

return true;
}


@Override

protected void

onPostExecute
(Boolean success) {
}
}.execute()
;
}
}
public class
MainPresenter
implements
LoaderManager.LoaderCallbacks<Cursor> {

private final
MainView
view
;

private final
MainModel
model
;


public

MainPresenter
(MainView view
,
MainModel model) {

this
.
view
= view
;

this
.
model
= model
;
}


@Override

public
Loader<Cursor>
onCreateLoader
(
int
id
,
Bundle args) {

return

model
.getMainLoader(
view
.getActivity())
;
}


@Override

public void

onLoadFinished
(Loader loader
,
Cursor cursor) {
List<DemoEntity> entities =
new
ArrayList<>();

if
(cursor !=
null
) {
cursor.moveToFirst()
;

while
(!cursor.isAfterLast()) {
entities.add(
new
DemoEntity(cursor));
cursor.moveToNext()
;
}
}
displayItems(entities)
;
}


@Override

public void

onLoaderReset
(Loader loader) {
}


private void

displayItems
(List<DemoEntity> entities) {

view
.setAdapter(
new
DemoEntitiesAdapter(
view
.getActivity()
,
entities))
;
}
}
public class
MainView
extends
ActivityView {


private final
Bus
bus
;

private
RecyclerView
list
;


public

MainView
(AppCompatActivity activity
,
Bus bus) {

super
(activity)
;

this
.
bus
= bus
;

list
= (RecyclerView) activity.findViewById(R.id.
list
)
;

list
.setLayoutManager(
new
LinearLayoutManager(activity))
;
}


public void
setAdapter
(DemoEntitiesAdapter adapter) {

list
.setAdapter(adapter)
;

list
.setOnTouchListener(
new
OnTouchListener() {

@Override

public boolean

onTouch
(View v
,
MotionEvent e) {

bus
.post(
new
ListTouchedEvent())
;

return false;
}
});
}


public static class
ListTouchedEvent {
}
}
public class
MainActivity
extends
AppCompatActivity {

private
MainPresenter
presenter
;


@Override

protected void

onCreate
(Bundle savedInstanceState) {

super
.onCreate(savedInstanceState)
;
setContentView(R.layout.
activity_main
)
;

Bus bus = BusProvider.getInstance()
;

presenter
=
new
MainPresenter(

new
MainView(
this,
bus)
,

new
MainModel(
new
DemoRepository())
)
;
}


@Override

protected void

onResume
() {

super
.onResume()
;
BusProvider.
register
(
presenter
)
;
}


@Override

protected void

onPause
() {

super
.onPause()
;
BusProvider.
unregister
(
presenter
)
;
}
}
Bus events
Notifican eventos sin conocer a los destinatarios (
subscribers
)

Post
Invocado desde el que notifica
Recibe como parámetro una instancia de un evento.
Subscribe
Anotación de método que escucha eventos.
Recibe como parámetro el evento.
Generalmente
, es el presenter.
Bus events
Activity
Registra y des-registra al presenter en el bus.
Obtiene una instancia del bus y la entrega a la vista y al modelo (en caso necesario)
Presenter
Se suscribe a eventos posteados por la vista o el modelo.
Vista y Modelo
Pueden (o no) postear eventos al bus.
Full transcript