Instantiate instance with a callback composed of events

Viewed 74

I am currently implementing an app that uses a printer from Hoin. They provided an SDK that I can integrate in Android Studio (done) and tell me to instantiate the instance like this:

val mHoinPrinter = HoinPrinter.getInstance(context, mode, callback)

For context and mode, no biggies. For the callback I am having troubles. In their documentation they specify that the callback is of interface PrinterCallback which is composed like so:

Public interface PrinterCallback {
     Public void onState(int state); //state callback, parameter is status code, refer to status code definition
     Public void onError(int errorCode); //Error callback, parameter is error code, refer to error code definition
     Public void onEvent(PrinterEvent event); //Event callback, parameter is PrinterEvent event, refer to PrinterEvent definition
}

Which is great because it gives me 4 events that should be triggered at some point.

My issue is that I have no clue how to actually instantiate it. I know that I need to do it like so val mHoinPrinter = HoinPrinter.getInstance(this.context, 2, callback) but where I have troubles is in defining the callback. Should I write a function? should I setup something extra? How can I define logics for this callback?

It's very confusing when reading the docs online.

1 Answers

You have to create an implementation of the PrinterCallback interface and then pass that implementation as a parameter to getInstance method. The implementation can be created as so

val callback = object: PrinterCallback{
    override fun onState(state: Int) {
        // DO SOMETHING
    }

    override fun onError(errorCode: Int) {
         // DO SOMETHING
    }

    override fun onEvent(event: Int) {
         // DO SOMETHING
    }
}

If you do everything else as specified in the documentation then these three functions will be invoked on their respective events

Related