From 3c34d4ac05349b21aa4e0f6d12853e0a5ad8c304 Mon Sep 17 00:00:00 2001 From: ThibaultBee <37510686+ThibaultBee@users.noreply.github.com> Date: Thu, 12 Oct 2023 10:10:57 +0200 Subject: [PATCH] fix(android): return a disconnect event when `stopStream` is called explicitly See https://github.com/apivideo/api.video-flutter-live-stream/issues/38 --- .../livestream/FlutterLiveStreamView.kt | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/android/src/main/kotlin/video/api/flutter/livestream/FlutterLiveStreamView.kt b/android/src/main/kotlin/video/api/flutter/livestream/FlutterLiveStreamView.kt index 62b50be..37f60ec 100644 --- a/android/src/main/kotlin/video/api/flutter/livestream/FlutterLiveStreamView.kt +++ b/android/src/main/kotlin/video/api/flutter/livestream/FlutterLiveStreamView.kt @@ -140,8 +140,12 @@ class FlutterLiveStreamView( } fun stopStream() { + val isConnected = streamer.isConnected streamer.stopStream() streamer.disconnect() + if (isConnected) { + sendDisconnected() + } _isStreaming = false } @@ -168,15 +172,11 @@ class FlutterLiveStreamView( override fun onSuccess() { - Handler(Looper.getMainLooper()).post { - eventSink?.success(mapOf("type" to "connected")) - } + sendConnected() } override fun onLost(message: String) { - Handler(Looper.getMainLooper()).post { - eventSink?.success(mapOf("type" to "disconnected")) - } + sendDisconnected() } override fun onFailed(message: String) { @@ -190,6 +190,20 @@ class FlutterLiveStreamView( } } + private fun sendEvent(type: String) { + Handler(Looper.getMainLooper()).post { + eventSink?.success(mapOf("type" to type)) + } + } + + private fun sendConnected() { + sendEvent("connected") + } + + private fun sendDisconnected() { + sendEvent("disconnected") + } + private fun sendConnectionFailed(message: String) { Handler(Looper.getMainLooper()).post { eventSink?.success(mapOf("type" to "connectionFailed", "message" to message))