1

I'm making a network request inside a Repository class in my Android app. I'm doing this for learning purposes, so I'm trying to understand, without using RXJava, how would I update the UI all the way from my Repository?

The trail of calls goes like so MainActivity -> Presenter -> Interactor -> Repository -> Network

And here is my code in the repository

WeatherRepository {
    WeatherNetwork network = new WeatherNetwork();

    public CurrentWeather getCurrentWeather(float lat, float lng) {
        network.getDailyWeather(lat, lng, new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onResponse(Response response) throws IOException {
                try{
                    String jsonData = response.body().string();
                    if (response.isSuccessful()) {
                        CurrentWeather currentWeather = getCurrentWeatherData(jsonData);

                    }
                }  catch(JSONException e) {
                    Log.d("DWPresen" + " JSONEXCEPTION", e.getMessage());
                } catch(IOException e){
                    Log.d("DWPresent" + " IOEXCEPTION", e.getMessage());
                }

            }
        });
    }
}
Rafa
  • 3,219
  • 4
  • 38
  • 70

2 Answers2

1

Imho there are two classical ways to do such a thing:

One way would be to pass a callback object down to the repository and call the callback action when you get your response.

The second (and imho more elegant) way would be to use local broadcasts in your app. They can be used to transfer messages (like if something happened in your service and you want to notify other threads - like the UI thread - about it.)

There are quite a lot of tutorials about (local) broadcasts in android. Normal broadcasts are used to communicate between apps (and between the system and an app), while local broadcasts can be used to just send information within your app.

stamanuel
  • 3,731
  • 1
  • 30
  • 45
0

There is a library called EventBus that works very well with MVP structure.

Briefly, it passes the event(an object with data) from one class to another. In your case, you can pass an event with CurrentWeather object inside of it from Repository to Presenter and then update UI.