execute method

Future<void> execute(
  1. BuildContext context, {
  2. Map<String, dynamic>? data,
  3. Map<String, String>? customLucyActionParams,
})

Implementation

Future<void> execute(BuildContext context,
    {Map<String, dynamic>? data,
    Map<String, String>? customLucyActionParams}) async {
  try {
    if (data == null) data = {};
    var lucyActionParams = lucyActionEx?.params ?? {};
    lucyActionParams = await _preprocessParams(lucyActionParams, data);

    dynamic lucyActionResult =
        await lucyActionEx?.execute(lucyActionParams) ?? {};

    if (lucyActionResult is String) {
      data['lucyActionResult'] = jsonDecode(lucyActionResult);
      data['result'] = jsonDecode(lucyActionResult);
    } else {
      data['lucyActionResult'] = lucyActionResult;
      data['result'] = lucyActionResult;
    }

    String? typeEx = type;

    if ((goToHome ?? false) == true) typeEx = 'go-home';

    if (typeEx == null) {
      if (redirectUrl != null) typeEx = 'open-url';
      //if (redirectPage != null) typeEx = 'open-page';
      if (redirectWidget != null) typeEx = 'open-widget';
      if (lucyAction != null) typeEx = 'execute-action';
      if (mobileUI != null) typeEx = 'open-mobile-ui';
    }

    if (typeEx == 'go-home') {
      navigatorKey.currentState
          ?.pushNamedAndRemoveUntil('/home', (route) => false);
    } else if (typeEx == 'open-addon') {
      navigatorKey.currentState
          ?.pushNamed((redirectAddOn?["addon"]?["Name"]));
    } else if (typeEx == 'open-url') {
      String url = redirectUrl?['url'] ?? '';

      url = replaceStringJsonPath(url, data);
      Uri? uri = Uri.tryParse(url);

      if (uri?.host == "") {
        if ((uri?.isScheme("about") ?? true)) {
          return;
        }
        if ((uri?.isScheme("map") ?? true)) {
          launchMap(uri);

          return;
        }
        if ((uri?.isScheme("sms") ?? true)) {
          //sms:<phone number>	sms:5550101234
          launchUrl(Uri(scheme: "sms", path: uri.toString()));

          return;
        }
        if ((uri?.isScheme("tel") ?? true)) {
          //tel:<phone number>	tel:+1-555-010-999
          launchUrl(Uri(scheme: "tel", path: uri.toString()));

          return;
        }
        if ((uri?.isScheme("mailto") ?? true)) {
          //mailto:<email address>?subject=<subject>&body=<body> - example
          //mailto:smith@example.org?subject=News&body=New%20plugin
          launchUrl(Uri(scheme: "mailto", path: uri.toString()));

          return;
        }
        if ((uri?.isScheme("geo") ?? true)) {
          // example - geo:$latitude,$longitude?z=$zoomLevel
          launchUrl(Uri(scheme: "geo", path: uri.toString()));

          return;
        }
        url = IvivaAccount().getAccountPath(url);
      }
      if (redirectUrl != null) {
        if (await canLaunchUrl(Uri.parse(url))) {
          await launchUrl(Uri.parse(url));
        } else {
          throw 'Could not launch $url';
        }
      }
    } else if (typeEx == 'open-widget') {
      String nameEx = "";
      if (redirectWidget?.widget?.name?.contains('{\$.') ?? false) {
        nameEx =
            replaceStringJsonPath(redirectWidget?.widget?.name ?? '', data);
      }
      UXPProps? props;
      Widget? uxpWidget;
      if (nameEx != '') {
        props = WidgetsRepository().getUXPProps(null, nameEx);
        uxpWidget = WidgetsRepository().getUXPWidget(
          null,
          nameEx,
        );
      } else {
        props = WidgetsRepository().getUXPProps(
            redirectWidget?.widget?.id, redirectWidget?.widget?.name);
        uxpWidget = WidgetsRepository().getUXPWidget(
          redirectWidget?.widget?.id,
          redirectWidget?.widget?.name,
        );
      }

      if (props == null) {
        return;
      }

      if (props.template?.type == "empty-widget" ||
          props.template?.type == "object-widget" ||
          props.template?.type == "form-widget" ||
          props.template?.type == "mapsindoor" ||
          props.template?.type == "resource-booking" ||
          props.template?.type == "search-widget") {
        if (uxpWidget != null) {
          //   /*
          //   IMPORTANT - DONT CHANGE BELOW LINE
          //   below json decode/encode is required for dart deep copy other wise result repeats
          // */
          Map<String, String> redirectParams = Map<String, String>.from(
              jsonDecode(jsonEncode(redirectWidget?.params ?? {})));

          redirectParams = await _preprocessParams(redirectParams, data);

          if (props.template?.type == "empty-widget" ||
              props.template?.type == "form-widget") {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => Scaffold(
                  // appBar: getAppBar(props?.uiProps, props?.name),
                  appBar: FormAppBar(
                    name: props?.name,
                    uiProps: props?.uiProps,
                  ),
                  body: Container(
                    height: MediaQuery.of(context).size.height,
                    width: double.infinity,
                    child: UXPFormWidgetBody(
                      type: props?.template?.type ?? "",
                      uiProps: props?.uiProps,
                      params: redirectParams,
                    ),
                  ),
                ),
              ),
            );
          } else if (props.template?.type == "mapsindoor") {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => MapWidget(
                  apiKey: props?.uiProps?['MapsIndoorAPIKey'],
                ),
              ),
            );
          } else if (props.template?.type == "object-widget") {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => ObjectWidgetBody(
                  uiProps: props?.uiProps,
                  params: redirectParams,
                ),
                // builder: (context) => SearchScreen( ),
              ),
            );
          } else if (props.template?.type == "resource-booking") {
            LMContainerConfig? body = LMContainerConfig.fromJson(
                props.uiProps?['body'] ?? props.uiProps?['body'] ?? {});

            Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (context) =>
                      ChangeNotifierProvider<TimeSlotProvider>(
                        create: (_) => TimeSlotProvider(),
                        child: Scaffold(
                          backgroundColor: body.background?.color ??
                              Theme.of(context).appBarTheme.backgroundColor,
                          body: ResourceBookingBody(uiProps: props?.uiProps),
                        ),
                      )),
            );
          } else if (props.template?.type == "search-widget") {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => SearchScreen(
                  uiProps: props?.uiProps,
                  params: redirectParams,
                ),
              ),
            );
          }
        }
      } else {
        showDialog(
            context: context,
            builder: (c) {
              return Center(
                child: uxpWidget,
              );
            });
      }
    }
    // else if (typeEx == 'open-widget') {
    //   /*
    //   IMPORTANT - DONT CHANGE BELOW LINE
    //   below json decode/encode is required for dart deep copy other wise result repeats
    // */
    //   Map<String, String> redirectParams = Map<String, String>.from(
    //       jsonDecode(jsonEncode(redirectPage?.params ?? {})));

    //   for (var key in redirectParams.keys) {
    //     String value = redirectParams[key] ?? '';
    //     value = replaceStringJsonPath(value, data);
    //     redirectParams[key] = value;
    //   }

    //   if (context != null) {
    //     Navigator.push(
    //       context,
    //       MaterialPageRoute(
    //         builder: (context) => AppPageScreen(
    //           pageID: redirectPage?.pageID ?? '',
    //           params: redirectParams,
    //         ),
    //       ),
    //     );
    //   }
    // }
    else if (typeEx == 'execute-action') {
      _executeAction(customLucyActionParams, data, context);
    } else if (typeEx == 'execute-service') {
      _executeService(data, context);
    } else if (typeEx == 'open-mobile-ui') {
      Navigator.pushNamed(context, '/' + (mobileUI ?? ''));
    }
  } catch (e) {
    showFlash(
      context: context,
      duration: Duration(
        seconds: 3,
      ),
      builder: (context, controller) {
        return Align(
          alignment: Alignment.bottomCenter,
          child: SizedBox(
            child: Text(
              e.toString(),
              style: TextStyle(
                color: Colors.white,
              ),
            ),
          ),
        );
      },
    );
  }
}