Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -96,6 +97,8 @@ class MainViewModel @Inject constructor(
val video = MutableStateFlow<Video?>(null)
val clip = MutableStateFlow<Clip?>(null)
val user = MutableStateFlow<User?>(null)
private val _stream = MutableStateFlow<Stream?>(null)
val stream: MutableStateFlow<Stream?> = _stream

val updateUrl = MutableSharedFlow<String?>()

Expand Down Expand Up @@ -933,4 +936,39 @@ class MainViewModel @Inject constructor(
offlineRepository.deleteOldImages()
}
}

fun handleChannelLink(login: String?, networkLibrary: String?, gqlHeaders: Map<String, String>, helixHeaders: Map<String, String>, 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)
}
}
}
}
}