# Services

**Shared Instance**

Use the shared instance as your interface to the MOVE SDK.

```swift
static let shared: MoveSDK
```

## **Detection Services**

### **Start Automatic Detection**

Starts the required detection services stated in the [MoveConfig](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/moveconfig) passed on initialization. Starting the service will set the [MoveSDKState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movestate) to `.running`.

{% hint style="warning" %}
**Precondition**: SDK should be in`.ready`[MoveSDKState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movestate)
{% endhint %}

```swift
func startAutomaticDetection()
```

### **Stop Automatic Detection**

Stops the automatic detection service, including all SDK services like driving detection, points of interest, walking and places. Stopping the service will set the [MoveSDKState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movestate) back to `.ready`.&#x20;

{% hint style="warning" %}
**Precondition**: SDK should be in`.running`[MoveSDKState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movestate).
{% endhint %}

```swift
func stopAutomaticDetection()
```

### **Force Trip Recognition**

Temporarily calibrates the SDK to the highest detection mode.

In order to optimize battery consumption, the SDK goes through different detection modes with different battery consumption levels, depending on the user's current behavior and activity. In general, the SDK is responsible for alternating between those different detection modes.&#x20;

The SDK also provides hosting apps this API to call if they have their own reasons (like buttons, sensors or beacons) to believe that the user is starting a trip. This will make sure the SDK is on the highest detection state to detect the trip faster.

{% hint style="warning" %}
&#x20;**Precondition**: SDK should be in`.running` [MoveSDKState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movestate).
{% endhint %}

```swift
func forceTripRecognition()
```

### **Finish Current Trip**

Ends the current ongoing trip.

This API will end the ongoing trip and set [MoveTripState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movetripstate) back to `.idle`. The SDK is responsible for detecting the trip start and end points. The SDK also provides hosting apps this API to call if they have their own reasons (like sensors or beacons) to believe that the user's trip has ended.

{% hint style="warning" %}
**Precondition**:[ MoveTripState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movetripstate) should be in an active trip.
{% endhint %}

```swift
func finishCurrentTrip()
```

{% hint style="info" %}
If this API is called while the user is active on a driving trip, a new trip will start again right away. If the intention is to un-track the ongoing trip, call `ignoreCurrentTrip`API.

More information about trip states can be found in [MoveTripState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movetripstate).
{% endhint %}

### **Ignore Current Trip**

Ignores the current ongoing trip.&#x20;

This API will set the ongoing [MoveTripState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movetripstate) to `.ignored`. Data of ignored trips is purged and not sent to the server.

{% hint style="warning" %}
**Precondition**: [MoveTripState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movetripstate) should be in an active trip.
{% endhint %}

```swift
func ignoreCurrentTrip()
```

## Processing Services

### **Get SDK State**

Gets the current[ MoveSDKState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movestate).

```swift
func getSDKState() -> MoveSDKState
```

| **Return**                                                                               |                  |
| ---------------------------------------------------------------------------------------- | ---------------- |
| [MoveSDKState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movestate) | Latest SDK State |

### **Set SDK State Listener**

Provide a block to be invoked every time [MoveSDKState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movestate) changes.

{% hint style="info" %}
Set the [MoveSDKState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movestate) listener before initializing the SDK to anticipate the SDK [State](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movestate) changes triggered by the initialization API. \
Here the host app can start the SDK services when [MoveSDKState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movestate) is`.ready`or handle errors if occurred.
{% endhint %}

```swift
func setSDKStateListener(listener: SDKStateCallback)
func setSDKStateListener(_ listener: SDKStateCallback)
```

| **Callback**                                                                                               |                                                                                                                                                                                                                                                            |
| ---------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [SDKStateCallback](https://docs.movesdk.com/move-platform/move-sdk-1.x/models/untitled#sdk-state-listener) | Block that gets invoked every time[ MoveSDKState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movestate) is changed and provides the updated [ MoveSDKState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movestate). |

### **Get SDK Auth State**

Gets the current [MoveAuthState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/moveauthstate).

```swift
func getAuthState() -> MoveAuthState
```

| **Return**                                                                                    |                  |
| --------------------------------------------------------------------------------------------- | ---------------- |
| [MoveAuthState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/moveauthstate) | Latest SDK State |

### **Set SDK Auth State Listener**

Provide a block to be invoked every time [MoveAuthState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/moveauthstate) changes.

```swift
func setAuthStateUpdateListener(listener: AuthStateCallback)
func setAuthStateUpdateListener(_ listener: AuthStateCallback)
```

| **Callback**                                                                                                        |                                                                                                                                              |
| ------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
| [AuthStateCallback](https://docs.movesdk.com/move-platform/move-sdk-1.x/models/untitled#auth-state-update-listener) | Block that gets invoked every time [MoveAuthState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/moveauthstate) is updated. |

{% hint style="warning" %}

### Authentication Expiry

The host app is expected to monitor [MoveAuthState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/moveauthstate) updates via`authStateUpdateListener`(Android / [iOS](#set-sdk-auth-state-listener)) API and handle those changes accordingly.

Check [Authentication updates and expiry](https://docs.movesdk.com/move-platform/move-sdk-1.x/models/moveauthstate#authentication-updates-and-expiry) for more details about authentication expiry and renewal.
{% endhint %}

### **Get Trip State**

&#x20;Gets the current [MoveTripState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movetripstate).

```swift
func getTripState() -> MoveTripState
```

| **Return**                                                                                    |                                                                                                       |
| --------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- |
| [MoveTripState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movetripstate) | Current [MoveTripState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movetripstate) |

### **Set Trip State Listener**

Provide a block to be invoked every time [MoveTripState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movetripstate) changes.

```swift
func setTripStateListener(listener: TripStateCallback)
func setTripStateListener(_ listener: TripStateCallback)
```

| Callback                                                                                                     |                                                                                                                                                                                                                                                                       |
| ------------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [TripStateCallback](https://docs.movesdk.com/move-platform/move-sdk-1.x/models/untitled#trip-state-listener) | Block that gets invoked every time  [MoveTripState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movetripstate) is changed and provides the updated  [MoveTripState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movetripstate). |

### Get Device Status

Gets the current [MoveDeviceStatus](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movedevicestatus)*.*

```swift
func getDeviceStatus() -> MoveDeviceStatus
```

| Return                                                                                              |                                                                                                             |
| --------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------- |
| [MoveDeviceStatus](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movedevicestatus) | Current [MoveDeviceStatus](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movedevicestatus) |

### **Set Device Status Listener**

Provide a block to be invoked every time [MoveDeviceStatus](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movedevicestatus) changes.

```swift
func setDeviceStatusListener(listener: DeviceStatusCallback)
func setDeviceStatusListener(_ listener: DeviceStatusCallback)
```

| Callback                                                                                                           |                                                                                                                                                                                                                                                                                 |
| ------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [DeviceStatusCallback](https://docs.movesdk.com/move-platform/move-sdk-1.x/models/untitled#device-status-callback) | Block that gets invoked every time [MoveDeviceStatus](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movedevicestatus) is changed and provides the updated [MoveDeviceStatus](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movedevicestatus). |

### **Set Metadata Listener**

Provide a block returning bundle of key-value pairs to be appended to the trip metadata before the trip finishes.

Host apps can use this API to add any app-level information (for ex. Böuetooth beacon detected, foreground/background time, etc) to append to a trip as metadata. This metadata will be forwarded back along with the trip when fetched by the client-server, so the host app can utilize it in its app. Note: The SDK will not use this metadata element in any way, it is just passed through to the product.

The block provides the trip's start and stop times. Make sure to only include metadata events that are relevant for the given start and end periods.

```swift
func setTripMetaDataListener(listener: MetaDataCallback)
func setTripMetaDataListener(_ listener: MetaDataCallback)
```

| Callback                                                                                                       |                                                                                                   |
| -------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------- |
| [MetaDataCallback](https://docs.movesdk.com/move-platform/move-sdk-1.x/models/untitled#trip-metadata-provider) | Block that gets invoked on every trip finish. The block provides the trip's start and stop times. |

**Usage Example**

```swift
MoveSDK.shared.setMetadataListener{ (tripStartTime, tripEndTime)
  getCollectedMetadataForPeriod(start: tripStartTime, end: tripEndTime)
}
```

### **Set Log Listener**

Provide a block to be invoked every time a new SDK log event occurs.

```swift
func setLogListener(listener: LogCallback)
func setLogListener(_ listener: LogCallback)
```

| Callback                                                                                        |                                                   |
| ----------------------------------------------------------------------------------------------- | ------------------------------------------------- |
| [LogCallback](https://docs.movesdk.com/move-platform/move-sdk-1.x/models/untitled#log-callback) | Block that gets invoked with every new log event. |

### **Set Console Logging State** <a href="#set-log-listener" id="set-log-listener"></a>

Toggles console logging enabled state.

This doesn't affect recieving the logs via  `setLogListener(_ listener: MoveLogCallback)` API.

Enabled by default.

```swift
func setConsoleLogging(enabled: Bool) 
```

| Parameter |                                                          |
| --------- | -------------------------------------------------------- |
| enabled   | Boolean that sets SDK console logging to enabled or not. |

### **Delete Local Data**

Deletes all the collected user SDK data stored on the device. This doesn't affect the [MoveSDKState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movestate).

{% hint style="warning" %}
**Precondition:** [MoveSDKState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movestate) should be initialized.
{% endhint %}

```swift
func deleteLocalData()
```

### **Shutdown SDK**

Shutdown SDK shared instance.

Stops SDK services, send the queued user data, and de-initializes the SDK. After that is executed,  the [MoveSDKState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movestate) will transit to `.uninitialized`.

{% hint style="warning" %}
**Precondition:** SDK should be initialized.
{% endhint %}

```swift
func shutDown()
```

### **Synchronize User Data**

Used to force synchronizing user data with the SDK server. Limited to once per 2 minutes.

```swift
func synchronizeUserData()
```

Returns FALSE in case there are Timeline-relevant packages in the queue that remain to be sent to the SDK Backend; and it returns TRUE in case the queue is empty.

## Configuration services

### Update authentication

Updates SDK [MoveAuth](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/moveauth). On SDK auth expiry, the Builder.authStateListner will trigger expired state. The host app is expected to fetch a new [MoveAuth](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/moveauth) using its product API key and pass it to the MoveSDK using the following API:

```swift
func update(auth: MoveAuth, completion: ((MoveConfigurationError?) -> Void)? = nil)
```

| Parameter |                                                    |
| --------- | -------------------------------------------------- |
| auth      | The user's updated Auth object with the new token. |

| **Callback** |                                                                                                                                                                                                                   |
| ------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| completion   | Block that gets invoked on auth update completion with an optional [MoveConfigurationError](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/moveconfigurationerror) for potential updating errors. |
