99 lines
3.2 KiB
Dart
99 lines
3.2 KiB
Dart
import '../network/api_client.dart';
|
|
|
|
/// Asset utility helpers to map ISIN codes, symbols, and company names to logos and details.
|
|
class AssetUtils {
|
|
static final Map<String, String> _isinToNameMap = {
|
|
'US0378331005': 'Apple Inc.',
|
|
'US5949181045': 'Microsoft Corp.',
|
|
'US0231351067': 'Amazon.com Inc.',
|
|
'US67066G1040': 'NVIDIA Corp.',
|
|
'US88160R1014': 'Tesla Inc.',
|
|
'US02079K3059': 'Alphabet Inc.',
|
|
'US30303M1027': 'Meta Platforms',
|
|
'DE0007164600': 'SAP SE',
|
|
'DE0007236101': 'Siemens AG',
|
|
'DE0008469008': 'Allianz SE',
|
|
'FR0004125920': 'Amundi',
|
|
};
|
|
|
|
static final Map<String, String> _nameToIsinMap = {
|
|
'APPLE INC.': 'US0378331005',
|
|
'APPLE': 'US0378331005',
|
|
'MICROSOFT CORP.': 'US5949181045',
|
|
'MICROSOFT': 'US5949181045',
|
|
'AMAZON.COM INC.': 'US0231351067',
|
|
'AMAZON': 'US0231351067',
|
|
'NVIDIA CORP.': 'US67066G1040',
|
|
'NVIDIA': 'US67066G1040',
|
|
'TESLA INC.': 'US88160R1014',
|
|
'TESLA': 'US88160R1014',
|
|
'ALPHABET INC.': 'US02079K3059',
|
|
'ALPHABET': 'US02079K3059',
|
|
'META PLATFORMS': 'US30303M1027',
|
|
'META': 'US30303M1027',
|
|
'SAP SE': 'DE0007164600',
|
|
'SAP': 'DE0007164600',
|
|
'SIEMENS AG': 'DE0007236101',
|
|
'SIEMENS': 'DE0007236101',
|
|
'ALLIANZ SE': 'DE0008469008',
|
|
'ALLIANZ': 'DE0008469008',
|
|
'AMUNDI': 'FR0004125920',
|
|
};
|
|
|
|
static final Map<String, String> _imageMap = {};
|
|
|
|
/// Registers an ISIN, Name, and optional Logo Image URL.
|
|
static void registerAsset(String isin, String name, [String? imageUrl]) {
|
|
final cleanIsin = isin.trim().toUpperCase();
|
|
final cleanName = name.trim();
|
|
if (cleanIsin.isNotEmpty && cleanName.isNotEmpty) {
|
|
_isinToNameMap[cleanIsin] = cleanName;
|
|
_nameToIsinMap[cleanName.toUpperCase()] = cleanIsin;
|
|
}
|
|
if (imageUrl != null && imageUrl.isNotEmpty) {
|
|
final resolved = resolveUrl(imageUrl);
|
|
if (cleanIsin.isNotEmpty) _imageMap[cleanIsin] = resolved;
|
|
if (cleanName.isNotEmpty) _imageMap[cleanName.toUpperCase()] = resolved;
|
|
}
|
|
}
|
|
|
|
/// Resolves relative logo URLs (/api/logo/...) to complete backend endpoints.
|
|
static String resolveUrl(String url) {
|
|
if (url.startsWith('http://') || url.startsWith('https://')) {
|
|
return url;
|
|
}
|
|
if (url.startsWith('/')) {
|
|
return '${ApiClient.baseUrl}$url';
|
|
}
|
|
return '${ApiClient.baseUrl}/$url';
|
|
}
|
|
|
|
/// Resolves an ISIN or symbol to readable asset name.
|
|
static String getAssetName(String isinOrSymbol) {
|
|
final key = isinOrSymbol.trim().toUpperCase();
|
|
if (_isinToNameMap.containsKey(key)) {
|
|
return _isinToNameMap[key]!;
|
|
}
|
|
return isinOrSymbol;
|
|
}
|
|
|
|
/// Resolves an asset name or symbol to ISIN.
|
|
static String? getIsin(String nameOrSymbol) {
|
|
final key = nameOrSymbol.trim().toUpperCase();
|
|
if (_isinToNameMap.containsKey(key)) return key;
|
|
return _nameToIsinMap[key];
|
|
}
|
|
|
|
/// Returns official local backend logo URL for given symbol/name/ISIN.
|
|
static String? getLogoUrl(String symbolOrName) {
|
|
final key = symbolOrName.trim().toUpperCase();
|
|
if (_imageMap.containsKey(key)) return resolveUrl(_imageMap[key]!);
|
|
|
|
final isin = getIsin(key) ?? (key.length == 12 ? key : null);
|
|
if (isin != null && isin.length == 12) {
|
|
return '${ApiClient.baseUrl}/api/logo/$isin';
|
|
}
|
|
return null;
|
|
}
|
|
}
|