# Migrate from Old SDK

### Updating the old repository

Formerly we had:

```kotlin
maven { 
  url = uri("https://artifactory.datalf.chat/artifactory/app-consent-v2-release")
}
```

From now on, it is necessary to:

```kotlin
maven { 
  url = uri("https://artifactory.datalf.chat/artifactory/appconsent")
}
```

### Implementation change

Previously, we had:

```kotlin
dependencies { 
    implementation("com.sfbx.appconsent:appconsent-ui-v3:X.Y.Z")
}
```

From now on, you must:

```kotlin
dependencies { 
    implementation("io.sfbx.appconsent:unifiedsdk:X.Y.Z")
}
```

### Change in integration

#### Imports

Before we had:

```kotlin
import com.sfbx.appconsentv3.*
```

Now the imports are as follows:

```kotlin
import io.sfbx.appconsent.unifiedsdk.*
```

#### Initializing the SDK

This is very similar to the old SDK (like many other libraries).

Before, we had:

1. Initialization of the SDK
2. Registration of callbacks to intercept the user's response
3. Attempt to display the CMP
4. Deletion of callbacks regardless of the result

```kotlin
AppConsentSDK.initialize(
  appKey = "YOUR_APP_KEY"
) { appConsentInitialized ->
  appConsentInitialized.setOnPresentNoticeListener(object : OnPresentNoticeListener {
    override fun presentConsentError(error: Throwable?) {
      appConsentInitialized.setOnPresentNoticeListener(null)
    }

     override fun presentConsentGiven() {
       appConsentInitialized.setOnPresentNoticeListener(null)
     }
  })
  
  if(false == appConsentInitialized.tryToDisplayNoticeFromUiContext(uiContext = this@MainActivity, force = false)) {
    appConsentInitialized.setOnPresentNoticeListener(null)
  }
}
```

Now :

1. Initializing the SDK
2. Display request (the display & consent response is returned directly in the method callbacks)

```kotlin
SFBX.initialize(
  context = this@MyActivity.context,
  configuration = Configuration(appkey = "YOUR-APP-KEY"),
  onCmpReady = { appconsent ->
    appconsent.presentNotice(
      context = context,
      onCmpDisplayed = { },
      onCmpNotDisplayed = { },
      onCmpClosed = { },
    )},
  onCmpOnError = { appconsentExceptions ->
     },
)
```

### Displaying the consent screen to your users

This allows your users to view their consent and modify it if necessary.

Before, we had:

```kotlin
AppConsentSDK.getInstance()
  ?.setOnPresentNoticeListener(object : OnPresentNoticeListener {
    override fun presentConsentError(error: Throwable?) {
      AppConsentSDK.getInstance()?.setOnPresentNoticeListener(null)
    }

    override fun presentConsentGiven() {
      AppConsentSDK.getInstance()?.setOnPresentNoticeListener(null)
    }
  })

  val isCmpDisplayed =
    AppConsentSDK.getInstance()?.tryToDisplayNotice(force = true) ?: false
  if (false == isCmpDisplayed) {
    AppConsentSDK.getInstance()?.setOnPresentNoticeListener(null)
  }
```

Now :

```kotlin
try {
    SFBX.getInstance()
} catch (_: Exception) {
    null
}?.displayLayer2(context,
    onCmpDisplayed = { },
    onCmpNotDisplayed = { },
    onCmpClosed = { },
)


// Alternatively, the following approach is more idiomatic Kotlin,
// using runCatching to handle the exception in a functional style.


runCatching {
    SFBX.getInstance()
}.onSuccess { appconsent ->
    appconsent.displayLayer2(context,
        onCmpDisplayed = { },
        onCmpNotDisplayed = { },
        onCmpClosed = { },
    )
}.onFailure {
}
```
