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, }, ); } } } 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 "未知"; } } }