diff --git a/lib/src/providers/environment_provider.dart b/lib/src/providers/environment_provider.dart index 022bc65..6a5fc35 100644 --- a/lib/src/providers/environment_provider.dart +++ b/lib/src/providers/environment_provider.dart @@ -11,22 +11,18 @@ class ArcaneEnvironment extends Cubit { ArcaneEnvironment() : super(Environment.normal); /// Enables debug mode by setting the environment to `Environment.debug`. - /// - /// If provided, [onDebugModeSet] is a callback that will be awaited before switching - /// to debug mode. This is useful for performing any setup required when enabling - /// demo mode. - /// - /// Example: - /// ```dart - /// await environmentCubit.enableDebugMode(() async { - /// // Perform some setup when enabling demo mode. - /// }); - /// ``` - Future enableDebugMode(Future Function()? onDebugModeSet) async { - if (onDebugModeSet != null) await onDebugModeSet(); + void enableDebugMode() { + if (state == Environment.debug) return; emit(Environment.debug); } + + /// Disables debug mode by setting the environment to `Environment.normal`. + void disableDebugMode() { + if (state == Environment.normal) return; + + emit(Environment.normal); + } } /// A widget that provides `ArcaneEnvironment` to the widget tree using `BlocProvider`. diff --git a/lib/src/services/authentication/authentication_service.dart b/lib/src/services/authentication/authentication_service.dart index b06dc7d..55f769b 100644 --- a/lib/src/services/authentication/authentication_service.dart +++ b/lib/src/services/authentication/authentication_service.dart @@ -72,13 +72,55 @@ class ArcaneAuthenticationService extends ArcaneService { try { environment = context.read(); - await environment.enableDebugMode(onDebugModeSet); + final Environment previousEnvironment = environment.state; + + if (previousEnvironment == Environment.debug) return; + + environment.enableDebugMode(); + + final Environment currentEnvironment = environment.state; + + if (previousEnvironment == currentEnvironment) { + throw Exception("Unable to switch to debug mode."); + } + + _setStatus(AuthenticationStatus.debug); + if (onDebugModeSet != null) await onDebugModeSet(); } catch (_) { throw Exception("No ArcaneEnvironment found in BuildContext"); } + } - _setStatus(AuthenticationStatus.debug); - if (onDebugModeSet != null) await onDebugModeSet(); + /// Sets `status` to `AuthenticationStatus.normal`. If `onDebugModeUnset` has + /// been specified, the method will be triggered after the new status has been + /// set. + Future setNormal( + BuildContext context, { + Future Function()? onDebugModeUnset, + }) async { + if (_mocked) return; + + ArcaneEnvironment? environment; + + try { + environment = context.read(); + final Environment previousEnvironment = environment.state; + + if (previousEnvironment == Environment.normal) return; + + environment.disableDebugMode(); + + final Environment currentEnvironment = environment.state; + + if (previousEnvironment == currentEnvironment) { + throw Exception("Unable to switch to normal mode."); + } + + _setStatus(AuthenticationStatus.debug); + if (onDebugModeUnset != null) await onDebugModeUnset(); + } catch (_) { + throw Exception("No ArcaneEnvironment found in BuildContext"); + } } /// Sets `status` to `AuthenticationStatus.authenticated`.