import 'dart:io'; import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import 'gravity_engine_platform_interface.dart'; /// An implementation of [GravityEnginePlatform] that uses method channels. class MethodChannelGravityEngine extends GravityEnginePlatform { /// The method channel used to interact with the native platform. @visibleForTesting final methodChannel = const MethodChannel('gravity_engine'); /// Initializes the Gravity Engine SDK. /// Returns `true` if the user is attributed, `false` otherwise. @override Future initialize(String appId, String accessToken, String clientId, String channel, bool debug) async { final result = await methodChannel.invokeMethod( 'initialize', { 'appId': appId, 'accessToken': accessToken, 'debug': debug, 'clientId': clientId, 'channel': channel, }, ); return result ?? false; } @override Future trackEvent(String eventName, {Map? eventProperties}) async { await methodChannel.invokeMethod( 'track', { 'eventId': eventName, 'eventParams': eventProperties, }, ); } @override Future trackPay(String orderNo, String itemName, int amountCent, String currency, PayType payType) async { await methodChannel.invokeMethod( 'trackPay', { "orderNo": orderNo, "itemName": itemName, "amount": amountCent, "currency": currency, "payType": payType.name, }, ); } @override Future logout() async { // if android if (Platform.isAndroid) { await methodChannel.invokeMethod('logout'); } } @override Future login(String clientId) async { // if android if (Platform.isAndroid) { await methodChannel.invokeMethod( 'login', { 'clientId': clientId, }, ); } } @override Future trackAdLoadEvent(String? adUnionType, String? adPlacementId, String? adSourceId, String? adType, String? adnType) { return methodChannel.invokeMethod( 'trackAdLoadEvent', { 'adUnionType': adUnionType, 'adPlacementId': adPlacementId, 'adSourceId': adSourceId, 'adType': adType, 'adnType': adnType, }, ); } @override Future trackAdShowEvent(String? adUnionType, String? adPlacementId, String? adSourceId, String? adType, String? adnType, double? ecpm) { return methodChannel.invokeMethod( 'trackAdShowEvent', { 'adUnionType': adUnionType, 'adPlacementId': adPlacementId, 'adSourceId': adSourceId, 'adType': adType, 'adnType': adnType, 'ecpm': ecpm, }, ); } @override Future trackAdClickEvent(String? adUnionType, String? adPlacementId, String? adSourceId, String? adType, String? adnType, double? ecpm) { return methodChannel.invokeMethod( 'trackAdClickEvent', { 'adUnionType': adUnionType, 'adPlacementId': adPlacementId, 'adSourceId': adSourceId, 'adType': adType, 'adnType': adnType, 'ecpm': ecpm, }, ); } @override Future trackAdPlayStartEvent(String? adUnionType, String? adPlacementId, String? adSourceId, String? adType, String? adnType, double? ecpm) { return methodChannel.invokeMethod( 'trackAdPlayStartEvent', { 'adUnionType': adUnionType, 'adPlacementId': adPlacementId, 'adSourceId': adSourceId, 'adType': adType, 'adnType': adnType, 'ecpm': ecpm, }, ); } @override Future trackAdPlayEndEvent( String? adUnionType, String? adPlacementId, String? adSourceId, String? adType, String? adnType, double? ecpm, int? duration, bool? isPlayOver) { return methodChannel.invokeMethod( 'trackAdPlayEndEvent', { 'adUnionType': adUnionType, 'adPlacementId': adPlacementId, 'adSourceId': adSourceId, 'adType': adType, 'adnType': adnType, 'ecpm': ecpm, 'duration': duration, 'isPlayOver': isPlayOver, }, ); } } enum PayType { alipay, wechat, apple, unknown, } extension PayTypeExtension on PayType { String get name { switch (this) { case PayType.alipay: return "支付宝"; case PayType.wechat: return "微信"; case PayType.apple: return "苹果支付"; default: return "未知"; } } }