Повторный вход в аккаунт, очистка кэша

This commit is contained in:
Халимов Рустам
2026-05-15 00:10:34 +03:00
parent 0b013def2e
commit 40589dbb75
21 changed files with 1732 additions and 309 deletions
@@ -9,6 +9,7 @@ import 'chat_state.dart';
class ChatBloc extends Bloc<ChatEvent, ChatState> {
final ChatRepository chatRepository;
StreamSubscription? _messageSubscription;
StreamSubscription? _typingSubscription;
ChatBloc({required this.chatRepository}) : super(const ChatState.initial()) {
on<ChatEventStarted>(_onStarted);
@@ -17,20 +18,29 @@ class ChatBloc extends Bloc<ChatEvent, ChatState> {
on<ChatEventMessagesRequested>(_onMessagesRequested);
on<ChatEventMessageSent>(_onMessageSent);
on<ChatEventFavoritesRequested>(_onFavoritesRequested);
on<ChatEventTypingUpdated>(_onTypingUpdated);
on<ChatEventSendTypingStatus>(_onSendTypingStatus);
on<ChatEventCacheCleared>(_onCacheCleared);
_messageSubscription = chatRepository.messageStream.listen((message) {
// Handle real-time messages
add(ChatEvent.messagesRequested(message.chatId));
});
_typingSubscription = chatRepository.typingStream.listen((data) {
add(ChatEvent.typingUpdated(data['chatId'], data['userId'], data['isTyping']));
});
}
Future<void> _onStarted(ChatEventStarted event, Emitter<ChatState> emit) async {
// ignore: avoid_print
print('[DEBUG] ChatBloc._onStarted for user: ${event.userId}');
// Start SignalR in background
chatRepository.initSignalR().catchError((e) {
// Log or handle SignalR init error
});
// Immediately trigger chats loading
add(const ChatEvent.chatsLoaded());
add(ChatEvent.chatsLoaded(currentUserId: event.userId));
add(const ChatEvent.favoritesRequested());
}
Future<void> _onChatsLoaded(ChatEventChatsLoaded event, Emitter<ChatState> emit) async {
@@ -83,9 +93,42 @@ class ChatBloc extends Bloc<ChatEvent, ChatState> {
);
}
Future<void> _onTypingUpdated(ChatEventTypingUpdated event, Emitter<ChatState> emit) async {
final Map<String, Set<String>> newTypingUsers = Map.from(state.typingUsers);
final Set<String> chatTypingUsers = Set.from(newTypingUsers[event.chatId] ?? {});
if (event.isTyping) {
chatTypingUsers.add(event.userId);
} else {
chatTypingUsers.remove(event.userId);
}
if (chatTypingUsers.isEmpty) {
newTypingUsers.remove(event.chatId);
} else {
newTypingUsers[event.chatId] = chatTypingUsers;
}
emit(state.copyWith(typingUsers: newTypingUsers));
}
Future<void> _onSendTypingStatus(ChatEventSendTypingStatus event, Emitter<ChatState> emit) async {
await chatRepository.sendTypingStatus(event.chatId, event.isTyping);
}
Future<void> _onCacheCleared(ChatEventCacheCleared event, Emitter<ChatState> emit) async {
// ignore: avoid_print
print('[DEBUG] ChatBloc._onCacheCleared START');
await chatRepository.clearCache();
// ignore: avoid_print
print('[DEBUG] ChatBloc._onCacheCleared END');
emit(const ChatState.initial());
}
@override
Future<void> close() {
_messageSubscription?.cancel();
_typingSubscription?.cancel();
chatRepository.disposeSignalR();
return super.close();
}