Add code comments + some cleanup

This commit is contained in:
Jarne Demeulemeester 2021-06-12 16:32:15 +02:00
parent 980cb321fb
commit b1efd7648f
No known key found for this signature in database
GPG key ID: B61B7B150DB6A6D2
4 changed files with 68 additions and 17 deletions

View file

@ -9,6 +9,14 @@ import org.jellyfin.sdk.api.operations.SystemApi
import org.jellyfin.sdk.api.operations.UserApi
import org.jellyfin.sdk.model.ClientInfo
/**
* Jellyfin API class using org.jellyfin.sdk:jellyfin-platform-android
*
* @param context The context
* @param baseUrl The url of the server
* @constructor Creates a new [JellyfinApi] instance
*/
class JellyfinApi(context: Context, baseUrl: String) {
val jellyfin = Jellyfin {
clientInfo =
@ -27,6 +35,14 @@ class JellyfinApi(context: Context, baseUrl: String) {
@Volatile
private var INSTANCE: JellyfinApi? = null
/**
* Creates or gets a new instance of [JellyfinApi]
*
* If there already is an instance, it will return that instance and ignore the [baseUrl] parameter
*
* @param context The context
* @param baseUrl The url of the server
*/
fun getInstance(context: Context, baseUrl: String): JellyfinApi {
synchronized(this) {
var instance = INSTANCE
@ -38,6 +54,12 @@ class JellyfinApi(context: Context, baseUrl: String) {
}
}
/**
* Create a new [JellyfinApi] instance
*
* @param context The context
* @param baseUrl The url of the server
*/
fun newInstance(context: Context, baseUrl: String): JellyfinApi {
synchronized(this) {
val instance = JellyfinApi(context.applicationContext, baseUrl)

View file

@ -18,13 +18,17 @@ class AddServerViewModel(val application: Application) : ViewModel() {
private val database = ServerDatabase.getInstance(application).serverDatabaseDao
private val _navigateToLogin = MutableLiveData<Boolean>()
val navigateToLogin: LiveData<Boolean>
get() = _navigateToLogin
val navigateToLogin: LiveData<Boolean> = _navigateToLogin
private val _error = MutableLiveData<String>()
val error: LiveData<String>
get() = _error
val error: LiveData<String> = _error
/**
* Run multiple check on the server before continuing:
*
* - Connect to server and check if it is a Jellyfin server
* - Check if server is not already in Database
*/
fun checkServer(baseUrl: String) {
_error.value = null
@ -49,6 +53,12 @@ class AddServerViewModel(val application: Application) : ViewModel() {
}
}
/**
* Check if server is already in database using server ID
*
* @param id Server ID
* @return True if server is already in database
*/
private suspend fun serverAlreadyInDatabase(id: String?): Boolean {
val servers: List<Server>
withContext(Dispatchers.IO) {

View file

@ -16,24 +16,38 @@ import org.jellyfin.sdk.model.api.AuthenticateUserByName
import java.lang.Exception
class LoginViewModel(application: Application) : ViewModel() {
// BaseUrl can be empty string because we want to get the existing instance.
private val jellyfinApi = JellyfinApi.getInstance(application, "")
private val database = ServerDatabase.getInstance(application).serverDatabaseDao
private val _error = MutableLiveData<String>()
val error: LiveData<String>
get() = _error
val error: LiveData<String> = _error
/**
* Send a authentication request to the Jellyfin server
*
* @param username Username
* @param password Password
*/
fun login(username: String, password: String) {
viewModelScope.launch {
try {
val authenticationResult by jellyfinApi.userApi.authenticateUserByName(
data = AuthenticateUserByName(
username = username,
pw = password)
pw = password
)
)
_error.value = null
val serverInfo by jellyfinApi.systemApi.getPublicSystemInfo()
val server = Server(serverInfo.id!!, serverInfo.serverName!!, jellyfinApi.api.baseUrl!!, authenticationResult.user?.id.toString(), authenticationResult.user?.name!!, authenticationResult.accessToken!!)
val server = Server(
serverInfo.id!!,
serverInfo.serverName!!,
jellyfinApi.api.baseUrl!!,
authenticationResult.user?.id.toString(),
authenticationResult.user?.name!!,
authenticationResult.accessToken!!
)
insert(server)
} catch (e: Exception) {
Log.e("LoginViewModel", "${e.message}")
@ -42,6 +56,11 @@ class LoginViewModel(application: Application) : ViewModel() {
}
}
/**
* Add server to the database
*
* @param server The server
*/
private suspend fun insert(server: Server) {
withContext(Dispatchers.IO) {
database.insert(server)

View file

@ -15,18 +15,18 @@ class ServerSelectViewModel(
val application: Application,
) : ViewModel() {
private val _servers = database.getAllServers()
val servers: LiveData<List<Server>>
get() = _servers
val servers: LiveData<List<Server>> = _servers
/**
* Delete server from database
*
* @param server The server
*/
fun deleteServer(server: Server) {
viewModelScope.launch {
delete(server)
}
}
private suspend fun delete(server: Server) {
withContext(Dispatchers.IO) {
database.delete(server.id)
}
}
}
}