# Android SDK

### This sample app shows how to integrate the IMGA front row seat SDK into an Android application.

Supported IDEs:

* [Android Studio](https://github.com/IMGARENA/front-row-seat-android#androidstudio)

Supported build systems:

* [Gradle](https://github.com/IMGARENA/front-row-seat-android#gradle)

### Adding the library to the project

#### Using Github Packages Maven repository

Add the repository to your build.gradle file

```
repositories {
     maven {
            url = uri("https://maven.pkg.github.com/IMGARENA/front-row-seat-android-package")
            credentials {
                username = "username"
                password = "password"
            }
        }
}
```

Add the package dependencies to your build.gradle file

```
dependencies {

...

implementation 'com.imgarena:eventcentre:1.0.2'
}
```

#### Adding the library locally to the project

Copy the eventcentre.aar to the libs folder of your application:

```
flatDir {
   dirs 'libs'
}
```

Then include the event centre in the dependencies section of your application:

```
dependencies {
   implementation(name:'eventcentre', ext:'aar')
}
```

### Usage

#### Initialiasing SDK

**Configuration parameters**

```
var params: EventCentreParams = EventCentreParams(
    operator = "[OPERATOR-NAME]",
    sport = "mma",
    version = "5.x",
    eventId = "667",
    targetModule = "full",
    language = "en"
)

// Optionally initialise a context
params.initialContext = mapOf(
	"view" to "Fight",
	"fightId" to "Insert fight ID",
)
```

**Creating the Event Center instance**

```
var imga = EventCentre(container, params, listener)
```

#### Event Centre: Emitting Messages

The **emitMessage** method is available on the instance of the eventCentre, it is used to send messages to the Event Centre. Only supported message topics will be passed to the Event Centre.

```
fun emitMessage(topic: EventCentreMessageTopics, message: EventCentreMessage)
```

The **EventCentreMessageTopics** enum contains the following values:

* CONTEXT\_UPDATE: Topic for covering general UI state updates, for example navigation changes or the user selecting a player in the UI.
* SELECTION\_UPDATE: Dedicated topic for handling user selection updates. The selected field within the message data handles both selecting and deselecting updates.

The **EventCentreMessage** type is an alias of a HashMap\<String, Any> class. Keys must be Strings and the Values can be any type.

**Updating the Context**

There is a convinience method to update the context:

```
fun updateContext ( context: EventCentreContext) {
```

This method will update the context of the widget, and receives only the parameter context of type **EventCentreContext**. This type is a subclass of **EventCentreMessage** and contains the following properties:

| Name.   | Type    |
| ------- | ------- |
| fightId | integer |

#### Event Centre: Receiving Messages

In order to subscribe to different messages emitted by the SDK, the **EventCentreListener** interface must be implemented.

```
class TestActivity : AppCompatActivity(), EventCentreListener {
```

**Subscribing to status changes**

The SDK will send every change in its status using the following Listener method:

```
override fun onStatusChanged(status: EventCentreStatus) {
    Log.d("eventcentretest", "Transition to status: ${status.name}")

    Toast.makeText(this, status.toString(), Toast.LENGTH_SHORT).show()
}
```

The **EventCentreStatus** enum contains the following values:

* INITIALISING: the SDK is initialising and not fully loaded.
* INITIALIZED: the SDK is ready to interact.
* ACTIVE: All the views are fully loaded.
* EMITTING\_MESSAGE: the SDK is emitting a message.
* INACTIVE: the SDK is closed and can't receive any message.

**Subscribing to new messages**

To receive context or selection updates from the SDK, the host app must subscribe to the Listener using this method:

```
override fun onMessageReceived(topic: EventCentreMessageTopics, message: JSONObject) {
    Log.d("eventcentretest", "Message received: $message")
    Toast.makeText(this, message.toString(2), Toast.LENGTH_SHORT).show()
}
```

The **EventCentreMessageTopics** enum contains the following values:

* HANDSHAKE\_ATTEMPT:
* HANDSHAKE\_FAILED: Dedicated topic to signify the handshake between Event Centre and your site failed to complete. This topic is emitted by the integration library, it should only be subscribed to, not emitted.
* HANDSHAKE\_REPLY:
* APP\_LOADED: Topic sent when all the views are fully loaded.
* CLEAR\_SELECTIONS:
* CONTEXT\_UPDATE: Topic for covering general UI state updates, for example navigation changes or the user selecting a player in the UI.
* SELECTION\_UPDATE: Dedicated topic for handling user selection updates. The selected field within the message data handles both selecting and deselecting updates.
* OPTIONS\_UPDATE:
* ERROR: Topic sent if the SDK had any problem to be initialized. This topic is emitted by the integration library, it should only be subscribed to, not emitted.

#### Stop the sdk

```
// Instance the sdk
var imga = EventCentre(container, params, listener )

// Close the instance
imga.close()
```

### Credits

IMGA SDK is owned and maintained by the IMGA Development team.

### License

Private license
