intimacy_analyze_repository.dart 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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_config_response.dart';
  12. import '../api/response/intimacy_analyze_response.dart';
  13. /// 亲密度分析Repository层
  14. @LazySingleton()
  15. class IntimacyAnalyzeRepository {
  16. final String tag = "IntimacyAnalyzeRepository";
  17. final AtmobApi atmobApi;
  18. /// 亲密度配置
  19. Rxn<IntimacyAnalyzeConfigResponse> intimacyAnalyzeConfig =
  20. Rxn<IntimacyAnalyzeConfigResponse>();
  21. IntimacyAnalyzeRepository(this.atmobApi) {
  22. AtmobLog.d(tag, '$tag...init');
  23. // 初始化时,刷新配置
  24. refreshIntimacyAnalyzeConfig();
  25. }
  26. /// 从Getx的依赖注入中,获取实例,适合在没有依赖注入上下文的地方使用
  27. IntimacyAnalyzeRepository getInstance() {
  28. return getIt.get<IntimacyAnalyzeRepository>();
  29. }
  30. /// 刷新亲密度配置
  31. void refreshIntimacyAnalyzeConfig() {
  32. AsyncUtil.retry(
  33. () => requestIntimacyAnalyzeConfig(),
  34. Duration(seconds: 3),
  35. maxRetry: 100,
  36. ).then((IntimacyAnalyzeConfigResponse configResponse) {
  37. AtmobLog.d(tag, "获取亲密度配置成功: ${configResponse.toJson()}");
  38. intimacyAnalyzeConfig.value = configResponse;
  39. });
  40. }
  41. /// 获取亲密度配置
  42. Future<IntimacyAnalyzeConfigResponse> requestIntimacyAnalyzeConfig() {
  43. return atmobApi
  44. .getIntimacyAnalyzeConfig(AppBaseRequest())
  45. .then(HttpHandler.handle(true));
  46. }
  47. /// 分析亲密度
  48. Future<IntimacyAnalyzeResponse> getIntimacyAnalyze(
  49. IntimacyAnalyzeRequest request,
  50. ) {
  51. return atmobApi.getIntimacyAnalyze(request).then(HttpHandler.handle(true));
  52. }
  53. }