app_environment_config.dart
3.04 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import 'package:doublefeel_flutter/core/network/dio_client.dart';
import 'package:doublefeel_flutter/pigeon/platform_api.g.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.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();
final stored = await _storage.readRegion();
if (stored == null) {
final detected = _detectRegionFromLocale();
await setRegion(detected);
} else {
region.value = stored;
}
return this;
}
AppRegion _detectRegionFromLocale() {
final country = WidgetsBinding
.instance.platformDispatcher.locale.countryCode
?.toUpperCase();
return switch (country) {
'CN' => AppRegion.china,
_ => AppRegion.global,
};
}
Future<void> setEnvironment(AppEnvironment value) async {
environment.value = value;
await _storage.writeEnvironment(value);
}
bool _isNativeDebug = false;
bool get isDebug => kDebugMode || _isNativeDebug;
final Rx<AppRegion> region = AppRegion.global.obs;
Future<void> setRegion(AppRegion value) async {
region.value = value;
await _storage.writeRegion(value);
Get.find<DioClient>().refreshBaseUrl();
}
String get serverBaseUrl {
final regionUrl = switch (region.value) {
AppRegion.china => AppConst.serverBaseUrl,
AppRegion.global => AppConst.serverBaseUrlGlobal,
};
return resolveServerUrl(regionUrl);
}
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;
}
enum AppRegion { china, global }