Fix other call not always refreshed when ended #4028

This commit is contained in:
ganfra 2021-09-16 18:53:41 +02:00
parent 226b0e6c9d
commit 82864b2b98
5 changed files with 41 additions and 19 deletions

1
changelog.d/4028.bugfix Normal file
View File

@ -0,0 +1 @@
Fix other call not always refreshed when ended

View File

@ -36,7 +36,7 @@ class CurrentCallsViewPresenter {
this.currentCall = currentCall this.currentCall = currentCall
this.currentCall?.addListener(tickListener) this.currentCall?.addListener(tickListener)
this.calls = calls this.calls = calls
val hasActiveCall = currentCall != null val hasActiveCall = calls.isNotEmpty()
currentCallsView?.isVisible = hasActiveCall currentCallsView?.isVisible = hasActiveCall
currentCallsView?.render(calls, currentCall?.formattedDuration() ?: "") currentCallsView?.render(calls, currentCall?.formattedDuration() ?: "")
} }

View File

@ -41,7 +41,7 @@ class SharedKnownCallsViewModel @Inject constructor(
} }
} }
private val currentCallListener = object : WebRtcCallManager.CurrentCallListener { private val callManagerListener = object : WebRtcCallManager.Listener {
override fun onCurrentCallChange(call: WebRtcCall?) { override fun onCurrentCallChange(call: WebRtcCall?) {
val knownCalls = callManager.getCalls() val knownCalls = callManager.getCalls()
liveKnownCalls.postValue(knownCalls) liveKnownCalls.postValue(knownCalls)
@ -50,12 +50,17 @@ class SharedKnownCallsViewModel @Inject constructor(
it.addListener(callListener) it.addListener(callListener)
} }
} }
override fun onCallEnded(callId: String) {
val knownCalls = callManager.getCalls()
liveKnownCalls.postValue(knownCalls)
}
} }
init { init {
val knownCalls = callManager.getCalls() val knownCalls = callManager.getCalls()
liveKnownCalls.postValue(knownCalls) liveKnownCalls.postValue(knownCalls)
callManager.addCurrentCallListener(currentCallListener) callManager.addListener(callManagerListener)
knownCalls.forEach { knownCalls.forEach {
it.addListener(callListener) it.addListener(callListener)
} }
@ -65,7 +70,7 @@ class SharedKnownCallsViewModel @Inject constructor(
callManager.getCalls().forEach { callManager.getCalls().forEach {
it.removeListener(callListener) it.removeListener(callListener)
} }
callManager.removeCurrentCallListener(currentCallListener) callManager.removeListener(callManagerListener)
super.onCleared() super.onCleared()
} }
} }

View File

@ -134,7 +134,15 @@ class VectorCallViewModel @AssistedInject constructor(
} ?: VectorCallViewState.TransfereeState.UnknownTransferee } ?: VectorCallViewState.TransfereeState.UnknownTransferee
} }
private val currentCallListener = object : WebRtcCallManager.CurrentCallListener { private val callManagerListener = object : WebRtcCallManager.Listener {
override fun onCallEnded(callId: String) {
withState { state ->
if (state.otherKnownCallInfo?.callId == callId) {
setState { copy(otherKnownCallInfo = null) }
}
}
}
override fun onCurrentCallChange(call: WebRtcCall?) { override fun onCurrentCallChange(call: WebRtcCall?) {
if (call != null) { if (call != null) {
@ -159,9 +167,7 @@ class VectorCallViewModel @AssistedInject constructor(
} }
private fun updateOtherKnownCall(currentCall: WebRtcCall) { private fun updateOtherKnownCall(currentCall: WebRtcCall) {
val otherCall = callManager.getCalls().firstOrNull { val otherCall = getOtherKnownCall(currentCall)
it.callId != currentCall.callId && it.mxCall.state is CallState.Connected
}
setState { setState {
if (otherCall == null) { if (otherCall == null) {
copy(otherKnownCallInfo = null) copy(otherKnownCallInfo = null)
@ -171,6 +177,12 @@ class VectorCallViewModel @AssistedInject constructor(
} }
} }
private fun getOtherKnownCall(currentCall: WebRtcCall): WebRtcCall? {
return callManager.getCalls().firstOrNull {
it.callId != currentCall.callId && it.mxCall.state is CallState.Connected
}
}
init { init {
setupCallWithCurrentState() setupCallWithCurrentState()
} }
@ -184,7 +196,7 @@ class VectorCallViewModel @AssistedInject constructor(
} }
} else { } else {
call = webRtcCall call = webRtcCall
callManager.addCurrentCallListener(currentCallListener) callManager.addListener(callManagerListener)
webRtcCall.addListener(callListener) webRtcCall.addListener(callListener)
val currentSoundDevice = callManager.audioManager.selectedDevice val currentSoundDevice = callManager.audioManager.selectedDevice
if (currentSoundDevice == CallAudioManager.Device.Phone) { if (currentSoundDevice == CallAudioManager.Device.Phone) {
@ -230,7 +242,7 @@ class VectorCallViewModel @AssistedInject constructor(
} }
override fun onCleared() { override fun onCleared() {
callManager.removeCurrentCallListener(currentCallListener) callManager.removeListener(callManagerListener)
call?.removeListener(callListener) call?.removeListener(callListener)
call = null call = null
proximityManager.stop() proximityManager.stop()
@ -310,10 +322,10 @@ class VectorCallViewModel @AssistedInject constructor(
VectorCallViewEvents.ShowCallTransferScreen VectorCallViewEvents.ShowCallTransferScreen
) )
} }
VectorCallViewActions.TransferCall -> { VectorCallViewActions.TransferCall -> {
handleCallTransfer() handleCallTransfer()
} }
is VectorCallViewActions.SwitchCall -> { is VectorCallViewActions.SwitchCall -> {
setState { VectorCallViewState(action.callArgs) } setState { VectorCallViewState(action.callArgs) }
setupCallWithCurrentState() setupCallWithCurrentState()
} }

View File

@ -84,9 +84,10 @@ class WebRtcCallManager @Inject constructor(
private val sessionScope: CoroutineScope? private val sessionScope: CoroutineScope?
get() = currentSession?.coroutineScope get() = currentSession?.coroutineScope
interface CurrentCallListener { interface Listener {
fun onCurrentCallChange(call: WebRtcCall?) {} fun onCallEnded(callId: String) = Unit
fun onAudioDevicesChange() {} fun onCurrentCallChange(call: WebRtcCall?) = Unit
fun onAudioDevicesChange() = Unit
} }
val supportedPSTNProtocol: String? val supportedPSTNProtocol: String?
@ -106,13 +107,13 @@ class WebRtcCallManager @Inject constructor(
protocolsChecker?.removeListener(listener) protocolsChecker?.removeListener(listener)
} }
private val currentCallsListeners = CopyOnWriteArrayList<CurrentCallListener>() private val currentCallsListeners = CopyOnWriteArrayList<Listener>()
fun addCurrentCallListener(listener: CurrentCallListener) { fun addListener(listener: Listener) {
currentCallsListeners.add(listener) currentCallsListeners.add(listener)
} }
fun removeCurrentCallListener(listener: CurrentCallListener) { fun removeListener(listener: Listener) {
currentCallsListeners.remove(listener) currentCallsListeners.remove(listener)
} }
@ -250,10 +251,13 @@ class WebRtcCallManager @Inject constructor(
callsByRoomId[webRtcCall.signalingRoomId]?.remove(webRtcCall) callsByRoomId[webRtcCall.signalingRoomId]?.remove(webRtcCall)
callsByRoomId[webRtcCall.nativeRoomId]?.remove(webRtcCall) callsByRoomId[webRtcCall.nativeRoomId]?.remove(webRtcCall)
transferees.remove(callId) transferees.remove(callId)
if (getCurrentCall()?.callId == callId) { if (currentCall.get()?.callId == callId) {
val otherCall = getCalls().lastOrNull() val otherCall = getCalls().lastOrNull()
currentCall.setAndNotify(otherCall) currentCall.setAndNotify(otherCall)
} }
tryOrNull {
currentCallsListeners.forEach { it.onCallEnded(callId) }
}
// There is no active calls // There is no active calls
if (getCurrentCall() == null) { if (getCurrentCall() == null) {
Timber.tag(loggerTag.value).v("Dispose peerConnectionFactory as there is no need to keep one") Timber.tag(loggerTag.value).v("Dispose peerConnectionFactory as there is no need to keep one")