Skip to content

Commit

Permalink
Some tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxr1998 committed Jun 14, 2021
1 parent a73abbc commit 4dbc1ce
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ class ApiController(
}

private fun configureApiClientUser(userId: String, accessToken: String) {
apiClient.userId = userId.toUUID()
apiClient.accessToken = accessToken
// Append user id to device id to ensure uniqueness across sessions
apiClient.deviceInfo = baseDeviceInfo.copy(id = baseDeviceInfo.id + userId)
apiClient.accessToken = accessToken
apiClient.userId = userId.toUUID()
}

private fun resetApiClientUser() {
apiClient.deviceInfo = baseDeviceInfo
apiClient.accessToken = null
apiClient.userId = null
apiClient.accessToken = null
apiClient.deviceInfo = baseDeviceInfo
}
}
7 changes: 6 additions & 1 deletion app/src/main/java/org/jellyfin/mobile/media/MediaService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,12 @@ class MediaService : MediaBrowserServiceCompat() {
loadingJob.join()

val items = try {
if (apiClient.userId != null) libraryBrowser.loadLibrary(parentId) else null
if (apiClient.userId != null) {
libraryBrowser.loadLibrary(parentId)
} else {
Timber.e("Missing userId in ApiClient")
null
}
} catch (e: ApiClientException) {
Timber.e(e)
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import org.jellyfin.sdk.model.api.SortOrder
import org.jellyfin.sdk.model.serializer.toUUID
import org.jellyfin.sdk.model.serializer.toUUIDOrNull
import timber.log.Timber
import java.util.*
import java.util.UUID

@Suppress("TooManyFunctions")
class LibraryBrowser(
Expand Down Expand Up @@ -76,8 +76,10 @@ class LibraryBrowser(
val split = parentId.split('|')

@Suppress("MagicNumber")
if (split.size !in 1..3)
if (split.size !in 1..3) {
Timber.e("Invalid libraryId format '$parentId'")
return null
}

val type = split[0]
val libraryId = split.getOrNull(1)?.toUUIDOrNull()
Expand Down Expand Up @@ -114,8 +116,10 @@ class LibraryBrowser(
suspend fun buildPlayQueue(mediaId: String): Pair<List<MediaMetadataCompat>, Int>? {
val split = mediaId.split('|')
@Suppress("MagicNumber")
if (split.size != 3)
if (split.size != 3) {
Timber.e("Invalid mediaId format '$mediaId'")
return null
}

val type = split[0]
val collectionId = split[1].toUUID()
Expand Down Expand Up @@ -221,7 +225,7 @@ class LibraryBrowser(
.map { item ->
val itemImageUrl = imageApi.getItemImageUrl(
itemId = item.id,
imageType = ImageType.PRIMARY
imageType = ImageType.PRIMARY,
)

val description = MediaDescriptionCompat.Builder().apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.jellyfin.mobile.BuildConfig
import org.jellyfin.mobile.PLAYER_EVENT_CHANNEL
import org.jellyfin.mobile.controller.ApiController
import org.jellyfin.mobile.player.source.JellyfinMediaSource
import org.jellyfin.mobile.player.source.MediaQueueManager
import org.jellyfin.mobile.utils.Constants
Expand All @@ -51,10 +50,8 @@ import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import org.koin.core.qualifier.named
import timber.log.Timber
import java.util.*

class PlayerViewModel(application: Application) : AndroidViewModel(application), KoinComponent, Player.Listener {
private val apiController by inject<ApiController>()
private val playStateApi by inject<PlayStateApi>()

private val lifecycleObserver = PlayerLifecycleObserver(this)
Expand Down Expand Up @@ -243,9 +240,7 @@ class PlayerViewModel(application: Application) : AndroidViewModel(application),

// Mark video as watched if playback finished
if (hasFinished) {
playStateApi.markPlayedItem(
itemId = mediaSource.itemId,
)
playStateApi.markPlayedItem(itemId = mediaSource.itemId)
}
} catch (e: ApiClientException) {
Timber.e(e, "Failed to report playback stop")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.jellyfin.sdk.model.api.DeviceProfile
import org.jellyfin.sdk.model.api.PlaybackInfoDto
import org.jellyfin.sdk.model.serializer.toUUIDOrNull
import timber.log.Timber
import java.util.*
import java.util.UUID

class MediaSourceResolver(
private val apiClient: ApiClient,
Expand All @@ -25,7 +25,7 @@ class MediaSourceResolver(
): Result<JellyfinMediaSource> {
// Load media source info
val mediaSourceInfo = try {
val playbackInfoResponse by mediaInfoApi.getPostedPlaybackInfo(
val response by mediaInfoApi.getPostedPlaybackInfo(
itemId = itemId,
data = PlaybackInfoDto(
userId = apiClient.userId,
Expand All @@ -37,7 +37,7 @@ class MediaSourceResolver(
),
)

playbackInfoResponse.mediaSources?.find { source ->
response.mediaSources?.find { source ->
source.id?.toUUIDOrNull() == itemId
} ?: return Result.failure(PlayerException.UnsupportedContent())
} catch (e: ApiClientException) {
Expand All @@ -47,10 +47,8 @@ class MediaSourceResolver(

// Load additional item info if possible
val item = try {
val itemsResponse by itemsApi.getItemsByUserId(
ids = listOf(itemId),
)
itemsResponse.items?.firstOrNull()
val response by itemsApi.getItemsByUserId(ids = listOf(itemId))
response.items?.firstOrNull()
} catch (e: ApiClientException) {
Timber.e(e, "Failed to load item for media source $itemId")
null
Expand Down

0 comments on commit 4dbc1ce

Please sign in to comment.