import 'dart:io'; import 'package:get/get_rx/src/rx_types/rx_types.dart'; import 'package:injectable/injectable.dart'; import 'package:keyboard/base/app_base_request.dart'; import '../../di/get_it.dart'; import '../../utils/async_util.dart'; import '../../utils/atmob_log.dart'; import '../../utils/http_handler.dart'; import '../api/atmob_api.dart'; import '../api/request/intimacy_analyze_request.dart'; import '../api/response/intimacy_analyze_chat_config_response.dart'; import '../api/response/intimacy_analyze_config_response.dart'; import '../api/response/intimacy_analyze_reply_config_response.dart'; import '../api/response/intimacy_analyze_response.dart'; /// 亲密度分析Repository层 @LazySingleton() class IntimacyAnalyzeRepository { final String tag = "IntimacyAnalyzeRepository"; final AtmobApi atmobApi; /// 亲密度配置 Rxn intimacyAnalyzeConfig = Rxn(); /// 对话分析配置 Rxn intimacyAnalyzeChatConfig = Rxn(); /// 识图回复配置 Rxn intimacyAnalyzeReplyConfig = Rxn(); IntimacyAnalyzeRepository(this.atmobApi) { AtmobLog.d(tag, '$tag...init'); // 初始化时,刷新配置 _refreshIntimacyAnalyzeConfig(); _refreshIntimacyAnalyzeChatConfig(); _refreshIntimacyAnalyzeReplyConfig(); } /// 从Getx的依赖注入中,获取实例,适合在没有依赖注入上下文的地方使用 IntimacyAnalyzeRepository getInstance() { return getIt.get(); } /// 刷新亲密度配置 void _refreshIntimacyAnalyzeConfig() { AsyncUtil.retry( () => requestIntimacyAnalyzeConfig(), Duration(seconds: 3), maxRetry: 100, ).then((IntimacyAnalyzeConfigResponse configResponse) { AtmobLog.d(tag, "获取亲密度配置成功: ${configResponse.toJson()}"); intimacyAnalyzeConfig.value = configResponse; }); } /// 刷新对话分析配置 void _refreshIntimacyAnalyzeChatConfig() { AsyncUtil.retry( () => requestIntimacyAnalyzeChatConfig(), Duration(seconds: 3), maxRetry: 100, ).then((IntimacyAnalyzeChatConfigResponse configResponse) { AtmobLog.d(tag, "获取对话分析配置成功: ${configResponse.toJson()}"); intimacyAnalyzeChatConfig.value = configResponse; }); } /// 刷新识图回复配置 void _refreshIntimacyAnalyzeReplyConfig() { AsyncUtil.retry( () => requestIntimacyAnalyzeReplyConfig(), Duration(seconds: 3), maxRetry: 100, ).then((IntimacyAnalyzeReplyConfigResponse configResponse) { AtmobLog.d(tag, "获取识图回复配置成功: ${configResponse.toJson()}"); intimacyAnalyzeReplyConfig.value = configResponse; }); } /// 获取亲密度配置 Future requestIntimacyAnalyzeConfig() { return atmobApi .getIntimacyAnalyzeConfig(AppBaseRequest()) .then(HttpHandler.handle(true)); } /// 获取对话分析配置 Future requestIntimacyAnalyzeChatConfig() { return atmobApi .getIntimacyAnalyzeChatConfig(AppBaseRequest()) .then(HttpHandler.handle(true)); } /// 获取识图回复配置 Future requestIntimacyAnalyzeReplyConfig() { return atmobApi .getIntimacyAnalyzeReplyConfig(AppBaseRequest()) .then(HttpHandler.handle(true)); } /// 分析亲密度 Future getIntimacyAnalyze( IntimacyAnalyzeRequest request, ) { return atmobApi.getIntimacyAnalyze(request).then(HttpHandler.handle(true)); } }