Skip to content

Commit

Permalink
fix: update jellyfinApi before navigating to login fragment
Browse files Browse the repository at this point in the history
Also move logic to viewmodel
  • Loading branch information
jarnedemeulemeester committed Sep 25, 2023
1 parent 493d5ac commit 347313f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,14 @@ import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.fragment.findNavController
import dagger.hilt.android.AndroidEntryPoint
import dev.jdtech.jellyfin.adapters.ServerGridAdapter
import dev.jdtech.jellyfin.database.ServerDatabaseDao
import dev.jdtech.jellyfin.databinding.FragmentServerSelectBinding
import dev.jdtech.jellyfin.dialogs.DeleteServerDialogFragment
import dev.jdtech.jellyfin.viewmodels.ServerSelectViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject
import timber.log.Timber

@AndroidEntryPoint
class ServerSelectFragment : Fragment() {

@Inject
lateinit var database: ServerDatabaseDao

private lateinit var binding: FragmentServerSelectBinding
private val viewModel: ServerSelectViewModel by viewModels()

Expand All @@ -39,11 +33,7 @@ class ServerSelectFragment : Fragment() {
binding.serversRecyclerView.adapter =
ServerGridAdapter(
onClickListener = { server ->
database.getServerCurrentUser(server.id)?.let {
viewModel.connectToServer(server)
return@OnClickListener
}
navigateToLoginFragment()
viewModel.connectToServer(server)
},
onLongClickListener = { server ->
DeleteServerDialogFragment(viewModel, server).show(
Expand Down Expand Up @@ -75,6 +65,11 @@ class ServerSelectFragment : Fragment() {
if (it) navigateToMainActivity()
}
}
launch {
viewModel.navigateToLogin.collect {
if (it) navigateToLoginFragment()
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ constructor(
private val _navigateToMain = MutableSharedFlow<Boolean>()
val navigateToMain = _navigateToMain.asSharedFlow()

private val _navigateToLogin = MutableSharedFlow<Boolean>()
val navigateToLogin = _navigateToLogin.asSharedFlow()

sealed class UiState {
data class Normal(val servers: List<Server>) : UiState()
data object Loading : UiState()
Expand Down Expand Up @@ -62,7 +65,19 @@ constructor(
viewModelScope.launch {
val serverWithAddressesAndUsers = database.getServerWithAddressesAndUsers(server.id) ?: return@launch
val serverAddress = serverWithAddressesAndUsers.addresses.firstOrNull { it.id == server.currentServerAddressId } ?: return@launch
val user = serverWithAddressesAndUsers.users.firstOrNull { it.id == server.currentUserId } ?: return@launch
val user = serverWithAddressesAndUsers.users.firstOrNull { it.id == server.currentUserId }

// If server has no selected user, navigate to login fragment
if (user == null) {
jellyfinApi.apply {
api.baseUrl = serverAddress.address
api.accessToken = null
userId = null
}
appPreferences.currentServer = server.id
_navigateToLogin.emit(true)
return@launch
}

jellyfinApi.apply {
api.baseUrl = serverAddress.address
Expand Down

0 comments on commit 347313f

Please sign in to comment.