Skip to content

Commit

Permalink
refactor: move set current server to repository
Browse files Browse the repository at this point in the history
  • Loading branch information
jarnedemeulemeester committed Nov 16, 2024
1 parent fc865bc commit 39be758
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ class SetupRepositoryImpl(
return jellyfinApi.quickConnectApi.getQuickConnectState(secret).content
}

override suspend fun setCurrentServer(serverId: String) {
val serverWithAddressAndUser = database.getServerWithAddressAndUser(serverId) ?: return
val serverAddress = serverWithAddressAndUser.address ?: return

jellyfinApi.apply {
api.update(
baseUrl = serverAddress.address,
accessToken = null,
)
userId = null
}
}

override suspend fun addServer(address: String): Server {
// Check if address is not blank
if (address.isBlank()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ interface SetupRepository {

suspend fun getQuickConnectState(secret: String): QuickConnectResult

suspend fun setCurrentServer(serverId: String)

suspend fun addServer(address: String): Server

suspend fun loadDisclaimer(): String?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import dev.jdtech.jellyfin.AppPreferences
import dev.jdtech.jellyfin.api.JellyfinApi
import dev.jdtech.jellyfin.database.ServerDatabaseDao
import dev.jdtech.jellyfin.setup.domain.SetupRepository
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -19,8 +17,6 @@ class ServersViewModel
@Inject
constructor(
private val repository: SetupRepository,
private val database: ServerDatabaseDao,
private val jellyfinApi: JellyfinApi,
private val appPreferences: AppPreferences,
) : ViewModel() {
private val _state = MutableStateFlow(ServersState())
Expand All @@ -40,16 +36,7 @@ constructor(

private fun connectToServer(serverId: String) {
viewModelScope.launch {
val serverWithAddressAndUser = database.getServerWithAddressAndUser(serverId) ?: return@launch
val serverAddress = serverWithAddressAndUser.address ?: return@launch

jellyfinApi.apply {
api.update(
baseUrl = serverAddress.address,
accessToken = null,
)
userId = null
}
repository.setCurrentServer(serverId)

appPreferences.currentServer = serverId

Expand Down

0 comments on commit 39be758

Please sign in to comment.