TryReadElementFromXml method

  1. @override
Future<bool> TryReadElementFromXml(
  1. EwsServiceXmlReader reader
)
override
Tries to read element from XML. The reader.

Implementation

@override
Future<bool> TryReadElementFromXml(EwsServiceXmlReader reader) async {
  switch (reader.LocalName) {
    case XmlElementNames.TimeZone:
      LegacyAvailabilityTimeZone legacyTimeZone =
          LegacyAvailabilityTimeZone();
      await legacyTimeZone.LoadFromXml(reader, reader.LocalName);

      _timeZone = legacyTimeZone.ToTimeZoneInfo();

      return true;
    case XmlElementNames.WorkingPeriodArray:
      List<WorkingPeriod> workingPeriods = <WorkingPeriod>[];

      do {
        await reader.Read();

        if (reader.IsStartElementWithNamespace(
          XmlNamespace.Types,
          XmlElementNames.WorkingPeriod,
        )) {
          WorkingPeriod workingPeriod = WorkingPeriod();

          await workingPeriod.LoadFromXml(reader, reader.LocalName);

          workingPeriods.add(workingPeriod);
        }
      } while (!reader.IsEndElementWithNamespace(
        XmlNamespace.Types,
        XmlElementNames.WorkingPeriodArray,
      ));

      // Availability supports a structure that can technically represent different working
      // times for each day of the week. This is apparently how the information is stored in
      // Exchange. However, no client (Outlook, OWA) either will let you specify different
      // working times for each day of the week, and Outlook won't either honor that complex
      // structure if it happens to be in Exchange.
      // So here we'll do what Outlook and OWA do: we'll use the start and end times of the
      // first working period, but we'll use the week days of all the periods.
      _startTime = workingPeriods[0].StartTime;
      _endTime = workingPeriods[0].EndTime;

      for (WorkingPeriod workingPeriod in workingPeriods) {
        for (DayOfTheWeek dayOfWeek in workingPeriod.DaysOfWeek) {
          if (!_daysOfTheWeek.contains(dayOfWeek)) {
            _daysOfTheWeek.add(dayOfWeek);
          }
        }
      }

      return true;
    default:
      return false;
  }
}