Add AddServerViewModel and checkServer

This commit is contained in:
Jarne Demeulemeester 2021-06-09 21:58:13 +02:00
parent 1171e66076
commit 9953165415
No known key found for this signature in database
GPG key ID: 60884A0C1EBA43E5
4 changed files with 59 additions and 5 deletions

View file

@ -5,22 +5,29 @@ import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.findNavController
import dev.jdtech.jellyfin.R
import dev.jdtech.jellyfin.databinding.FragmentAddServerBinding
import dev.jdtech.jellyfin.viewmodels.AddServerViewModel
import dev.jdtech.jellyfin.viewmodels.AddServerViewModelFactory
class AddServerFragment : Fragment() {
private var _binding: FragmentAddServerBinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentAddServerBinding.inflate(inflater, container, false)
val application = requireNotNull(this.activity).application
val binding = FragmentAddServerBinding.inflate(inflater)
val viewModelFactory = AddServerViewModelFactory(application)
val viewModel = ViewModelProvider(this, viewModelFactory).get(AddServerViewModel::class.java)
binding.lifecycleOwner = this
binding.viewModel = viewModel
binding.buttonConnect.setOnClickListener { v: View ->
v.findNavController().navigate(R.id.action_addServerFragment_to_loginFragment)
viewModel.checkServer(binding.editTextServerAddress.text.toString())
//v.findNavController().navigate(R.id.action_addServerFragment_to_loginFragment)
}
return binding.root

View file

@ -0,0 +1,23 @@
package dev.jdtech.jellyfin.viewmodels
import android.app.Application
import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dev.jdtech.jellyfin.api.JellyfinApi
import kotlinx.coroutines.launch
import org.jellyfin.sdk.api.client.exception.ApiClientException
import java.lang.Exception
class AddServerViewModel(val application: Application) : ViewModel() {
fun checkServer(baseUrl: String) {
viewModelScope.launch {
val jellyfinApi = JellyfinApi.newInstance(application, baseUrl)
try {
jellyfinApi.systemApi.getPublicSystemInfo()
} catch (e: Exception) {
Log.e("JellyfinApi", "${e.message}")
}
}
}
}

View file

@ -0,0 +1,18 @@
package dev.jdtech.jellyfin.viewmodels
import android.app.Application
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import java.lang.IllegalArgumentException
class AddServerViewModelFactory(
private val application: Application
) : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(AddServerViewModel::class.java)) {
@Suppress("UNCHECKED_CAST")
return AddServerViewModel(application) as T
}
throw IllegalArgumentException("Unknown ViewModel class")
}
}

View file

@ -4,6 +4,12 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="viewModel"
type="dev.jdtech.jellyfin.viewmodels.AddServerViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"