config_repository.dart 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import 'package:flutter/Material.dart';
  2. import 'package:get/get.dart';
  3. import '../../utils/async_util.dart';
  4. import '../../utils/http_handler.dart';
  5. import '../api/atmob_api.dart';
  6. import '../api/request/config_request.dart';
  7. import '../api/response/config_response.dart';
  8. import '../bean/member_pop_up_bean.dart';
  9. class ConfigRepository {
  10. ConfigRepository._();
  11. static const _memberPopUpConfigKey = "member_pop_up";
  12. static const _discountPageCloseConfigKey = "discount_page_close";
  13. final discountShowAfterStore = false.obs;
  14. final showDiscountClose = true.obs;
  15. Future<ConfigResponse> requestConfigsData() {
  16. return atmobApi
  17. .confs(ConfigRequest(
  18. confCodes: [_memberPopUpConfigKey, _discountPageCloseConfigKey]))
  19. .then(HttpHandler.handle(true));
  20. }
  21. void refreshConfig() {
  22. AsyncUtil.retry(() => requestConfigsData(), Duration(seconds: 3),
  23. maxRetry: 100)
  24. .then((configsResponse) {
  25. final list = configsResponse.list;
  26. if (list == null || list.isEmpty) {
  27. debugPrint('refreshConfig....list is empty');
  28. return;
  29. }
  30. for (var config in list) {
  31. var key = config.confCode;
  32. var value = config.value;
  33. debugPrint(
  34. 'refreshConfig....key: $key, value: $value, type: ${value.runtimeType}');
  35. if (value == null || value is! Map<String, dynamic>) {
  36. continue;
  37. }
  38. switch (key) {
  39. case _memberPopUpConfigKey:
  40. onMemberPopUpConfigUpdate(value);
  41. break;
  42. case _discountPageCloseConfigKey:
  43. onDiscountPageCloseConfigUpdate(value);
  44. break;
  45. default:
  46. debugPrint('refreshConfig....unknown config key: $key');
  47. break;
  48. }
  49. }
  50. });
  51. }
  52. void onMemberPopUpConfigUpdate(value) {
  53. final memberPopUpBean = MemberPopUpBean.fromJson(value);
  54. discountShowAfterStore.value = memberPopUpBean.enable == true;
  55. }
  56. void onDiscountPageCloseConfigUpdate(value) {
  57. showDiscountClose.value = value['showClose'] == true;
  58. }
  59. }
  60. final configRepository = ConfigRepository._();