Slide 1

Slide 1 text

Event Bus Android Simplificado

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Componentes Android Activity Service Fragment Fragment Custom View

Slide 4

Slide 4 text

Comunicação Android Activity Service Fragment Fragment Custom View

Slide 5

Slide 5 text

Delegate Android Activity Service Fragment Fragment Custom View

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

Publish / Subscribe Papéis Publisher Event Bus Subscriber Event Event

Slide 8

Slide 8 text

Event Bus Android Service Fragment Fragment Custom View Event Bus Activity

Slide 9

Slide 9 text

EventBus by greenrobot

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

// 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

Slide 12

Slide 12 text

// Sticky Publish bus.postSticky(new AnyEvent()); // Sticky Register bus.registerSticky(this); EventBus https://github.com/greenrobot/EventBus

Slide 13

Slide 13 text

Otto by Square

Slide 14

Slide 14 text

// 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/

Slide 15

Slide 15 text

Otto // Register - onCreate, onStart bus.register(this); // Unregister - onDestroy, onStop bus.unregister(this); @Subscribe public void onAnyEvent(AnyEvent e) { [...] } https://square.github.io/otto/

Slide 16

Slide 16 text

Otto @Subscribe public void onAnyEvent(AnyEvent e) { [...] } @Produce public AnyEvent produceAnyEvent() { return AnyEvent.getLast(); } https://square.github.io/otto/

Slide 17

Slide 17 text

Via Reflection Fornece um Bus Default Valor Anterior via *Sticky Threading feita no Evento Otto EventBus Via Annotation Gerenciamento Manual de Bus Valor Inicial via @Produce Threading feita no Bus

Slide 18

Slide 18 text

Caso de uso Activity Fragment Custom View Fragment

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

420 LOC 0 Interfaces Eventos organizados Delegate EventBus 750+ LOC 9 Interfaces Interfaces fragmentadas

Slide 21

Slide 21 text

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) https://medium.com/google-developer-experts/event-driven-programming-for-android-part-iii-3a2e68c3faa4

Slide 22

Slide 22 text

@mickele @ademar111190