Importing the Copilot.cx Android SDK for Flutter
Setting up the android side for Copilot.cx
Please follow steps 1 through 5 in Importing the Android SDK
Creating CopilotModule to interface with Dart
- Create a CopilotModule class that will interact with the Dart code.
Inside the
setupMethodChannel
function, we'll "capture" function calls from the dart side(with or without parameters) and point them to our native side fuctions. In this following example we're using thesignup
,login
,logout
andsendEvent
functions.
class CopilotModule(
private val flutterEngine: FlutterEngine,
private val application: Application,
private val context: Context,
) {
private val channel = "myProjectChannelName"
init {
setupMethodChannel()
}
private fun setupMethodChannel() {
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, channel).setMethodCallHandler { call, result ->
when (call.method) {
"signup" -> {
val email = call.argument<String>("email")
val password = call.argument<String>("password")
val firstName = call.argument<String>("firstName")
val lastName = call.argument<String>("lastName")
if (email != null && password != null && firstName != null && lastName != null) {
signup(email, password, firstName, lastName, result)
}
}
"login" -> {
val email = call.argument<String>("email")
val password = call.argument<String>("password")
if (email != null && password != null) {
login(email, password, result)
}
}
"logout" -> {
logout(result)
}
"sendEvent" -> {
val eventName = call.argument<String>("eventName")
val eventParams = call.argument<Map<String, String>>("eventParams")
if (eventName != null && eventParams != null) {
sendEvent(eventName, eventParams)
}
}
else -> {
result.notImplemented()
}
}
}
}
private fun signup(email: String, password: String, firstName: String, lastName: String, result: MethodChannel.Result) {
Copilot.getInstance().Manage.Sphere.Auth.signup().withCopilotAnalysisConsent(true).withEmailPassword(email, password, firstName, lastName).build()
.execute(object : RequestListener<Void, SignupError> {
override fun success(response: Void?) {
result.success(true)
//Your code here
}
override fun error(error: SignupError?) {
result.error("Signup failed, ${error?.debugMessage}", null, null)
//Your code here
}
})
}
private fun login(email: String, password: String, result: MethocChannel.Result) {
Copilot.getInstance().Manage.Sphere.Auth.login().withEmailPassword(email, password).build().execute(object: RequestListener<Void, LoginError> {
override fun success(response: Void?) {
result.success(true)
//Your code here
}
override fun error(error: LoginError?) {
result.error("Login failed, ${error?.debugMessage}", null, null)
//Your code here
}
})
}
private fun logout() {
Copilot.getInstance().Manage.Sphere.Auth.logout().build().execute(object: RequestListener<Void, LogoutError> {
override fun success(response: Void?) {
result.success(true)
//Your code here
}
override fun error(error: LogoutError?) {
result.error("Logout failed, ${error?.debugMessage}", null, null)
//Your code here
}
})
}
private fun sendEvent(eventName: String, map: Map<String, String>) {
val customEvent = CustomAnalyticsEvent(eventName, map)
Copilot.getInstance().Report.logEvent(customEvent)
}
}
- Create a
FlutterApplication
class and init the Copilot.Cx Sdk inside theonCreate
function. Don't forget to replace the application class name inside theAndroidManifest.xml
file.
class MyApplication : FlutterApplication() {
override fun onCreate() {
super.onCreate()
Copilot.setup(this, listOf(FirebaseEventLogProvider(this)))
}
}
- Inside your
MainActivity
add an instance forCopilotModule
and initialize it inside configureFlutterEngine and setup the Copilot.cx SDK with theEventLogProvider
class MainActivity: FlutterActivity() {
lateinit var copilotModule : CopilotModule
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
...
copilotModule = CopilotModule(flutterEngine,application, this)
...
}
}