65 lines
2.1 KiB
Dart
65 lines
2.1 KiB
Dart
import '../../favorites/models/favorite_asset_model.dart';
|
|
import '../models/fundamental_data_model.dart';
|
|
|
|
class TickerResolver {
|
|
/// Resolves the optimal ticker according to user prioritization:
|
|
/// 1. Candidate / active user-selected symbol (if valid and not equal to ISIN)
|
|
/// 2. Favorite selected ticker (if asset is in favorites and has a valid ticker)
|
|
/// 3. Primary Ticker (from Fundamentals header)
|
|
/// 4. First available ticker from AvailableTickers list
|
|
/// 5. ISIN fallback
|
|
static String? resolve({
|
|
required String isin,
|
|
String? candidateSymbol,
|
|
List<FavoriteAssetModel>? favoriteDetails,
|
|
FundamentalDataModel? fundamentals,
|
|
}) {
|
|
final cleanIsin = isin.trim().toUpperCase();
|
|
|
|
// 1. Check Candidate / User-selected symbol
|
|
if (candidateSymbol != null && candidateSymbol.trim().isNotEmpty) {
|
|
final cClean = candidateSymbol.trim();
|
|
if (cClean.toUpperCase() != cleanIsin) {
|
|
return cClean;
|
|
}
|
|
}
|
|
|
|
// 2. Check Favorite selected ticker
|
|
if (favoriteDetails != null && favoriteDetails.isNotEmpty) {
|
|
for (final f in favoriteDetails) {
|
|
final fIsin = f.isin.trim().toUpperCase();
|
|
final fSym = f.symbol.trim().toUpperCase();
|
|
if (fIsin == cleanIsin || fSym == cleanIsin) {
|
|
if (f.symbol.trim().isNotEmpty && f.symbol.trim().toUpperCase() != cleanIsin) {
|
|
return f.symbol.trim();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 3. Check Fundamentals Primary Ticker
|
|
if (fundamentals != null) {
|
|
final primary = fundamentals.primaryTicker.trim();
|
|
if (primary.isNotEmpty && primary.toUpperCase() != cleanIsin) {
|
|
return primary;
|
|
}
|
|
|
|
final fTicker = fundamentals.ticker.trim();
|
|
if (fTicker.isNotEmpty && fTicker.toUpperCase() != cleanIsin) {
|
|
return fTicker;
|
|
}
|
|
|
|
// 4. First available ticker in availableTickers
|
|
for (final t in fundamentals.availableTickers) {
|
|
final tTick = t.ticker.trim();
|
|
if (tTick.isNotEmpty && tTick.toUpperCase() != cleanIsin) {
|
|
return tTick;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 5. Fallback: ISIN
|
|
return isin.trim().isNotEmpty ? isin.trim() : null;
|
|
}
|
|
}
|