Reputation: 7932
As a background, I am using MVP mosby for my android application.
Currently I have this UI design requirement, where from almost everywhere across the app. (4 different activity/fragment/recycler adapter). If the user taps on an item, I should present a dialog (same everywhere) and the dialog itself needs to make API calls, and need to handle any error that comes back.
I wrote this the present dialog logic inside a helper class.
@EBean
public class DialogService {
Dialog d;
public void presentUniversalDialog(Context context, Data data) {
d = new Dialog(context);
.. set view
.. change some text view based on data
.. make some api calls
}
private void makeAPiCall() {
.. some API calls here. On Return, update the dialog d if not null
}
}
So then in my other activities, I just need to inject this service and I can easily show the dialog by calling
@EActivity
public class MyRandomActivity extends Activity {
@Bean
DialogService dialogService;
@Click(R.id.my_random_button)
void onButtonClick() {
dialogService.presentUniversalDialog(this, data);
}
}
Now, the good news is that all the random activities should not be bothered by this dialog as long as it is launched. So I don't need to pass random event listeners around.
But how do I structure my dialogService code to deal with async events?
For example, the "data" field might contain only an id so I need to make API calls to populate the whole data. And once user clicks on OK. I need to send a request to confirm.
For now, I worked around by basically keeping track of the API calls via some member fields inside the DialogService. But as code gets large, this will quickly fill up and starts to be super confusing.
What is a recommended way of writing this "universal dialog"? Should I perhaps only use service per dialog? Or are there some other ways?
Upvotes: 0
Views: 464
Reputation: 15929
Treat dialog as View (in MVP) and give it its own presenter as gateway to your business logic (to make http request). So just treat Dialog not different than you would treat a Activity or Fragment in MVP. Also worthwhile checking DialogFragment
Upvotes: 1