showSlidePopup function

dynamic showSlidePopup(
  1. BuildContext context,
  2. LucyAction? lucyAction,
  3. IvivaService? ivivaService,
  4. String dialogTitle,
  5. String executionText,
  6. LMExecutionConfig? postExecution,
)

Implementation

showSlidePopup(
  BuildContext context,
  LucyAction? lucyAction,
  IvivaService? ivivaService,
  //Icon? icon,
  String dialogTitle,
  String executionText,
  LMExecutionConfig? postExecution,
) {
  showModalBottomSheet(
      context: context,
      isDismissible: false,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.vertical(
          top: Radius.circular(40),
        ),
      ),
      builder: (context) {
        // return ModelBottomSheetBodyEx(
        //   lucyAction: lucyAction,
        //   ivivaService: ivivaService,
        //   //icon: icon,
        //   dialogTitle: dialogTitle,
        //   executionText: executionText,
        //   postExecution: postExecution,
        // );

        return ModelBottomSheetBody(
          title: dialogTitle,
          body: BasicBody(
            child: Column(
              children: [
                SizedBox(
                  height: 40,
                  width: 40,
                  child: CircularProgressIndicator(),
                ),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text(
                    localizedText(executionText),
                    style: TextStyle(fontSize: 12),
                  ),
                ),
              ],
            ),
          ),
          button: null,
        );
      });

  lucyAction?.execute().then((value) {
    LMAction? lmAction;
    if (value is Map<String, dynamic>) {
      lmAction = LMAction.fromJson(value);
    }

    if (lmAction?.type != null) {
      Navigator.pop(context);
      lmAction?.execute(context);
    } else {
      String msg;
      if (value is String) {
        if (value.isNotEmpty)
          msg = value;
        else
          msg = postExecution?.success?.msg ?? "";
      } else {
        msg = postExecution?.success?.msg ?? "";
      }

      // We'll use this timer variable to track the timer created later
      // The one defined at top scope will be used in the showModalBottomSheet
      Function executeFunction = () {
        if (postExecution?.success?.goToHome == true) {
          navigatorKey.currentState
              ?.pushNamedAndRemoveUntil('/home', (route) => false);
        } else if ((postExecution?.success?.goBack ?? 0) > 0) {
          int n = (postExecution?.success?.goBack ?? 0) + 1;
          int count = 0;
          Navigator.popUntil(context, (route) {
            return count++ == n;
          });
        } else {
          Navigator.pop(context);
          if (Navigator.canPop(context)) {
            Navigator.pop(context);
          }
        }
      };

      Widget bodyEx = BasicBody(
          child: Column(
        children: [
          Icon(
            Icons.check_circle,
            color: Colors.green,
            size: 40,
          ),
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: Text(
              localizedText(msg),
              style: TextStyle(fontSize: 12),
            ),
          ),
        ],
      ));

      Navigator.pop(context);

      // Create the timer variable outside the builder to ensure it's properly managed
      Timer? autoExecuteTimer;

      showModalBottomSheet(
          isDismissible: false,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.vertical(
              top: Radius.circular(40),
            ),
          ),
          context: context,
          builder: (c) {
            // Create the timer after bottom sheet is shown
            WidgetsBinding.instance.addPostFrameCallback((_) {
              if (postExecution?.success?.autoClosePopup ?? false) {
                autoExecuteTimer = Timer(
                    Duration(
                        seconds: postExecution
                                ?.success?.autoClosePopupAfterSeconds ??
                            3), () {
                  // Check if context is still mounted before executing
                  if (Navigator.of(c).mounted) {
                    executeFunction();
                  }
                });
              }
            });

            return ModelBottomSheetBody(
              title: dialogTitle,
              body: bodyEx,
              button: ElevatedButton(
                style: ButtonStyle(
                    backgroundColor: WidgetStateProperty.all<Color>(
                      Color(0xff0066FF),
                    ),
                    shape: WidgetStateProperty.all<RoundedRectangleBorder>(
                        RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(18.0),
                    ))),
                onPressed: () {
                  if (autoExecuteTimer != null && autoExecuteTimer!.isActive) {
                    autoExecuteTimer!.cancel();
                  }

                  executeFunction();

                  //widget.postExecution?.success?.action?.execute(context);
                },
                child: Text(
                  localizedText(postExecution?.success?.buttonTitle ?? ""),
                  style: TextStyle(fontSize: 14, color: Colors.white),
                ),
              ),
            );
          }).then((_) {
        if (autoExecuteTimer != null && autoExecuteTimer!.isActive) {
          autoExecuteTimer!.cancel();
        }
      });
    }
  }, onError: (error) {
    Widget bodyEx = BasicBody(
      child: Column(
        children: [
          Icon(
            Icons.error,
            color: Colors.red,
            size: 40,
          ),
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: Text(
              localizedText(
                  (error != "") ? error : (postExecution?.error?.msg ?? "")),
              style: TextStyle(fontSize: 12),
            ),
          ),
        ],
      ),
    );
    Navigator.pop(context);
    showModalBottomSheet(
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.vertical(
            top: Radius.circular(40),
          ),
        ),
        isDismissible: true,
        context: context,
        builder: (c) {
          return ModelBottomSheetBody(
            title: dialogTitle,
            body: bodyEx,
            button: null,
          );
        });
  });

  ivivaService?.execute().then((value) {
    if (value != null && (value is String)) {
      Widget body = SuccessBody(
        successMessage: postExecution?.success?.msg ?? "",
        value: value,
      );

      Navigator.pop(context);

      // Create a timer variable for the ivivaService case with a different name
      Timer? serviceAutoExecuteTimer;

      showModalBottomSheet(
          isDismissible: false,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.vertical(
              top: Radius.circular(40),
            ),
          ),
          context: context,
          builder: (c) {
            // Check if we need to set up an auto-close timer
            WidgetsBinding.instance.addPostFrameCallback((_) {
              if (postExecution?.success?.autoClosePopup ?? false) {
                serviceAutoExecuteTimer = Timer(
                    Duration(
                        seconds: postExecution
                                ?.success?.autoClosePopupAfterSeconds ??
                            3), () {
                  // Check if context is still mounted before executing
                  if (Navigator.of(c).mounted) {
                    if (postExecution?.success?.goToHome == true) {
                      navigatorKey.currentState
                          ?.pushNamedAndRemoveUntil('/home', (route) => false);
                    } else {
                      Navigator.pop(c);
                      navigatorKey.currentState
                          ?.pushNamedAndRemoveUntil('/home', (route) => false);
                    }
                  }
                });
              }
            });

            return ModelBottomSheetBody(
              title: dialogTitle,
              body: body,
              button: ElevatedButton(
                style: ButtonStyle(
                    backgroundColor: WidgetStateProperty.all<Color>(
                      Color(0xff0066FF),
                    ),
                    shape: WidgetStateProperty.all<RoundedRectangleBorder>(
                        RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(18.0),
                    ))),
                onPressed: () {
                  if (serviceAutoExecuteTimer != null &&
                      serviceAutoExecuteTimer!.isActive) {
                    serviceAutoExecuteTimer!.cancel();
                  }

                  if (postExecution?.success?.goToHome == true) {
                    navigatorKey.currentState
                        ?.pushNamedAndRemoveUntil('/home', (route) => false);
                  } else {
                    Navigator.pop(context);
                    navigatorKey.currentState
                        ?.pushNamedAndRemoveUntil('/home', (route) => false);
                  }

                  //widget.postExecution?.success?.action?.execute(context);
                },
                child: Text(
                  localizedText(postExecution?.success?.buttonTitle ?? ""),
                  style: TextStyle(fontSize: 14, color: Colors.white),
                ),
              ),
            );
          }).then((_) {
        if (serviceAutoExecuteTimer != null &&
            serviceAutoExecuteTimer!.isActive) {
          serviceAutoExecuteTimer!.cancel();
        }
      });
    }
  }, onError: (error) {
    Widget body = ErrorBody(
      value: error.toString(),
      errorMessage: postExecution?.error?.msg ?? "",
    );
    showModalBottomSheet(
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.vertical(
            top: Radius.circular(40),
          ),
        ),
        isDismissible: true,
        context: context,
        builder: (c) {
          return ModelBottomSheetBody(
            title: dialogTitle,
            body: body,
            button: null,
          );
        });
  });
}