João Martins
João Martins

Reputation: 794

How do I know what changed in my Observable class?

So I'm using the Observer pattern in my app in order to get notified of changes in another class without having to look for them.

I have a Singleton class which extends Observable. Inside this class I have two CountDownTimer type variables. Eachs of these contains two methods: onTick() and onFinished().

Let's call those Timers A and B for the sake of simplicity.

Every time A.onTick(), A.onFinished(), B.onTick(), B.onFinished() are called, I must call notifyObservers() to notify my Observer that something has changed.

Until here everything works fine. The problem is that I know something has changed, but I don't know what exactly has changed. Depending on which one notified me, I must execute some code on the Observer side.

How do I know which of these methods notified me?

Upvotes: 2

Views: 401

Answers (2)

matdev
matdev

Reputation: 4283

You can create a custom EventType class and pass it to Observable.notifyObservers(Object arg):

public class EventType {

    String eventType; //"onTick" or "onFinish"
    TimerType timerType;

    EventType(String eventType, TimerType timerType){

        this.eventType = eventType;
        this.timerType = timerType;
    }
} 

TimerType is an enum type:

public enum TimerType {
    A,
    B;        
}

and create TimerA and TimerB classes extending CountDownTimer:

private class TimerA extends CountDownTimer {

    final EventType onTickEvent = new EventType("onTick", TimerType.A);
    final EventType onFinishEvent = new EventType("onFinish", TimerType.A);

    @Override
    public void onTick(long millisUntilFinished) {
        notifyObservers(onTickEvent);
    }

    @Override
    public void onFinish() {
        notifyObservers(onFinishEvent)
    }
}

The Observerwill receive the EventType instance via its update(Observable o, Object arg); in the arg argument

Upvotes: 2

92AlanC
92AlanC

Reputation: 1387

Use LiveData instead of Observable. LiveData is quite useful because not only it's observable but also it binds to your activity's lifecycle so you don't have to worry about handling it yourself.

Maybe this example will help you:

public class MyTimerWrapper {

    public static MyTimerWrapper getInstance() {
        // Your singleton logic

        createTimers();

        return instance;
    }

    private CountDownTimer timerA;
    private CountDownTimer timerB;

    private MutableLiveData<TimerEvent> timerALiveData = new MutableLiveData<TimerEvent>();
    private MutableLiveData<TimerEvent> timerBLiveData = new MutableLiveData<TimerEvent>();

    public LiveData<TimerEvent> startTimerA() {
        timerA.start();
        return timerALiveData;
    }

    public LiveData<TimerEvent> startTimerB() {
        timerB.start();
        return timerBLiveData;
    }

    private void createTimers() {
        createTimerA();
        createTimerB();
    }

    private void createTimerA() {
        timerA = new CountDownTimer(30000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                // If you're running on another thread
                timerALiveData.postValue(TimerEvent.TICK);

                // Otherwise
                timerALiveData.setValue(TimerEvent.TICK);
            }

            @Override
            public void onFinish() {
                // If you're running on another thread
                timerALiveData.postValue(TimerEvent.FINISH);

                // Otherwise
                timerALiveData.setValue(TimerEvent.FINISH);
            }
        }
    }

    private void createTimerB() {
        // Same as createTimerA, but with timerB
    }
}

public enum TimerEvent {
    TICK,
    FINISH
}

Now to observe that data in your activity:

MyTimerWrapper timerWrapper = MyTimerWrapper.getInstance();
timerWrapper.startTimerA().observe(this, new Observer {
    @Override
    public void onChanged(TimerEvent timerEvent) {
        // Here you'll be able to see whether timerA is ticking or finished
    }
})

Upvotes: 2

Related Questions