DiscussionRootNode.decode constructor

DiscussionRootNode.decode(
  1. int index,
  2. Map<String, dynamic> nav,
  3. List<DiscussionNode> children
)

Implementation

factory decode(
  int index,
  Map<String, dynamic> nav,
  List<DiscussionNode> children,
) {
  final matchedDate =
      _dateLabelParser.firstMatch(nav.get<String>('libelleDate')) ??
      _secondDateLabelParser.firstMatch(nav.get<String>('libelleDate'));
  final DateTime parsedDate;

  if (matchedDate != null) {
    parsedDate = DateTime.utc(
      !matchedDate.groupNames.contains('year')
          ? DateTime.now().year
          : 2000 + .parse(matchedDate.namedGroup('year')!),
      .parse(matchedDate.namedGroup('month')!),
      .parse(matchedDate.namedGroup('day')!),
    );
  } else {
    final matchedTime = _thirdDateLabelParser.firstMatch(
      nav.get<String>('libelleDate'),
    );

    if (matchedTime != null) {
      parsedDate = DateTime.now().copyWith(
        isUtc: true,
        hour: int.tryParse(matchedTime.namedGroup('hour')!),
        minute: int.tryParse(matchedTime.namedGroup('minute')!),
      );
    } else {
      final matchedCombination = _fourthDateLabelParser.firstMatch(
        nav.get<String>('libelleDate'),
      );

      if (matchedCombination == null) {
        // We fallback to now because this might make important messages sink
        // to the bottom of the discussion list if we set the date to epoch.
        parsedDate = DateTime.now().copyWith(isUtc: true);
      } else {
        // Trickiest to implement as to know which day it is, we need to match
        // a localized weekday name to a date...
        final curAttempt = DateTime.now().copyWith(
          isUtc: true,
          hour: int.tryParse(matchedCombination.namedGroup('hour')!),
          minute: int.tryParse(matchedCombination.namedGroup('minute')!),
        );

        final rawWeekday = matchedCombination.namedGroup('weekday');
        final weekDay = rawWeekday == null ? null : weekday(rawWeekday);

        if (weekDay != null) {
          parsedDate = curAttempt.subtract(
            Duration(days: (curAttempt.weekday - weekDay).abs()),
          );
        } else {
          parsedDate = curAttempt;
        }
      }
    }
  }

  return .new(
    id: nav.get('N'),
    index: index,
    recipients: nav.getLM('listePossessionsMessages').mapL((e) => .decode(e)),
    isNotARecipient: nav.get('estNonPossede') ?? false,
    depth: nav.get('profondeur'),
    messageCount: nav.get('nombreMessages'),
    subject: nav.get('objet'),
    withResponse: nav.get('avecReponse') ?? false,
    messageIdForParticipants: nav.go('messagePourParticipants').get('N'),
    public: nav.get('public'),
    publicCount: nav.get('nbPublic'),
    initiator: nav.get('initiateur'),
    read: nav.get('lu') ?? false,
    dateLabel: nav.get('libelleDate'),
    parsedDateLabel: parsedDate,
    closeable: nav.get('fermable') ?? false,
    closed: nav.get('ferme') ?? false,
    canEdit: nav.get('avecModifObjet') ?? false,
    children: children,
  );
}