You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
fluffychat/lib/pangea/choreographer/controllers/error_service.dart

95 lines
1.9 KiB
Dart

import 'package:flutter/material.dart';
import 'package:fluffychat/pangea/choreographer/controllers/choreographer.dart';
import '../../common/utils/error_handler.dart';
enum ChoreoErrorType {
unknown,
classDisabled,
userDisabled,
}
class ChoreoError {
final ChoreoErrorType type;
final Object? raw;
ChoreoError({required this.type, this.raw});
String title(BuildContext context) {
switch (type) {
case ChoreoErrorType.classDisabled:
return "Class Disabled";
case ChoreoErrorType.userDisabled:
return "User Disabled";
default:
return ErrorCopy(context, raw).title;
}
}
String description(BuildContext context) {
switch (type) {
case ChoreoErrorType.classDisabled:
return "Class Disabled";
case ChoreoErrorType.userDisabled:
return "User Disabled";
default:
return ErrorCopy(context, raw).body;
}
}
IconData get icon {
switch (type) {
case ChoreoErrorType.classDisabled:
return Icons.history_edu_outlined;
case ChoreoErrorType.userDisabled:
return Icons.history_edu_outlined;
default:
return Icons.error_outline;
}
}
}
class ErrorService {
ChoreoError? _error;
int coolDownSeconds = 0;
final Choreographer controller;
ErrorService(this.controller);
bool get isError => _error != null;
ChoreoError? get error => _error;
Duration get defaultCooldown {
coolDownSeconds += 3;
return Duration(seconds: coolDownSeconds);
}
setError(ChoreoError? error, {Duration? duration}) {
_error = error;
Future.delayed(duration ?? defaultCooldown, () {
clear();
_setState();
});
_setState();
}
setErrorAndLock(ChoreoError? error) {
_error = error;
_setState();
}
resetError() {
clear();
_setState();
}
void _setState() {
controller.setState();
}
void clear() {
_error = null;
}
}