🚸 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.IBinder
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.v7.preference.Preference
import android.support.v7.preference.PreferenceFragmentCompat
import android.support.v7.preference.PreferenceScreen
import android.support.v7.preference.SwitchPreferenceCompat
import android.view.View
import android.widget.Toast
import ch.dissem.apps.abit.service.BatchProcessorService
import ch.dissem.apps.abit.service.SimpleJob
@ -46,23 +50,24 @@ import java.util.*
/**
* @author Christian Basler
*/
class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedPreferenceChangeListener {
class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedPreferenceChangeListener,
PreferenceFragmentCompat.OnPreferenceStartScreenCallback {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.preferences)
setPreferencesFromResource(R.xml.preferences, rootKey)
findPreference("about")?.onPreferenceClickListener = aboutClickListener()
val cleanup = findPreference("cleanup")
cleanup?.onPreferenceClickListener = cleanupClickListener(cleanup)
findPreference("cleanup")?.let { it.onPreferenceClickListener = cleanupClickListener(it) }
findPreference("export")?.onPreferenceClickListener = exportClickListener()
findPreference("import")?.onPreferenceClickListener = importClickListener()
findPreference("status").onPreferenceClickListener = statusClickListener()
val conversationInit = findPreference("emulate_conversations_initialize") as? SwitchPreferenceCompat
findPreference("status")?.onPreferenceClickListener = statusClickListener()
val emulateConversations = findPreference("emulate_conversations") as? SwitchPreferenceCompat
val conversationInit = findPreference("emulate_conversations_initialize")
emulateConversations?.onPreferenceChangeListener = emulateConversationChangeListener(conversationInit)
conversationInit?.onPreferenceClickListener = conversationInitClickListener()
findPreference("emulate_conversations")?.apply {
onPreferenceChangeListener = emulateConversationChangeListener(conversationInit)
isEnabled = conversationInit?.isChecked ?: false
}
conversationInit?.isEnabled = emulateConversations?.isChecked ?: false
}
private fun aboutClickListener() = Preference.OnPreferenceClickListener {
@ -210,21 +215,23 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP
val messageRepo = Singleton.getMessageRepository(service.service)
val conversationService = Singleton.getConversationService(service.service)
service.process(SimpleJob<Plaintext>(
messageRepo.count(),
{ messageRepo.findNextLegacyMessages(it) },
{ msg ->
if (msg.encoding == Plaintext.Encoding.SIMPLE) {
conversationService.getSubject(listOf(msg))?.let { subject ->
msg.conversationId = UUID.nameUUIDFromBytes(subject.toByteArray())
messageRepo.save(msg)
Thread.yield()
service.process(
SimpleJob<Plaintext>(
messageRepo.count(),
{ messageRepo.findNextLegacyMessages(it) },
{ msg ->
if (msg.encoding == Plaintext.Encoding.SIMPLE) {
conversationService.getSubject(listOf(msg))?.let { subject ->
msg.conversationId = UUID.nameUUIDFromBytes(subject.toByteArray())
messageRepo.save(msg)
Thread.yield()
}
}
}
},
R.drawable.ic_notification_batch,
R.string.emulate_conversations_batch
))
},
R.drawable.ic_notification_batch,
R.string.emulate_conversations_batch
)
)
}
}
@ -239,11 +246,39 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP
true
}
private fun emulateConversationChangeListener(conversationInit: Preference?) = Preference.OnPreferenceChangeListener { _, newValue ->
conversationInit?.isEnabled = newValue as Boolean
true
private fun emulateConversationChangeListener(conversationInit: Preference?) =
Preference.OnPreferenceChangeListener { _, newValue ->
conversationInit?.isEnabled = newValue as Boolean
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 {
const val WRITE_EXPORT_REQUEST_CODE = 1
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_initialize">Group 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>

View File

@ -1,42 +1,95 @@
<?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.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:defaultValue="true"
android:key="emulate_conversations"
android:summary="@string/emulate_conversations_summary"
android:title="@string/emulate_conversations" />
<android.support.v7.preference.Preference
android:defaultValue="true"
android:key="emulate_conversations_initialize"
android:summary="@string/emulate_conversations_summary"
android:title="@string/emulate_conversations_initialize" />
<android.support.v7.preference.SwitchPreferenceCompat
android:defaultValue="true"
android:key="request_acknowledgements"
android:summary="@string/request_acknowledgements_summary"
android:title="@string/request_acknowledgements" />
<android.support.v7.preference.EditTextPreference
android:inputType="textUri"
android:key="trusted_node"
android:summary="@string/trusted_node_summary"
android:title="@string/trusted_node" />
<android.support.v7.preference.EditTextPreference
android:defaultValue="120"
android:inputType="number"
android:key="sync_timeout"
android:summary="@string/sync_timeout_summary"
android:title="@string/sync_timeout" />
<android.support.v7.preference.SwitchPreferenceCompat
android:defaultValue="false"
android:dependency="trusted_node"
android:key="server_pow"
android:summary="@string/server_pow_summary"
android:title="@string/server_pow" />
<android.support.v7.preference.PreferenceScreen
android:key="preference_ux"
android:title="@string/preference_group_user_experience"
android:summary="@string/preference_group_user_experience_summary"
android:persistent="false">
<android.support.v7.preference.SwitchPreferenceCompat
android:defaultValue="true"
android:key="emulate_conversations"
android:summary="@string/emulate_conversations_summary"
android:title="@string/emulate_conversations" />
<android.support.v7.preference.Preference
android:defaultValue="true"
android:key="emulate_conversations_initialize"
android:summary="@string/emulate_conversations_summary"
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:defaultValue="true"
android:key="request_acknowledgements"
android:summary="@string/request_acknowledgements_summary"
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:inputType="textUri"
android:key="trusted_node"
android:summary="@string/trusted_node_summary"
android:title="@string/trusted_node" />
<android.support.v7.preference.EditTextPreference
android:defaultValue="120"
android:inputType="number"
android:key="sync_timeout"
android:summary="@string/sync_timeout_summary"
android:title="@string/sync_timeout" />
<android.support.v7.preference.SwitchPreferenceCompat
android:defaultValue="false"
android:dependency="trusted_node"
android:key="server_pow"
android:summary="@string/server_pow_summary"
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:key="about"
android:summary="@string/about_summary"
@ -49,20 +102,4 @@
android:action="android.intent.action.VIEW"
android:data="@string/help_out_link" />
</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>