Check if server is already in the database
This commit is contained in:
parent
d3ebf29efa
commit
980cb321fb
2 changed files with 36 additions and 3 deletions
|
@ -23,6 +23,9 @@ interface ServerDatabaseDao {
|
||||||
@Query("select * from servers")
|
@Query("select * from servers")
|
||||||
fun getAllServers(): LiveData<List<Server>>
|
fun getAllServers(): LiveData<List<Server>>
|
||||||
|
|
||||||
|
@Query("select * from servers")
|
||||||
|
fun getAllServersSync(): List<Server>
|
||||||
|
|
||||||
@Query("delete from servers where id = :id")
|
@Query("delete from servers where id = :id")
|
||||||
fun delete(id: String)
|
fun delete(id: String)
|
||||||
}
|
}
|
|
@ -7,10 +7,16 @@ import androidx.lifecycle.MutableLiveData
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import dev.jdtech.jellyfin.api.JellyfinApi
|
import dev.jdtech.jellyfin.api.JellyfinApi
|
||||||
|
import dev.jdtech.jellyfin.database.Server
|
||||||
|
import dev.jdtech.jellyfin.database.ServerDatabase
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
import java.lang.Exception
|
import java.lang.Exception
|
||||||
|
|
||||||
class AddServerViewModel(val application: Application) : ViewModel() {
|
class AddServerViewModel(val application: Application) : ViewModel() {
|
||||||
|
private val database = ServerDatabase.getInstance(application).serverDatabaseDao
|
||||||
|
|
||||||
private val _navigateToLogin = MutableLiveData<Boolean>()
|
private val _navigateToLogin = MutableLiveData<Boolean>()
|
||||||
val navigateToLogin: LiveData<Boolean>
|
val navigateToLogin: LiveData<Boolean>
|
||||||
get() = _navigateToLogin
|
get() = _navigateToLogin
|
||||||
|
@ -20,12 +26,21 @@ class AddServerViewModel(val application: Application) : ViewModel() {
|
||||||
get() = _error
|
get() = _error
|
||||||
|
|
||||||
fun checkServer(baseUrl: String) {
|
fun checkServer(baseUrl: String) {
|
||||||
|
_error.value = null
|
||||||
|
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
val jellyfinApi = JellyfinApi.newInstance(application, baseUrl)
|
val jellyfinApi = JellyfinApi.newInstance(application, baseUrl)
|
||||||
try {
|
try {
|
||||||
jellyfinApi.systemApi.getPublicSystemInfo()
|
val publicSystemInfo by jellyfinApi.systemApi.getPublicSystemInfo()
|
||||||
|
Log.i("AddServerViewModel", "Remote server: ${publicSystemInfo.id}")
|
||||||
|
|
||||||
|
if (serverAlreadyInDatabase(publicSystemInfo.id)) {
|
||||||
|
_error.value = "Server already added"
|
||||||
|
_navigateToLogin.value = false
|
||||||
|
} else {
|
||||||
_error.value = null
|
_error.value = null
|
||||||
_navigateToLogin.value = true
|
_navigateToLogin.value = true
|
||||||
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e("AddServerViewModel", "${e.message}")
|
Log.e("AddServerViewModel", "${e.message}")
|
||||||
_error.value = e.message
|
_error.value = e.message
|
||||||
|
@ -34,6 +49,21 @@ class AddServerViewModel(val application: Application) : ViewModel() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private suspend fun serverAlreadyInDatabase(id: String?): Boolean {
|
||||||
|
val servers: List<Server>
|
||||||
|
withContext(Dispatchers.IO) {
|
||||||
|
servers = database.getAllServersSync()
|
||||||
|
}
|
||||||
|
for (server in servers) {
|
||||||
|
Log.i("AddServerViewModel", "Database server: ${server.id}")
|
||||||
|
if (server.id == id) {
|
||||||
|
Log.i("AddServerViewModel", "Server already in the database")
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
fun onNavigateToLoginDone() {
|
fun onNavigateToLoginDone() {
|
||||||
_navigateToLogin.value = false
|
_navigateToLogin.value = false
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue