safer config reader
This commit is contained in:
84
frontend/pshared/lib/config/apply.dart
Normal file
84
frontend/pshared/lib/config/apply.dart
Normal file
@@ -0,0 +1,84 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:pshared/config/reader.dart';
|
||||
|
||||
|
||||
void applyCommonConfiguration(
|
||||
Map<String, dynamic> configJson, {
|
||||
required String currentApiProto,
|
||||
required void Function(String) setApiProto,
|
||||
required String currentApiHost,
|
||||
required void Function(String) setApiHost,
|
||||
required String currentApiEndpoint,
|
||||
required void Function(String) setApiEndpoint,
|
||||
required String currentAmplitudeSecret,
|
||||
required void Function(String) setAmplitudeSecret,
|
||||
required String currentAmplitudeServerZone,
|
||||
required void Function(String) setAmplitudeServerZone,
|
||||
required String currentPosthogApiKey,
|
||||
required void Function(String) setPosthogApiKey,
|
||||
required String currentPosthogHost,
|
||||
required void Function(String) setPosthogHost,
|
||||
required Locale currentDefaultLocale,
|
||||
required void Function(Locale) setDefaultLocale,
|
||||
required String currentDefaultCurrency,
|
||||
required void Function(String) setDefaultCurrency,
|
||||
required String currentWsProto,
|
||||
required void Function(String) setWsProto,
|
||||
required String currentWsEndpoint,
|
||||
required void Function(String) setWsEndpoint,
|
||||
required int currentDefaultDimensionLength,
|
||||
required void Function(int) setDefaultDimensionLength,
|
||||
required String currentClientId,
|
||||
required void Function(String) setClientId,
|
||||
required Color currentThemeColor,
|
||||
required void Function(Color) setThemeColor,
|
||||
}) {
|
||||
setApiProto(readConfigString(configJson, 'apiProto', currentApiProto));
|
||||
setApiHost(readConfigString(configJson, 'apiHost', currentApiHost));
|
||||
setApiEndpoint(
|
||||
readConfigString(configJson, 'apiEndpoint', currentApiEndpoint),
|
||||
);
|
||||
setAmplitudeSecret(
|
||||
readConfigString(configJson, 'amplitudeSecret', currentAmplitudeSecret),
|
||||
);
|
||||
setAmplitudeServerZone(
|
||||
readConfigString(
|
||||
configJson,
|
||||
'amplitudeServerZone',
|
||||
currentAmplitudeServerZone,
|
||||
),
|
||||
);
|
||||
setPosthogApiKey(
|
||||
readConfigString(configJson, 'posthogApiKey', currentPosthogApiKey),
|
||||
);
|
||||
setPosthogHost(
|
||||
readConfigString(configJson, 'posthogHost', currentPosthogHost),
|
||||
);
|
||||
setDefaultLocale(
|
||||
Locale(
|
||||
readConfigString(
|
||||
configJson,
|
||||
'defaultLocale',
|
||||
currentDefaultLocale.languageCode,
|
||||
),
|
||||
),
|
||||
);
|
||||
setDefaultCurrency(
|
||||
readConfigString(configJson, 'defaultCurrency', currentDefaultCurrency),
|
||||
);
|
||||
setWsProto(readConfigString(configJson, 'wsProto', currentWsProto));
|
||||
setWsEndpoint(readConfigString(configJson, 'wsEndpoint', currentWsEndpoint));
|
||||
setDefaultDimensionLength(
|
||||
readConfigInt(
|
||||
configJson,
|
||||
'defaultDimensionLength',
|
||||
currentDefaultDimensionLength,
|
||||
),
|
||||
);
|
||||
setClientId(readConfigString(configJson, 'clientId', currentClientId));
|
||||
setThemeColor(
|
||||
Color(
|
||||
readConfigInt(configJson, 'themeColor', currentThemeColor.toARGB32()),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:pshared/config/apply.dart';
|
||||
|
||||
class CommonConstants {
|
||||
static String apiProto = 'https';
|
||||
@@ -7,7 +7,8 @@ class CommonConstants {
|
||||
static String apiEndpoint = '/api/v1';
|
||||
static String amplitudeSecret = 'c3d75b3e2520d708440acbb16b923e79';
|
||||
static String amplitudeServerZone = 'EU';
|
||||
static String posthogApiKey = 'phc_lVhbruaZpxiQxppHBJpL36ARnPlkqbCewv6cauoceTN';
|
||||
static String posthogApiKey =
|
||||
'phc_lVhbruaZpxiQxppHBJpL36ARnPlkqbCewv6cauoceTN';
|
||||
static String posthogHost = 'https://eu.i.posthog.com';
|
||||
static Locale defaultLocale = const Locale('en');
|
||||
static String defaultCurrency = 'EUR';
|
||||
@@ -29,21 +30,36 @@ class CommonConstants {
|
||||
|
||||
// Method to apply the configuration, called by platform-specific implementations
|
||||
static void applyConfiguration(Map<String, dynamic> configJson) {
|
||||
apiProto = configJson['apiProto'] ?? apiProto;
|
||||
apiHost = configJson['apiHost'] ?? apiHost;
|
||||
apiEndpoint = configJson['apiEndpoint'] ?? apiEndpoint;
|
||||
amplitudeSecret = configJson['amplitudeSecret'] ?? amplitudeSecret;
|
||||
amplitudeServerZone = configJson['amplitudeServerZone'] ?? amplitudeServerZone;
|
||||
posthogApiKey = configJson['posthogApiKey'] ?? posthogApiKey;
|
||||
posthogHost = configJson['posthogHost'] ?? posthogHost;
|
||||
defaultLocale = Locale(configJson['defaultLocale'] ?? defaultLocale.languageCode);
|
||||
defaultCurrency = configJson['defaultCurrency'] ?? defaultCurrency;
|
||||
wsProto = configJson['wsProto'] ?? wsProto;
|
||||
wsEndpoint = configJson['wsEndpoint'] ?? wsEndpoint;
|
||||
defaultDimensionLength = configJson['defaultDimensionLength'] ?? defaultDimensionLength;
|
||||
clientId = configJson['clientId'] ?? clientId;
|
||||
if (configJson.containsKey('themeColor')) {
|
||||
themeColor = Color(int.parse(configJson['themeColor']));
|
||||
}
|
||||
applyCommonConfiguration(
|
||||
configJson,
|
||||
currentApiProto: apiProto,
|
||||
setApiProto: (value) => apiProto = value,
|
||||
currentApiHost: apiHost,
|
||||
setApiHost: (value) => apiHost = value,
|
||||
currentApiEndpoint: apiEndpoint,
|
||||
setApiEndpoint: (value) => apiEndpoint = value,
|
||||
currentAmplitudeSecret: amplitudeSecret,
|
||||
setAmplitudeSecret: (value) => amplitudeSecret = value,
|
||||
currentAmplitudeServerZone: amplitudeServerZone,
|
||||
setAmplitudeServerZone: (value) => amplitudeServerZone = value,
|
||||
currentPosthogApiKey: posthogApiKey,
|
||||
setPosthogApiKey: (value) => posthogApiKey = value,
|
||||
currentPosthogHost: posthogHost,
|
||||
setPosthogHost: (value) => posthogHost = value,
|
||||
currentDefaultLocale: defaultLocale,
|
||||
setDefaultLocale: (value) => defaultLocale = value,
|
||||
currentDefaultCurrency: defaultCurrency,
|
||||
setDefaultCurrency: (value) => defaultCurrency = value,
|
||||
currentWsProto: wsProto,
|
||||
setWsProto: (value) => wsProto = value,
|
||||
currentWsEndpoint: wsEndpoint,
|
||||
setWsEndpoint: (value) => wsEndpoint = value,
|
||||
currentDefaultDimensionLength: defaultDimensionLength,
|
||||
setDefaultDimensionLength: (value) => defaultDimensionLength = value,
|
||||
currentClientId: clientId,
|
||||
setClientId: (value) => clientId = value,
|
||||
currentThemeColor: themeColor,
|
||||
setThemeColor: (value) => themeColor = value,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:pshared/config/apply.dart';
|
||||
|
||||
class CommonConstants {
|
||||
static String apiProto = 'http';
|
||||
@@ -7,7 +7,8 @@ class CommonConstants {
|
||||
static String apiEndpoint = '/api/v1';
|
||||
static String amplitudeSecret = 'c3d75b3e2520d708440acbb16b923e79';
|
||||
static String amplitudeServerZone = 'EU';
|
||||
static String posthogApiKey = 'phc_lVhbruaZpxiQxppHBJpL36ARnPlkqbCewv6cauoceTN';
|
||||
static String posthogApiKey =
|
||||
'phc_lVhbruaZpxiQxppHBJpL36ARnPlkqbCewv6cauoceTN';
|
||||
static String posthogHost = 'https://eu.i.posthog.com';
|
||||
static Locale defaultLocale = const Locale('en');
|
||||
static String defaultCurrency = 'EUR';
|
||||
@@ -29,21 +30,36 @@ class CommonConstants {
|
||||
|
||||
// Method to apply the configuration, called by platform-specific implementations
|
||||
static void applyConfiguration(Map<String, dynamic> configJson) {
|
||||
apiProto = configJson['apiProto'] ?? apiProto;
|
||||
apiHost = configJson['apiHost'] ?? apiHost;
|
||||
apiEndpoint = configJson['apiEndpoint'] ?? apiEndpoint;
|
||||
amplitudeSecret = configJson['amplitudeSecret'] ?? amplitudeSecret;
|
||||
amplitudeServerZone = configJson['amplitudeServerZone'] ?? amplitudeServerZone;
|
||||
posthogApiKey = configJson['posthogApiKey'] ?? posthogApiKey;
|
||||
posthogHost = configJson['posthogHost'] ?? posthogHost;
|
||||
defaultLocale = Locale(configJson['defaultLocale'] ?? defaultLocale.languageCode);
|
||||
defaultCurrency = configJson['defaultCurrency'] ?? defaultCurrency;
|
||||
wsProto = configJson['wsProto'] ?? wsProto;
|
||||
wsEndpoint = configJson['wsEndpoint'] ?? wsEndpoint;
|
||||
defaultDimensionLength = configJson['defaultDimensionLength'] ?? defaultDimensionLength;
|
||||
clientId = configJson['clientId'] ?? clientId;
|
||||
if (configJson.containsKey('themeColor')) {
|
||||
themeColor = Color(int.parse(configJson['themeColor']));
|
||||
}
|
||||
applyCommonConfiguration(
|
||||
configJson,
|
||||
currentApiProto: apiProto,
|
||||
setApiProto: (value) => apiProto = value,
|
||||
currentApiHost: apiHost,
|
||||
setApiHost: (value) => apiHost = value,
|
||||
currentApiEndpoint: apiEndpoint,
|
||||
setApiEndpoint: (value) => apiEndpoint = value,
|
||||
currentAmplitudeSecret: amplitudeSecret,
|
||||
setAmplitudeSecret: (value) => amplitudeSecret = value,
|
||||
currentAmplitudeServerZone: amplitudeServerZone,
|
||||
setAmplitudeServerZone: (value) => amplitudeServerZone = value,
|
||||
currentPosthogApiKey: posthogApiKey,
|
||||
setPosthogApiKey: (value) => posthogApiKey = value,
|
||||
currentPosthogHost: posthogHost,
|
||||
setPosthogHost: (value) => posthogHost = value,
|
||||
currentDefaultLocale: defaultLocale,
|
||||
setDefaultLocale: (value) => defaultLocale = value,
|
||||
currentDefaultCurrency: defaultCurrency,
|
||||
setDefaultCurrency: (value) => defaultCurrency = value,
|
||||
currentWsProto: wsProto,
|
||||
setWsProto: (value) => wsProto = value,
|
||||
currentWsEndpoint: wsEndpoint,
|
||||
setWsEndpoint: (value) => wsEndpoint = value,
|
||||
currentDefaultDimensionLength: defaultDimensionLength,
|
||||
setDefaultDimensionLength: (value) => defaultDimensionLength = value,
|
||||
currentClientId: clientId,
|
||||
setClientId: (value) => clientId = value,
|
||||
currentThemeColor: themeColor,
|
||||
setThemeColor: (value) => themeColor = value,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
30
frontend/pshared/lib/config/reader.dart
Normal file
30
frontend/pshared/lib/config/reader.dart
Normal file
@@ -0,0 +1,30 @@
|
||||
String readConfigString(
|
||||
Map<String, dynamic> configJson,
|
||||
String key,
|
||||
String fallback,
|
||||
) {
|
||||
final value = configJson[key];
|
||||
if (value == null) {
|
||||
return fallback;
|
||||
}
|
||||
final text = value.toString().trim();
|
||||
if (text.isEmpty || text == 'undefined' || text == 'null') {
|
||||
return fallback;
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
int readConfigInt(Map<String, dynamic> configJson, String key, int fallback) {
|
||||
final value = configJson[key];
|
||||
if (value == null) {
|
||||
return fallback;
|
||||
}
|
||||
if (value is int) {
|
||||
return value;
|
||||
}
|
||||
final text = value.toString().trim();
|
||||
if (text.isEmpty || text == 'undefined' || text == 'null') {
|
||||
return fallback;
|
||||
}
|
||||
return int.tryParse(text) ?? fallback;
|
||||
}
|
||||
Reference in New Issue
Block a user