Skip to content

Implementing listeners

Vladimír Záhradník edited this page Aug 8, 2019 · 3 revisions

In order to use our library, user needs to implement a listener and register for events. When an event occurs, our library will call one of methods implemented by an user to do some specific action.

Create a listener

As a part of our library, we've created several abstract classes, representing interfaces to be implemented by an user. These interfaces represent an API (a contract), so that there's a guarantee that an object registered as a listener does provide all the required methods.

Create an OnClickListener

To create an listener, which is able to receive OnClick events, create a class, which inherits from IOnClickListener interface and provide implementation for an onClick(...) callback method, i.e.:

#include <ObjectButton.h>
#include <interfaces/IOnClickListener.h>

class OnClickListener : private virtual IOnClickListener {
public:
    OnClickListener() = default;
    ....

private:
    void onClick(ObjectButton &button) override;
    ...
};

void OnClickListener::onClick(ObjectButton &button) {
  // React to button click
}

Create an OnDoubleClickListener

Very similarily, to create an listener, which is able to receive OnDoubleClick events, create a class, which inherits from IOnDoubleClickListener interface and provide implementation for an onDoubleClick(...) callback method, i.e.:

#include <ObjectButton.h>
#include <interfaces/IOnClickListener.h>

class OnDoubleClickListener : private virtual IOnDoubleClickListener {
public:
    OnDoubleClickListener() = default;
    ....

private:
    void onDoubleClick(ObjectButton &button) override;
    ...
};

void OnDoubleClickListener::onDoubleClick(ObjectButton &button) {
  // React to button click
}

Create an OnPressListener

Finally, to create an listener, which is able to receive OnPress, OnRelease, OnLongPressStart and OnLongPressEnd events, create a class, which inherits from IOnPressListener interface and provide implementation for methods defined in the OnPressListener.

Note: If you are not interested in some of the events received by this listener, you could provide void implementation of a specific callback method. See an example below.

#include <ObjectButton.h>
#include <interfaces/IOnPressListener.h>

class OnPressListener : private virtual IOnPressListener {
public:
    OnPressListener() = default;
    ...

private:
    // Void implementation, which does nothing
    void onPress(ObjectButton &button) override {};

    // Void implementation, which does nothing
    void onRelease(ObjectButton &button) override {};

    void onLongPressStart(ObjectButton &button) override;

    void onLongPressEnd(ObjectButton &button) override;
    ...
};

void OnPressListener::onLongPressStart(ObjectButton &button) {
    // Do some action after long press has started
}

void OnPressListener::onLongPressEnd(ObjectButton &button) {
    // Do some action after long press has started
}
Clone this wiki locally