Skip to content

Commit

Permalink
FVS-84: fix duplicate participant, dateTime parsing, reset StreamVide…
Browse files Browse the repository at this point in the history
…o on uregister (#491)
  • Loading branch information
kanat committed Sep 26, 2023
1 parent 9fec1b8 commit 6d543d0
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 10 deletions.
9 changes: 5 additions & 4 deletions dogfooding/lib/di/injector.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// 📦 Package imports:
import 'package:flutter/foundation.dart';
// 🌎 Project imports:
import 'package:flutter_dogfooding/core/repos/app_preferences.dart';
import 'package:flutter_dogfooding/core/repos/user_chat_repository.dart';
import 'package:get_it/get_it.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:share_plus/share_plus.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:stream_chat_flutter/stream_chat_flutter.dart' hide User;
import 'package:stream_video_flutter/stream_video_flutter.dart';

// 🌎 Project imports:
import 'package:flutter_dogfooding/core/repos/app_preferences.dart';
import 'package:flutter_dogfooding/core/repos/user_chat_repository.dart';
import 'package:stream_video_push_notification/stream_video_push_notification.dart';

import '../app/user_auth_controller.dart';
import '../core/repos/token_service.dart';
import '../core/repos/user_auth_repository.dart';
Expand Down Expand Up @@ -69,6 +69,7 @@ class AppInjector {
static StreamVideo registerStreamVideo(User user) {
_setupLogger();
return locator.registerSingleton(
dispose: (_) => StreamVideo.reset(),
_initStreamVideo(
user,
tokenLoader: switch (user.type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,17 @@ DateTime? mapDateTime(dynamic map, String key, [String? pattern]) {
}
return null;
}

extension on Map<String, String> {
Map<String, DateTime> convertValueToDateTime() {
final Map<String, DateTime> output = {};
for (var key in keys) {
final value = this[key];
if (value == null) continue;
final dateTime = DateTime.tryParse(value);
if (dateTime == null) continue;
output[key] = dateTime;
}
return output;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,17 @@ class CallSessionResponse {
});
return true;
}());

return CallSessionResponse(
acceptedBy: mapCastOfType<String, DateTime>(json, r'accepted_by')!,
acceptedBy: mapCastOfType<String, String>(json, r'accepted_by')!
.convertValueToDateTime(),
endedAt: mapDateTime(json, r'ended_at', ''),
id: mapValueOfType<String>(json, r'id')!,
liveEndedAt: mapDateTime(json, r'live_ended_at', ''),
liveStartedAt: mapDateTime(json, r'live_started_at', ''),
participants: CallParticipantResponse.listFromJson(json[r'participants']),
participantsCountByRole: mapCastOfType<String, int>(json, r'participants_count_by_role')!,
rejectedBy: mapCastOfType<String, DateTime>(json, r'rejected_by')!,
rejectedBy: mapCastOfType<String, String>(json, r'rejected_by')!
.convertValueToDateTime(),
startedAt: mapDateTime(json, r'started_at', ''),
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:state_notifier/state_notifier.dart';

import '../../call_state.dart';
import '../../logger/impl/tagged_logger.dart';
import '../../models/call_preferences.dart';
import '../../state_emitter.dart';
import 'mixins/state_call_actions_mixin.dart';
Expand All @@ -24,6 +25,8 @@ class CallStateNotifier extends StateNotifier<CallState>
MutableStateEmitterImpl<CallState>(initialState, sync: true);
}

final _logger = taggedLogger(tag: 'CallStateNotifier');

@override
final CallPreferences callPreferences;

Expand All @@ -32,6 +35,11 @@ class CallStateNotifier extends StateNotifier<CallState>

@override
set state(CallState value) {
_logger.v(() => '[setState] ${value.status} <= ${super.state.status}');
_logger.v(() => '[setState] ${value.callParticipants.map((it) {
return (it.sessionId, it.userId);
})}',);

super.state = value;
callStateStream.value = value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ mixin StateSfuMixin on StateNotifier<CallState> {
final participants = event.callState.participants.map((aParticipant) {
final isLocal = aParticipant.userId == state.currentUserId;
final existing = state.callParticipants.firstWhereOrNull(
(it) => it.userId == aParticipant.userId,
(it) => it.userId == aParticipant.userId
&& it.sessionId == aParticipant.sessionId,
);
final existingName = existing?.name ?? '';
final existingRole = existing?.role ?? '';
Expand Down Expand Up @@ -212,10 +213,20 @@ mixin StateSfuMixin on StateNotifier<CallState> {
isLocal: isLocal,
isOnline: !isLocal,
);
var isExisting = false;
final participants = state.callParticipants.map((it) {
if (it.userId == participant.userId &&
it.sessionId == participant.sessionId) {
isExisting = true;
return participant;
} else {
return it;
}
});
state = state.copyWith(
callParticipants: [
...state.callParticipants,
participant,
...participants,
if (!isExisting) participant,
],
);
}
Expand Down

0 comments on commit 6d543d0

Please sign in to comment.