Fix bugs with switching users

This commit is contained in:
Damontecres 2025-10-04 23:29:37 -04:00
parent 833e1d2ea1
commit dd08bb3e13
No known key found for this signature in database
6 changed files with 55 additions and 45 deletions

View file

@ -38,20 +38,6 @@ class ServerRepository
changeServer(server)
}
suspend fun addAndChangeUser(
server: JellyfinServer,
user: JellyfinUser,
) {
if (server.id != user.serverId) {
throw IllegalStateException()
}
withContext(Dispatchers.IO) {
serverDao.addServer(server)
serverDao.addUser(user)
}
changeUser(server, user)
}
fun changeServer(server: JellyfinServer) {
apiClient.update(baseUrl = server.url, accessToken = null)
_currentServer = server
@ -60,10 +46,13 @@ class ServerRepository
suspend fun changeUser(
server: JellyfinServer,
user: JellyfinUser,
) {
) = withContext(Dispatchers.IO) {
try {
if (server.id != user.serverId) {
throw IllegalStateException()
}
Timber.v("Changing user to ${user.name} on ${server.url}")
apiClient.update(baseUrl = server.url, accessToken = user.accessToken)
try {
val userDto =
apiClient.userApi
.getCurrentUser()
@ -75,21 +64,31 @@ class ServerRepository
id = userDto.id.toString(),
name = userDto.name,
)
withContext(Dispatchers.IO) {
serverDao.addServer(updatedServer)
serverDao.addUser(updatedUser)
userPreferencesDataStore.updateData {
it
.toBuilder()
.apply {
currentServerId = updatedServer.id
currentUserId = updatedUser.id
}.build()
}
withContext(Dispatchers.Main) {
_currentUserDto = userDto
_currentServer = updatedServer
_currentUser = updatedUser
}
} catch (e: InvalidStatusException) {
// TODO
Timber.e(e)
if (e.status == 401) {
withContext(Dispatchers.Main) {
// Unauthorized
_currentServer = null
_currentUser = null
return
}
if (e.status == 401) {
return@withContext
}
}
}
@ -115,7 +114,7 @@ class ServerRepository
suspend fun changeUser(
serverUrl: String,
authenticationResult: AuthenticationResult,
) {
) = withContext(Dispatchers.IO) {
val accessToken = authenticationResult.accessToken
if (accessToken != null) {
val authedUser = authenticationResult.user
@ -138,15 +137,7 @@ class ServerRepository
)
}
if (user != null) {
userPreferencesDataStore.updateData {
it
.toBuilder()
.apply {
currentServerId = server.id
currentUserId = user.id
}.build()
}
addAndChangeUser(server, user)
changeUser(server, user)
}
}
}

View file

@ -65,11 +65,11 @@ data class HomeRow(
)
@Composable
fun MainPage(
fun HomePage(
preferences: UserPreferences,
navigationManager: NavigationManager,
modifier: Modifier,
viewModel: MainViewModel = hiltViewModel(),
viewModel: HomeViewModel = hiltViewModel(),
) {
OneTimeLaunchedEffect {
viewModel.init(preferences)

View file

@ -27,7 +27,7 @@ import timber.log.Timber
import javax.inject.Inject
@HiltViewModel
class MainViewModel
class HomeViewModel
@Inject
constructor(
val api: ApiClient,
@ -135,7 +135,7 @@ class MainViewModel
.getNextUp(request)
.content
.items
.map { BaseItem.Companion.from(it, api) }
.map { BaseItem.Companion.from(it, api, true) }
listOf(
HomeRow(
section = section,
@ -158,7 +158,7 @@ class MainViewModel
}.flatten()
.filter { it.items.isNotEmpty() }
withContext(Dispatchers.Main) {
this@MainViewModel.homeRows.value = homeRows
this@HomeViewModel.homeRows.value = homeRows
loadingState.value = LoadingState.Success
}
}

View file

@ -51,6 +51,8 @@ fun ApplicationContent(
preferences = preferences,
navigationManager = navigationManager,
deviceProfile = deviceProfile,
user = user,
server = server,
modifier = modifier,
)
}

View file

@ -12,7 +12,7 @@ import com.github.damontecres.dolphin.ui.detail.SeriesDetails
import com.github.damontecres.dolphin.ui.detail.VideoDetails
import com.github.damontecres.dolphin.ui.detail.movie.MovieDetails
import com.github.damontecres.dolphin.ui.detail.series.SeriesOverview
import com.github.damontecres.dolphin.ui.main.MainPage
import com.github.damontecres.dolphin.ui.main.HomePage
import com.github.damontecres.dolphin.ui.playback.PlaybackContent
import com.github.damontecres.dolphin.ui.preferences.PreferenceScreenOption
import com.github.damontecres.dolphin.ui.preferences.PreferencesPage
@ -31,7 +31,7 @@ fun DestinationContent(
) {
when (destination) {
is Destination.Main ->
MainPage(
HomePage(
preferences = preferences,
navigationManager = navigationManager,
modifier = modifier,

View file

@ -50,6 +50,8 @@ import androidx.tv.material3.Text
import androidx.tv.material3.rememberDrawerState
import androidx.tv.material3.surfaceColorAtElevation
import com.github.damontecres.dolphin.R
import com.github.damontecres.dolphin.data.JellyfinServer
import com.github.damontecres.dolphin.data.JellyfinUser
import com.github.damontecres.dolphin.data.ServerRepository
import com.github.damontecres.dolphin.data.model.Library
import com.github.damontecres.dolphin.preferences.UserPreferences
@ -89,9 +91,15 @@ fun NavDrawer(
destination: Destination,
preferences: UserPreferences,
navigationManager: NavigationManager,
user: JellyfinUser?,
server: JellyfinServer?,
deviceProfile: DeviceProfile,
modifier: Modifier = Modifier,
viewModel: NavDrawerViewModel = hiltViewModel(LocalView.current.findViewTreeViewModelStoreOwner()!!),
viewModel: NavDrawerViewModel =
hiltViewModel(
LocalView.current.findViewTreeViewModelStoreOwner()!!,
key = "${server?.id}_${user?.id}",
),
) {
val drawerState = rememberDrawerState(DrawerValue.Closed)
val listState = rememberLazyListState()
@ -102,8 +110,6 @@ fun NavDrawer(
drawerState.setValue(DrawerValue.Open)
drawerFocusRequester.requestFocus()
}
val user = viewModel.serverRepository.currentUser
val libraries by viewModel.libraries.observeAsState(listOf())
NavigationDrawer(
@ -125,6 +131,7 @@ fun NavDrawer(
) {
IconNavItem(
text = user?.name ?: "",
subtext = server?.name ?: server?.url,
icon = Icons.Default.AccountCircle,
onClick = { navigationManager.navigateTo(Destination.UserList) },
)
@ -194,6 +201,7 @@ fun NavigationDrawerScope.IconNavItem(
icon: ImageVector,
onClick: () -> Unit,
modifier: Modifier = Modifier,
subtext: String? = null,
) {
NavigationDrawerItem(
modifier = modifier,
@ -205,6 +213,15 @@ fun NavigationDrawerScope.IconNavItem(
contentDescription = null,
)
},
supportingContent =
subtext?.let {
{
Text(
text = it,
maxLines = 1,
)
}
},
interactionSource = null,
) {
Text(