2

I know there are similar questions but it still doesn't answer my question in the manner I need for my current situation.

I have three activity presenters that each need to call a certain data remotely which will therefore call the activity presenter back when data arrives. To create this data listener I created an interface listener and since all three Presenters ask for the same data and retrieve it, all three presenters implement this interface listener.

Interface listener:

interface ListenerInterface {

     onGotData();

}

Presenter one:

class PresenterOne implements ListenerInterface{

    public void getData() {
        DataManager dataManager = new DataManager();
        dataManager.getData(this);
    }

    @Override
    public void onGotData(Data data) {
        //Do something with data
    }
}

Presenter two very similar to presenter one:

class PresenterTwo implements ListenerInterface{

    public void getData() {
        DataManager dataManager = new DataManager();
        dataManager.getData(this);
    }

    @Override
    public void onGotData(Data data) {
        //Do something with data
    }
}

Assume Presenter three is exactly the same as the previous. The data manager class:

class DataManager {

    public void getData(final ListenerInterface listener) {

        //Gets data
        addOnCompleteListener(new OnCompleteListener<Data data > () {
            @Override
            public void onComplete (@NonNull DataCall < Data > dataCall) {
                listener.onGotData(dataCall.getResults());
            }

        });
    }
}

Would doing so someone call all three presenters since the interface is the one doing the calling or only call the presenter that is passed? Is there anything I should worry about if I followed way? If anyone who knows the Android framework well could provide a detailed answer so I could learn from it more that would be great. The reason I want to do this is I want to communicate through interfaces between classes.

Sorry if this question is simple for some people but I am still learning.

Thank you very much in advance.

Mr.O
  • 813
  • 7
  • 14
  • 1
    Hi, I hope you will learn more and more each day, and become a professional. Do not worry, everybody in that industry learns something each day. Maybe you can look [here](https://stackoverflow.com/a/16443645/6254994) to see a small example of how to use interfaces between classes. If you do not understand this, I will try my best to give you a different example. – Ahmet Batu Orhan Jun 25 '18 at 11:01
  • Thanks for your positivity. The example does help thank you, the example answers the question that only the class being passed in the parameter will get called but if anyone reading this could confirm so, that would be great. Thank you Ahmet! – Mr.O Jun 25 '18 at 11:09
  • 1
    You have not to see the interface as an object. The interface is never the one doing the call. It's always your presenter. The interface is just a way to tell "This object respect this specification", the specification being defined by the interface. So when DataManger calls listener.onGotData(), it always call this mehod on the given object (the given presenter). So only one will be triggered – vincrichaud Jun 25 '18 at 11:13
  • Alright I understand, thanks a lot @vincrichaud. – Mr.O Jun 25 '18 at 11:19
  • Hey @vincrichaud I know it's been a while but add your comment as an answer and I'll mark it as answer – Mr.O Jul 16 '18 at 14:16

1 Answers1

1

you can use RxBus implementation to make global event (e.g. your onGotData). First you have to create RxBus class.

public class RxBus {
private static RxBus instance;
private PublishSubject<Event> subject = PublishSubject.create();

public static synchronized RxBus getInstance() {
    if (instance == null) {
        instance = new RxBus();
    }

    return instance;
}

private RxBus(){}

public void postEvent(Event event){
    subject.onNext(event);
}

public Observable<Event> getEvents(){
    return subject;
}

}

And now, you should subscribe to it in BaseActivity or something like this (depends or your project structure).

private RxBus rxbus;
private Subscription rxBusSubscription;

@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rxBus = RxBus.getInstance();
    }

@Override
protected void onResume() {
    super.onResume();

    if (shouldSubscribeRxBus()) {
        rxBusSubscription = rxBus.getEvents()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(event -> {
                    if (event.getEventType() == Event.EventType.DATA_RECEIVED) {
                        onGotData(data);
                    }
                });
    }
}

Now implement you onGotData as you want.

When you catch data received call:

class DataManager {

public void getData(final ListenerInterface listener) {

    //Gets data
    addOnCompleteListener(new OnCompleteListener<Data data > () {
        @Override
        public void onComplete (@NonNull DataCall < Data > dataCall) {
            RxBus.getInstance().postEvent(new GotDataEvent(dataCall.getResults()));
        }

    });
}
}

You can create your Event classes structure as you want.

Ikazuchi
  • 433
  • 2
  • 12
  • Hi @Ikazuchi, thank you very much for your answer. It's a very interesting solution to what I'm trying to do and which is helpful to learn for future use but it didn't answer my direct question about the effects of using interfaces the same way the commentators answered above. I will give you an up vote for this answer does look into different solutions, my up vote will take effect when my reputation rises above 15. Thank you :) – Mr.O Jun 25 '18 at 20:10