| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- import 'dart:ffi';
- import 'package:flutter/foundation.dart';
- import 'package:flutter/services.dart';
- import 'classify_photo_platform_interface.dart';
- /// An implementation of [ClassifyPhotoPlatform] that uses method channels.
- class MethodChannelClassifyPhoto extends ClassifyPhotoPlatform {
- /// The method channel used to interact with the native platform.
- @visibleForTesting
- final methodChannel = const MethodChannel('classify_photo');
- @override
- Future<String?> getPlatformVersion() async {
- final version = await methodChannel.invokeMethod<String>('getPlatformVersion');
- return version;
- }
- @override
- Future<bool> checkTrialEligibility(String appleId) async {
- try {
- // 调用原生方法并处理可能的空值
- final result = await methodChannel.invokeMethod<bool>('checkTrialEligibility',
- {'appleId': appleId},
- );
- return result ?? false; // 如果结果为 null,返回 false
- } on PlatformException catch (e) {
- print('检查试用资格失败: ${e.message}');
- return false; // 发生错误时返回 false
- } catch (e) {
- print('未知错误: $e');
- return false; // 其他错误情况返回 false
- }
- }
- @override
- Future<List<Map<String, dynamic>>?> getPhoto() async {
- try {
- print('Flutter: 调用 getPhoto 方法');
- final List<dynamic>? result = await methodChannel.invokeMethod<List<dynamic>>(
- 'getPhoto',
- );
- print('Flutter: 收到原生返回结果: $result');
- if (result == null) {
- print('Flutter: 结果为空');
- return null;
- }
- return result.map((group) => Map<String, dynamic>.from(group)).toList();
- } on PlatformException catch (e) {
- print('Flutter: Platform Exception: ${e.message}');
- rethrow;
- } catch (e) {
- print('Flutter: Error: $e');
- rethrow;
- }
- }
- // 实现获取存储信息的方法
- @override
- Future<Map<String, int>> getStorageInfo() async {
- try {
- final result = await methodChannel.invokeMethod<Map<dynamic, dynamic>>('getStorageInfo');
- if (result == null) {
- return {};
- }
- // 转换返回的数据类型
- return result.map((key, value) => MapEntry(
- key.toString(),
- (value as num).toInt(),
- ));
- } on PlatformException catch (e) {
- debugPrint('获取存储信息失败: ${e.message}');
- return {};
- }
- }
- @override
- Future<Map<String, dynamic>> getPhotoExif(String filePath) async {
- try {
- final result = await methodChannel.invokeMethod<Map<dynamic, dynamic>>(
- 'getExifInfo',
- {'filePath': filePath},
- );
- // 转换结果为 Map<String, dynamic>
- return _convertMap(result ?? {});
- } catch (e) {
- print('获取照片 EXIF 信息失败: $e');
- return {};
- }
- }
- /// 转换 Map 类型
- Map<String, dynamic> _convertMap(Map<dynamic, dynamic> map) {
- return map.map((key, value) {
- if (value is Map) {
- return MapEntry(key.toString(), _convertMap(value));
- }
- return MapEntry(key.toString(), value);
- });
- }
- @override
- Future<void> finishTransaction() async {
- print('Flutter: 清除自动订阅记录');
- await methodChannel.invokeMethod<Map<dynamic, dynamic>>('finishTran');
- }
- @override
- Future<int> calculatePhotoSize(List<String> assetIds) async {
- try {
- final result = await methodChannel.invokeMethod<int>(
- 'getPhotosSize',
- {'assetIds': assetIds},
- );
- // 转换结果为 Map<String, dynamic>
- return result ?? 0;
- } catch (e) {
- print('获取照片容量大小错误: $e');
- return 0;
- }
- }
- }
|