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

View file

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

View file

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

View file

@ -51,6 +51,8 @@ fun ApplicationContent(
preferences = preferences, preferences = preferences,
navigationManager = navigationManager, navigationManager = navigationManager,
deviceProfile = deviceProfile, deviceProfile = deviceProfile,
user = user,
server = server,
modifier = modifier, 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.VideoDetails
import com.github.damontecres.dolphin.ui.detail.movie.MovieDetails import com.github.damontecres.dolphin.ui.detail.movie.MovieDetails
import com.github.damontecres.dolphin.ui.detail.series.SeriesOverview 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.playback.PlaybackContent
import com.github.damontecres.dolphin.ui.preferences.PreferenceScreenOption import com.github.damontecres.dolphin.ui.preferences.PreferenceScreenOption
import com.github.damontecres.dolphin.ui.preferences.PreferencesPage import com.github.damontecres.dolphin.ui.preferences.PreferencesPage
@ -31,7 +31,7 @@ fun DestinationContent(
) { ) {
when (destination) { when (destination) {
is Destination.Main -> is Destination.Main ->
MainPage( HomePage(
preferences = preferences, preferences = preferences,
navigationManager = navigationManager, navigationManager = navigationManager,
modifier = modifier, modifier = modifier,

View file

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