From ab16ac880e1627bd66ec153b069f4976504269a5 Mon Sep 17 00:00:00 2001 From: williamvds Date: Sun, 6 Jan 2019 16:25:47 +0000 Subject: [PATCH 1/5] Add account selection spinner to account settings --- .../account/AccountSettingsActivity.kt | 121 +++++++++++++++++- .../main/res/layout/account_spinner_item.xml | 30 +++++ .../res/layout/activity_account_settings.xml | 12 +- 3 files changed, 156 insertions(+), 7 deletions(-) create mode 100644 app/ui/src/main/res/layout/account_spinner_item.xml diff --git a/app/ui/src/main/java/com/fsck/k9/ui/settings/account/AccountSettingsActivity.kt b/app/ui/src/main/java/com/fsck/k9/ui/settings/account/AccountSettingsActivity.kt index 4a0349d4f..6e7b543d0 100644 --- a/app/ui/src/main/java/com/fsck/k9/ui/settings/account/AccountSettingsActivity.kt +++ b/app/ui/src/main/java/com/fsck/k9/ui/settings/account/AccountSettingsActivity.kt @@ -2,27 +2,41 @@ package com.fsck.k9.ui.settings.account import android.content.Context import android.content.Intent +import android.graphics.drawable.Drawable import android.os.Bundle import android.support.v7.preference.PreferenceFragmentCompat import android.support.v7.preference.PreferenceFragmentCompat.OnPreferenceStartScreenCallback import android.support.v7.preference.PreferenceScreen +import android.util.AttributeSet +import android.view.LayoutInflater import android.view.MenuItem +import android.view.MotionEvent +import android.view.View +import android.view.ViewGroup +import android.widget.AdapterView +import android.widget.ArrayAdapter +import android.widget.Spinner +import com.fsck.k9.Account +import com.fsck.k9.Preferences import com.fsck.k9.activity.K9Activity import com.fsck.k9.ui.R import com.fsck.k9.ui.fragmentTransaction import com.fsck.k9.ui.fragmentTransactionWithBackStack import com.fsck.k9.ui.observe +import com.fsck.k9.ui.observeNotNull +import com.fsck.k9.ui.settings.SettingsViewModel +import kotlinx.android.synthetic.main.account_list_item.view.* +import kotlinx.android.synthetic.main.activity_account_settings.* +import kotlinx.android.synthetic.main.toolbar.* import org.koin.android.architecture.ext.viewModel import timber.log.Timber - -class AccountSettingsActivity : K9Activity(), OnPreferenceStartScreenCallback { - private val viewModel: AccountSettingsViewModel by viewModel() +class AccountSettingsActivity : K9Activity(), OnPreferenceStartScreenCallback, AdapterView.OnItemSelectedListener { + private val accountViewModel: AccountSettingsViewModel by viewModel() private lateinit var accountUuid: String private var startScreenKey: String? = null private var fragmentAdded = false - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setLayout(R.layout.activity_account_settings) @@ -38,9 +52,31 @@ class AccountSettingsActivity : K9Activity(), OnPreferenceStartScreenCallback { loadAccount() } + override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) { + val uuid = accountSpinner.selection.uuid + + if (uuid == accountUuid) + return; + + start(this, uuid) + finish() + } + + override fun onNothingSelected(parent: AdapterView<*>) {} + private fun initializeActionBar() { val actionBar = supportActionBar ?: throw RuntimeException("getSupportActionBar() == null") actionBar.setDisplayHomeAsUpEnabled(true) + actionBar.setDisplayShowTitleEnabled(false) + + accountSpinner.title = title + + accountSpinner.onItemSelectedListener = this + val prefs = Preferences.getPreferences(this) + val viewModel: SettingsViewModel by viewModel() + viewModel.accounts.observeNotNull(this) { + accountSpinner.loadAccounts(prefs.accounts) + } } private fun decodeArguments(): Boolean { @@ -50,14 +86,14 @@ class AccountSettingsActivity : K9Activity(), OnPreferenceStartScreenCallback { } private fun loadAccount() { - viewModel.getAccount(accountUuid).observe(this) { account -> + accountViewModel.getAccount(accountUuid).observe(this) { account -> if (account == null) { Timber.w("Account with UUID %s not found", accountUuid) finish() return@observe } - supportActionBar!!.subtitle = account.email + accountSpinner.selection = account addAccountSettingsFragment() } } @@ -88,9 +124,16 @@ class AccountSettingsActivity : K9Activity(), OnPreferenceStartScreenCallback { replace(R.id.accountSettingsContainer, AccountSettingsFragment.create(accountUuid, preferenceScreen.key)) } + accountSpinner.title = preferenceScreen.title + return true } + override fun onBackPressed() { + super.onBackPressed() + + accountSpinner.title = title + } companion object { private const val ARG_ACCOUNT_UUID = "accountUuid" @@ -114,3 +157,69 @@ class AccountSettingsActivity : K9Activity(), OnPreferenceStartScreenCallback { } } } + +class AccountSelectionSpinner : Spinner { + var selection: Account + get() = selectedItem as Account + set(value) { + val adapter = adapter as AccountsAdapter + Spinner@setSelection(adapter.getPosition(value)) + } + + var title: CharSequence = "" + set(value) { + val adapter = adapter as AccountsAdapter + adapter.title = value + adapter.notifyDataSetChanged() + } + + private lateinit var cachedBackground: Drawable + + constructor(context: Context) : super(context) + constructor(context: Context, attrs: AttributeSet) : super(context, attrs) + + init { + adapter = AccountsAdapter(context) + cachedBackground = background + } + + public fun loadAccounts(accounts: List) { + val adapter = adapter as AccountsAdapter + adapter.clear() + adapter.addAll(accounts) + + setEnabled(accounts.size > 1) + background = if (accounts.size > 1) cachedBackground else null + } + + internal class AccountsAdapter(context: Context) : ArrayAdapter(context, 0) { + var title: CharSequence = "" + + override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { + val account = getItem(position) + + val view = convertView + ?: LayoutInflater.from(context).inflate(R.layout.account_spinner_item, parent, false) + + return view.apply { + setPadding(0, paddingTop, paddingRight, paddingBottom) + name.text = AccountsAdapter@title + email.text = account.email + } + } + + override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View + { + val account = getItem(position) + + val view = convertView + ?: LayoutInflater.from(context).inflate(R.layout.account_spinner_item, parent, false) + + return view.apply { + setPadding(paddingLeft, paddingLeft / 2, paddingLeft, paddingLeft / 2) + name.text = account.description + email.text = account.email + } + } + } +} diff --git a/app/ui/src/main/res/layout/account_spinner_item.xml b/app/ui/src/main/res/layout/account_spinner_item.xml new file mode 100644 index 000000000..3280832ab --- /dev/null +++ b/app/ui/src/main/res/layout/account_spinner_item.xml @@ -0,0 +1,30 @@ + + + + + + + diff --git a/app/ui/src/main/res/layout/activity_account_settings.xml b/app/ui/src/main/res/layout/activity_account_settings.xml index 05ae8f6a3..8153df507 100644 --- a/app/ui/src/main/res/layout/activity_account_settings.xml +++ b/app/ui/src/main/res/layout/activity_account_settings.xml @@ -4,7 +4,17 @@ android:layout_height="fill_parent" android:layout_width="fill_parent"> - + + + Date: Thu, 7 Feb 2019 20:32:39 +0000 Subject: [PATCH 2/5] Move AccountSelectionSpinner into its own file --- .../account/AccountSelectionSpinner.kt | 80 +++++++++++++++++++ .../account/AccountSettingsActivity.kt | 75 ----------------- .../layout/account_spinner_dropdown_item.xml | 30 +++++++ .../main/res/layout/account_spinner_item.xml | 3 - 4 files changed, 110 insertions(+), 78 deletions(-) create mode 100644 app/ui/src/main/java/com/fsck/k9/ui/settings/account/AccountSelectionSpinner.kt create mode 100644 app/ui/src/main/res/layout/account_spinner_dropdown_item.xml diff --git a/app/ui/src/main/java/com/fsck/k9/ui/settings/account/AccountSelectionSpinner.kt b/app/ui/src/main/java/com/fsck/k9/ui/settings/account/AccountSelectionSpinner.kt new file mode 100644 index 000000000..b615d81a0 --- /dev/null +++ b/app/ui/src/main/java/com/fsck/k9/ui/settings/account/AccountSelectionSpinner.kt @@ -0,0 +1,80 @@ +package com.fsck.k9.ui.settings.account + +import android.content.Context +import android.graphics.drawable.Drawable +import android.util.AttributeSet +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.ArrayAdapter +import android.widget.Spinner + +import com.fsck.k9.Account +import com.fsck.k9.ui.R + +import kotlinx.android.synthetic.main.account_list_item.view.* + +class AccountSelectionSpinner : Spinner { + var selection: Account + get() = selectedItem as Account + set(value) { + selectedAccount = value + val adapter = adapter as AccountsAdapter + Spinner@setSelection(adapter.getPosition(value), false) + } + + private val cachedBackground: Drawable + private var selectedAccount = Account("") + + constructor(context: Context) : super(context) + constructor(context: Context, attrs: AttributeSet) : super(context, attrs) + + init { + adapter = AccountsAdapter(context) + cachedBackground = background + } + + public fun setTitle(title: CharSequence) { + val adapter = adapter as AccountsAdapter + adapter.title = title + adapter.notifyDataSetChanged() + } + + public fun setAccounts(accounts: List) { + val adapter = adapter as AccountsAdapter + adapter.clear() + adapter.addAll(accounts) + selection = selectedAccount + + setEnabled(accounts.size > 1) + background = if (accounts.size > 1) cachedBackground else null + } + + internal class AccountsAdapter(context: Context) : ArrayAdapter(context, 0) { + var title: CharSequence = "" + + override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { + val account = getItem(position) + + val view = convertView + ?: LayoutInflater.from(context).inflate(R.layout.account_spinner_item, parent, false) + + return view.apply { + name.text = AccountsAdapter@title + email.text = account.email + } + } + + override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View { + val account = getItem(position) + + val view = convertView + ?: LayoutInflater.from(context).inflate(R.layout.account_spinner_dropdown_item, parent, false) + + return view.apply { + name.text = account.description + email.text = account.email + } + } + } +} diff --git a/app/ui/src/main/java/com/fsck/k9/ui/settings/account/AccountSettingsActivity.kt b/app/ui/src/main/java/com/fsck/k9/ui/settings/account/AccountSettingsActivity.kt index 6e7b543d0..40da5ceb2 100644 --- a/app/ui/src/main/java/com/fsck/k9/ui/settings/account/AccountSettingsActivity.kt +++ b/app/ui/src/main/java/com/fsck/k9/ui/settings/account/AccountSettingsActivity.kt @@ -2,20 +2,13 @@ package com.fsck.k9.ui.settings.account import android.content.Context import android.content.Intent -import android.graphics.drawable.Drawable import android.os.Bundle import android.support.v7.preference.PreferenceFragmentCompat import android.support.v7.preference.PreferenceFragmentCompat.OnPreferenceStartScreenCallback import android.support.v7.preference.PreferenceScreen -import android.util.AttributeSet -import android.view.LayoutInflater import android.view.MenuItem -import android.view.MotionEvent import android.view.View -import android.view.ViewGroup import android.widget.AdapterView -import android.widget.ArrayAdapter -import android.widget.Spinner import com.fsck.k9.Account import com.fsck.k9.Preferences import com.fsck.k9.activity.K9Activity @@ -25,9 +18,7 @@ import com.fsck.k9.ui.fragmentTransactionWithBackStack import com.fsck.k9.ui.observe import com.fsck.k9.ui.observeNotNull import com.fsck.k9.ui.settings.SettingsViewModel -import kotlinx.android.synthetic.main.account_list_item.view.* import kotlinx.android.synthetic.main.activity_account_settings.* -import kotlinx.android.synthetic.main.toolbar.* import org.koin.android.architecture.ext.viewModel import timber.log.Timber @@ -157,69 +148,3 @@ class AccountSettingsActivity : K9Activity(), OnPreferenceStartScreenCallback, A } } } - -class AccountSelectionSpinner : Spinner { - var selection: Account - get() = selectedItem as Account - set(value) { - val adapter = adapter as AccountsAdapter - Spinner@setSelection(adapter.getPosition(value)) - } - - var title: CharSequence = "" - set(value) { - val adapter = adapter as AccountsAdapter - adapter.title = value - adapter.notifyDataSetChanged() - } - - private lateinit var cachedBackground: Drawable - - constructor(context: Context) : super(context) - constructor(context: Context, attrs: AttributeSet) : super(context, attrs) - - init { - adapter = AccountsAdapter(context) - cachedBackground = background - } - - public fun loadAccounts(accounts: List) { - val adapter = adapter as AccountsAdapter - adapter.clear() - adapter.addAll(accounts) - - setEnabled(accounts.size > 1) - background = if (accounts.size > 1) cachedBackground else null - } - - internal class AccountsAdapter(context: Context) : ArrayAdapter(context, 0) { - var title: CharSequence = "" - - override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { - val account = getItem(position) - - val view = convertView - ?: LayoutInflater.from(context).inflate(R.layout.account_spinner_item, parent, false) - - return view.apply { - setPadding(0, paddingTop, paddingRight, paddingBottom) - name.text = AccountsAdapter@title - email.text = account.email - } - } - - override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View - { - val account = getItem(position) - - val view = convertView - ?: LayoutInflater.from(context).inflate(R.layout.account_spinner_item, parent, false) - - return view.apply { - setPadding(paddingLeft, paddingLeft / 2, paddingLeft, paddingLeft / 2) - name.text = account.description - email.text = account.email - } - } - } -} diff --git a/app/ui/src/main/res/layout/account_spinner_dropdown_item.xml b/app/ui/src/main/res/layout/account_spinner_dropdown_item.xml new file mode 100644 index 000000000..ffffff719 --- /dev/null +++ b/app/ui/src/main/res/layout/account_spinner_dropdown_item.xml @@ -0,0 +1,30 @@ + + + + + + + diff --git a/app/ui/src/main/res/layout/account_spinner_item.xml b/app/ui/src/main/res/layout/account_spinner_item.xml index 3280832ab..cedc27fed 100644 --- a/app/ui/src/main/res/layout/account_spinner_item.xml +++ b/app/ui/src/main/res/layout/account_spinner_item.xml @@ -5,7 +5,6 @@ android:layout_height="match_parent" android:gravity="center_vertical" android:orientation="vertical" - android:paddingLeft="16dp" android:paddingRight="16dp"> From 0fd31b6e551e7ae3bd350172c6bead6affd59970 Mon Sep 17 00:00:00 2001 From: williamvds Date: Thu, 7 Feb 2019 20:35:23 +0000 Subject: [PATCH 3/5] Clean up adding account selection spinner --- .../account/AccountSettingsActivity.kt | 41 +++++++++---------- .../account/AccountSettingsViewModel.kt | 2 + 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/app/ui/src/main/java/com/fsck/k9/ui/settings/account/AccountSettingsActivity.kt b/app/ui/src/main/java/com/fsck/k9/ui/settings/account/AccountSettingsActivity.kt index 40da5ceb2..532875fd3 100644 --- a/app/ui/src/main/java/com/fsck/k9/ui/settings/account/AccountSettingsActivity.kt +++ b/app/ui/src/main/java/com/fsck/k9/ui/settings/account/AccountSettingsActivity.kt @@ -22,7 +22,7 @@ import kotlinx.android.synthetic.main.activity_account_settings.* import org.koin.android.architecture.ext.viewModel import timber.log.Timber -class AccountSettingsActivity : K9Activity(), OnPreferenceStartScreenCallback, AdapterView.OnItemSelectedListener { +class AccountSettingsActivity : K9Activity(), OnPreferenceStartScreenCallback { private val accountViewModel: AccountSettingsViewModel by viewModel() private lateinit var accountUuid: String private var startScreenKey: String? = null @@ -43,30 +43,26 @@ class AccountSettingsActivity : K9Activity(), OnPreferenceStartScreenCallback, A loadAccount() } - override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) { - val uuid = accountSpinner.selection.uuid - - if (uuid == accountUuid) - return; - - start(this, uuid) - finish() - } - - override fun onNothingSelected(parent: AdapterView<*>) {} - private fun initializeActionBar() { val actionBar = supportActionBar ?: throw RuntimeException("getSupportActionBar() == null") actionBar.setDisplayHomeAsUpEnabled(true) actionBar.setDisplayShowTitleEnabled(false) - accountSpinner.title = title + accountSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { + override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) { + val selectedAccountUuid = accountSpinner.selection.uuid - accountSpinner.onItemSelectedListener = this - val prefs = Preferences.getPreferences(this) - val viewModel: SettingsViewModel by viewModel() - viewModel.accounts.observeNotNull(this) { - accountSpinner.loadAccounts(prefs.accounts) + if (selectedAccountUuid == accountUuid) return + + start(this@AccountSettingsActivity, selectedAccountUuid) + finish() + } + + override fun onNothingSelected(parent: AdapterView<*>) {} + } + + accountViewModel.accounts.observeNotNull(this) { accounts -> + accountSpinner.setAccounts(accounts) } } @@ -115,15 +111,16 @@ class AccountSettingsActivity : K9Activity(), OnPreferenceStartScreenCallback, A replace(R.id.accountSettingsContainer, AccountSettingsFragment.create(accountUuid, preferenceScreen.key)) } - accountSpinner.title = preferenceScreen.title - return true } override fun onBackPressed() { super.onBackPressed() + } - accountSpinner.title = title + override fun setTitle(title: CharSequence) { + super.setTitle(title) + accountSpinner.setTitle(title) } companion object { diff --git a/app/ui/src/main/java/com/fsck/k9/ui/settings/account/AccountSettingsViewModel.kt b/app/ui/src/main/java/com/fsck/k9/ui/settings/account/AccountSettingsViewModel.kt index 2b713fc39..aff5fc83d 100644 --- a/app/ui/src/main/java/com/fsck/k9/ui/settings/account/AccountSettingsViewModel.kt +++ b/app/ui/src/main/java/com/fsck/k9/ui/settings/account/AccountSettingsViewModel.kt @@ -7,6 +7,7 @@ import com.fsck.k9.Account import com.fsck.k9.Preferences import com.fsck.k9.mailstore.FolderRepositoryManager import com.fsck.k9.mailstore.RemoteFolderInfo +import com.fsck.k9.ui.account.AccountsLiveData import kotlinx.coroutines.experimental.android.UI import kotlinx.coroutines.experimental.launch import org.jetbrains.anko.coroutines.experimental.bg @@ -15,6 +16,7 @@ class AccountSettingsViewModel( private val preferences: Preferences, private val folderRepositoryManager: FolderRepositoryManager ) : ViewModel() { + public val accounts = AccountsLiveData(preferences) private val accountLiveData = MutableLiveData() private val foldersLiveData = MutableLiveData() From 2d9a12286ba84abe3af2db9d466e31990866ae2f Mon Sep 17 00:00:00 2001 From: williamvds Date: Thu, 7 Feb 2019 20:35:39 +0000 Subject: [PATCH 4/5] Use wrap_content in account selection spinner --- app/ui/src/main/res/layout/activity_account_settings.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/ui/src/main/res/layout/activity_account_settings.xml b/app/ui/src/main/res/layout/activity_account_settings.xml index 8153df507..3ddb1f25f 100644 --- a/app/ui/src/main/res/layout/activity_account_settings.xml +++ b/app/ui/src/main/res/layout/activity_account_settings.xml @@ -12,7 +12,7 @@ android:elevation="4dp"> From 5d3fc6397b7f302c2a8ba4a127ab0c4d53e5889b Mon Sep 17 00:00:00 2001 From: cketti Date: Sat, 9 Feb 2019 21:14:23 +0100 Subject: [PATCH 5/5] Code cleanup --- .../account/AccountSelectionSpinner.kt | 33 +++++++++++-------- .../account/AccountSettingsActivity.kt | 24 ++++++-------- .../res/layout/activity_account_settings.xml | 32 +++++++++--------- 3 files changed, 46 insertions(+), 43 deletions(-) diff --git a/app/ui/src/main/java/com/fsck/k9/ui/settings/account/AccountSelectionSpinner.kt b/app/ui/src/main/java/com/fsck/k9/ui/settings/account/AccountSelectionSpinner.kt index b615d81a0..3fd9ab813 100644 --- a/app/ui/src/main/java/com/fsck/k9/ui/settings/account/AccountSelectionSpinner.kt +++ b/app/ui/src/main/java/com/fsck/k9/ui/settings/account/AccountSelectionSpinner.kt @@ -17,14 +17,16 @@ import kotlinx.android.synthetic.main.account_list_item.view.* class AccountSelectionSpinner : Spinner { var selection: Account get() = selectedItem as Account - set(value) { - selectedAccount = value + set(account) { + selectedAccount = account val adapter = adapter as AccountsAdapter - Spinner@setSelection(adapter.getPosition(value), false) + val adapterPosition = adapter.getPosition(account) + setSelection(adapterPosition, false) } private val cachedBackground: Drawable - private var selectedAccount = Account("") + private var selectedAccount: Account? = null + constructor(context: Context) : super(context) constructor(context: Context, attrs: AttributeSet) : super(context, attrs) @@ -34,33 +36,36 @@ class AccountSelectionSpinner : Spinner { cachedBackground = background } - public fun setTitle(title: CharSequence) { + fun setTitle(title: CharSequence) { val adapter = adapter as AccountsAdapter adapter.title = title adapter.notifyDataSetChanged() } - public fun setAccounts(accounts: List) { + fun setAccounts(accounts: List) { val adapter = adapter as AccountsAdapter adapter.clear() adapter.addAll(accounts) - selection = selectedAccount - setEnabled(accounts.size > 1) - background = if (accounts.size > 1) cachedBackground else null + selectedAccount?.let { selection = it } + + val showAccountSwitcher = accounts.size > 1 + isEnabled = showAccountSwitcher + background = if (showAccountSwitcher) cachedBackground else null } - internal class AccountsAdapter(context: Context) : ArrayAdapter(context, 0) { + + internal class AccountsAdapter(context: Context) : ArrayAdapter(context, 0) { var title: CharSequence = "" + override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { val account = getItem(position) - val view = convertView - ?: LayoutInflater.from(context).inflate(R.layout.account_spinner_item, parent, false) + val view = convertView ?: LayoutInflater.from(context).inflate(R.layout.account_spinner_item, parent, false) return view.apply { - name.text = AccountsAdapter@title + name.text = title email.text = account.email } } @@ -69,7 +74,7 @@ class AccountSelectionSpinner : Spinner { val account = getItem(position) val view = convertView - ?: LayoutInflater.from(context).inflate(R.layout.account_spinner_dropdown_item, parent, false) + ?: LayoutInflater.from(context).inflate(R.layout.account_spinner_dropdown_item, parent, false) return view.apply { name.text = account.description diff --git a/app/ui/src/main/java/com/fsck/k9/ui/settings/account/AccountSettingsActivity.kt b/app/ui/src/main/java/com/fsck/k9/ui/settings/account/AccountSettingsActivity.kt index 532875fd3..2dced0154 100644 --- a/app/ui/src/main/java/com/fsck/k9/ui/settings/account/AccountSettingsActivity.kt +++ b/app/ui/src/main/java/com/fsck/k9/ui/settings/account/AccountSettingsActivity.kt @@ -9,15 +9,12 @@ import android.support.v7.preference.PreferenceScreen import android.view.MenuItem import android.view.View import android.widget.AdapterView -import com.fsck.k9.Account -import com.fsck.k9.Preferences import com.fsck.k9.activity.K9Activity import com.fsck.k9.ui.R import com.fsck.k9.ui.fragmentTransaction import com.fsck.k9.ui.fragmentTransactionWithBackStack import com.fsck.k9.ui.observe import com.fsck.k9.ui.observeNotNull -import com.fsck.k9.ui.settings.SettingsViewModel import kotlinx.android.synthetic.main.activity_account_settings.* import org.koin.android.architecture.ext.viewModel import timber.log.Timber @@ -50,15 +47,10 @@ class AccountSettingsActivity : K9Activity(), OnPreferenceStartScreenCallback { accountSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) { - val selectedAccountUuid = accountSpinner.selection.uuid - - if (selectedAccountUuid == accountUuid) return - - start(this@AccountSettingsActivity, selectedAccountUuid) - finish() + onAccountSelected(selectedAccountUuid = accountSpinner.selection.uuid) } - override fun onNothingSelected(parent: AdapterView<*>) {} + override fun onNothingSelected(parent: AdapterView<*>) = Unit } accountViewModel.accounts.observeNotNull(this) { accounts -> @@ -66,6 +58,13 @@ class AccountSettingsActivity : K9Activity(), OnPreferenceStartScreenCallback { } } + private fun onAccountSelected(selectedAccountUuid: String) { + if (selectedAccountUuid != accountUuid) { + start(this, selectedAccountUuid) + finish() + } + } + private fun decodeArguments(): Boolean { accountUuid = intent.getStringExtra(ARG_ACCOUNT_UUID) ?: return false startScreenKey = intent.getStringExtra(ARG_START_SCREEN_KEY) @@ -114,15 +113,12 @@ class AccountSettingsActivity : K9Activity(), OnPreferenceStartScreenCallback { return true } - override fun onBackPressed() { - super.onBackPressed() - } - override fun setTitle(title: CharSequence) { super.setTitle(title) accountSpinner.setTitle(title) } + companion object { private const val ARG_ACCOUNT_UUID = "accountUuid" private const val ARG_START_SCREEN_KEY = "startScreen" diff --git a/app/ui/src/main/res/layout/activity_account_settings.xml b/app/ui/src/main/res/layout/activity_account_settings.xml index 3ddb1f25f..f190bfe50 100644 --- a/app/ui/src/main/res/layout/activity_account_settings.xml +++ b/app/ui/src/main/res/layout/activity_account_settings.xml @@ -1,25 +1,27 @@ + android:layout_width="fill_parent" + android:layout_height="fill_parent" + android:orientation="vertical"> + android:id="@+id/toolbar" + android:layout_width="match_parent" + android:layout_height="?attr/actionBarSize" + android:background="?attr/colorPrimary" + android:elevation="4dp"> + + android:id="@+id/accountSpinner" + android:layout_width="wrap_content" + android:layout_height="wrap_content" /> + + android:id="@+id/accountSettingsContainer" + android:layout_width="match_parent" + android:layout_height="0dip" + android:layout_weight="1" />