feat: 修复了插件需要外部Provider的问题;加上了一个简易的车控状态返回
This commit is contained in:
24
lib/bloc/ai_chat_cubit.dart
Normal file
24
lib/bloc/ai_chat_cubit.dart
Normal file
@@ -0,0 +1,24 @@
|
||||
import '../enums/vehicle_command_type.dart';
|
||||
import '../services/command_service.dart';
|
||||
import 'easy_bloc.dart';
|
||||
import 'command_state.dart';
|
||||
|
||||
class AIChatCommandCubit extends EasyCubit<AIChatCommandState> {
|
||||
AIChatCommandCubit() : super(const AIChatCommandState());
|
||||
|
||||
// 重置状态
|
||||
void reset() {
|
||||
emit(const AIChatCommandState());
|
||||
}
|
||||
|
||||
// 生成唯一命令ID
|
||||
String _generateCommandId() {
|
||||
return '${DateTime.now().millisecondsSinceEpoch}_${state.commandType?.name ?? 'unknown'}';
|
||||
}
|
||||
|
||||
// 检查当前是否有命令在执行
|
||||
bool get isExecuting => state.status == AIChatCommandStatus.executing;
|
||||
|
||||
// 获取当前命令ID
|
||||
String? get currentCommandId => state.commandId;
|
||||
}
|
||||
68
lib/bloc/command_state.dart
Normal file
68
lib/bloc/command_state.dart
Normal file
@@ -0,0 +1,68 @@
|
||||
import '../enums/vehicle_command_type.dart';
|
||||
|
||||
// AI Chat Command States
|
||||
enum AIChatCommandStatus {
|
||||
idle,
|
||||
executing,
|
||||
success,
|
||||
failure,
|
||||
cancelled,
|
||||
}
|
||||
|
||||
class AIChatCommandState {
|
||||
final String? commandId;
|
||||
final VehicleCommandType? commandType;
|
||||
final Map<String, dynamic>? params;
|
||||
final AIChatCommandStatus status;
|
||||
final String? errorMessage;
|
||||
final Map<String, dynamic>? result;
|
||||
final DateTime? timestamp;
|
||||
|
||||
const AIChatCommandState({
|
||||
this.commandId,
|
||||
this.commandType,
|
||||
this.params,
|
||||
this.status = AIChatCommandStatus.idle,
|
||||
this.errorMessage,
|
||||
this.result,
|
||||
this.timestamp,
|
||||
});
|
||||
|
||||
AIChatCommandState copyWith({
|
||||
String? commandId,
|
||||
VehicleCommandType? commandType,
|
||||
Map<String, dynamic>? params,
|
||||
AIChatCommandStatus? status,
|
||||
String? errorMessage,
|
||||
Map<String, dynamic>? result,
|
||||
DateTime? timestamp,
|
||||
}) {
|
||||
return AIChatCommandState(
|
||||
commandId: commandId ?? this.commandId,
|
||||
commandType: commandType ?? this.commandType,
|
||||
params: params ?? this.params,
|
||||
status: status ?? this.status,
|
||||
errorMessage: errorMessage ?? this.errorMessage,
|
||||
result: result ?? this.result,
|
||||
timestamp: timestamp ?? this.timestamp,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
if (identical(this, other)) return true;
|
||||
return other is AIChatCommandState &&
|
||||
other.commandId == commandId &&
|
||||
other.commandType == commandType &&
|
||||
other.status == status &&
|
||||
other.errorMessage == errorMessage;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode {
|
||||
return commandId.hashCode ^
|
||||
commandType.hashCode ^
|
||||
status.hashCode ^
|
||||
errorMessage.hashCode;
|
||||
}
|
||||
}
|
||||
78
lib/bloc/easy_bloc.dart
Normal file
78
lib/bloc/easy_bloc.dart
Normal file
@@ -0,0 +1,78 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
// 简易的Bloc, 简易的状态管理
|
||||
|
||||
abstract class EasyBlocBase<State> {
|
||||
|
||||
EasyBlocBase(this.state): assert(state != null);
|
||||
|
||||
final StreamController<State> _stateController = StreamController<State>.broadcast();
|
||||
|
||||
Stream<State> get stream => _stateController.stream;
|
||||
State state;
|
||||
|
||||
bool _emitted = false;
|
||||
|
||||
// 添加监听器方法
|
||||
StreamSubscription<State> listen(
|
||||
void Function(State state) onData, {
|
||||
Function? onError,
|
||||
void Function()? onDone,
|
||||
bool? cancelOnError,
|
||||
}) {
|
||||
return stream.listen(
|
||||
onData,
|
||||
onError: onError,
|
||||
onDone: onDone,
|
||||
cancelOnError: cancelOnError,
|
||||
);
|
||||
}
|
||||
|
||||
// 新状态
|
||||
void emit(State newState) {
|
||||
if (_stateController.isClosed) return;
|
||||
if (newState == state && _emitted) return;
|
||||
this.state = newState;
|
||||
_stateController.add(state);
|
||||
_emitted = true;
|
||||
}
|
||||
|
||||
@mustCallSuper
|
||||
Future<void> close() async {
|
||||
await _stateController.close();
|
||||
}
|
||||
}
|
||||
|
||||
class EasyBloc<Event, State> extends EasyBlocBase<State> {
|
||||
EasyBloc(State initialState) : super(initialState) {
|
||||
_eventController.stream.listen(_mapEventToState);
|
||||
}
|
||||
|
||||
final StreamController<Event> _eventController = StreamController<Event>();
|
||||
|
||||
// 子类需要实现这个方法来处理事件
|
||||
@mustBeOverridden
|
||||
void _mapEventToState(Event event) {
|
||||
// 默认不做任何处理,如果不实现会抛出异常
|
||||
throw UnimplementedError('_mapEventToState must be implemented by subclasses');
|
||||
}
|
||||
|
||||
// 添加事件
|
||||
void add(Event event) {
|
||||
if (_eventController.isClosed) return;
|
||||
_eventController.add(event);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> close() async {
|
||||
await _eventController.close();
|
||||
await super.close();
|
||||
}
|
||||
}
|
||||
|
||||
class EasyCubit<State> extends EasyBlocBase<State> {
|
||||
EasyCubit(State initialState) : super(initialState);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user