diff --git a/app/src/main/java/com/github/andreyasadchy/xtra/ui/main/MainActivity.kt b/app/src/main/java/com/github/andreyasadchy/xtra/ui/main/MainActivity.kt index e55223259..95ab26a53 100644 --- a/app/src/main/java/com/github/andreyasadchy/xtra/ui/main/MainActivity.kt +++ b/app/src/main/java/com/github/andreyasadchy/xtra/ui/main/MainActivity.kt @@ -520,13 +520,23 @@ class MainActivity : AppCompatActivity(), SlidingLayout.Listener { else -> { val login = url.substringAfter("twitch.tv/").takeIf { it.isNotBlank() }?.let { it.substringBefore("?", it.substringBefore("/")) } if (!login.isNullOrBlank()) { - viewModel.loadUser( + viewModel.handleChannelLink( login, prefs.getString(C.NETWORK_LIBRARY, "OkHttp"), TwitchApiHelper.getGQLHeaders(this), TwitchApiHelper.getHelixHeaders(this), prefs.getBoolean(C.ENABLE_INTEGRITY, false), ) + lifecycleScope.launch { + repeatOnLifecycle(Lifecycle.State.STARTED) { + viewModel.stream.collectLatest { stream -> + if (stream != null) { + startStream(stream) + viewModel.stream.value = null + } + } + } + } lifecycleScope.launch { repeatOnLifecycle(Lifecycle.State.STARTED) { viewModel.user.collectLatest { user -> diff --git a/app/src/main/java/com/github/andreyasadchy/xtra/ui/main/MainViewModel.kt b/app/src/main/java/com/github/andreyasadchy/xtra/ui/main/MainViewModel.kt index 6636a2c6c..3cc8a3037 100644 --- a/app/src/main/java/com/github/andreyasadchy/xtra/ui/main/MainViewModel.kt +++ b/app/src/main/java/com/github/andreyasadchy/xtra/ui/main/MainViewModel.kt @@ -22,6 +22,7 @@ import com.github.andreyasadchy.xtra.R import com.github.andreyasadchy.xtra.model.VideoPosition import com.github.andreyasadchy.xtra.model.ui.Clip import com.github.andreyasadchy.xtra.model.ui.OfflineVideo +import com.github.andreyasadchy.xtra.model.ui.Stream import com.github.andreyasadchy.xtra.model.ui.User import com.github.andreyasadchy.xtra.model.ui.Video import com.github.andreyasadchy.xtra.repository.AuthRepository @@ -96,6 +97,8 @@ class MainViewModel @Inject constructor( val video = MutableStateFlow(null) val clip = MutableStateFlow(null) val user = MutableStateFlow(null) + private val _stream = MutableStateFlow(null) + val stream: MutableStateFlow = _stream val updateUrl = MutableSharedFlow() @@ -933,4 +936,39 @@ class MainViewModel @Inject constructor( offlineRepository.deleteOldImages() } } + + fun handleChannelLink(login: String?, networkLibrary: String?, gqlHeaders: Map, helixHeaders: Map, enableIntegrity: Boolean) { + if (stream.value == null && user.value == null) { + viewModelScope.launch { + try { + val response = graphQLRepository.loadQueryUsersStream(networkLibrary, gqlHeaders, logins = listOf(login!!)) + if (enableIntegrity && integrity.value == null) { + response.errors?.find { it.message == "failed integrity check" }?.let { + integrity.value = "refresh" + return@launch + } + } + val userData = response.data?.users?.firstOrNull() + val streamData = userData?.stream + if (streamData != null) { + _stream.value = Stream( + channelId = userData?.id, + channelLogin = userData?.login, + channelName = userData?.displayName, + profileImageUrl = userData?.profileImageURL, + title = streamData.broadcaster?.broadcastSettings?.title, + gameId = streamData.game?.id, + gameName = streamData.game?.displayName, + viewerCount = streamData.viewersCount, + thumbnailUrl = streamData.previewImageURL, + ) + } else { + loadUser(login, networkLibrary, gqlHeaders, helixHeaders, enableIntegrity) + } + } catch (e: Exception) { + loadUser(login, networkLibrary, gqlHeaders, helixHeaders, enableIntegrity) + } + } + } + } } \ No newline at end of file