onInit method
override
Called immediately after the widget is allocated in memory. You might use this to initialize something for the controller.
Implementation
@override
void onInit() {
_subscription = _callService.calls.changes.listen((event) {
switch (event.op) {
case OperationKind.added:
if (WebUtils.containsCall(event.key!)) {
// Call's popup is already displayed, perhaps by another tab, so
// don't react to this call at all.
return;
}
// Unfocus any inputs being active.
FocusManager.instance.primaryFocus?.unfocus();
bool window = false;
var ongoingCall = event.value!.value;
if (PlatformUtils.isWeb &&
!PlatformUtils.isMobile &&
_settings.value?.enablePopups != false) {
window = WebUtils.openPopupCall(
event.key!,
withAudio:
ongoingCall.audioState.value == LocalTrackState.enabling ||
ongoingCall.audioState.value == LocalTrackState.enabled,
withVideo:
ongoingCall.videoState.value == LocalTrackState.enabling ||
ongoingCall.videoState.value == LocalTrackState.enabled,
withScreen:
ongoingCall.screenShareState.value ==
LocalTrackState.enabling ||
ongoingCall.screenShareState.value == LocalTrackState.enabled,
);
// If [window] is `true`, then a new popup window is created, so
// treat this call as a popup windowed call.
if (window) {
WebUtils.setCall(ongoingCall.toStored());
if (ongoingCall.callChatItemId == null ||
ongoingCall.deviceId == null) {
_workers[event.key!] = ever(event.value!.value.call, (
ChatCall? call,
) {
WebUtils.setCall(
WebStoredCall(
chatId: ongoingCall.chatId.value,
call: call,
creds: ongoingCall.creds,
deviceId: ongoingCall.deviceId,
state: ongoingCall.state.value,
),
);
if (call?.id != null) {
_workers[event.key!]?.dispose();
}
});
}
} else {
Future.delayed(Duration.zero, () {
ongoingCall.addError('err_call_popup_was_blocked'.l10n);
});
}
}
if (!window) {
// Otherwise the popup creation request failed or wasn't invoked, so
// add this call to the [calls] to display it in the view.
calls.add(OverlayCall(event.value!));
event.value?.value.init(getChat: _chatService.get);
}
break;
case OperationKind.removed:
calls.removeWhere((e) => e.call.value.chatId.value == event.key);
final OngoingCall call = event.value!.value;
final WebStoredCall? web = WebUtils.getCall(event.key!);
if (call.callChatItemId == null ||
call.connected ||
web?.state == OngoingCallState.pending) {
WebUtils.removeCall(event.key!);
}
break;
case OperationKind.updated:
// No-op.
break;
}
});
super.onInit();
}