0

I want to have a Splash screen that has an inderteminate ProgressDialog and its progress gets updated by async calls from within a Presenter class (from MVP architecture).

I have a number of API calls to make to my BaaS server and for every successfull call, I would like to update the progress bar.

What's the best way to accomplish this?

I have been trying using EventBus to send notifications to my SplashActivity but it seems that all the API calls are first completed and only then the bus notifications are getting consumed and updating the UI.

What I have done so far is:

SplashActivity:

@Subscribe(threadMode = ThreadMode.MAIN)
    public void onProgressBar(String event) {
        Timber.d("onProgressBar");
        if(event.contains("Done")) {
            roundCornerProgressBar.setProgress(100);
        } else {
            roundCornerProgressBar.setProgress(roundCornerProgressBar.getProgress() + 10);
        }
        textViewTips.setText(event);
    }

Presenter:

InstanceID iid = InstanceID.getInstance(ctx);
String id = iid.getId();
mDataManager.getPreferencesHelper().putInstanceId(id);
GSUtil.instance().deviceAuthentication(id, "android", mDataManager);
GSUtil.instance().getPropertySetRequest("PRTSET", mDataManager);

GSUtil:

public void deviceAuthentication(String deviceId, String deviceOS, final DataManager mDataManager) {
        gs.getRequestBuilder().createDeviceAuthenticationRequest()
                .setDeviceId(deviceId)
                .setDeviceOS(deviceOS)
                .send(new GSEventConsumer<GSResponseBuilder.AuthenticationResponse>() {
                    @Override
                    public void onEvent(GSResponseBuilder.AuthenticationResponse authenticationResponse) {
                        if(mDataManager != null) {
                            mDataManager.getPreferencesHelper().putGameSparksUserId(authenticationResponse.getUserId());
                        }
                        EventBus.getDefault().post("Reading player data");
                    }
                });
    }

public void getPropertySetRequest(String propertySetShortCode, final DataManager mDataManager) {
        gs.getRequestBuilder().createGetPropertySetRequest()
                .setPropertySetShortCode(propertySetShortCode)
                .send(new GSEventConsumer<GSResponseBuilder.GetPropertySetResponse>() {
                    @Override
                    public void onEvent(GSResponseBuilder.GetPropertySetResponse getPropertySetResponse) {
                        GSData propertySet = getPropertySetResponse.getPropertySet();
                        GSData scriptData = getPropertySetResponse.getScriptData();
                        try {
                            JSONObject jObject = new JSONObject(propertySet.getAttribute("max_tickets").toString());
                            mDataManager.getPreferencesHelper().putGameDataMaxTickets(jObject.getInt("max_tickets"));
                            jObject = new JSONObject(propertySet.getAttribute("tickets_refresh_time").toString());
                            mDataManager.getPreferencesHelper().putGameDataTicketsRefreshTime(jObject.getLong("refresh_time"));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        EventBus.getDefault().post("Game data ready");
                        EventBus.getDefault().post("Done!");
                    }
                });
    }

Right now I am just showing you 2 API calls, but I will need another 2. Thank you

Felipe Caldas
  • 2,492
  • 3
  • 36
  • 55

1 Answers1

0

I found the answer! It's easier that I thought, which is unfortunate as I spend about 4 hours on this:

First, I created two new methods on my MVPView interface:

public interface SplashMvpView extends MvpView {

    void updateProgressBarWithTips(float prog, String tip);
    void gameDataLoaded();
}

Then, in the presenter itself, I call every API call and for every call, I update the View with the updateProgressBarWithTips method and when everything is completed, I finalise it so I can move from Splash screen to Main screen:

private void doGSData(String id) {
        getMvpView().updateProgressBarWithTips(10, "Synced player data");
        GSAndroidPlatform.gs().getRequestBuilder().createDeviceAuthenticationRequest()
                .setDeviceId(id)
                .setDeviceOS("android")
                .send(new GSEventConsumer<GSResponseBuilder.AuthenticationResponse>() {
                    @Override
                    public void onEvent(GSResponseBuilder.AuthenticationResponse authenticationResponse) {
                        if(mDataManager != null) {
                            mDataManager.getPreferencesHelper().putGameSparksUserId(authenticationResponse.getUserId());
                        }

                        getMvpView().updateProgressBarWithTips(10, "Synced game data");
                        GSAndroidPlatform.gs().getRequestBuilder().createGetPropertySetRequest()
                                .setPropertySetShortCode("PRTSET")
                                .send(new GSEventConsumer<GSResponseBuilder.GetPropertySetResponse>() {
                                    @Override
                                    public void onEvent(GSResponseBuilder.GetPropertySetResponse getPropertySetResponse) {
                                        GSData propertySet = getPropertySetResponse.getPropertySet();
                                        GSData scriptData = getPropertySetResponse.getScriptData();
                                        try {
                                            JSONObject jObject = new JSONObject(propertySet.getAttribute("max_tickets").toString());
                                            mDataManager.getPreferencesHelper().putGameDataMaxTickets(jObject.getInt("max_tickets"));
                                            jObject = new JSONObject(propertySet.getAttribute("tickets_refresh_time").toString());
                                            mDataManager.getPreferencesHelper().putGameDataTicketsRefreshTime(jObject.getLong("refresh_time"));
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                        getMvpView().gameDataLoaded();
                                    }
                                });
                    }
                });
    }

I hope this helps someone, if you're using MVP architecture.

Cheers

Felipe Caldas
  • 2,492
  • 3
  • 36
  • 55