Navigate to login fragment when server is found
This commit is contained in:
parent
9953165415
commit
e6313378d5
3 changed files with 53 additions and 8 deletions
|
@ -6,7 +6,7 @@ import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import androidx.lifecycle.ViewModelProvider
|
import androidx.lifecycle.ViewModelProvider
|
||||||
import androidx.navigation.findNavController
|
import androidx.navigation.fragment.findNavController
|
||||||
import dev.jdtech.jellyfin.R
|
import dev.jdtech.jellyfin.R
|
||||||
import dev.jdtech.jellyfin.databinding.FragmentAddServerBinding
|
import dev.jdtech.jellyfin.databinding.FragmentAddServerBinding
|
||||||
import dev.jdtech.jellyfin.viewmodels.AddServerViewModel
|
import dev.jdtech.jellyfin.viewmodels.AddServerViewModel
|
||||||
|
@ -20,16 +20,30 @@ class AddServerFragment : Fragment() {
|
||||||
val application = requireNotNull(this.activity).application
|
val application = requireNotNull(this.activity).application
|
||||||
val binding = FragmentAddServerBinding.inflate(inflater)
|
val binding = FragmentAddServerBinding.inflate(inflater)
|
||||||
val viewModelFactory = AddServerViewModelFactory(application)
|
val viewModelFactory = AddServerViewModelFactory(application)
|
||||||
val viewModel = ViewModelProvider(this, viewModelFactory).get(AddServerViewModel::class.java)
|
val viewModel =
|
||||||
|
ViewModelProvider(this, viewModelFactory).get(AddServerViewModel::class.java)
|
||||||
|
|
||||||
binding.lifecycleOwner = this
|
binding.lifecycleOwner = this
|
||||||
binding.viewModel = viewModel
|
binding.viewModel = viewModel
|
||||||
|
|
||||||
binding.buttonConnect.setOnClickListener { v: View ->
|
binding.buttonConnect.setOnClickListener { v: View ->
|
||||||
viewModel.checkServer(binding.editTextServerAddress.text.toString())
|
val serverAddress = binding.editTextServerAddress.text.toString()
|
||||||
//v.findNavController().navigate(R.id.action_addServerFragment_to_loginFragment)
|
if (serverAddress.isNotBlank()) {
|
||||||
|
viewModel.checkServer(serverAddress)
|
||||||
|
binding.progressCircular.visibility = View.VISIBLE
|
||||||
|
} else {
|
||||||
|
binding.editTextServerAddress.error = "Empty server address"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
viewModel.navigateToLogin.observe(viewLifecycleOwner, {
|
||||||
|
if (it) {
|
||||||
|
this.findNavController().navigate(R.id.action_addServerFragment_to_loginFragment)
|
||||||
|
viewModel.onNavigateToLoginDone()
|
||||||
|
}
|
||||||
|
binding.progressCircular.visibility = View.GONE
|
||||||
|
})
|
||||||
|
|
||||||
return binding.root
|
return binding.root
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -2,6 +2,8 @@ package dev.jdtech.jellyfin.viewmodels
|
||||||
|
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
|
import androidx.lifecycle.LiveData
|
||||||
|
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
|
||||||
|
@ -10,14 +12,24 @@ import org.jellyfin.sdk.api.client.exception.ApiClientException
|
||||||
import java.lang.Exception
|
import java.lang.Exception
|
||||||
|
|
||||||
class AddServerViewModel(val application: Application) : ViewModel() {
|
class AddServerViewModel(val application: Application) : ViewModel() {
|
||||||
|
private val _navigateToLogin = MutableLiveData<Boolean>()
|
||||||
|
val navigateToLogin: LiveData<Boolean>
|
||||||
|
get() = _navigateToLogin
|
||||||
|
|
||||||
fun checkServer(baseUrl: String) {
|
fun checkServer(baseUrl: String) {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
val jellyfinApi = JellyfinApi.newInstance(application, baseUrl)
|
val jellyfinApi = JellyfinApi.newInstance(application, baseUrl)
|
||||||
try {
|
try {
|
||||||
jellyfinApi.systemApi.getPublicSystemInfo()
|
jellyfinApi.systemApi.getPublicSystemInfo()
|
||||||
|
_navigateToLogin.value = true
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e("JellyfinApi", "${e.message}")
|
Log.e("JellyfinApi", "${e.message}")
|
||||||
|
_navigateToLogin.value = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun onNavigateToLoginDone() {
|
||||||
|
_navigateToLogin.value = false
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -5,6 +5,7 @@
|
||||||
xmlns:tools="http://schemas.android.com/tools">
|
xmlns:tools="http://schemas.android.com/tools">
|
||||||
|
|
||||||
<data>
|
<data>
|
||||||
|
|
||||||
<variable
|
<variable
|
||||||
name="viewModel"
|
name="viewModel"
|
||||||
type="dev.jdtech.jellyfin.viewmodels.AddServerViewModel" />
|
type="dev.jdtech.jellyfin.viewmodels.AddServerViewModel" />
|
||||||
|
@ -27,6 +28,7 @@
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toTopOf="parent" />
|
app:layout_constraintTop_toTopOf="parent" />
|
||||||
|
|
||||||
|
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
android:id="@+id/linearLayout"
|
android:id="@+id/linearLayout"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
|
@ -62,11 +64,28 @@
|
||||||
android:textAppearance="@style/text_subtitle"
|
android:textAppearance="@style/text_subtitle"
|
||||||
android:textColorHint="@color/neutral_400" />
|
android:textColorHint="@color/neutral_400" />
|
||||||
|
|
||||||
<Button
|
<RelativeLayout
|
||||||
android:id="@+id/button_connect"
|
|
||||||
style="@style/setup_button"
|
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:text="@string/button_connect" />
|
android:layout_height="wrap_content">
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/button_connect"
|
||||||
|
style="@style/setup_button"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:drawableStart="@drawable/ic_launcher_foreground"
|
||||||
|
android:text="@string/button_connect" />
|
||||||
|
|
||||||
|
<ProgressBar
|
||||||
|
android:id="@+id/progress_circular"
|
||||||
|
android:layout_width="48dp"
|
||||||
|
android:layout_height="48dp"
|
||||||
|
android:elevation="8dp"
|
||||||
|
android:indeterminateTint="@color/white"
|
||||||
|
android:padding="8dp"
|
||||||
|
android:visibility="invisible" />
|
||||||
|
|
||||||
|
</RelativeLayout>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
</layout>
|
</layout>
|
||||||
|
|
Loading…
Reference in a new issue