app_environment_config.dart
2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import 'package:doublefeel_flutter/pigeon/platform_api.g.dart';
import 'package:flutter/foundation.dart';
import 'package:get/get.dart';
import '../constants/app_const.dart';
import '../constants/storage_const.dart';
import '../../data/local/local_storage.dart';
import 'app_environment.dart';
/// Resolves API base URL and third-party keys by environment.
class AppEnvironmentConfig {
AppEnvironmentConfig(this._storage);
final LocalStorage _storage;
final PlatformHostApi _platformHostApi = PlatformHostApi();
final Rx<AppEnvironment> environment = AppEnvironment.release.obs;
Future<AppEnvironmentConfig> init() async {
environment.value = await _storage.readEnvironment();
_isNativeDebug = await _platformHostApi.isDebugEnvoriment();
return this;
}
Future<void> setEnvironment(AppEnvironment value) async {
environment.value = value;
await _storage.writeEnvironment(value);
}
bool _isNativeDebug = false;
bool get isDebug => kDebugMode || _isNativeDebug;
String get serverBaseUrl => resolveServerUrl(AppConst.serverBaseUrl);
String resolveServerUrl(String url) {
return resolveServerUrlFor(environment.value, url);
}
String resolveServerUrlFor(AppEnvironment targetEnvironment, String url) {
try {
final uri = Uri.parse(url);
final host = uri.host;
final resolvedHost = switch (targetEnvironment) {
AppEnvironment.xlab => '${AppConst.prefixXlabServerHost}-$host',
AppEnvironment.dev => '${AppConst.prefixDevServerHost}-$host',
AppEnvironment.release => host,
};
if (resolvedHost == host) {
return url;
}
return uri.replace(host: resolvedHost).toString();
} catch (_) {
return url;
}
}
String get rongCloudAppKey => environment.value == AppEnvironment.dev
? AppConst.appKeyRongCloudDev
: AppConst.appKeyRongCloudProduct;
String get thinkingDataAppId => environment.value == AppEnvironment.dev
? AppConst.appIdThinkingDataDebug
: AppConst.appIdThinkingDataRelease;
static const environmentPrefsName = StorageConst.appEnvironmentPrefsName;
}