getBalance static method
String?
getBalance(
- dynamic message, [
- BalanceKeywordType keyWordType = BalanceKeywordType.available
Gets balance from the message
Implementation
static String? getBalance(
dynamic message, [
BalanceKeywordType keyWordType = BalanceKeywordType.available,
]) {
final processedMessage = MessageProcessor.getProcessedMessage(message);
final messageString = processedMessage.join(' ');
int indexOfKeyword = -1;
String balance = '';
final balanceKeywords = keyWordType == BalanceKeywordType.available
? availableBalanceKeywords
: outstandingBalanceKeywords;
for (final word in balanceKeywords) {
indexOfKeyword = messageString.indexOf(word);
if (indexOfKeyword != -1) {
indexOfKeyword += word.length;
break;
}
}
// Found the index of keyword, moving on to finding 'rs.' occurring after indexOfKeyword
int index = indexOfKeyword;
int indexOfRs = -1;
// If keyword not found or not enough space for 'rs.', check non-standard
if (indexOfKeyword == -1 || index + 3 > messageString.length) {
balance = _findNonStandardBalance(messageString, keyWordType) ?? '';
return balance.isNotEmpty
? MessageProcessor.padCurrencyValue(balance)
: null;
}
String nextThreeChars = messageString.substring(index, index + 3);
index += 3;
while (index < messageString.length) {
// Discard first char and add the current char at the end
nextThreeChars = nextThreeChars.substring(1) + messageString[index];
if (nextThreeChars == 'rs.') {
indexOfRs = index + 2;
break;
}
index += 1;
}
// No occurrence of 'rs.'
if (indexOfRs == -1) {
// Check for non-standard balance
balance = _findNonStandardBalance(messageString, keyWordType) ?? '';
return balance.isNotEmpty
? MessageProcessor.padCurrencyValue(balance)
: null;
}
balance = _extractBalance(indexOfRs, messageString, messageString.length);
return balance.isNotEmpty
? MessageProcessor.padCurrencyValue(balance)
: null;
}