Maintain AccountSelectActivity scroll position as well

This commit is contained in:
Luna712 2023-11-04 16:39:33 -06:00
parent 6ec8634836
commit 28367de5c3
3 changed files with 12 additions and 5 deletions

View file

@ -308,7 +308,7 @@ object AccountHelper {
recyclerView.setLinearListLayout(isHorizontal = true)
viewModel.handleAccountUpdate(activity)
viewModel.updateAccounts(activity)
activity.observe(viewModel.accounts) { liveAccounts ->
recyclerView.adapter = AccountAdapter(
@ -319,7 +319,7 @@ object AccountHelper {
},
accountCreateCallback = { viewModel.handleAccountUpdate(it, activity) },
accountEditCallback = { viewModel.handleAccountUpdate(it, activity) },
accountDeleteCallback = { viewModel.handleAccountUpdate(activity) }
accountDeleteCallback = { viewModel.updateAccounts(activity) }
)
activity.observe(viewModel.selectedKeyIndex) { selectedKeyIndex ->

View file

@ -5,6 +5,7 @@ import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import com.lagradost.cloudstream3.CommonActivity
import com.lagradost.cloudstream3.CommonActivity.loadThemes
import com.lagradost.cloudstream3.MainActivity
@ -77,7 +78,7 @@ class AccountSelectActivity : AppCompatActivity() {
navigateToMainActivity()
}
},
accountDeleteCallback = { viewModel.handleAccountUpdate(this@AccountSelectActivity) }
accountDeleteCallback = { viewModel.updateAccounts(this@AccountSelectActivity) }
)
recyclerView.adapter = adapter
@ -88,6 +89,12 @@ class AccountSelectActivity : AppCompatActivity() {
)
}
observe(viewModel.selectedKeyIndex) { selectedKeyIndex ->
// Scroll to current account (which is focused by default)
val layoutManager = recyclerView.layoutManager as GridLayoutManager
layoutManager.scrollToPositionWithOffset(selectedKeyIndex, 0)
}
observe(viewModel.isEditing) { isEditing ->
if (isEditing) {
binding.editAccountButton.setImageResource(R.drawable.ic_baseline_close_24)

View file

@ -23,7 +23,7 @@ class AccountViewModel : ViewModel() {
private val _isAllowedLogin = MutableLiveData(false)
val isAllowedLogin: LiveData<Boolean> = _isAllowedLogin
private val _selectedKeyIndex = MutableLiveData(0)
private val _selectedKeyIndex = MutableLiveData(DataStoreHelper.selectedKeyIndex)
val selectedKeyIndex: LiveData<Int> = _selectedKeyIndex
fun setIsEditing(value: Boolean) {
@ -34,7 +34,7 @@ class AccountViewModel : ViewModel() {
_isEditing.postValue(!(_isEditing.value ?: false))
}
fun handleAccountUpdate(context: Context) {
fun updateAccounts(context: Context) {
_accounts.postValue(getAccounts(context))
_selectedKeyIndex.postValue(DataStoreHelper.selectedKeyIndex)
}