gravity_engine_method_channel.dart 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import 'package:flutter/foundation.dart';
  2. import 'package:flutter/services.dart';
  3. import 'gravity_engine_platform_interface.dart';
  4. /// An implementation of [GravityEnginePlatform] that uses method channels.
  5. class MethodChannelGravityEngine extends GravityEnginePlatform {
  6. /// The method channel used to interact with the native platform.
  7. @visibleForTesting
  8. final methodChannel = const MethodChannel('gravity_engine');
  9. /// Initializes the Gravity Engine SDK.
  10. /// Returns `true` if the user is attributed, `false` otherwise.
  11. @override
  12. Future<bool> initialize(String appId, String accessToken, String clientId,
  13. String channel, bool debug) async {
  14. final result = await methodChannel.invokeMethod<bool>(
  15. 'initialize',
  16. <String, dynamic>{
  17. 'appId': appId,
  18. 'accessToken': accessToken,
  19. 'debug': debug,
  20. 'clientId': clientId,
  21. 'channel': channel,
  22. },
  23. );
  24. return result ?? false;
  25. }
  26. @override
  27. Future<void> trackEvent(
  28. String eventName, Map<String, dynamic> eventProperties) async {
  29. await methodChannel.invokeMethod<void>(
  30. 'track',
  31. <String, dynamic>{
  32. 'eventId': eventName,
  33. 'eventParams': eventProperties,
  34. },
  35. );
  36. }
  37. @override
  38. Future<void> trackPay(String orderNo, String itemName, int amountCent,
  39. String currency, PayType payType) async {
  40. await methodChannel.invokeMethod<void>(
  41. 'trackPay',
  42. <String, dynamic>{
  43. "orderNo": orderNo,
  44. "itemName": itemName,
  45. "amount": amountCent,
  46. "currency": currency,
  47. "payType": payType.name,
  48. },
  49. );
  50. }
  51. }
  52. enum PayType {
  53. alipay,
  54. wechat,
  55. }
  56. extension PayTypeExtension on PayType {
  57. String get name {
  58. switch (this) {
  59. case PayType.alipay:
  60. return "支付宝";
  61. case PayType.wechat:
  62. return "微信";
  63. }
  64. }
  65. }