updateNotifier<T extends Object> function

Future<void> updateNotifier<T extends Object>({
  1. required LikeStateResponse response,
  2. BuildContext? context,
  3. Future<void> onInit(
    1. LikeState state
    )?,
  4. Future<void> onSuccess(
    1. T data
    )?,
  5. Future<void> onError(
    1. LikeError error
    )?,
  6. Future<void> onException(
    1. String message
    )?,
  7. bool disableLoadingToast = true,
  8. bool disableSuccessToast = false,
  9. bool disableErrorToast = false,
  10. bool disableExceptionToast = false,
  11. bool disableCancelledToast = true,
  12. bool enableHaptics = true,
  13. Map<LikeState, String>? messageOverrides,
})

updateNotifier is the "Full" action handler. It provides built-in toasting, haptics, and state callbacks.

Use this for primary actions where you want automatic user feedback.

Implementation

Future<void> updateNotifier<T extends Object>({
  required LikeStateResponse<dynamic> response,
  BuildContext? context,
  Future<void> Function(LikeState state)? onInit,
  Future<void> Function(T data)? onSuccess,
  Future<void> Function(LikeError error)? onError,
  Future<void> Function(String message)? onException,

  // Toast Control Flags
  bool disableLoadingToast = true,
  bool disableSuccessToast = false,
  bool disableErrorToast = false,
  bool disableExceptionToast = false,
  bool disableCancelledToast = true,

  bool enableHaptics = true,
  Map<LikeState, String>? messageOverrides,
}) async {
  if (onInit != null) await onInit(response.state);

  T? castData(dynamic data) {
    if (data == null) return null;
    if (data is T) return data;
    try {
      return data as T;
    } catch (_) {
      return null;
    }
  }

  String resolveMessage(String defaultMsg, LikeState state) {
    return messageOverrides != null && messageOverrides.containsKey(state)
        ? messageOverrides[state]!
        : defaultMsg;
  }

  switch (response.state) {
    case LikeState.idle:
      break;

    case LikeState.loading:
      if (!disableLoadingToast) {
        if (context != null && !context.mounted) return;
        LikeToastManager.showLoadingToast(
          context: context,
          title: 'Loading',
          message: resolveMessage(response.message, LikeState.loading),
        );
      }
      break;

    case LikeState.success:
    case LikeState.refreshing:
    case LikeState.staleWhileRevalidate:
      if (onSuccess != null) {
        final data = castData(response.data);
        if (data != null) await onSuccess(data);
      }

      if (response.state == LikeState.success && !disableSuccessToast) {
        if (enableHaptics) HapticFeedback.lightImpact();
        if (context != null && !context.mounted) return;
        LikeToastManager.showToast(
          context: context,
          message: resolveMessage(response.resolvedMessage, LikeState.success),
          type: ToastificationType.success,
        );
      }
      break;

    case LikeState.error:
      final error =
          response.error ??
          LikeError(
            message: response.message,
            type: response.errorType ?? LikeApiErrorType.unknown,
            code: response.code,
          );

      await onError?.call(error);

      if (!disableErrorToast) {
        if (enableHaptics) HapticFeedback.mediumImpact();
        if (error.type != LikeApiErrorType.cancelled ||
            !disableCancelledToast) {
          if (context != null && !context.mounted) return;
          LikeToastManager.showToast(
            context: context,
            message: resolveMessage(response.message, LikeState.error),
            type: ToastificationType.warning,
          );
        }
      }
      break;

    case LikeState.exception:
      await onException?.call(response.message);

      if (!disableExceptionToast) {
        if (enableHaptics) HapticFeedback.heavyImpact();
        if (context != null && !context.mounted) return;
        LikeToastManager.showToast(
          context: context,
          message: resolveMessage(response.message, LikeState.exception),
          type: ToastificationType.error,
        );
      }
      break;
  }
}