|
|
|
import 'package:doublefeel_flutter/core/result/app_result.dart';
|
|
|
|
import 'package:get/get.dart';
|
|
|
|
|
|
|
|
import '../logging/app_logger.dart';
|
|
|
|
import '../network/api/config_api.dart';
|
|
|
|
import '../../data/models/config/config_models.dart';
|
|
|
|
|
|
|
|
/// Globally accessible service that fetches and caches [AppConfig] from the
|
|
|
|
/// server. Registered with [permanent: true] so GetX never disposes it.
|
|
|
|
///
|
|
|
|
/// Usage (anywhere in the app):
|
|
|
|
/// ```dart
|
|
|
|
/// // One-off read
|
|
|
|
/// final show = AppConfigService.to.config.showYearVipDiscount;
|
|
|
|
///
|
|
|
|
/// // Reactive (inside Obx / ever)
|
|
|
|
/// Obx(() => AppConfigService.to.config.showYearVipDiscount ? ... : ...)
|
|
|
|
/// ```
|
|
|
|
class AppConfigService extends GetxService {
|
|
|
|
AppConfigService(this._configApi);
|
|
|
|
|
|
|
|
static AppConfigService get to => Get.find();
|
|
|
|
|
|
|
|
final ConfigApi _configApi;
|
|
|
|
final Rx<AppConfig> _config = const AppConfig().obs;
|
|
|
|
|
|
|
|
/// Latest remote [AppConfig]. Defaults to [AppConfig()] until [fetch] succeeds.
|
|
|
|
AppConfig get config => _config.value;
|
|
|
|
|
|
|
|
/// Reactive [AppConfig] stream — use inside `Obx` / `ever`.
|
|
|
|
Rx<AppConfig> get configRx => _config;
|
|
|
|
|
|
|
|
/// Fetches [AppConfig] from the server and updates [config].
|
|
|
|
/// Safe to call multiple times; failures are logged and ignored.
|
|
|
|
Future<void> fetch() async {
|
|
|
|
final res = await _configApi.getAppConfigList();
|
|
|
|
switch (res) {
|
|
|
|
case AppSuccess(:final data):
|
|
|
|
{
|
|
|
|
_config.value = data;
|
|
|
|
AppLogger.i('AppConfigService: config fetched — $data');
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AppFailure():
|
|
|
|
{
|
|
|
|
AppLogger.w('AppConfigService: fetch failed', res.error);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|