InstagramBasicDisplayApiPlugin
class InstagramBasicDisplayApiPlugin : FlutterPlugin, ActivityAware {
private lateinit var methodCallHandler: MethodCallHandlerImpl
private val instagramBasicDisplayApi: InstagramBasicDisplayApi = getKoinInstance()
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
methodCallHandler = MethodCallHandlerImpl(instagramBasicDisplayApi)
methodCallHandler.startListening(flutterPluginBinding.binaryMessenger)
}
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
methodCallHandler.stopListening()
}
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
instagramBasicDisplayApi.setActivityPluginBinding(binding)
}
override fun onDetachedFromActivity() {
instagramBasicDisplayApi.detachActivity()
}
override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
onAttachedToActivity(binding)
}
override fun onDetachedFromActivityForConfigChanges() {
onDetachedFromActivity()
}
}
MethodCallHandlerImpl
class MethodCallHandlerImpl(private val instagramBasicDisplayApi: InstagramBasicDisplayApi) :
MethodChannel.MethodCallHandler {
private val TAG = javaClass.name
private var channel: MethodChannel? = null
init {
instagramBasicDisplayApi.startListening(
userUpdated = {
channel?.invokeMethod("userUpdated", hashMapOf("ID" to it.id, "USER_NAME" to it.username, "ACCOUNT_TYPE" to it.accountType))
},
mediasUpdated = {
channel?.invokeMethod("mediasUpdated", hashMapOf("DATA" to it))
},
albumDetailUpdated = {
channel?.invokeMethod("albumDetailUpdated", hashMapOf("DATA" to it))
},
mediaItemUpdated = {
channel?.invokeMethod("mediaItemUpdated", it)
},
errorUpdated = {
channel?.invokeMethod("errorUpdated", hashMapOf("ERROR_TYPE" to it))
}
)
}
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: MethodChannel.Result) {
when (call.method) {
"getPlatformVersion" -> {
result.success("Android ${android.os.Build.VERSION.RELEASE}")
}
"askInstagramToken" -> {
instagramBasicDisplayApi.askInstagramToken()
result.success(null)
}
"getInstagramUser" -> {
instagramBasicDisplayApi.getInstagramUser()
result.success(null)
}
"getMedias" -> {
instagramBasicDisplayApi.getMedias()
result.success(null)
}
"getAlbumDetail" -> {
println("getAlbumDetail\n")
val arguments = call.arguments as? HashMap<*, *> ?: return
val albumId = arguments["albumId"] as String? ?: return
println("getAlbumDetail albumId = $albumId")
instagramBasicDisplayApi.getAlbumDetail(albumId)
result.success(null)
}
"getMediaItem" -> {
println("getMediaItem\n")
val arguments = call.arguments as? HashMap<*, *> ?: return
val mediaId = arguments["mediaId"] as String? ?: return
println("getMediaItem mediaId = $mediaId")
instagramBasicDisplayApi.getMediaItem(mediaId)
result.success(null)
}
"logout" -> {
instagramBasicDisplayApi.logout()
result.success(null)
}
else -> {
result.notImplemented()
}
}
}
fun startListening(messenger: BinaryMessenger?) {
if (channel != null) {
Log.wtf(TAG, "Setting a method call handler before the last was disposed.")
stopListening()
}
channel = MethodChannel(messenger, "instagram_basic_display_api").apply {
setMethodCallHandler(this@MethodCallHandlerImpl)
}
}
fun stopListening() {
if (channel == null) {
Log.d(TAG, "Tried to stop listening when no MethodChannel had been initialized.")
return
}
channel!!.setMethodCallHandler(null)
channel = null
}
}