Display discovered servers on AddServerFragment (#143)
This commit is contained in:
parent
5d2409c93e
commit
7b85df6e69
10 changed files with 274 additions and 6 deletions
|
@ -3,6 +3,7 @@
|
|||
package="dev.jdtech.jellyfin">
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
|
||||
|
||||
<uses-feature android:name="android.software.leanback" android:required="false" />
|
||||
<uses-feature android:name="android.hardware.touchscreen" android:required="false" />
|
||||
|
|
|
@ -0,0 +1,58 @@
|
|||
package dev.jdtech.jellyfin.adapters
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import androidx.recyclerview.widget.ListAdapter
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import dev.jdtech.jellyfin.databinding.DiscoveredServerItemBinding
|
||||
import dev.jdtech.jellyfin.models.DiscoveredServer
|
||||
|
||||
class DiscoveredServerListAdapter(
|
||||
private val clickListener: (server: DiscoveredServer) -> Unit) :
|
||||
ListAdapter<DiscoveredServer, DiscoveredServerListAdapter.DiscoveredServerViewHolder>(
|
||||
DiffCallback
|
||||
) {
|
||||
class DiscoveredServerViewHolder(private var binding: DiscoveredServerItemBinding) :
|
||||
RecyclerView.ViewHolder(binding.root) {
|
||||
fun bind(server: DiscoveredServer) {
|
||||
binding.server = server
|
||||
binding.executePendingBindings()
|
||||
}
|
||||
}
|
||||
|
||||
companion object DiffCallback : DiffUtil.ItemCallback<DiscoveredServer>() {
|
||||
override fun areItemsTheSame(
|
||||
oldItem: DiscoveredServer,
|
||||
newItem: DiscoveredServer
|
||||
): Boolean {
|
||||
return oldItem.id == newItem.id
|
||||
}
|
||||
|
||||
override fun areContentsTheSame(
|
||||
oldItem: DiscoveredServer,
|
||||
newItem: DiscoveredServer
|
||||
): Boolean {
|
||||
return oldItem == newItem
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(
|
||||
parent: ViewGroup,
|
||||
viewType: Int
|
||||
): DiscoveredServerViewHolder {
|
||||
return DiscoveredServerViewHolder(
|
||||
DiscoveredServerItemBinding.inflate(
|
||||
LayoutInflater.from(parent.context),
|
||||
parent,
|
||||
false
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: DiscoveredServerViewHolder, position: Int) {
|
||||
val server = getItem(position)
|
||||
holder.itemView.setOnClickListener { clickListener(server) }
|
||||
holder.bind(server)
|
||||
}
|
||||
}
|
|
@ -17,6 +17,7 @@ import androidx.lifecycle.lifecycleScope
|
|||
import androidx.lifecycle.repeatOnLifecycle
|
||||
import androidx.navigation.fragment.findNavController
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import dev.jdtech.jellyfin.adapters.DiscoveredServerListAdapter
|
||||
import dev.jdtech.jellyfin.databinding.FragmentAddServerBinding
|
||||
import dev.jdtech.jellyfin.viewmodels.AddServerViewModel
|
||||
import kotlinx.coroutines.launch
|
||||
|
@ -51,6 +52,11 @@ class AddServerFragment : Fragment() {
|
|||
connectToServer()
|
||||
}
|
||||
|
||||
binding.serversRecyclerView.adapter = DiscoveredServerListAdapter { server ->
|
||||
(binding.editTextServerAddress as AppCompatEditText).setText(server.address)
|
||||
connectToServer()
|
||||
}
|
||||
|
||||
viewLifecycleOwner.lifecycleScope.launch {
|
||||
viewLifecycleOwner.lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
|
||||
viewModel.uiState.collect { uiState ->
|
||||
|
@ -64,6 +70,17 @@ class AddServerFragment : Fragment() {
|
|||
}
|
||||
}
|
||||
|
||||
viewLifecycleOwner.lifecycleScope.launch {
|
||||
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
|
||||
viewModel.discoveredServersState.collect { serversState ->
|
||||
when (serversState) {
|
||||
is AddServerViewModel.DiscoveredServersState.Loading -> Unit
|
||||
is AddServerViewModel.DiscoveredServersState.Servers -> bindDiscoveredServersStateServers(serversState)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
viewLifecycleOwner.lifecycleScope.launch {
|
||||
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
|
||||
viewModel.navigateToLogin.collect {
|
||||
|
@ -102,6 +119,16 @@ class AddServerFragment : Fragment() {
|
|||
}
|
||||
}
|
||||
|
||||
private fun bindDiscoveredServersStateServers(serversState: AddServerViewModel.DiscoveredServersState.Servers) {
|
||||
val servers = serversState.servers
|
||||
if (servers.isEmpty()) {
|
||||
binding.serversRecyclerView.isVisible = false
|
||||
} else {
|
||||
binding.serversRecyclerView.isVisible = true
|
||||
(binding.serversRecyclerView.adapter as DiscoveredServerListAdapter).submitList(servers)
|
||||
}
|
||||
}
|
||||
|
||||
private fun connectToServer() {
|
||||
val serverAddress = (binding.editTextServerAddress as AppCompatEditText).text.toString()
|
||||
viewModel.checkServer(serverAddress.removeSuffix("/"))
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
package dev.jdtech.jellyfin.models
|
||||
|
||||
data class DiscoveredServer(
|
||||
val id: String,
|
||||
val name: String,
|
||||
val address: String
|
||||
)
|
|
@ -10,6 +10,7 @@ import dev.jdtech.jellyfin.R
|
|||
import dev.jdtech.jellyfin.api.JellyfinApi
|
||||
import dev.jdtech.jellyfin.database.Server
|
||||
import dev.jdtech.jellyfin.database.ServerDatabaseDao
|
||||
import dev.jdtech.jellyfin.models.DiscoveredServer
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.flow.*
|
||||
import org.jellyfin.sdk.discovery.RecommendedServerInfo
|
||||
|
@ -32,7 +33,10 @@ constructor(
|
|||
val uiState = _uiState.asStateFlow()
|
||||
private val _navigateToLogin = MutableSharedFlow<Boolean>()
|
||||
val navigateToLogin = _navigateToLogin.asSharedFlow()
|
||||
private val _discoveredServersState = MutableStateFlow<DiscoveredServersState>(DiscoveredServersState.Loading)
|
||||
val discoveredServersState = _discoveredServersState.asStateFlow()
|
||||
|
||||
private val discoveredServers = mutableListOf<DiscoveredServer>()
|
||||
private var serverFound = false
|
||||
|
||||
sealed class UiState {
|
||||
|
@ -41,6 +45,27 @@ constructor(
|
|||
data class Error(val message: String) : UiState()
|
||||
}
|
||||
|
||||
sealed class DiscoveredServersState {
|
||||
object Loading : DiscoveredServersState()
|
||||
data class Servers(val servers: List<DiscoveredServer>) : DiscoveredServersState()
|
||||
}
|
||||
|
||||
init {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
val servers = jellyfinApi.jellyfin.discovery.discoverLocalServers()
|
||||
servers.collect { serverDiscoveryInfo ->
|
||||
discoveredServers.add(DiscoveredServer(
|
||||
serverDiscoveryInfo.id,
|
||||
serverDiscoveryInfo.name,
|
||||
serverDiscoveryInfo.address
|
||||
))
|
||||
_discoveredServersState.emit(
|
||||
DiscoveredServersState.Servers(discoveredServers)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Run multiple check on the server before continuing:
|
||||
*
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<layout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<data>
|
||||
|
||||
<variable
|
||||
name="server"
|
||||
type="dev.jdtech.jellyfin.models.DiscoveredServer" />
|
||||
</data>
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:layout_width="64dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginHorizontal="12dp"
|
||||
android:background="@drawable/focus_border"
|
||||
android:clickable="true"
|
||||
android:focusable="true"
|
||||
android:focusableInTouchMode="true">
|
||||
|
||||
<FrameLayout
|
||||
android:id="@+id/server_icon"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_marginBottom="8dp"
|
||||
android:background="@drawable/button_setup_background"
|
||||
app:layout_constraintDimensionRatio="w,1:1"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent">
|
||||
|
||||
<ImageView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center"
|
||||
android:importantForAccessibility="no"
|
||||
app:srcCompat="@drawable/ic_server"
|
||||
app:tint="@android:color/white" />
|
||||
|
||||
</FrameLayout>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/server_name"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="8dp"
|
||||
android:ellipsize="end"
|
||||
android:maxLines="1"
|
||||
android:text="@{server.name}"
|
||||
android:textAlignment="center"
|
||||
android:textAppearance="@style/TextAppearance.Material3.BodyMedium"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/server_icon"
|
||||
tools:text="username" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
||||
</layout>
|
|
@ -26,8 +26,6 @@
|
|||
android:id="@+id/linearLayout"
|
||||
android:layout_width="@dimen/setup_container_width"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="24dp"
|
||||
android:layout_marginEnd="24dp"
|
||||
android:orientation="vertical"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
|
@ -39,15 +37,31 @@
|
|||
android:id="@+id/text_add_server"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginHorizontal="24dp"
|
||||
android:layout_marginBottom="32dp"
|
||||
android:text="@string/add_server"
|
||||
android:textAppearance="@style/TextAppearance.MaterialComponents.Headline5"
|
||||
android:textColor="?android:textColorPrimary" />
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/servers_recycler_view"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="12dp"
|
||||
android:clipToPadding="false"
|
||||
android:orientation="horizontal"
|
||||
android:paddingHorizontal="12dp"
|
||||
android:visibility="gone"
|
||||
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
|
||||
tools:itemCount="2"
|
||||
tools:listitem="@layout/discovered_server_item"
|
||||
tools:visibility="visible" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/edit_text_server_address"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginHorizontal="24dp"
|
||||
android:layout_marginBottom="8dp"
|
||||
android:autofillHints=""
|
||||
android:focusedByDefault="true"
|
||||
|
@ -57,7 +71,8 @@
|
|||
|
||||
<RelativeLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginHorizontal="24dp">
|
||||
|
||||
<Button
|
||||
android:id="@+id/button_connect"
|
||||
|
|
59
app/src/main/res/layout/discovered_server_item.xml
Normal file
59
app/src/main/res/layout/discovered_server_item.xml
Normal file
|
@ -0,0 +1,59 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<layout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<data>
|
||||
|
||||
<variable
|
||||
name="server"
|
||||
type="dev.jdtech.jellyfin.models.DiscoveredServer" />
|
||||
</data>
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:layout_width="64dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginHorizontal="12dp"
|
||||
android:clickable="true"
|
||||
android:focusable="true"
|
||||
android:foreground="@drawable/ripple_background">
|
||||
|
||||
<FrameLayout
|
||||
android:id="@+id/server_icon"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_marginBottom="8dp"
|
||||
android:background="@drawable/button_setup_background"
|
||||
app:layout_constraintDimensionRatio="w,1:1"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent">
|
||||
|
||||
<ImageView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center"
|
||||
android:importantForAccessibility="no"
|
||||
app:srcCompat="@drawable/ic_server"
|
||||
app:tint="?attr/colorOnPrimary" />
|
||||
|
||||
</FrameLayout>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/server_name"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="8dp"
|
||||
android:maxLines="1"
|
||||
android:ellipsize="end"
|
||||
android:text="@{server.name}"
|
||||
android:textAlignment="center"
|
||||
android:textAppearance="@style/TextAppearance.Material3.BodyMedium"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/server_icon"
|
||||
tools:text="server name" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
||||
</layout>
|
|
@ -26,8 +26,6 @@
|
|||
android:id="@+id/linearLayout"
|
||||
android:layout_width="@dimen/setup_container_width"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="24dp"
|
||||
android:layout_marginEnd="24dp"
|
||||
android:orientation="vertical"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
|
@ -39,16 +37,32 @@
|
|||
android:id="@+id/text_add_server"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginHorizontal="24dp"
|
||||
android:layout_marginBottom="32dp"
|
||||
android:text="@string/add_server"
|
||||
android:textAppearance="@style/TextAppearance.Material3.HeadlineMedium"
|
||||
android:textColor="?android:textColorPrimary" />
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/servers_recycler_view"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="12dp"
|
||||
android:clipToPadding="false"
|
||||
android:orientation="horizontal"
|
||||
android:paddingHorizontal="12dp"
|
||||
android:visibility="gone"
|
||||
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
|
||||
tools:itemCount="2"
|
||||
tools:listitem="@layout/discovered_server_item"
|
||||
tools:visibility="visible" />
|
||||
|
||||
<com.google.android.material.textfield.TextInputLayout
|
||||
android:id="@+id/edit_text_server_address_layout"
|
||||
style="@style/Widget.Material3.TextInputLayout.OutlinedBox"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginHorizontal="24dp"
|
||||
android:layout_marginBottom="8dp"
|
||||
android:hint="@string/edit_text_server_address_hint"
|
||||
app:errorEnabled="true"
|
||||
|
@ -66,7 +80,8 @@
|
|||
|
||||
<RelativeLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginHorizontal="24dp">
|
||||
|
||||
<Button
|
||||
android:id="@+id/button_connect"
|
||||
|
|
|
@ -36,6 +36,7 @@
|
|||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="8dp"
|
||||
android:ellipsize="end"
|
||||
android:maxLines="1"
|
||||
android:text="@{user.name}"
|
||||
android:textAlignment="center"
|
||||
|
|
Loading…
Reference in a new issue