Merge pull request #3850 from williamvds/settingsRedesign

Add account selection spinner to account settings
This commit is contained in:
cketti 2019-02-09 21:39:11 +01:00 committed by GitHub
commit 00120ede97
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 196 additions and 13 deletions

View file

@ -0,0 +1,85 @@
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(account) {
selectedAccount = account
val adapter = adapter as AccountsAdapter
val adapterPosition = adapter.getPosition(account)
setSelection(adapterPosition, false)
}
private val cachedBackground: Drawable
private var selectedAccount: Account? = null
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
init {
adapter = AccountsAdapter(context)
cachedBackground = background
}
fun setTitle(title: CharSequence) {
val adapter = adapter as AccountsAdapter
adapter.title = title
adapter.notifyDataSetChanged()
}
fun setAccounts(accounts: List<Account>) {
val adapter = adapter as AccountsAdapter
adapter.clear()
adapter.addAll(accounts)
selectedAccount?.let { selection = it }
val showAccountSwitcher = accounts.size > 1
isEnabled = showAccountSwitcher
background = if (showAccountSwitcher) cachedBackground else null
}
internal class AccountsAdapter(context: Context) : ArrayAdapter<Account>(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 = 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
}
}
}
}

View file

@ -7,22 +7,24 @@ import android.support.v7.preference.PreferenceFragmentCompat
import android.support.v7.preference.PreferenceFragmentCompat.OnPreferenceStartScreenCallback
import android.support.v7.preference.PreferenceScreen
import android.view.MenuItem
import android.view.View
import android.widget.AdapterView
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 kotlinx.android.synthetic.main.activity_account_settings.*
import org.koin.android.architecture.ext.viewModel
import timber.log.Timber
class AccountSettingsActivity : K9Activity(), OnPreferenceStartScreenCallback {
private val viewModel: AccountSettingsViewModel by viewModel()
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)
@ -41,6 +43,26 @@ class AccountSettingsActivity : K9Activity(), OnPreferenceStartScreenCallback {
private fun initializeActionBar() {
val actionBar = supportActionBar ?: throw RuntimeException("getSupportActionBar() == null")
actionBar.setDisplayHomeAsUpEnabled(true)
actionBar.setDisplayShowTitleEnabled(false)
accountSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
onAccountSelected(selectedAccountUuid = accountSpinner.selection.uuid)
}
override fun onNothingSelected(parent: AdapterView<*>) = Unit
}
accountViewModel.accounts.observeNotNull(this) { accounts ->
accountSpinner.setAccounts(accounts)
}
}
private fun onAccountSelected(selectedAccountUuid: String) {
if (selectedAccountUuid != accountUuid) {
start(this, selectedAccountUuid)
finish()
}
}
private fun decodeArguments(): Boolean {
@ -50,14 +72,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()
}
}
@ -91,6 +113,11 @@ class AccountSettingsActivity : K9Activity(), OnPreferenceStartScreenCallback {
return true
}
override fun setTitle(title: CharSequence) {
super.setTitle(title)
accountSpinner.setTitle(title)
}
companion object {
private const val ARG_ACCOUNT_UUID = "accountUuid"

View file

@ -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<Account>()
private val foldersLiveData = MutableLiveData<RemoteFolderInfo>()

View file

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:paddingLeft="16dp"
android:paddingRight="16dp">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceListItem"
tools:text="Personal" />
<TextView
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceSmall"
tools:text="test@example.org" />
</LinearLayout>

View file

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical"
android:paddingRight="16dp">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceListItem"
tools:text="Personal" />
<TextView
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceSmall"
tools:text="test@example.org" />
</LinearLayout>

View file

@ -1,15 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<include layout="@layout/toolbar" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp">
<com.fsck.k9.ui.settings.account.AccountSelectionSpinner
android:id="@+id/accountSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
<FrameLayout
android:id="@+id/accountSettingsContainer"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"/>
android:id="@+id/accountSettingsContainer"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
</LinearLayout>