classify_photo_method_channel.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import 'dart:ffi';
  2. import 'package:flutter/foundation.dart';
  3. import 'package:flutter/services.dart';
  4. import 'classify_photo_platform_interface.dart';
  5. /// An implementation of [ClassifyPhotoPlatform] that uses method channels.
  6. class MethodChannelClassifyPhoto extends ClassifyPhotoPlatform {
  7. /// The method channel used to interact with the native platform.
  8. @visibleForTesting
  9. final methodChannel = const MethodChannel('classify_photo');
  10. @override
  11. Future<String?> getPlatformVersion() async {
  12. final version = await methodChannel.invokeMethod<String>('getPlatformVersion');
  13. return version;
  14. }
  15. @override
  16. Future<bool> checkTrialEligibility() async {
  17. try {
  18. // 调用原生方法并处理可能的空值
  19. final result = await methodChannel.invokeMethod<bool>('checkTrialEligibility');
  20. return result ?? false; // 如果结果为 null,返回 false
  21. } on PlatformException catch (e) {
  22. print('检查试用资格失败: ${e.message}');
  23. return false; // 发生错误时返回 false
  24. } catch (e) {
  25. print('未知错误: $e');
  26. return false; // 其他错误情况返回 false
  27. }
  28. }
  29. @override
  30. Future<List<Map<String, dynamic>>?> getPhoto() async {
  31. try {
  32. print('Flutter: 调用 getPhoto 方法');
  33. final List<dynamic>? result = await methodChannel.invokeMethod<List<dynamic>>(
  34. 'getPhoto',
  35. );
  36. print('Flutter: 收到原生返回结果: $result');
  37. if (result == null) {
  38. print('Flutter: 结果为空');
  39. return null;
  40. }
  41. return result.map((group) => Map<String, dynamic>.from(group)).toList();
  42. } on PlatformException catch (e) {
  43. print('Flutter: Platform Exception: ${e.message}');
  44. rethrow;
  45. } catch (e) {
  46. print('Flutter: Error: $e');
  47. rethrow;
  48. }
  49. }
  50. // 实现获取存储信息的方法
  51. @override
  52. Future<Map<String, int>> getStorageInfo() async {
  53. try {
  54. final result = await methodChannel.invokeMethod<Map<dynamic, dynamic>>('getStorageInfo');
  55. if (result == null) {
  56. return {};
  57. }
  58. // 转换返回的数据类型
  59. return result.map((key, value) => MapEntry(
  60. key.toString(),
  61. (value as num).toInt(),
  62. ));
  63. } on PlatformException catch (e) {
  64. debugPrint('获取存储信息失败: ${e.message}');
  65. return {};
  66. }
  67. }
  68. @override
  69. Future<Map<String, dynamic>> getPhotoExif(String filePath) async {
  70. try {
  71. final result = await methodChannel.invokeMethod<Map<dynamic, dynamic>>(
  72. 'getExifInfo',
  73. {'filePath': filePath},
  74. );
  75. // 转换结果为 Map<String, dynamic>
  76. return _convertMap(result ?? {});
  77. } catch (e) {
  78. print('获取照片 EXIF 信息失败: $e');
  79. return {};
  80. }
  81. }
  82. /// 转换 Map 类型
  83. Map<String, dynamic> _convertMap(Map<dynamic, dynamic> map) {
  84. return map.map((key, value) {
  85. if (value is Map) {
  86. return MapEntry(key.toString(), _convertMap(value));
  87. }
  88. return MapEntry(key.toString(), value);
  89. });
  90. }
  91. @override
  92. Future<void> finishTransaction() async {
  93. print('Flutter: 清除自动订阅记录');
  94. await methodChannel.invokeMethod<Map<dynamic, dynamic>>('finishTran');
  95. }
  96. }