Merge pull request #4019 from k9mail/settings_list_fragment

Extract settings list code to 'SettingsListFragment'
This commit is contained in:
cketti 2019-04-14 15:57:18 +02:00 committed by GitHub
commit da49739c5e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 122 additions and 97 deletions

View file

@ -3,90 +3,22 @@ package com.fsck.k9.ui.settings
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import android.view.MenuItem
import com.fsck.k9.Account
import com.fsck.k9.activity.K9Activity
import com.fsck.k9.activity.setup.WelcomeMessage
import com.fsck.k9.ui.R
import com.fsck.k9.ui.observeNotNull
import com.fsck.k9.ui.settings.account.AccountSettingsActivity
import com.xwray.groupie.GroupAdapter
import com.xwray.groupie.Item
import com.xwray.groupie.Section
import com.xwray.groupie.kotlinandroidextensions.ViewHolder
import kotlinx.android.synthetic.main.activity_settings.*
import org.koin.android.architecture.ext.viewModel
class SettingsActivity : K9Activity() {
private val viewModel: SettingsViewModel by viewModel()
private lateinit var settingsAdapter: GroupAdapter<ViewHolder>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setLayout(R.layout.activity_settings)
initializeActionBar()
initializeSettingsList()
populateSettingsList()
}
private fun initializeActionBar() {
supportActionBar!!.setDisplayHomeAsUpEnabled(true)
}
private fun initializeSettingsList() {
settingsAdapter = GroupAdapter()
settingsAdapter.setOnItemClickListener { item, _ ->
handleItemClick(item)
}
with(settings_list) {
adapter = settingsAdapter
layoutManager = LinearLayoutManager(this@SettingsActivity)
}
}
private fun populateSettingsList() {
viewModel.accounts.observeNotNull(this) { accounts ->
if (accounts.isEmpty()) {
launchWelcomeScreen()
} else {
populateSettingsList(accounts)
}
}
}
private fun populateSettingsList(accounts: List<Account>) {
settingsAdapter.clear()
val miscSection = Section().apply {
val item = SettingsActionItem(getString(R.string.about_action), SettingsAction.ABOUT_SCREEN, R.attr.iconSettingsAbout)
add(item)
}
settingsAdapter.add(miscSection)
val generalSection = Section().apply {
val item = SettingsActionItem(getString(R.string.general_settings_title),
SettingsAction.GENERAL_SETTINGS, R.attr.iconSettingsGeneral)
add(item)
}
settingsAdapter.add(generalSection)
val accountSection = Section().apply {
for (account in accounts) {
add(AccountItem(account))
}
val item = SettingsActionItem(getString(R.string.add_account_action), SettingsAction.ADD_ACCOUNT, R.attr.iconSettingsAccountAdd)
add(item)
}
accountSection.setHeader(SettingsDividerItem(getString(R.string.accounts_title)))
settingsAdapter.add(accountSection)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
android.R.id.home -> {
@ -97,22 +29,6 @@ class SettingsActivity : K9Activity() {
}
}
private fun handleItemClick(item: Item<*>) {
when (item) {
is AccountItem -> launchAccountSettings(item.account)
is SettingsActionItem -> item.action.execute(this)
}
}
private fun launchAccountSettings(account: Account) {
AccountSettingsActivity.start(this, account.uuid)
}
private fun launchWelcomeScreen() {
WelcomeMessage.showWelcomeMessage(this)
finish()
}
companion object {
@JvmStatic fun launch(activity: Activity) {

View file

@ -0,0 +1,101 @@
package com.fsck.k9.ui.settings
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.fsck.k9.Account
import com.fsck.k9.activity.setup.WelcomeMessage
import com.fsck.k9.ui.R
import com.fsck.k9.ui.observeNotNull
import com.fsck.k9.ui.settings.account.AccountSettingsActivity
import com.xwray.groupie.GroupAdapter
import com.xwray.groupie.Item
import com.xwray.groupie.Section
import com.xwray.groupie.kotlinandroidextensions.ViewHolder
import kotlinx.android.synthetic.main.fragment_settings_list.*
import org.koin.android.architecture.ext.viewModel
class SettingsListFragment : Fragment() {
private val viewModel: SettingsViewModel by viewModel()
private lateinit var settingsAdapter: GroupAdapter<ViewHolder>
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_settings_list, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
initializeSettingsList()
populateSettingsList()
}
private fun initializeSettingsList() {
settingsAdapter = GroupAdapter()
settingsAdapter.setOnItemClickListener { item, _ ->
handleItemClick(item)
}
with(settings_list) {
adapter = settingsAdapter
layoutManager = LinearLayoutManager(context)
}
}
private fun populateSettingsList() {
viewModel.accounts.observeNotNull(this) { accounts ->
if (accounts.isEmpty()) {
launchWelcomeScreen()
} else {
populateSettingsList(accounts)
}
}
}
private fun populateSettingsList(accounts: List<Account>) {
settingsAdapter.clear()
val miscSection = Section().apply {
val item = SettingsActionItem(getString(R.string.about_action), SettingsAction.ABOUT_SCREEN, R.attr.iconSettingsAbout)
add(item)
}
settingsAdapter.add(miscSection)
val generalSection = Section().apply {
val item = SettingsActionItem(getString(R.string.general_settings_title),
SettingsAction.GENERAL_SETTINGS, R.attr.iconSettingsGeneral)
add(item)
}
settingsAdapter.add(generalSection)
val accountSection = Section().apply {
for (account in accounts) {
add(AccountItem(account))
}
val item = SettingsActionItem(getString(R.string.add_account_action), SettingsAction.ADD_ACCOUNT, R.attr.iconSettingsAccountAdd)
add(item)
}
accountSection.setHeader(SettingsDividerItem(getString(R.string.accounts_title)))
settingsAdapter.add(accountSection)
}
private fun handleItemClick(item: Item<*>) {
when (item) {
is AccountItem -> launchAccountSettings(item.account)
is SettingsActionItem -> item.action.execute(requireActivity())
}
}
private fun launchAccountSettings(account: Account) {
AccountSettingsActivity.start(requireActivity(), account.uuid)
}
private fun launchWelcomeScreen() {
val activity = requireActivity()
WelcomeMessage.showWelcomeMessage(activity)
activity.finish()
}
}

View file

@ -1,19 +1,19 @@
<?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:orientation="vertical">
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:orientation="vertical">
<include layout="@layout/toolbar" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/settings_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:scrollbars="vertical"
tools:context="com.fsck.k9.ui.settings.SettingsActivity"/>
<fragment
android:id="@+id/settings_list_fragment"
android:name="com.fsck.k9.ui.settings.SettingsListFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
tools:layout="@layout/fragment_settings_list" />
</LinearLayout>

View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/settings_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:scrollbars="vertical" />