mirror of
https://github.com/hanskokx/arcane_framework.git
synced 2026-05-14 02:19:08 +02:00
Added the ability to switch back to normal mode in the environment provider. Adjusted logic to prevent switching to a mode we're already in.
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
@@ -11,22 +11,18 @@ class ArcaneEnvironment extends Cubit<Environment> {
|
||||
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<void> enableDebugMode(Future<void> 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`.
|
||||
|
||||
@@ -72,13 +72,55 @@ class ArcaneAuthenticationService extends ArcaneService {
|
||||
|
||||
try {
|
||||
environment = context.read<ArcaneEnvironment>();
|
||||
await environment.enableDebugMode(onDebugModeSet);
|
||||
} catch (_) {
|
||||
throw Exception("No ArcaneEnvironment found in BuildContext");
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets `status` to `AuthenticationStatus.normal`. If `onDebugModeUnset` has
|
||||
/// been specified, the method will be triggered after the new status has been
|
||||
/// set.
|
||||
Future<void> setNormal(
|
||||
BuildContext context, {
|
||||
Future<void> Function()? onDebugModeUnset,
|
||||
}) async {
|
||||
if (_mocked) return;
|
||||
|
||||
ArcaneEnvironment? environment;
|
||||
|
||||
try {
|
||||
environment = context.read<ArcaneEnvironment>();
|
||||
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`.
|
||||
|
||||
Reference in New Issue
Block a user