forked from eatvio/eatvio_app
feat(push notifications): test implemintation of push notifcations
This commit is contained in:
@@ -11,10 +11,13 @@ import 'package:eatvio_app/core/websockets/ws_manager.dart';
|
||||
import 'package:eatvio_app/features/chat/models/chat_message_model.dart';
|
||||
import 'package:eatvio_app/features/chat/presentation/bloc/chat_room_cubit/chat_room_state.dart';
|
||||
import 'package:eatvio_app/features/chat/repositories/chat_repository.dart';
|
||||
import 'package:eatvio_app/features/my_finance/models/card_model.dart';
|
||||
import 'package:eatvio_app/features/my_finance/repositories/cards_repository.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
class ChatRoomCubit extends Cubit<ChatRoomState> {
|
||||
final ChatRepository chatRepository;
|
||||
final CardsRepository cardsRepository;
|
||||
final WsManager wsManager;
|
||||
|
||||
StreamSubscription<ChannelReadEvent>? typingSubscription;
|
||||
@@ -23,34 +26,22 @@ class ChatRoomCubit extends Cubit<ChatRoomState> {
|
||||
late final int lastPage;
|
||||
bool isLoading = false;
|
||||
|
||||
ChatRoomCubit({
|
||||
required this.chatRepository,
|
||||
required this.wsManager,
|
||||
}) : super(ChatRoomState.loading());
|
||||
ChatRoomCubit({required this.chatRepository, required this.wsManager, required this.cardsRepository}) : super(ChatRoomState.loading());
|
||||
|
||||
loadNextPage(String chatId) async {
|
||||
if (currentPage < lastPage && !isLoading) {
|
||||
isLoading = true;
|
||||
|
||||
emit(
|
||||
ChatRoomState.loaded(
|
||||
chatRoom: (state as ChatRoomStateLoaded).chatRoom,
|
||||
chatMessages: (state as ChatRoomStateLoaded).chatMessages,
|
||||
isTyping: (state as ChatRoomStateLoaded).isTyping,
|
||||
isLoading: isLoading,
|
||||
),
|
||||
);
|
||||
emit((state as ChatRoomStateLoaded).copyWith(isLoading: isLoading));
|
||||
|
||||
try {
|
||||
final chatMessages = await chatRepository.getChatMessages(chatId, currentPage + 1);
|
||||
|
||||
isLoading = false;
|
||||
emit(
|
||||
ChatRoomState.loaded(
|
||||
chatRoom: (state as ChatRoomStateLoaded).chatRoom,
|
||||
chatMessages: [...(state as ChatRoomStateLoaded).chatMessages, ...chatMessages.messagesList],
|
||||
isTyping: (state as ChatRoomStateLoaded).isTyping,
|
||||
(state as ChatRoomStateLoaded).copyWith(
|
||||
isLoading: isLoading,
|
||||
chatMessages: [...(state as ChatRoomStateLoaded).chatMessages, ...chatMessages.messagesList],
|
||||
),
|
||||
);
|
||||
|
||||
@@ -61,6 +52,24 @@ class ChatRoomCubit extends Cubit<ChatRoomState> {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> checkCards() async {
|
||||
try {
|
||||
final cards = await cardsRepository.getCards();
|
||||
setCard(cards: cards);
|
||||
} catch (e) {
|
||||
emit(ChatRoomState.error(errorMessage: 'Ошибка получеия данных'));
|
||||
}
|
||||
}
|
||||
|
||||
void setCard({required List<CardModel> cards}) {
|
||||
final currentState = state;
|
||||
if (currentState is ChatRoomStateLoaded) {
|
||||
if (cards.where((card) => card.isDefault).isNotEmpty) {
|
||||
emit(currentState.copyWith(hasCard: true));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
initChat(String chatId) async {
|
||||
try {
|
||||
final chatRoom = await chatRepository.getChatRoom(chatId);
|
||||
@@ -70,14 +79,9 @@ class ChatRoomCubit extends Cubit<ChatRoomState> {
|
||||
|
||||
await _subscribeToChatChannel(chatId: chatId);
|
||||
|
||||
emit(
|
||||
ChatRoomState.loaded(
|
||||
chatRoom: chatRoom,
|
||||
chatMessages: chatMessages.messagesList,
|
||||
isTyping: false,
|
||||
isLoading: isLoading,
|
||||
),
|
||||
);
|
||||
emit(ChatRoomState.loaded(chatRoom: chatRoom, chatMessages: chatMessages.messagesList, isTyping: false, isLoading: isLoading, hasCard: false));
|
||||
final cards = await cardsRepository.getCards();
|
||||
setCard(cards: cards);
|
||||
} catch (e) {
|
||||
typingSubscription?.cancel();
|
||||
messagesSubscription?.cancel();
|
||||
@@ -90,17 +94,13 @@ class ChatRoomCubit extends Cubit<ChatRoomState> {
|
||||
try {
|
||||
await chatRepository.cancelChosenPlan(id: planId);
|
||||
final chatRoom = await chatRepository.getChatRoom(chatId);
|
||||
emit(
|
||||
(state as ChatRoomStateLoaded).copyWith(chatRoom: chatRoom),
|
||||
);
|
||||
emit((state as ChatRoomStateLoaded).copyWith(chatRoom: chatRoom));
|
||||
} catch (e) {
|
||||
emit(ChatRoomState.error(errorMessage: 'Ошибка получеия данных'));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _subscribeToChatChannel({
|
||||
required String chatId,
|
||||
}) async {
|
||||
Future<void> _subscribeToChatChannel({required String chatId}) async {
|
||||
final client = wsManager.client;
|
||||
if (client == null) return;
|
||||
await wsManager.initPrivateDirectChatChannel(chatId);
|
||||
@@ -108,56 +108,20 @@ class ChatRoomCubit extends Cubit<ChatRoomState> {
|
||||
messagesSubscription = wsManager.directChatStream?.listen((event) {
|
||||
AudioPlayer().play((AssetSource('audio/recive_message.wav')));
|
||||
final message = ChatMessageModel.fromJson(jsonDecode(event.data)['message']);
|
||||
List<ChatMessageModel> messagesList = [
|
||||
message,
|
||||
...(state as ChatRoomStateLoaded).chatMessages,
|
||||
];
|
||||
List<ChatMessageModel> messagesList = [message, ...(state as ChatRoomStateLoaded).chatMessages];
|
||||
chatRepository.emitNewMessageToChats(message);
|
||||
emit(
|
||||
ChatRoomState.loaded(
|
||||
chatRoom: (state as ChatRoomStateLoaded).chatRoom,
|
||||
chatMessages: messagesList,
|
||||
isTyping: (state as ChatRoomStateLoaded).isTyping,
|
||||
isLoading: isLoading,
|
||||
),
|
||||
);
|
||||
emit((state as ChatRoomStateLoaded).copyWith(chatMessages: messagesList, isLoading: isLoading));
|
||||
});
|
||||
|
||||
typingSubscription = wsManager.clientTypingStream?.listen((event) {
|
||||
emit(
|
||||
ChatRoomState.loaded(
|
||||
chatRoom: (state as ChatRoomStateLoaded).chatRoom,
|
||||
chatMessages: (state as ChatRoomStateLoaded).chatMessages,
|
||||
isTyping: true,
|
||||
isLoading: isLoading,
|
||||
),
|
||||
);
|
||||
EasyDebounce.debounce(
|
||||
'client-typing',
|
||||
Duration(seconds: 3),
|
||||
() => emit(
|
||||
ChatRoomState.loaded(
|
||||
chatRoom: (state as ChatRoomStateLoaded).chatRoom,
|
||||
chatMessages: (state as ChatRoomStateLoaded).chatMessages,
|
||||
isTyping: false,
|
||||
isLoading: isLoading,
|
||||
),
|
||||
),
|
||||
);
|
||||
emit((state as ChatRoomStateLoaded).copyWith(isTyping: true, isLoading: isLoading));
|
||||
EasyDebounce.debounce('client-typing', Duration(seconds: 3), () => emit((state as ChatRoomStateLoaded).copyWith(isTyping: false, isLoading: isLoading)));
|
||||
});
|
||||
}
|
||||
|
||||
Future<bool> sendMessage({
|
||||
required String chatId,
|
||||
String? data,
|
||||
List<File>? files,
|
||||
}) async {
|
||||
Future<bool> sendMessage({required String chatId, String? data, List<File>? files}) async {
|
||||
try {
|
||||
await chatRepository.sendMessage(
|
||||
chatId: chatId,
|
||||
data: data,
|
||||
files: files != null ? await ImageCompressUtility.compressFilesList(files) : null,
|
||||
);
|
||||
await chatRepository.sendMessage(chatId: chatId, data: data, files: files != null ? await ImageCompressUtility.compressFilesList(files) : null);
|
||||
AudioPlayer().play((AssetSource('audio/send_message.wav')));
|
||||
return true;
|
||||
} catch (e, st) {
|
||||
@@ -176,9 +140,7 @@ class ChatRoomCubit extends Cubit<ChatRoomState> {
|
||||
currentMessages[elem.index] = currentMessages[elem.index].copyWith(isRead: true);
|
||||
});
|
||||
if (!isClosed) {
|
||||
emit(
|
||||
(state as ChatRoomStateLoaded).copyWith(chatMessages: currentMessages),
|
||||
);
|
||||
emit((state as ChatRoomStateLoaded).copyWith(chatMessages: currentMessages));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ abstract class ChatRoomState with _$ChatRoomState {
|
||||
const factory ChatRoomState.loaded({
|
||||
required ChatRoomModel chatRoom,
|
||||
required List<ChatMessageModel> chatMessages,
|
||||
required bool hasCard,
|
||||
required bool isTyping,
|
||||
required bool isLoading,
|
||||
}) = ChatRoomStateLoaded;
|
||||
|
||||
@@ -24,6 +24,7 @@ mixin _$ChatRoomState {
|
||||
required TResult Function(
|
||||
ChatRoomModel chatRoom,
|
||||
List<ChatMessageModel> chatMessages,
|
||||
bool hasCard,
|
||||
bool isTyping,
|
||||
bool isLoading,
|
||||
)
|
||||
@@ -37,6 +38,7 @@ mixin _$ChatRoomState {
|
||||
TResult? Function(
|
||||
ChatRoomModel chatRoom,
|
||||
List<ChatMessageModel> chatMessages,
|
||||
bool hasCard,
|
||||
bool isTyping,
|
||||
bool isLoading,
|
||||
)?
|
||||
@@ -50,6 +52,7 @@ mixin _$ChatRoomState {
|
||||
TResult Function(
|
||||
ChatRoomModel chatRoom,
|
||||
List<ChatMessageModel> chatMessages,
|
||||
bool hasCard,
|
||||
bool isTyping,
|
||||
bool isLoading,
|
||||
)?
|
||||
@@ -151,6 +154,7 @@ class _$ChatRoomStateImpl implements _ChatRoomState {
|
||||
required TResult Function(
|
||||
ChatRoomModel chatRoom,
|
||||
List<ChatMessageModel> chatMessages,
|
||||
bool hasCard,
|
||||
bool isTyping,
|
||||
bool isLoading,
|
||||
)
|
||||
@@ -168,6 +172,7 @@ class _$ChatRoomStateImpl implements _ChatRoomState {
|
||||
TResult? Function(
|
||||
ChatRoomModel chatRoom,
|
||||
List<ChatMessageModel> chatMessages,
|
||||
bool hasCard,
|
||||
bool isTyping,
|
||||
bool isLoading,
|
||||
)?
|
||||
@@ -185,6 +190,7 @@ class _$ChatRoomStateImpl implements _ChatRoomState {
|
||||
TResult Function(
|
||||
ChatRoomModel chatRoom,
|
||||
List<ChatMessageModel> chatMessages,
|
||||
bool hasCard,
|
||||
bool isTyping,
|
||||
bool isLoading,
|
||||
)?
|
||||
@@ -289,6 +295,7 @@ class _$ChatRoomStateLoadingImpl implements ChatRoomStateLoading {
|
||||
required TResult Function(
|
||||
ChatRoomModel chatRoom,
|
||||
List<ChatMessageModel> chatMessages,
|
||||
bool hasCard,
|
||||
bool isTyping,
|
||||
bool isLoading,
|
||||
)
|
||||
@@ -306,6 +313,7 @@ class _$ChatRoomStateLoadingImpl implements ChatRoomStateLoading {
|
||||
TResult? Function(
|
||||
ChatRoomModel chatRoom,
|
||||
List<ChatMessageModel> chatMessages,
|
||||
bool hasCard,
|
||||
bool isTyping,
|
||||
bool isLoading,
|
||||
)?
|
||||
@@ -323,6 +331,7 @@ class _$ChatRoomStateLoadingImpl implements ChatRoomStateLoading {
|
||||
TResult Function(
|
||||
ChatRoomModel chatRoom,
|
||||
List<ChatMessageModel> chatMessages,
|
||||
bool hasCard,
|
||||
bool isTyping,
|
||||
bool isLoading,
|
||||
)?
|
||||
@@ -388,6 +397,7 @@ abstract class _$$ChatRoomStateLoadedImplCopyWith<$Res> {
|
||||
$Res call({
|
||||
ChatRoomModel chatRoom,
|
||||
List<ChatMessageModel> chatMessages,
|
||||
bool hasCard,
|
||||
bool isTyping,
|
||||
bool isLoading,
|
||||
});
|
||||
@@ -411,6 +421,7 @@ class __$$ChatRoomStateLoadedImplCopyWithImpl<$Res>
|
||||
$Res call({
|
||||
Object? chatRoom = null,
|
||||
Object? chatMessages = null,
|
||||
Object? hasCard = null,
|
||||
Object? isTyping = null,
|
||||
Object? isLoading = null,
|
||||
}) {
|
||||
@@ -426,6 +437,11 @@ class __$$ChatRoomStateLoadedImplCopyWithImpl<$Res>
|
||||
? _value._chatMessages
|
||||
: chatMessages // ignore: cast_nullable_to_non_nullable
|
||||
as List<ChatMessageModel>,
|
||||
hasCard:
|
||||
null == hasCard
|
||||
? _value.hasCard
|
||||
: hasCard // ignore: cast_nullable_to_non_nullable
|
||||
as bool,
|
||||
isTyping:
|
||||
null == isTyping
|
||||
? _value.isTyping
|
||||
@@ -457,6 +473,7 @@ class _$ChatRoomStateLoadedImpl implements ChatRoomStateLoaded {
|
||||
const _$ChatRoomStateLoadedImpl({
|
||||
required this.chatRoom,
|
||||
required final List<ChatMessageModel> chatMessages,
|
||||
required this.hasCard,
|
||||
required this.isTyping,
|
||||
required this.isLoading,
|
||||
}) : _chatMessages = chatMessages;
|
||||
@@ -471,6 +488,8 @@ class _$ChatRoomStateLoadedImpl implements ChatRoomStateLoaded {
|
||||
return EqualUnmodifiableListView(_chatMessages);
|
||||
}
|
||||
|
||||
@override
|
||||
final bool hasCard;
|
||||
@override
|
||||
final bool isTyping;
|
||||
@override
|
||||
@@ -478,7 +497,7 @@ class _$ChatRoomStateLoadedImpl implements ChatRoomStateLoaded {
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'ChatRoomState.loaded(chatRoom: $chatRoom, chatMessages: $chatMessages, isTyping: $isTyping, isLoading: $isLoading)';
|
||||
return 'ChatRoomState.loaded(chatRoom: $chatRoom, chatMessages: $chatMessages, hasCard: $hasCard, isTyping: $isTyping, isLoading: $isLoading)';
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -492,6 +511,7 @@ class _$ChatRoomStateLoadedImpl implements ChatRoomStateLoaded {
|
||||
other._chatMessages,
|
||||
_chatMessages,
|
||||
) &&
|
||||
(identical(other.hasCard, hasCard) || other.hasCard == hasCard) &&
|
||||
(identical(other.isTyping, isTyping) ||
|
||||
other.isTyping == isTyping) &&
|
||||
(identical(other.isLoading, isLoading) ||
|
||||
@@ -503,6 +523,7 @@ class _$ChatRoomStateLoadedImpl implements ChatRoomStateLoaded {
|
||||
runtimeType,
|
||||
chatRoom,
|
||||
const DeepCollectionEquality().hash(_chatMessages),
|
||||
hasCard,
|
||||
isTyping,
|
||||
isLoading,
|
||||
);
|
||||
@@ -526,13 +547,14 @@ class _$ChatRoomStateLoadedImpl implements ChatRoomStateLoaded {
|
||||
required TResult Function(
|
||||
ChatRoomModel chatRoom,
|
||||
List<ChatMessageModel> chatMessages,
|
||||
bool hasCard,
|
||||
bool isTyping,
|
||||
bool isLoading,
|
||||
)
|
||||
loaded,
|
||||
required TResult Function(String errorMessage) error,
|
||||
}) {
|
||||
return loaded(chatRoom, chatMessages, isTyping, isLoading);
|
||||
return loaded(chatRoom, chatMessages, hasCard, isTyping, isLoading);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -543,13 +565,14 @@ class _$ChatRoomStateLoadedImpl implements ChatRoomStateLoaded {
|
||||
TResult? Function(
|
||||
ChatRoomModel chatRoom,
|
||||
List<ChatMessageModel> chatMessages,
|
||||
bool hasCard,
|
||||
bool isTyping,
|
||||
bool isLoading,
|
||||
)?
|
||||
loaded,
|
||||
TResult? Function(String errorMessage)? error,
|
||||
}) {
|
||||
return loaded?.call(chatRoom, chatMessages, isTyping, isLoading);
|
||||
return loaded?.call(chatRoom, chatMessages, hasCard, isTyping, isLoading);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -560,6 +583,7 @@ class _$ChatRoomStateLoadedImpl implements ChatRoomStateLoaded {
|
||||
TResult Function(
|
||||
ChatRoomModel chatRoom,
|
||||
List<ChatMessageModel> chatMessages,
|
||||
bool hasCard,
|
||||
bool isTyping,
|
||||
bool isLoading,
|
||||
)?
|
||||
@@ -568,7 +592,7 @@ class _$ChatRoomStateLoadedImpl implements ChatRoomStateLoaded {
|
||||
required TResult orElse(),
|
||||
}) {
|
||||
if (loaded != null) {
|
||||
return loaded(chatRoom, chatMessages, isTyping, isLoading);
|
||||
return loaded(chatRoom, chatMessages, hasCard, isTyping, isLoading);
|
||||
}
|
||||
return orElse();
|
||||
}
|
||||
@@ -615,12 +639,14 @@ abstract class ChatRoomStateLoaded implements ChatRoomState {
|
||||
const factory ChatRoomStateLoaded({
|
||||
required final ChatRoomModel chatRoom,
|
||||
required final List<ChatMessageModel> chatMessages,
|
||||
required final bool hasCard,
|
||||
required final bool isTyping,
|
||||
required final bool isLoading,
|
||||
}) = _$ChatRoomStateLoadedImpl;
|
||||
|
||||
ChatRoomModel get chatRoom;
|
||||
List<ChatMessageModel> get chatMessages;
|
||||
bool get hasCard;
|
||||
bool get isTyping;
|
||||
bool get isLoading;
|
||||
|
||||
@@ -711,6 +737,7 @@ class _$ChatRoomStateErrorImpl implements ChatRoomStateError {
|
||||
required TResult Function(
|
||||
ChatRoomModel chatRoom,
|
||||
List<ChatMessageModel> chatMessages,
|
||||
bool hasCard,
|
||||
bool isTyping,
|
||||
bool isLoading,
|
||||
)
|
||||
@@ -728,6 +755,7 @@ class _$ChatRoomStateErrorImpl implements ChatRoomStateError {
|
||||
TResult? Function(
|
||||
ChatRoomModel chatRoom,
|
||||
List<ChatMessageModel> chatMessages,
|
||||
bool hasCard,
|
||||
bool isTyping,
|
||||
bool isLoading,
|
||||
)?
|
||||
@@ -745,6 +773,7 @@ class _$ChatRoomStateErrorImpl implements ChatRoomStateError {
|
||||
TResult Function(
|
||||
ChatRoomModel chatRoom,
|
||||
List<ChatMessageModel> chatMessages,
|
||||
bool hasCard,
|
||||
bool isTyping,
|
||||
bool isLoading,
|
||||
)?
|
||||
|
||||
Reference in New Issue
Block a user