classify_photo_method_channel.dart 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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(String appleId) async {
  17. try {
  18. // 调用原生方法并处理可能的空值
  19. final result = await methodChannel.invokeMethod<bool>('checkTrialEligibility',
  20. {'appleId': appleId},
  21. );
  22. return result ?? false; // 如果结果为 null,返回 false
  23. } on PlatformException catch (e) {
  24. print('检查试用资格失败: ${e.message}');
  25. return false; // 发生错误时返回 false
  26. } catch (e) {
  27. print('未知错误: $e');
  28. return false; // 其他错误情况返回 false
  29. }
  30. }
  31. @override
  32. Future<List<Map<String, dynamic>>?> getPhoto() async {
  33. try {
  34. print('Flutter: 调用 getPhoto 方法');
  35. final List<dynamic>? result = await methodChannel.invokeMethod<List<dynamic>>(
  36. 'getPhoto',
  37. );
  38. print('Flutter: 收到原生返回结果: $result');
  39. if (result == null) {
  40. print('Flutter: 结果为空');
  41. return null;
  42. }
  43. return result.map((group) => Map<String, dynamic>.from(group)).toList();
  44. } on PlatformException catch (e) {
  45. print('Flutter: Platform Exception: ${e.message}');
  46. rethrow;
  47. } catch (e) {
  48. print('Flutter: Error: $e');
  49. rethrow;
  50. }
  51. }
  52. // 实现获取存储信息的方法
  53. @override
  54. Future<Map<String, int>> getStorageInfo() async {
  55. try {
  56. final result = await methodChannel.invokeMethod<Map<dynamic, dynamic>>('getStorageInfo');
  57. if (result == null) {
  58. return {};
  59. }
  60. // 转换返回的数据类型
  61. return result.map((key, value) => MapEntry(
  62. key.toString(),
  63. (value as num).toInt(),
  64. ));
  65. } on PlatformException catch (e) {
  66. debugPrint('获取存储信息失败: ${e.message}');
  67. return {};
  68. }
  69. }
  70. @override
  71. Future<Map<String, dynamic>> getPhotoExif(String filePath) async {
  72. try {
  73. final result = await methodChannel.invokeMethod<Map<dynamic, dynamic>>(
  74. 'getExifInfo',
  75. {'filePath': filePath},
  76. );
  77. // 转换结果为 Map<String, dynamic>
  78. return _convertMap(result ?? {});
  79. } catch (e) {
  80. print('获取照片 EXIF 信息失败: $e');
  81. return {};
  82. }
  83. }
  84. /// 转换 Map 类型
  85. Map<String, dynamic> _convertMap(Map<dynamic, dynamic> map) {
  86. return map.map((key, value) {
  87. if (value is Map) {
  88. return MapEntry(key.toString(), _convertMap(value));
  89. }
  90. return MapEntry(key.toString(), value);
  91. });
  92. }
  93. @override
  94. Future<void> finishTransaction() async {
  95. print('Flutter: 清除自动订阅记录');
  96. await methodChannel.invokeMethod<Map<dynamic, dynamic>>('finishTran');
  97. }
  98. @override
  99. Future<int> calculatePhotoSize(List<String> assetIds) async {
  100. try {
  101. final result = await methodChannel.invokeMethod<int>(
  102. 'getPhotosSize',
  103. {'assetIds': assetIds},
  104. );
  105. // 转换结果为 Map<String, dynamic>
  106. return result ?? 0;
  107. } catch (e) {
  108. print('获取照片容量大小错误: $e');
  109. return 0;
  110. }
  111. }
  112. }