intimacy_analyze_repository.dart 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import 'dart:io';
  2. import 'package:get/get_rx/src/rx_types/rx_types.dart';
  3. import 'package:injectable/injectable.dart';
  4. import 'package:keyboard/base/app_base_request.dart';
  5. import '../../di/get_it.dart';
  6. import '../../utils/async_util.dart';
  7. import '../../utils/atmob_log.dart';
  8. import '../../utils/http_handler.dart';
  9. import '../api/atmob_api.dart';
  10. import '../api/request/intimacy_analyze_request.dart';
  11. import '../api/response/intimacy_analyze_chat_config_response.dart';
  12. import '../api/response/intimacy_analyze_config_response.dart';
  13. import '../api/response/intimacy_analyze_response.dart';
  14. /// 亲密度分析Repository层
  15. @LazySingleton()
  16. class IntimacyAnalyzeRepository {
  17. final String tag = "IntimacyAnalyzeRepository";
  18. final AtmobApi atmobApi;
  19. /// 亲密度配置
  20. Rxn<IntimacyAnalyzeConfigResponse> intimacyAnalyzeConfig =
  21. Rxn<IntimacyAnalyzeConfigResponse>();
  22. /// 对话分析配置
  23. Rxn<IntimacyAnalyzeChatConfigResponse> intimacyAnalyzeChatConfig =
  24. Rxn<IntimacyAnalyzeChatConfigResponse>();
  25. IntimacyAnalyzeRepository(this.atmobApi) {
  26. AtmobLog.d(tag, '$tag...init');
  27. // 初始化时,刷新配置
  28. _refreshIntimacyAnalyzeConfig();
  29. _refreshIntimacyAnalyzeChatConfig();
  30. }
  31. /// 从Getx的依赖注入中,获取实例,适合在没有依赖注入上下文的地方使用
  32. IntimacyAnalyzeRepository getInstance() {
  33. return getIt.get<IntimacyAnalyzeRepository>();
  34. }
  35. /// 刷新亲密度配置
  36. void _refreshIntimacyAnalyzeConfig() {
  37. AsyncUtil.retry(
  38. () => requestIntimacyAnalyzeConfig(),
  39. Duration(seconds: 3),
  40. maxRetry: 100,
  41. ).then((IntimacyAnalyzeConfigResponse configResponse) {
  42. AtmobLog.d(tag, "获取亲密度配置成功: ${configResponse.toJson()}");
  43. intimacyAnalyzeConfig.value = configResponse;
  44. });
  45. }
  46. /// 刷新对话分析配置
  47. void _refreshIntimacyAnalyzeChatConfig() {
  48. AsyncUtil.retry(
  49. () => requestIntimacyAnalyzeChatConfig(),
  50. Duration(seconds: 3),
  51. maxRetry: 100,
  52. ).then((IntimacyAnalyzeChatConfigResponse configResponse) {
  53. AtmobLog.d(tag, "获取对话分析配置成功: ${configResponse.toJson()}");
  54. intimacyAnalyzeChatConfig.value = configResponse;
  55. });
  56. }
  57. /// 获取亲密度配置
  58. Future<IntimacyAnalyzeConfigResponse> requestIntimacyAnalyzeConfig() {
  59. return atmobApi
  60. .getIntimacyAnalyzeConfig(AppBaseRequest())
  61. .then(HttpHandler.handle(true));
  62. }
  63. /// 获取对话分析配置
  64. Future<IntimacyAnalyzeChatConfigResponse> requestIntimacyAnalyzeChatConfig() {
  65. return atmobApi
  66. .getIntimacyAnalyzeChatConfig(AppBaseRequest())
  67. .then(HttpHandler.handle(true));
  68. }
  69. /// 分析亲密度
  70. Future<IntimacyAnalyzeResponse> getIntimacyAnalyze(
  71. IntimacyAnalyzeRequest request,
  72. ) {
  73. return atmobApi.getIntimacyAnalyze(request).then(HttpHandler.handle(true));
  74. }
  75. }