Skip to content

Commit

Permalink
fix chat and quiz state logic
Browse files Browse the repository at this point in the history
  • Loading branch information
GravityDarkLab committed Jan 29, 2024
1 parent 48ca10a commit 1929f70
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
24 changes: 23 additions & 1 deletion lib/view_models/chat_view_model.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -16,7 +17,7 @@ class ChatViewModel extends StateNotifier<ChatState> {
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,
Expand All @@ -26,6 +27,27 @@ class ChatViewModel extends StateNotifier<ChatState> {
}
}

Future<void> 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<ChatMessage>.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<void> postChatMessage(int streamId, String message) async {
try {
fetchChatMessages(streamId);
Expand Down
6 changes: 3 additions & 3 deletions lib/views/video_view/video_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ class VideoPlayerPageState extends ConsumerState<VideoPlayerPage> {
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;
Expand Down Expand Up @@ -208,7 +208,7 @@ class VideoPlayerPageState extends ConsumerState<VideoPlayerPage> {
}

bool _shouldMarkAsWatched(double progress) {
const watchedThreshold = 0.9; // 80%
const watchedThreshold = 0.9;
return progress >= watchedThreshold;
}

Expand Down

0 comments on commit 1929f70

Please sign in to comment.