2

Context

I've written an Android App to play single media file with its lyrics being displayed in the activity. The Activity screen also has a Play-Pause toggle button and a seek-bar to forward/rewind using drag. And the Activity launches the Started Service in its onCreate() method.

I'm attempting to layer the app confirming to MVP design pattern. I'm looking for sample code as guidance that fits this kind of a situation. Your help is much appreciated.

Things I'm keen to learn

  1. In case like mine where the Activity and the Started Service have two-way communications using EventBus, where does the code for EventBus listener lie in? Is Presenter not having any role in this?
  2. How is the code tested for EventBus related code - both unit and integrating testing?
  3. What code comes in Activity? What comes in Service? And What does the Presenter contract with Activity and Service look like? Lastly, how does the implementation of this presenter look like?
  4. How do you manage code for MenuOptions click events in the case of MVP?

Any reference to existing code-repository in Github/Bitbucket is really appreciated, if a detailed explanation hurts for you. Thanks in advance.

karthiks
  • 7,049
  • 7
  • 47
  • 62

1 Answers1

1

I, personally, wouldn't implement this with an EventBus. Anyway, here are my answers for your questions.

  1. In case like mine where the Activity and the Started Service have two-way communications using EventBus, where does the code for EventBus listener lie in? Is Presenter not having any role in this?

Yes, Presenter is registered to the EventBus to listen for incoming events and tells the view what to display. The other way round, if user clicks on Play / Pause button, this event is delivered to your service through the Presenter (maybe through EventBus. maybe through android intents that are delivered to the service, whatever ... but delivering this event i.e. through eventbus happens in Presenter ). So Activity is not communicating with Service directly. It's the presenter of your View (activity) that mediates between View (Activity) and playback service.

  1. How is the code tested for EventBus related code - both unit and integrating testing?

You don't have to test the EventBus per se. It's already tested by the author of the library. So pass the EventBus as constructor parameter to your presenter and while unit testing you can pass a Mocked EventBus to your Presenter to check if your Presenter registers / unregisters correctly and you can fire up some events to see if the Event is handled by the Presenter correctly and the expected method on your View is invoked (Mock the View) and vice versa for sending Events (like Play / Pause) to the service.

  1. What code comes in Activity? What comes in Service? And What does the Presenter contract with Activity and Service look like? Lastly, how does the implementation of this presenter look like?

See answer to 1. Activity just displays UI widget. Forwards clicks event to presenter. Presenter communicates with Service (for example over EventBus). The other way around: If your service change the state (like end of audio track reached) then it will inform the Presenter (i.e. trough EventBus) that audio playback is finished and Presenter tells the View to display the UI correspondingly.

  1. How do you manage code for MenuOptions click events in the case of MVP?

As described in 1. and 3. If its going to change the state of your business logic (i.e. Play / Pause) it "sinks" from your View (activity) through your Presenter down to the business logic (i.e. playback service).

sockeqwe
  • 15,574
  • 24
  • 88
  • 144
  • thank you very much indeed. I didn't frame Qn.2 right but got what I'm expecting from your answer to it as well. While I ended up with implementation like your advised, it feels so damn good to hear that from a seasoned person like you. Again thanks! – karthiks Jun 21 '17 at 13:04