gravity_engine_method_channel.dart 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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(
  13. String accessToken, String clientId, String channel, bool debug) async {
  14. final result = await methodChannel.invokeMethod<bool>(
  15. 'initialize',
  16. <String, dynamic>{
  17. 'accessToken': accessToken,
  18. 'debug': debug,
  19. 'clientId': clientId,
  20. 'channel': channel,
  21. },
  22. );
  23. return result ?? false;
  24. }
  25. @override
  26. Future<void> trackEvent(
  27. String eventName, Map<String, dynamic> eventProperties) async {
  28. await methodChannel.invokeMethod<void>(
  29. 'track',
  30. <String, dynamic>{
  31. 'eventId': eventName,
  32. 'eventParams': eventProperties,
  33. },
  34. );
  35. }
  36. @override
  37. Future<void> trackPay(String orderNo, String itemName, int amountCent,
  38. String currency, PayType payType) async {
  39. await methodChannel.invokeMethod<void>(
  40. 'trackPay',
  41. <String, dynamic>{
  42. "orderNo": orderNo,
  43. "itemName": itemName,
  44. "amount": amountCent,
  45. "currency": currency,
  46. "payType": payType.name,
  47. },
  48. );
  49. }
  50. }
  51. enum PayType {
  52. alipay,
  53. wechat,
  54. }
  55. extension PayTypeExtension on PayType {
  56. String get name {
  57. switch (this) {
  58. case PayType.alipay:
  59. return "支付宝";
  60. case PayType.wechat:
  61. return "微信";
  62. }
  63. }
  64. }