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.api.operations.UserApi
import org.jellyfin.sdk.model.ClientInfo 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) { class JellyfinApi(context: Context, baseUrl: String) {
val jellyfin = Jellyfin { val jellyfin = Jellyfin {
clientInfo = clientInfo =
@ -27,6 +35,14 @@ class JellyfinApi(context: Context, baseUrl: String) {
@Volatile @Volatile
private var INSTANCE: JellyfinApi? = null 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 { fun getInstance(context: Context, baseUrl: String): JellyfinApi {
synchronized(this) { synchronized(this) {
var instance = INSTANCE 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 { fun newInstance(context: Context, baseUrl: String): JellyfinApi {
synchronized(this) { synchronized(this) {
val instance = JellyfinApi(context.applicationContext, baseUrl) 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 database = ServerDatabase.getInstance(application).serverDatabaseDao
private val _navigateToLogin = MutableLiveData<Boolean>() private val _navigateToLogin = MutableLiveData<Boolean>()
val navigateToLogin: LiveData<Boolean> val navigateToLogin: LiveData<Boolean> = _navigateToLogin
get() = _navigateToLogin
private val _error = MutableLiveData<String>() private val _error = MutableLiveData<String>()
val error: LiveData<String> val error: LiveData<String> = _error
get() = _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) { fun checkServer(baseUrl: String) {
_error.value = null _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 { private suspend fun serverAlreadyInDatabase(id: String?): Boolean {
val servers: List<Server> val servers: List<Server>
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {

View file

@ -16,24 +16,38 @@ import org.jellyfin.sdk.model.api.AuthenticateUserByName
import java.lang.Exception import java.lang.Exception
class LoginViewModel(application: Application) : ViewModel() { 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 jellyfinApi = JellyfinApi.getInstance(application, "")
private val database = ServerDatabase.getInstance(application).serverDatabaseDao private val database = ServerDatabase.getInstance(application).serverDatabaseDao
private val _error = MutableLiveData<String>() private val _error = MutableLiveData<String>()
val error: LiveData<String> val error: LiveData<String> = _error
get() = _error
/**
* Send a authentication request to the Jellyfin server
*
* @param username Username
* @param password Password
*/
fun login(username: String, password: String) { fun login(username: String, password: String) {
viewModelScope.launch { viewModelScope.launch {
try { try {
val authenticationResult by jellyfinApi.userApi.authenticateUserByName( val authenticationResult by jellyfinApi.userApi.authenticateUserByName(
data = AuthenticateUserByName( data = AuthenticateUserByName(
username = username, username = username,
pw = password) pw = password
)
) )
_error.value = null _error.value = null
val serverInfo by jellyfinApi.systemApi.getPublicSystemInfo() 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) insert(server)
} catch (e: Exception) { } catch (e: Exception) {
Log.e("LoginViewModel", "${e.message}") 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) { private suspend fun insert(server: Server) {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
database.insert(server) database.insert(server)

View file

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