🚸 Improve settings structure

This commit is contained in:
Christian Basler 2018-04-17 19:55:56 +02:00
parent eee1be873a
commit 61e579c0d4
3 changed files with 160 additions and 80 deletions

View File

@ -21,10 +21,14 @@ import android.content.*
import android.os.Bundle import android.os.Bundle
import android.os.IBinder import android.os.IBinder
import android.preference.PreferenceManager import android.preference.PreferenceManager
import android.support.v4.app.Fragment
import android.support.v4.content.ContextCompat
import android.support.v4.content.FileProvider.getUriForFile import android.support.v4.content.FileProvider.getUriForFile
import android.support.v7.preference.Preference import android.support.v7.preference.Preference
import android.support.v7.preference.PreferenceFragmentCompat import android.support.v7.preference.PreferenceFragmentCompat
import android.support.v7.preference.PreferenceScreen
import android.support.v7.preference.SwitchPreferenceCompat import android.support.v7.preference.SwitchPreferenceCompat
import android.view.View
import android.widget.Toast import android.widget.Toast
import ch.dissem.apps.abit.service.BatchProcessorService import ch.dissem.apps.abit.service.BatchProcessorService
import ch.dissem.apps.abit.service.SimpleJob import ch.dissem.apps.abit.service.SimpleJob
@ -46,23 +50,24 @@ import java.util.*
/** /**
* @author Christian Basler * @author Christian Basler
*/ */
class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedPreferenceChangeListener { class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedPreferenceChangeListener,
PreferenceFragmentCompat.OnPreferenceStartScreenCallback {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.preferences) setPreferencesFromResource(R.xml.preferences, rootKey)
findPreference("about")?.onPreferenceClickListener = aboutClickListener() findPreference("about")?.onPreferenceClickListener = aboutClickListener()
val cleanup = findPreference("cleanup") findPreference("cleanup")?.let { it.onPreferenceClickListener = cleanupClickListener(it) }
cleanup?.onPreferenceClickListener = cleanupClickListener(cleanup)
findPreference("export")?.onPreferenceClickListener = exportClickListener() findPreference("export")?.onPreferenceClickListener = exportClickListener()
findPreference("import")?.onPreferenceClickListener = importClickListener() findPreference("import")?.onPreferenceClickListener = importClickListener()
findPreference("status").onPreferenceClickListener = statusClickListener() findPreference("status")?.onPreferenceClickListener = statusClickListener()
val conversationInit = findPreference("emulate_conversations_initialize") as? SwitchPreferenceCompat
val emulateConversations = findPreference("emulate_conversations") as? SwitchPreferenceCompat
val conversationInit = findPreference("emulate_conversations_initialize")
emulateConversations?.onPreferenceChangeListener = emulateConversationChangeListener(conversationInit)
conversationInit?.onPreferenceClickListener = conversationInitClickListener() conversationInit?.onPreferenceClickListener = conversationInitClickListener()
findPreference("emulate_conversations")?.apply { conversationInit?.isEnabled = emulateConversations?.isChecked ?: false
onPreferenceChangeListener = emulateConversationChangeListener(conversationInit)
isEnabled = conversationInit?.isChecked ?: false
}
} }
private fun aboutClickListener() = Preference.OnPreferenceClickListener { private fun aboutClickListener() = Preference.OnPreferenceClickListener {
@ -210,7 +215,8 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP
val messageRepo = Singleton.getMessageRepository(service.service) val messageRepo = Singleton.getMessageRepository(service.service)
val conversationService = Singleton.getConversationService(service.service) val conversationService = Singleton.getConversationService(service.service)
service.process(SimpleJob<Plaintext>( service.process(
SimpleJob<Plaintext>(
messageRepo.count(), messageRepo.count(),
{ messageRepo.findNextLegacyMessages(it) }, { messageRepo.findNextLegacyMessages(it) },
{ msg -> { msg ->
@ -224,7 +230,8 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP
}, },
R.drawable.ic_notification_batch, R.drawable.ic_notification_batch,
R.string.emulate_conversations_batch R.string.emulate_conversations_batch
)) )
)
} }
} }
@ -239,11 +246,39 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP
true true
} }
private fun emulateConversationChangeListener(conversationInit: Preference?) = Preference.OnPreferenceChangeListener { _, newValue -> private fun emulateConversationChangeListener(conversationInit: Preference?) =
Preference.OnPreferenceChangeListener { _, newValue ->
conversationInit?.isEnabled = newValue as Boolean conversationInit?.isEnabled = newValue as Boolean
true true
} }
// The why-is-it-so-damn-hard-to-group-preferences section
override fun getCallbackFragment(): Fragment = this
override fun onPreferenceStartScreen(
preferenceFragmentCompat: PreferenceFragmentCompat,
preferenceScreen: PreferenceScreen
): Boolean {
fragmentManager?.beginTransaction()?.let { ft ->
val fragment = SettingsFragment()
fragment.arguments = Bundle().apply {
putString(PreferenceFragmentCompat.ARG_PREFERENCE_ROOT, preferenceScreen.key)
}
ft.add(R.id.item_list, fragment, preferenceScreen.key)
ft.addToBackStack(preferenceScreen.key)
ft.commit()
}
return true
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
context?.let { ctx -> view.setBackgroundColor(ContextCompat.getColor(ctx, R.color.contentBackground)) }
}
// End of the why-is-it-so-damn-hard-to-group-preferences section
// Afterthought: here it looks so simple: https://developer.android.com/guide/topics/ui/settings.html
// Remind me, why do we need to use PreferenceFragmentCompat?
companion object { companion object {
const val WRITE_EXPORT_REQUEST_CODE = 1 const val WRITE_EXPORT_REQUEST_CODE = 1
const val READ_IMPORT_REQUEST_CODE = 2 const val READ_IMPORT_REQUEST_CODE = 2

View File

@ -141,4 +141,12 @@ As an alternative you could configure a trusted node in the settings, but as of
<string name="emulate_conversations_summary">Use subject to determine which messages belong together. The order will likely be wrong.</string> <string name="emulate_conversations_summary">Use subject to determine which messages belong together. The order will likely be wrong.</string>
<string name="emulate_conversations_initialize">Group existing messages by subject</string> <string name="emulate_conversations_initialize">Group existing messages by subject</string>
<string name="emulate_conversations_batch">Grouping existing messages by subject</string> <string name="emulate_conversations_batch">Grouping existing messages by subject</string>
<string name="preference_group_user_experience">Behaviour</string>
<string name="preference_group_user_experience_summary">Change how messages are displayed</string>
<string name="preference_group_network_and_performance">Network &amp; Performance</string>
<string name="preference_group_network_and_performance_summary">Tweak network usage and protocol details</string>
<string name="preference_group_advanced">Advanced</string>
<string name="preference_group_advanced_summary"></string>
<string name="preference_group_experimental">Experimental</string>
<string name="preference_group_experimental_summary">Only change if you know what you\'re doing</string>
</resources> </resources>

View File

@ -1,10 +1,12 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <android.support.v7.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.preference.SwitchPreferenceCompat
android:defaultValue="true" <android.support.v7.preference.PreferenceScreen
android:key="wifi_only" android:key="preference_ux"
android:summary="@string/wifi_only_summary" android:title="@string/preference_group_user_experience"
android:title="@string/wifi_only" /> android:summary="@string/preference_group_user_experience_summary"
android:persistent="false">
<android.support.v7.preference.SwitchPreferenceCompat <android.support.v7.preference.SwitchPreferenceCompat
android:defaultValue="true" android:defaultValue="true"
android:key="emulate_conversations" android:key="emulate_conversations"
@ -15,11 +17,53 @@
android:key="emulate_conversations_initialize" android:key="emulate_conversations_initialize"
android:summary="@string/emulate_conversations_summary" android:summary="@string/emulate_conversations_summary"
android:title="@string/emulate_conversations_initialize" /> android:title="@string/emulate_conversations_initialize" />
</android.support.v7.preference.PreferenceScreen>
<android.support.v7.preference.PreferenceScreen
android:key="preference_network_and_performance"
android:title="@string/preference_group_network_and_performance"
android:summary="@string/preference_group_network_and_performance_summary"
android:persistent="false">
<android.support.v7.preference.SwitchPreferenceCompat
android:defaultValue="true"
android:key="wifi_only"
android:summary="@string/wifi_only_summary"
android:title="@string/wifi_only" />
<android.support.v7.preference.SwitchPreferenceCompat <android.support.v7.preference.SwitchPreferenceCompat
android:defaultValue="true" android:defaultValue="true"
android:key="request_acknowledgements" android:key="request_acknowledgements"
android:summary="@string/request_acknowledgements_summary" android:summary="@string/request_acknowledgements_summary"
android:title="@string/request_acknowledgements" /> android:title="@string/request_acknowledgements" />
</android.support.v7.preference.PreferenceScreen>
<android.support.v7.preference.PreferenceScreen
android:key="preference_advanced"
android:title="@string/preference_group_advanced"
android:summary="@string/preference_group_advanced_summary"
android:persistent="false">
<android.support.v7.preference.Preference
android:key="cleanup"
android:summary="@string/cleanup_summary"
android:title="@string/cleanup" />
<android.support.v7.preference.Preference
android:key="export"
android:summary="@string/export_data_summary"
android:title="@string/export_data" />
<android.support.v7.preference.Preference
android:key="import"
android:summary="@string/import_data_summary"
android:title="@string/import_data" />
<android.support.v7.preference.PreferenceScreen
android:key="preference_experimental"
android:title="@string/preference_group_experimental"
android:summary="@string/preference_group_experimental_summary"
android:persistent="false">
<android.support.v7.preference.EditTextPreference <android.support.v7.preference.EditTextPreference
android:inputType="textUri" android:inputType="textUri"
android:key="trusted_node" android:key="trusted_node"
@ -37,6 +81,15 @@
android:key="server_pow" android:key="server_pow"
android:summary="@string/server_pow_summary" android:summary="@string/server_pow_summary"
android:title="@string/server_pow" /> android:title="@string/server_pow" />
<android.support.v7.preference.Preference
android:key="status"
android:summary="@string/status_summary"
android:title="@string/status" />
</android.support.v7.preference.PreferenceScreen>
</android.support.v7.preference.PreferenceScreen>
<android.support.v7.preference.Preference <android.support.v7.preference.Preference
android:key="about" android:key="about"
android:summary="@string/about_summary" android:summary="@string/about_summary"
@ -49,20 +102,4 @@
android:action="android.intent.action.VIEW" android:action="android.intent.action.VIEW"
android:data="@string/help_out_link" /> android:data="@string/help_out_link" />
</android.support.v7.preference.Preference> </android.support.v7.preference.Preference>
<android.support.v7.preference.Preference
android:key="cleanup"
android:summary="@string/cleanup_summary"
android:title="@string/cleanup" />
<android.support.v7.preference.Preference
android:key="export"
android:summary="@string/export_data_summary"
android:title="@string/export_data" />
<android.support.v7.preference.Preference
android:key="import"
android:summary="@string/import_data_summary"
android:title="@string/import_data" />
<android.support.v7.preference.Preference
android:key="status"
android:summary="@string/status_summary"
android:title="@string/status" />
</android.support.v7.preference.PreferenceScreen> </android.support.v7.preference.PreferenceScreen>