fix: crash when instances refreshed during instance selection (out of bounds)

This commit is contained in:
Bnyro 2025-01-08 14:28:24 +01:00
parent 317a9d14e7
commit 234f90f347
3 changed files with 7 additions and 2 deletions

View File

@ -18,6 +18,7 @@ import com.github.libretube.ui.adapters.InstancesAdapter
import com.github.libretube.ui.base.BaseActivity import com.github.libretube.ui.base.BaseActivity
import com.github.libretube.ui.models.WelcomeModel import com.github.libretube.ui.models.WelcomeModel
import com.github.libretube.ui.preferences.BackupRestoreSettings import com.github.libretube.ui.preferences.BackupRestoreSettings
import com.google.common.collect.ImmutableList
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -49,7 +50,7 @@ class WelcomeActivity : BaseActivity() {
// ALl the binding values are optional due to two different possible layouts (normal, landscape) // ALl the binding values are optional due to two different possible layouts (normal, landscape)
viewModel.instances.observe(this) { instances -> viewModel.instances.observe(this) { instances ->
binding.instancesRecycler.layoutManager = LinearLayoutManager(this@WelcomeActivity) binding.instancesRecycler.layoutManager = LinearLayoutManager(this@WelcomeActivity)
binding.instancesRecycler.adapter = InstancesAdapter(instances, viewModel.selectedInstanceIndex.value) { index -> binding.instancesRecycler.adapter = InstancesAdapter(ImmutableList.copyOf(instances), viewModel.selectedInstanceIndex.value) { index ->
viewModel.selectedInstanceIndex.value = index viewModel.selectedInstanceIndex.value = index
binding.okay.alpha = 1f binding.okay.alpha = 1f
} }

View File

@ -8,9 +8,10 @@ import com.github.libretube.R
import com.github.libretube.api.obj.PipedInstance import com.github.libretube.api.obj.PipedInstance
import com.github.libretube.databinding.InstanceRowBinding import com.github.libretube.databinding.InstanceRowBinding
import com.github.libretube.ui.viewholders.InstancesViewHolder import com.github.libretube.ui.viewholders.InstancesViewHolder
import com.google.common.collect.ImmutableList
class InstancesAdapter( class InstancesAdapter(
private val instances: List<PipedInstance>, private val instances: ImmutableList<PipedInstance>,
initialSelectionApiIndex: Int?, initialSelectionApiIndex: Int?,
private val onSelectInstance: (index: Int) -> Unit private val onSelectInstance: (index: Int) -> Unit
) : RecyclerView.Adapter<InstancesViewHolder>() { ) : RecyclerView.Adapter<InstancesViewHolder>() {

View File

@ -26,6 +26,7 @@ import com.github.libretube.ui.dialogs.DeleteAccountDialog
import com.github.libretube.ui.dialogs.LoginDialog import com.github.libretube.ui.dialogs.LoginDialog
import com.github.libretube.ui.dialogs.LogoutDialog import com.github.libretube.ui.dialogs.LogoutDialog
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.common.collect.ImmutableList
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@ -184,6 +185,8 @@ class InstanceSettings : BasePreferenceFragment() {
val layoutInflater = LayoutInflater.from(context) val layoutInflater = LayoutInflater.from(context)
val binding = SimpleOptionsRecyclerBinding.inflate(layoutInflater) val binding = SimpleOptionsRecyclerBinding.inflate(layoutInflater)
binding.optionsRecycler.layoutManager = LinearLayoutManager(context) binding.optionsRecycler.layoutManager = LinearLayoutManager(context)
val instances = ImmutableList.copyOf(this.instances)
binding.optionsRecycler.adapter = InstancesAdapter(instances, selectedIndex) { binding.optionsRecycler.adapter = InstancesAdapter(instances, selectedIndex) {
selectedInstance = instances[it].apiUrl selectedInstance = instances[it].apiUrl
} }