Notify.fromXML constructor

Notify.fromXML(
  1. String xmlSTR
)

Implementation

factory Notify.fromXML(String xmlSTR) {
  final document = XmlDocument.parse(xmlSTR);

  String messageName = document.rootElement.attributes
      .firstWhere((p0) => p0.name.toString() == "messageName")
      .value;
  // String xmlns = document.rootElement.attributes.firstWhere((p0) => p0.name.toString() == "xmlns").value;
  String messageID = document.rootElement.attributes
      .firstWhere((p0) => p0.name.toString() == "messageID")
      .value;
  // String xsi = document.rootElement.attributes.firstWhere((p0) => p0.name.toString() == "xmlns:xsi").value;
  // String result = document.rootElement.childElements.first.attributes.firstWhere((p0) => p0.name.toString() == "result").value;

  NotificationType notifType = NotificationType.unknown;
  XmlElement notifyElement = document.findAllElements("notify").first;
  String name = "unknown";

  if (notifyElement.attributes
      .any((p0) => p0.name.toString() == "dataAvailable")) {
    if (notifyElement.attributes
            .firstWhere((p0) => p0.name.toString() == "dataAvailable")
            .value ==
        "true") {
      notifType = NotificationType.dataAvailable;
    }
  }
  if (notifyElement.attributes.isNotEmpty) {
    name = notifyElement.attributes.first.name.toString();
  } else {
    name = notifyElement.childElements.first.name.toString();
  }



  if (notifyElement.childElements.any(
      (element) => element.name.toString() == "deviceLockExpiredEvent")) {
    notifType = NotificationType.deviceLockExpire;
  }

  if (notifyElement.childElements.any(
      (element) => element.name.toString() == "deviceStatusNotification")) {
    notifType = NotificationType.deviceStatus;
  }

  notifType = NotificationType.values.firstWhere((element) => element.name==name,orElse:()=> NotificationType.unknown);

  return Notify(
      messageName: messageName,
      messageID: int.parse(messageID),
      notificationType: notifType,
      name: name);
}