Created
July 5, 2021 11:55
-
-
Save mondoktamas/f8b3560c05a952a931c569f529a93cc9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package passbaseplugin.pettersonapps.com.passbase_flutter | |
import android.content.Context | |
import android.view.View | |
import io.flutter.plugin.common.MethodCall | |
import io.flutter.plugin.common.MethodChannel | |
import io.flutter.plugin.common.MethodChannel.MethodCallHandler | |
import io.flutter.plugin.common.MethodChannel.Result | |
import io.flutter.plugin.common.PluginRegistry.Registrar | |
import com.passbase.passbase_sdk.PassbaseSDK | |
import com.passbase.passbase_sdk.PassbaseButton | |
import com.passbase.passbase_sdk.PassbaseSDKListener | |
import io.flutter.plugin.common.StandardMessageCodec | |
import io.flutter.plugin.platform.PlatformView | |
import io.flutter.plugin.platform.PlatformViewFactory | |
class PassbaseFlutterPlugin(private val passbase: PassbaseSDK, private val channel: MethodChannel) : MethodCallHandler { | |
companion object { | |
@JvmStatic | |
fun registerWith(registrar: Registrar) { | |
val channel = MethodChannel(registrar.messenger(), "passbase_flutter") | |
val passbase = PassbaseSDK(registrar.activity()) | |
val passbaseButton = PassbaseButton(context = registrar.activity()) | |
channel.setMethodCallHandler(PassbaseFlutterPlugin(passbase, channel)) | |
PassbaseButtonPlugin.registerWith(registrar, passbaseButton, passbase, channel) | |
} | |
} | |
override fun onMethodCall(call: MethodCall, result: Result) { | |
when (call.method) { | |
"initialize" -> { | |
PassbaseSDK.source = 3 | |
passbase.initialize(call.arguments as String) | |
result.success(null) | |
} | |
"startVerification" -> { | |
passbase.callback(object : PassbaseSDKListener { | |
override fun onStart() { | |
channel.invokeMethod("verification_start", "Verification started") | |
} | |
override fun onFinish(identityAccessKey: String) { | |
channel.invokeMethod("verification_complete", identityAccessKey) | |
} | |
override fun onError(errorCode: String) { | |
channel.invokeMethod("verification_error", errorCode) | |
} | |
override fun onSubmitted(identityAccessKey: String) { | |
channel.invokeMethod("verification_submitted", identityAccessKey) | |
} | |
}) | |
passbase.startVerification() | |
} | |
"setPrefillUserEmail" -> { | |
passbase.prefillUserEmail = call.arguments as String? | |
} | |
"setMetaData" -> { | |
passbase.metaData = call.arguments as String? | |
} | |
"setPrefillCountry" -> { | |
passbase.prefillCountry = call.arguments as String? | |
} | |
else -> { | |
result.notImplemented() | |
} | |
} | |
} | |
} | |
object PassbaseButtonPlugin { | |
fun registerWith(registrar: Registrar, passbaseButton: PassbaseButton, passbase: PassbaseSDK, channel: MethodChannel) { | |
val viewFactory = PassbaseButtonFactory(passbaseButton, passbase, channel) | |
registrar.platformViewRegistry() | |
.registerViewFactory("PassbaseButton", viewFactory) | |
} | |
} | |
class PassbaseButtonFactory(private val passbaseButton: PassbaseButton, private val passbase: PassbaseSDK, private val channel: MethodChannel) : | |
PlatformViewFactory(StandardMessageCodec.INSTANCE) { | |
override fun create(context: Context, | |
id: Int, | |
args: Any?): PlatformView { | |
passbaseButton.id = id | |
passbaseButton.setOnClickListener { | |
passbase.callback(object : PassbaseSDKListener { | |
override fun onStart() { | |
channel.invokeMethod("verification_start", "Verification started") | |
} | |
override fun onFinish(identityAccessKey: String) { | |
channel.invokeMethod("verification_complete", identityAccessKey) | |
} | |
override fun onError(errorCode: String) { | |
channel.invokeMethod("verification_error", errorCode) | |
} | |
override fun onSubmitted(identityAccessKey: String) { | |
channel.invokeMethod("verification_submitted", identityAccessKey) | |
} | |
}) | |
passbase.startVerification() | |
} | |
return PassbaseButtonContainer(passbaseButton) | |
} | |
} | |
class PassbaseButtonContainer internal constructor(private val passbaseButton: PassbaseButton) : PlatformView { | |
override fun dispose() { | |
} | |
override fun getView(): View { | |
return passbaseButton | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment