diff --git a/android/src/main/kotlin/com/shatsy/admobflutter/AdmobInterstitial.kt b/android/src/main/kotlin/com/shatsy/admobflutter/AdmobInterstitial.kt new file mode 100644 index 0000000..3d769ea --- /dev/null +++ b/android/src/main/kotlin/com/shatsy/admobflutter/AdmobInterstitial.kt @@ -0,0 +1,47 @@ +package com.shatsy.admobflutter + +import android.content.Context +import com.google.android.gms.ads.AdRequest +import com.google.android.gms.ads.InterstitialAd +import io.flutter.plugin.common.MethodCall +import io.flutter.plugin.common.MethodChannel + +class AdmobInterstitial(private val context: Context): MethodChannel.MethodCallHandler { + companion object { + val allAds: MutableMap = mutableMapOf() + } + override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) { + when(call.method) { + "load" -> { + val id = call.argument("id") + val adUnitId = call.argument("adUnitId") + val adRequest = AdRequest.Builder().build() + + allAds[id!!] = InterstitialAd(context) + allAds[id]!!.adUnitId = adUnitId + allAds[id]?.loadAd(adRequest) + result.success(null) + } + "isLoaded" -> { + val id = call.argument("id") + + if (allAds[id]!!.isLoaded) { + result.success(true) + } else result.success(false) + } + "show" -> { + val id = call.argument("id") + + if (allAds[id]!!.isLoaded) { + allAds[id]!!.show() + } else result.error(null, null, null) + } + "dispose" -> { + val id = call.argument("id") + + allAds.remove(id) + } + else -> result.notImplemented() + } + } +} diff --git a/lib/src/admob_interstitial.dart b/lib/src/admob_interstitial.dart new file mode 100644 index 0000000..72c01b3 --- /dev/null +++ b/lib/src/admob_interstitial.dart @@ -0,0 +1,46 @@ +import 'package:flutter/services.dart'; +import 'package:meta/meta.dart'; + +class AdmobInterstitial { + static Map _allAds = {}; + static const MethodChannel _channel = + const MethodChannel('admob_flutter/interstitial'); + + int id; + final String adUnitId; + + AdmobInterstitial({@required this.adUnitId}) { + id = hashCode; + _allAds[id] = this; + } + + Future get isLoaded async { + final bool result = await _channel.invokeMethod('isLoaded', { + 'id': id, + }); + return result; + } + + void loadAd() { + _channel.invokeMethod('load', { + 'id': id, + 'adUnitId': adUnitId, + }); + } + + void show() async { + if (await isLoaded == true) { + _channel.invokeMethod('show', { + 'id': id, + }); + } + } + + void dispose() async { + await _channel.invokeMethod('dispose', { + 'id': id, + }); + + _allAds.remove(id); + } +}