# Nonstop Swift SDK

## Performing Tasks

Users can complete Tasks with the integrating application calling Nonstop to tell the platform whenever one of the relevant user actions occurs. Each Task has it's own input model, that inherits from a base class, so that the parameters relevant to that task can be passed in. The relevant input model can be created by calling the createTask function and passing in the required type.

This input model can then have properties set and be passed to `userPerformedTask`.

Tasks that should be tracked by the client application are:

### Started Content

This should be called whenever a logged in user starts any content including Live, Episodes, Movies, VPPO Clips and ShowMS Clips. "Starts any content" refers to the point at which the user has started the video stream i.e. after any MVPD authentication or any pre-flight authorization checks have occurred. This is important to ensure points are correctly deducted from users. Please see [Purchase Validation](PurchaseValidation.md) for details.

* **contentId** - For VPPO content it should be passed the Partner API Id, for ShowMS Clips the ShowMS Id and for live either "US/Eastern" or "US/Pacific".
* **isWatchingNonstop** - This defaults to false and should be set to true when the user is starting a stream in Nonstop mode. The client app should know this from the result of the `presentChoiceCard` call. It does not need to be set for Live, VPPO or ShowMS Clips as these cannot be viewed Nonstop.

Create and set properties:

```swift
let startedContentTaskInput = TaskInput.createTask(type: .StartedContent) as! StartedContentTaskInput
startedContentTaskInput.contentId = "VDKA4220171"
startedContentTaskInput.isWatchingNonstop = true
```

### Watch 1 Minute of Content

Whenever the logged in user is watching any content this task should be called once per every minute of viewing.

* **contentId** - For VPPO content it should be passed the Partner API Id, for ShowMS Clips the ShowMS Id and for live either "US/Eastern" or "US/Pacific".

Create and set properties:

```swift
let watched1MinuteTaskInput = TaskInput.createTask(type: .Watched1Minute) as! Watch1MinuteTaskInput
watched1MinuteTaskInput.contentId = "VDKA4220171"
```

Note: While you cannot watch and earn on VPPO or ShowMS Clips by the minute we still recommend reporting Starts and Watch 1 Minute tasks for these content types.

### Connect MVPD Account

This should be called whenever a logged in user connects their MVPD Provider.

* **mvpdName** - MVPD name of the TV provider the user successfully connected. Note this should be the value obtained from the Adobe SDK for the provider Name, not the provider Id. For example, for Comcast it should be "Comcast Xfinity" not "Comcast_sso". The value should be camel cased.

Create and set properties:

```swift
let connectMVPDTaskInput = TaskInput.createTask(type: .ConnectedMVPD) as! MVPDTaskInput
connectMVPDTaskInput.mvpdName = "Spectrum"
```

### Calling `userPerformedTask`

The correct task input and parameters is then passed to `userPerformedTask` as follows:

```swift
Nonstop.sharedInstance.userPerformedTask(task: task).subscribe { event in
    switch event {
        case .success:
            // Task recorded successfully
        case .error (let error):
            // Error when recording task
        }
}.disposed(by: disposables)
```

Notes on calling `userPerformedTask`:

* If there is no logged in user an error will be thrown
* Any issues with the validation of the task will be returned as an error - e.g. content doesn't exist
* If the user should be receive points for completing the task then the SDK will assign them automatically
* If the user should see a Reward Card for completing the task the SDK will show it and play it before returning success
* If you wish to suppress the animation which will be played as a result of completing the task, every `TaskInput` has the property `shouldAnimate` which can be set to `false` to remove the animation

Success and Error should only be used for logging, if required.

### Internal Tasks

Note: For some tasks the completion of them is tracked internally within the SDK based on either user interactions within SDK components or based on other SDK calls. These are:

#### Enter Code

This will be recorded when a user enters a code within the Nonstop home.

#### Sign Up

This is recorded in the background whenever the Login function is called.

#### Complete Content - Nonstop Promos

This will be recorded automatically when users complete Nonstop promos from Nonstop home.

As such at present the Client applications do not have to call `userPerformedTask` for these tasks and they are defined as Internal within the SDK.
