gravity_engine_method_channel.dart 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. appStore,
  76. }
  77. extension PayTypeExtension on PayType {
  78. String get name {
  79. switch (this) {
  80. case PayType.alipay:
  81. return "支付宝";
  82. case PayType.wechat:
  83. return "微信";
  84. case PayType.appStore:
  85. return "AppStore";
  86. }
  87. }
  88. }