From 1929f7041964fa69f382e495171050f07f1105f6 Mon Sep 17 00:00:00 2001 From: Achraf Labidi Date: Mon, 29 Jan 2024 23:38:23 +0100 Subject: [PATCH] fix chat and quiz state logic --- lib/view_models/chat_view_model.dart | 24 +++++++++++++++++++++++- lib/views/video_view/video_player.dart | 6 +++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/view_models/chat_view_model.dart b/lib/view_models/chat_view_model.dart index cf8ce7c..4ff37e7 100644 --- a/lib/view_models/chat_view_model.dart +++ b/lib/view_models/chat_view_model.dart @@ -1,4 +1,5 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:gocast_mobile/base/networking/api/gocast/api_v2.pb.dart'; import 'package:gocast_mobile/base/networking/api/handler/chat_handler.dart'; import 'package:gocast_mobile/base/networking/api/handler/grpc_handler.dart'; @@ -16,7 +17,7 @@ class ChatViewModel extends StateNotifier { try { final messages = await ChatHandlers(_grpcHandler).getChatMessages(streamId); - state = state.copyWith(messages: messages, isLoading: false); + state = state.copyWith(messages: messages, isLoading: false, accessDenied: false); } catch (e) { state = state.copyWith( error: e as AppError, @@ -26,6 +27,27 @@ class ChatViewModel extends StateNotifier { } } + Future updateMessages(int streamId) async { + state = state.copyWith(isLoading: true); + state = state.clearError(); + if(state.messages == null) { + fetchChatMessages(streamId); + }else { + try { + final messages = await ChatHandlers(_grpcHandler).getChatMessages(streamId); + final combinedMessages = List.from(state.messages ?? []) + ..addAll(messages.where((newMessage) => !state.messages!.contains(newMessage))); + state = state.copyWith(messages: combinedMessages, isLoading: false, accessDenied: false); + } catch (e) { + state = state.copyWith( + error: e as AppError, + isLoading: false, + accessDenied: true, + ); + } + } + } + Future postChatMessage(int streamId, String message) async { try { fetchChatMessages(streamId); diff --git a/lib/views/video_view/video_player.dart b/lib/views/video_view/video_player.dart index f94f77f..6bcccb0 100644 --- a/lib/views/video_view/video_player.dart +++ b/lib/views/video_view/video_player.dart @@ -74,12 +74,12 @@ class VideoPlayerPageState extends ConsumerState { await ref .read(courseViewModelProvider.notifier) .getCourseWithID(widget.stream.courseID); + await ref.read(chatViewModelProvider.notifier).fetchChatMessages(widget.stream.id); Course? course = ref .read(courseViewModelProvider) .course; if (course != null) { - if ((course.chatEnabled || course.vodChatEnabled) && - widget.stream.chatEnabled) { + if (course.chatEnabled && course.vodChatEnabled && widget.stream.chatEnabled) { setState(() { _isChatActive = true; _isPollActive = true; @@ -208,7 +208,7 @@ class VideoPlayerPageState extends ConsumerState { } bool _shouldMarkAsWatched(double progress) { - const watchedThreshold = 0.9; // 80% + const watchedThreshold = 0.9; return progress >= watchedThreshold; }