A controller is a delegate of a Android view. It holds a
UiView that needs to be implemented
by the corresponding Android view. Whenever the controller changes its model, it calls
UiView#update()to update the view. Be careful when you are calling
UiView#update() on the non-UI thread,
because in this case, it needs to use
#uiThreadRunner to post a runnable wrapping
UiView#update() to ensure the view is updated on UI thread. Use
#runTask(Task) to
run heavy actions, especially network calls, on non-UI thread.
In the above way the controller works as a presenter in MVP pattern. If you prefer MVVM pattern.
You can define events and
#postEvent(Object) to the view. Since
#postEvent(Object)guarantees the event is posted onto UI thread, you don't need to worry about on which thread the
event is posted.
When some code needs to run on non-UI thread, use
#runTask(Task),
#runTask(Task,Task.Callback) to run it on a different thread. Make sure if
UiView#update() needs to be called in scope of @link Task#execute(Task.Monitor)} use
#uiThreadRunner to post back to UI thread.
The controller has 4 injected fields. They can be replaced by providing special or mocking objects
in unit tests.
- a
EventBus annotated by
EventBusC to receive events from managers and other non ui components
- a
EventBus annotated by
EventBusV to send event to Android views
- a
ExecutorService that runs
Task via
#runTask(Task) on non-UI thread.
by default, it has a fixed thread tool at size 10. You can inject a mocked
ExecutorServicethat runs everything on the main thread in your Unit Tests to avoid multi-thread complexity.
- a protected
UiThreadRunner to post a
Runnable onto UI thread. Make sure
use it to
UiView#update() view inside method block
Task#execute(Task.Monitor)which is run on non-UI thread. It's NOT necessary to use it in callback methods in
Task.Callback since the framework has already guaranteed it.
In non-Android environment, typically in unit tests, the framework will also provide a default
uiThreadRunner that runs everything on the same thread as the caller's.