Top Banner
Event Bus Android Simplificado
22

Event Bus: Android Simplificado

Jul 17, 2015

Download

Technology

mickele
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Event Bus: Android Simplificado

Event BusAndroid Simplificado

Page 3: Event Bus: Android Simplificado

Componentes Android

Activity Service

Fragment Fragment Custom View

Page 4: Event Bus: Android Simplificado

Comunicação Android

Activity Service

Fragment Fragment Custom View

Page 5: Event Bus: Android Simplificado

Delegate Android

Activity

ServiceFragment

Fragment Custom View

Page 6: Event Bus: Android Simplificado

Activity se torna God Object

Activity precisa implementar diversas interfaces

Excesso de cast e instance of

class GodActivity implements A, B, C, D, E, Fuck {}

Delegate :( Android

Page 7: Event Bus: Android Simplificado

Publish / Subscribe Papéis

Publisher Event Bus SubscriberEvent Event

Page 8: Event Bus: Android Simplificado

Event Bus Android

ServiceFragment

Fragment Custom View

Event Bus

Activity

Page 9: Event Bus: Android Simplificado

EventBus by greenrobot

Page 10: Event Bus: Android Simplificado

EventBus

// Create a bus

EventBus bus = new EventBus();

// Or use the default

EventBus bus = EventBus.getDefault();

// Publish

bus.post(new AnyEvent());

https://github.com/greenrobot/EventBus

Page 11: Event Bus: Android Simplificado

// Register - onCreate, onStart

bus.register(this);

// Unregister - onDestroy, onStop

bus.unregister(this);

public void onEvent(AnyEvent e) {

[...]

}

public void onEventMainThread(AnyEvent e) {}

EventBus https://github.com/greenrobot/EventBus

Page 12: Event Bus: Android Simplificado

// Sticky Publish

bus.postSticky(new AnyEvent());

// Sticky Register

bus.registerSticky(this);

EventBus https://github.com/greenrobot/EventBus

Page 13: Event Bus: Android Simplificado

Otto by Square

Page 14: Event Bus: Android Simplificado

// Create a main thread bus

Bus bus = new Bus();

// Create an any thread bus

Bus bus = new Bus(ThreadEnforcer.ANY);

// Publish

bus.post(new AnyEvent());

Otto https://square.github.io/otto/

Page 15: Event Bus: Android Simplificado

Otto

// Register - onCreate, onStart

bus.register(this);

// Unregister - onDestroy, onStop

bus.unregister(this);

@Subscribe

public void onAnyEvent(AnyEvent e) {

[...]

}

https://square.github.io/otto/

Page 16: Event Bus: Android Simplificado

Otto

@Subscribe

public void onAnyEvent(AnyEvent e) {

[...]

}

@Produce

public AnyEvent produceAnyEvent() {

return AnyEvent.getLast();

}

https://square.github.io/otto/

Page 17: Event Bus: Android Simplificado

Via Reflection

Fornece um Bus Default

Valor Anterior via *Sticky

Threading feita no Evento

OttoEventBus

Via Annotation

Gerenciamento Manual de Bus

Valor Inicial via @Produce

Threading feita no Bus

Page 18: Event Bus: Android Simplificado

Caso de usoActivity

Fragment

Custom ViewFragment

Page 19: Event Bus: Android Simplificado

Crie Suvinil https://play.google.com/store/apps/details?id=com.basf.suvinil.crie

Page 20: Event Bus: Android Simplificado

420 LOC

0 Interfaces

Eventos organizados

Delegate EventBus

750+ LOC

9 Interfaces

Interfaces fragmentadas

Page 21: Event Bus: Android Simplificado

Referências

Google Developer Experts

https://medium.com/google-developer-experts/

Event-driven programming for Android (part I)

https://medium.com/google-developer-experts/event-driven-programming-for-android-part-i-f5ea4a3c4eab

Event-driven programming for Android (part II)

https://medium.com/google-developer-experts/event-driven-programming-for-android-part-ii-b1e05698e440

Event-driven programming for Android (part III)

Em breve...