I’ve given a task at my company to modularise some core functionality on android project into separate library for common use.
Right now we have mvvm-base. For data, we have
Bean that extends
BaseObservable. (Apparently the naming derives from Spring? I don’t know).
We’re also have
ViewModel that holds the
Bean. (Now the
Bean is put into generics, like
class ViewModel<B extends Bean>).
ViewModel has responsibility to hold
Bean and also process logic like HTTP Request, updating
And then we’re also have
Presenter class that have responsibility to handle view events. Says when a button is clicked, a method in
Presenter is being called, inside that method, we call few method on
View. That method in the
View is executing some method to process logic on
DataBinding library is being used to bind
Presenter to layout file.
So our view (assuming there is button to press and data to show) will have at least one instance of
ViewModel and one instance of
Presenter. Also the UI flow become like this:
View (onLoad, instantiate ViewModel, bind ViewModel to layout) -> ViewModel (logic, if any, update Bean)
View (onClick event is bind to method on Presenter) -> Presenter (call View method) -> View (receive message from presenter, execute logic from ViewModel) -> ViewModel (logic)
I know it’s kind of cluttered and the function of
Presenter is kind of useless and responsibility of
ViewModel is ambiguous. For now I have an idea to pack the whole
Presenter into one class but I’m afraid I don’t have strong base as opinion to my team. I’d like to know your opinion about it.
Also as a bonus: In my team, they’ve been using
AsyncTask to make network request, my senior told me to research alternative and he is mentions
OkHttp. In my opinion,
OkHttp is far better because it’s actively maintained. What do you think?