config_repository.dart 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import 'dart:convert';
  2. import 'package:injectable/injectable.dart';
  3. import 'package:keyboard/data/api/response/character_custom_config_response.dart';
  4. import '../../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 '../../utils/intimacy_util.dart';
  10. import '../api/atmob_api.dart';
  11. import '../api/request/config_request.dart';
  12. import '../api/response/config_response.dart';
  13. import 'package:get/get.dart';
  14. import '../bean/custom_config_info.dart';
  15. import '../bean/default_avatar_info.dart';
  16. import '../bean/intimacy_config_info.dart';
  17. @lazySingleton
  18. class ConfigRepository {
  19. final tag = "ConfigRepository";
  20. final AtmobApi atmobApi;
  21. final Rxn<CustomConfigInfo> _characterCustomConfig = Rxn<CustomConfigInfo>();
  22. // 里面包含人设头像
  23. Rxn<CustomConfigInfo> get characterCustomConfig => _characterCustomConfig;
  24. // 人物和键盘的头像
  25. final Rxn<DefaultAvatarInfo> defaultAvatarInfo = Rxn<DefaultAvatarInfo>();
  26. final Rxn<IntimacyConfigInfo> intimacyConfigInfo = Rxn<IntimacyConfigInfo>();
  27. ConfigRepository(this.atmobApi) {
  28. AtmobLog.d(tag, '$tag....init');
  29. refreshConfig();
  30. refreshCharacterCustomConfig();
  31. }
  32. // 更新配置的值
  33. void refreshConfig() {
  34. AsyncUtil.retry(
  35. () => requestConfigsData(),
  36. Duration(seconds: 4),
  37. maxRetry: 100,
  38. ).then((configsResponse) {
  39. final list = configsResponse.list;
  40. if (list == null || list.isEmpty) {
  41. return;
  42. }
  43. if (list.isNotEmpty) {
  44. for (var config in list) {
  45. if (config.confCode == 'intimacy') {
  46. intimacyConfigInfo.value = IntimacyConfigInfo.fromJson(
  47. config.value,
  48. );
  49. AtmobLog.d(tag, '获取亲密度配置: ${jsonEncode(list.map((e) => e.toJson()).toList())}');
  50. // IntimacyUtil.setConfig(intimacyConfigInfo.value);
  51. } else if (config.confCode == 'default_avatar') {
  52. defaultAvatarInfo.value = DefaultAvatarInfo.fromJson(config.value);
  53. AtmobLog.d(tag, '获取默认头像配置: ${defaultAvatarInfo.value?.toJson()}');
  54. }
  55. }
  56. }
  57. });
  58. }
  59. Future<void> refreshCharacterCustomConfig() async {
  60. try {
  61. final value = await getCharacterCustomConfig();
  62. _characterCustomConfig.value = value.customConfig;
  63. } catch (e) {
  64. AtmobLog.e(tag, "获取定制人设配置失败: $e");
  65. }
  66. }
  67. /// 获取配置信息
  68. Future<ConfigResponse> requestConfigsData() {
  69. return atmobApi
  70. .confs(ConfigRequest(confCodes: ['intimacy', 'default_avatar']))
  71. .then(HttpHandler.handle(true));
  72. }
  73. /// 获取定制人设配置
  74. Future<CharacterCustomConfigResponse> getCharacterCustomConfig() {
  75. return atmobApi
  76. .getCharacterCustomConfig(AppBaseRequest())
  77. .then(HttpHandler.handle(true));
  78. }
  79. static ConfigRepository getInstance() => getIt.get<ConfigRepository>();
  80. }