ARBItemSpecialData.parseFromString constructor

ARBItemSpecialData.parseFromString(
  1. String str
)

creates new special data from string like: {sex, select, male{His birthday} female{Her birthday} other{Their birthday}} {count, plural, zero{You have no new messages} other{You have {count} new messages}}

Implementation

factory ARBItemSpecialData.parseFromString(String str) {
  if (!str.contains('plural') && !str.contains('select')) {
    throw Exception('String "$str" does not contain special token');
  }

  if (str[0] != '{' || str[str.length - 1] != '}') {
    throw Exception('"$str" is not valid special data string');
  }

  final key = str.substring(1, str.indexOf(',')).trim();
  final token = str.contains('plural') ? 'plural' : 'select';
  final type = token == 'plural'
      ? ARBItemSpecialDataType.plural
      : ARBItemSpecialDataType.select;
  final tokenEndIndex = str.indexOf(token) + token.length;
  String optionsStr = str.substring(tokenEndIndex, str.length).trim();
  if (optionsStr[0] == ',') {
    optionsStr = optionsStr.substring(1, optionsStr.length).trimLeft();
  }

  List<ARBItemSpecialDataOption> options = [];
  while (optionsStr.indexOf('{') > 0 && optionsStr.indexOf('}') > 0) {
    final openIx = optionsStr.indexOf('{');
    final closeIx = optionsStr.getClosingBracketIndex(openIx);
    final optionKey = optionsStr.substring(0, openIx).trim();
    final optionText = optionsStr.substring(openIx + 1, closeIx).trim();
    options.add(ARBItemSpecialDataOption(optionKey, optionText));
    optionsStr = optionsStr.substring(closeIx + 1, optionsStr.length);
  }

  return ARBItemSpecialData(
    key: key,
    type: type,
    options: options,
  );
}