From 04c763ccaf1d9d59e2713a09157310a9bf283cc3 Mon Sep 17 00:00:00 2001 From: cketti Date: Sat, 6 Apr 2019 02:24:38 +0200 Subject: [PATCH] Extract settings list code to 'SettingsListFragment' --- .../fsck/k9/ui/settings/SettingsActivity.kt | 84 --------------- .../k9/ui/settings/SettingsListFragment.kt | 101 ++++++++++++++++++ .../src/main/res/layout/activity_settings.xml | 26 ++--- .../res/layout/fragment_settings_list.xml | 8 ++ 4 files changed, 122 insertions(+), 97 deletions(-) create mode 100644 app/ui/src/main/java/com/fsck/k9/ui/settings/SettingsListFragment.kt create mode 100644 app/ui/src/main/res/layout/fragment_settings_list.xml diff --git a/app/ui/src/main/java/com/fsck/k9/ui/settings/SettingsActivity.kt b/app/ui/src/main/java/com/fsck/k9/ui/settings/SettingsActivity.kt index 157098ef6..7afc8e195 100644 --- a/app/ui/src/main/java/com/fsck/k9/ui/settings/SettingsActivity.kt +++ b/app/ui/src/main/java/com/fsck/k9/ui/settings/SettingsActivity.kt @@ -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 - - 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) { - 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) { diff --git a/app/ui/src/main/java/com/fsck/k9/ui/settings/SettingsListFragment.kt b/app/ui/src/main/java/com/fsck/k9/ui/settings/SettingsListFragment.kt new file mode 100644 index 000000000..326fc4f4c --- /dev/null +++ b/app/ui/src/main/java/com/fsck/k9/ui/settings/SettingsListFragment.kt @@ -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 + + + 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) { + 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() + } +} diff --git a/app/ui/src/main/res/layout/activity_settings.xml b/app/ui/src/main/res/layout/activity_settings.xml index 9ab6048c7..e3280b41c 100644 --- a/app/ui/src/main/res/layout/activity_settings.xml +++ b/app/ui/src/main/res/layout/activity_settings.xml @@ -1,19 +1,19 @@ + 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"> - + + diff --git a/app/ui/src/main/res/layout/fragment_settings_list.xml b/app/ui/src/main/res/layout/fragment_settings_list.xml new file mode 100644 index 000000000..9d1c643b8 --- /dev/null +++ b/app/ui/src/main/res/layout/fragment_settings_list.xml @@ -0,0 +1,8 @@ + +