gravity_engine_method_channel.dart 2.4 KB

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