app_environment_config.dart
5.59 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import 'package:doublefeel_flutter/app/utils/platform_compact.dart';
import 'package:doublefeel_flutter/core/logging/app_diagnostic_log_store.dart';
import 'package:doublefeel_flutter/core/network/dio_client.dart';
import 'package:doublefeel_flutter/core/platform/pigeon_api_facade.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 AppPlatformHostApi _platformHostApi = AppPlatformHostApi();
final Rx<AppEnvironment> environment = AppEnvironment.release.obs;
Future<AppEnvironmentConfig> init() async {
environment.value = await _storage.readEnvironment();
_isNativeDebug = await _platformHostApi.isDebugEnvoriment();
if (isOhos()) {
region.value = AppRegion.china;
await setRegion(AppRegion.china);
return this;
}
AppDiagnosticLogStore.instance.log(
category: 'Debug',
action: 'detect_debug_from_native_success',
details: {
'isDebug': _isNativeDebug,
},
);
final stored = await _storage.readRegion();
if (stored == null) {
final detected = await _detectRegion();
await setRegion(detected);
} else {
final stored = await _storage.readRegion();
if (stored == null) {
final detected = await _detectRegion();
await setRegion(detected);
} else {
region.value = stored;
}
if (stored == AppRegion.global) {
await _storage.setTermsAgreed(true);
}
}
return this;
}
Future<AppRegion> _detectRegion() async {
try {
final isChina = await _platformHostApi.isChinaRegion();
AppDiagnosticLogStore.instance.log(
category: 'Region',
action: 'detect_region_from_native_success',
details: {
'isChina': isChina,
},
);
return isChina ? AppRegion.china : AppRegion.global;
} catch (error, stackTrace) {
final fallback = _detectRegionFromLocale();
AppDiagnosticLogStore.instance.log(
category: 'Region',
action: 'detect_region_from_native_failed',
details: {
'error': error.toString(),
'stackTrace': stackTrace.toString(),
'fallbackResult': fallback.name,
},
);
return fallback;
}
}
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;
try {
await _storage.writeRegion(value);
if (Get.isRegistered<DioClient>()) {
Get.find<DioClient>().refreshBaseUrl();
}
} catch (e, stackTrace) {
debugPrint('setRegion failed: $e');
AppDiagnosticLogStore.instance.log(
category: 'Region',
action: 'set_region_failed',
details: {
'error': e.toString(),
'stackTrace': stackTrace.toString(),
'targetRegion': value.name,
},
);
}
}
get hideChooseRegion => _storage.hideChooseRegion;
Future<void> updateHideChooseRegion(bool value) async {
await _storage.writeHideChooseRegion(value);
}
String get serverBaseUrl {
final regionUrl = switch (region.value) {
AppRegion.china => AppConst.serverBaseUrl,
AppRegion.global => AppConst.serverBaseUrlGlobal,
};
return resolveServerUrl(regionUrl);
}
/// WebSocket endpoint resolved from the active API host.
///
/// This keeps the socket in the same China/global and dev/xlab environment
/// as the REST APIs.
String get webSocketUrl {
final serverUri = Uri.parse(serverBaseUrl);
final webSocketScheme = serverUri.scheme == 'https' ? 'wss' : 'ws';
return serverUri
.replace(
scheme: webSocketScheme,
path: AppConst.webSocketPath,
query: null,
fragment: null,
)
.toString();
}
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 }