Implemented updating lists on changes

This commit is contained in:
dankito 2020-09-28 15:09:37 +02:00
parent b7133ba7d5
commit 95215edc7c
2 changed files with 56 additions and 8 deletions

View File

@ -25,6 +25,12 @@ open class BankSettingsDialog : SettingsDialogBase() {
protected lateinit var bank: TypedBankData protected lateinit var bank: TypedBankData
protected lateinit var bankAccountsAdapter: FastAdapterRecyclerView<DraggableBankAccountAdapterItem>
protected var banksChangedListener = { _: List<TypedBankData> ->
updateBankAccountsAdapterItems()
}
fun show(bank: TypedBankData, activity: AppCompatActivity) { fun show(bank: TypedBankData, activity: AppCompatActivity) {
@ -39,6 +45,8 @@ open class BankSettingsDialog : SettingsDialogBase() {
setupUI(rootView) setupUI(rootView)
presenter.addBanksChangedListener(banksChangedListener)
return rootView return rootView
} }
@ -52,16 +60,32 @@ open class BankSettingsDialog : SettingsDialogBase() {
edtxtUserName.text = bank.userName edtxtUserName.text = bank.userName
edtxtPassword.text = bank.password edtxtPassword.text = bank.password
val items = bank.accountsSorted.map { DraggableBankAccountAdapterItem(it) } val items = createBankAccountsAdapterItems()
val adapter = FastAdapterRecyclerView(rootView.rcyBankAccounts, items, true) bankAccountsAdapter = FastAdapterRecyclerView(rootView.rcyBankAccounts, items, true)
adapter.onClickListener = { navigationToBankAccountSettingsDialog(it.account) } bankAccountsAdapter.onClickListener = { navigationToBankAccountSettingsDialog(it.account) }
adapter.itemDropped = { oldPosition, oldItem, newPosition, newItem -> reorderedBankAccounts(oldPosition, oldItem.account, newPosition, newItem.account) } bankAccountsAdapter.itemDropped = { oldPosition, oldItem, newPosition, newItem -> reorderedBankAccounts(oldPosition, oldItem.account, newPosition, newItem.account) }
btnDeleteAccount.setOnClickListener { askUserToDeleteAccount() } btnDeleteAccount.setOnClickListener { askUserToDeleteAccount() }
} }
} }
override fun onDestroy() {
presenter.removeBanksChangedListener(banksChangedListener)
super.onDestroy()
}
protected open fun createBankAccountsAdapterItems(): List<DraggableBankAccountAdapterItem> {
return bank.accountsSorted.map { DraggableBankAccountAdapterItem(it) }
}
protected open fun updateBankAccountsAdapterItems() {
bankAccountsAdapter.setItems(createBankAccountsAdapterItems())
}
protected open fun navigationToBankAccountSettingsDialog(account: TypedBankAccount) { protected open fun navigationToBankAccountSettingsDialog(account: TypedBankAccount) {
BankAccountSettingsDialog().show(account, requireActivity() as AppCompatActivity) BankAccountSettingsDialog().show(account, requireActivity() as AppCompatActivity)
} }

View File

@ -17,6 +17,12 @@ open class SettingsDialog : SettingsDialogBase() {
} }
protected lateinit var banksAdapter: FastAdapterRecyclerView<BankDataAdapterItem>
protected var banksChangedListener = { _: List<TypedBankData> ->
updateBanksAdapterItems()
}
fun show(activity: AppCompatActivity) { fun show(activity: AppCompatActivity) {
show(activity, DialogTag) show(activity, DialogTag)
@ -28,6 +34,8 @@ open class SettingsDialog : SettingsDialogBase() {
setupUI(rootView) setupUI(rootView)
presenter.addBanksChangedListener(banksChangedListener)
return rootView return rootView
} }
@ -37,16 +45,32 @@ open class SettingsDialog : SettingsDialogBase() {
setupToolbar(this, rootView.context.getString(R.string.dialog_settings_title), false) setupToolbar(this, rootView.context.getString(R.string.dialog_settings_title), false)
} }
val items = presenter.allBanksSortedByDisplayIndex.map { BankDataAdapterItem(it) } val items = createBanksAdapterItems()
val adapter = FastAdapterRecyclerView(rootView.rcyBankCredentials, items, true) banksAdapter = FastAdapterRecyclerView(rootView.rcyBankCredentials, items, true)
adapter.onClickListener = { navigationToBankSettingsDialog(it.bank) } banksAdapter.onClickListener = { navigationToBankSettingsDialog(it.bank) }
adapter.itemDropped = { oldPosition, oldItem, newPosition, newItem -> reorderedBanks(oldPosition, oldItem.bank, newPosition, newItem.bank) } banksAdapter.itemDropped = { oldPosition, oldItem, newPosition, newItem -> reorderedBanks(oldPosition, oldItem.bank, newPosition, newItem.bank) }
rootView.btnShowSendMessageLogDialog.setOnClickListener { presenter.showSendMessageLogDialog() } rootView.btnShowSendMessageLogDialog.setOnClickListener { presenter.showSendMessageLogDialog() }
} }
} }
override fun onDestroy() {
presenter.removeBanksChangedListener(banksChangedListener)
super.onDestroy()
}
protected open fun createBanksAdapterItems(): List<BankDataAdapterItem> {
return presenter.allBanksSortedByDisplayIndex.map { BankDataAdapterItem(it) }
}
protected open fun updateBanksAdapterItems() {
banksAdapter.setItems(createBanksAdapterItems())
}
protected open fun navigationToBankSettingsDialog(bank: TypedBankData) { protected open fun navigationToBankSettingsDialog(bank: TypedBankData) {
BankSettingsDialog().show(bank, requireActivity() as AppCompatActivity) BankSettingsDialog().show(bank, requireActivity() as AppCompatActivity)
} }