LoadFreeBusyViewFromXml method

Future<void> LoadFreeBusyViewFromXml(
  1. EwsServiceXmlReader reader,
  2. FreeBusyViewType viewType
)
Loads the free busy view from XML. The reader. Type of free/busy view.

Implementation

Future<void> LoadFreeBusyViewFromXml(
  EwsServiceXmlReader reader,
  FreeBusyViewType viewType,
) async {
  await reader.ReadStartElementWithNamespace(
    XmlNamespace.Messages,
    XmlElementNames.FreeBusyView,
  );

  String? viewTypeString = await reader.ReadElementValueWithNamespace<String>(
    XmlNamespace.Types,
    XmlElementNames.FreeBusyViewType,
  );

  _viewType = EwsUtilities.Parse<FreeBusyViewType>(viewTypeString);
  if (_viewType == null) {
    throw ArgumentException(
      "Requested value '$viewTypeString' was not found in FreeBusyViewType.",
    );
  }

  do {
    await reader.Read();

    if (reader.IsStartElement()) {
      switch (reader.LocalName) {
        case XmlElementNames.MergedFreeBusy:
          String mergedFreeBusy =
              (await reader.ReadElementValue<String>()) ?? "";

          for (int i = 0; i < mergedFreeBusy.length; i++) {
            _mergedFreeBusyStatus.add(
              LegacyFreeBusyStatus.values[int.parse(mergedFreeBusy[i])],
            );
          }

          break;
        case XmlElementNames.CalendarEventArray:
          do {
            await reader.Read();

            // Sometimes Exchange Online returns blank CalendarEventArray tag like bellow.
            // <CalendarEventArray xmlns="http://schemas.microsoft.com/exchange/services/2006/types" />
            // So we have to check the end of CalendarEventArray tag.
            if (reader.LocalName == XmlElementNames.FreeBusyView) {
              // There is no the end tag of CalendarEventArray, but the reader is reading the end tag of FreeBusyView.
              break;
            } else if (reader.LocalName == XmlElementNames.WorkingHours) {
              // There is no the end tag of CalendarEventArray, but the reader is reading the start tag of WorkingHours.
              await _LoadWorkingHoursFromXml(reader);
              break;
            }

            if (reader.IsStartElementWithNamespace(
              XmlNamespace.Types,
              XmlElementNames.CalendarEvent,
            )) {
              CalendarEvent calendarEvent = CalendarEvent();

              await calendarEvent.LoadFromXml(
                reader,
                XmlElementNames.CalendarEvent,
              );

              _calendarEvents.add(calendarEvent);
            }
          } while (!reader.IsEndElementWithNamespace(
            XmlNamespace.Types,
            XmlElementNames.CalendarEventArray,
          ));

          break;
        case XmlElementNames.WorkingHours:
          await _LoadWorkingHoursFromXml(reader);

          break;
      }
    }
  } while (!reader.IsEndElementWithNamespace(
    XmlNamespace.Messages,
    XmlElementNames.FreeBusyView,
  ));
}