Fix a few more issues (and maintain horizontal scroll position!)

This commit is contained in:
Luna712 2023-11-04 16:08:33 -06:00
parent 842ea97415
commit 6ec8634836
3 changed files with 28 additions and 10 deletions

View file

@ -14,6 +14,7 @@ import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.core.widget.doOnTextChanged
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.lagradost.cloudstream3.AcraApplication.Companion.removeKeys
@ -320,6 +321,12 @@ object AccountHelper {
accountEditCallback = { viewModel.handleAccountUpdate(it, activity) },
accountDeleteCallback = { viewModel.handleAccountUpdate(activity) }
)
activity.observe(viewModel.selectedKeyIndex) { selectedKeyIndex ->
// Scroll to current account (which is focused by default)
val layoutManager = recyclerView.layoutManager as LinearLayoutManager
layoutManager.scrollToPositionWithOffset(selectedKeyIndex, 0)
}
}
}
}

View file

@ -116,12 +116,12 @@ class AccountSelectActivity : AppCompatActivity() {
viewModel.toggleIsEditing()
}
}
if (isTvSettings()) {
recyclerView.spanCount = if (accounts.count() <= 6) {
accounts.count()
} else 6
if (isTvSettings()) {
recyclerView.spanCount = if (liveAccounts.count() + 1 <= 6) {
liveAccounts.count() + 1
} else 6
}
}
}

View file

@ -4,13 +4,17 @@ import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.lagradost.cloudstream3.AcraApplication.Companion.context
import com.lagradost.cloudstream3.ui.account.AccountHelper.showPinInputDialog
import com.lagradost.cloudstream3.utils.DataStoreHelper
import com.lagradost.cloudstream3.utils.DataStoreHelper.getAccounts
import com.lagradost.cloudstream3.utils.DataStoreHelper.setAccount
class AccountViewModel : ViewModel() {
private val _accounts: MutableLiveData<List<DataStoreHelper.Account>> = MutableLiveData(DataStoreHelper.accounts.toList())
private val _accounts: MutableLiveData<List<DataStoreHelper.Account>> = MutableLiveData(
context?.let { getAccounts(it) } ?: DataStoreHelper.accounts.toList()
)
val accounts: LiveData<List<DataStoreHelper.Account>> = _accounts
private val _isEditing = MutableLiveData(false)
@ -19,9 +23,8 @@ class AccountViewModel : ViewModel() {
private val _isAllowedLogin = MutableLiveData(false)
val isAllowedLogin: LiveData<Boolean> = _isAllowedLogin
fun handleAccountUpdate(context: Context) {
_accounts.postValue(getAccounts(context))
}
private val _selectedKeyIndex = MutableLiveData(0)
val selectedKeyIndex: LiveData<Int> = _selectedKeyIndex
fun setIsEditing(value: Boolean) {
_isEditing.postValue(value)
@ -31,11 +34,16 @@ class AccountViewModel : ViewModel() {
_isEditing.postValue(!(_isEditing.value ?: false))
}
fun handleAccountUpdate(context: Context) {
_accounts.postValue(getAccounts(context))
_selectedKeyIndex.postValue(DataStoreHelper.selectedKeyIndex)
}
fun handleAccountUpdate(
account: DataStoreHelper.Account,
context: Context
) {
val currentAccounts = DataStoreHelper.accounts.toMutableList()
val currentAccounts = getAccounts(context).toMutableList()
val overrideIndex = currentAccounts.indexOfFirst { it.keyIndex == account.keyIndex }
@ -51,6 +59,7 @@ class AccountViewModel : ViewModel() {
DataStoreHelper.accounts = currentAccounts.toTypedArray()
_accounts.postValue(getAccounts(context))
_selectedKeyIndex.postValue(account.keyIndex)
}
fun handleAccountSelect(
@ -68,11 +77,13 @@ class AccountViewModel : ViewModel() {
if (pin == null) return@showPinInputDialog
// Pin is correct, proceed
_isAllowedLogin.postValue(true)
_selectedKeyIndex.postValue(account.keyIndex)
setAccount(account, true)
}
} else {
// No PIN set for the selected account, proceed
_isAllowedLogin.postValue(true)
_selectedKeyIndex.postValue(account.keyIndex)
setAccount(account, true)
}
}