🚸 Improve settings structure
This commit is contained in:
parent
eee1be873a
commit
61e579c0d4
@ -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,21 +215,23 @@ 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(
|
||||||
messageRepo.count(),
|
SimpleJob<Plaintext>(
|
||||||
{ messageRepo.findNextLegacyMessages(it) },
|
messageRepo.count(),
|
||||||
{ msg ->
|
{ messageRepo.findNextLegacyMessages(it) },
|
||||||
if (msg.encoding == Plaintext.Encoding.SIMPLE) {
|
{ msg ->
|
||||||
conversationService.getSubject(listOf(msg))?.let { subject ->
|
if (msg.encoding == Plaintext.Encoding.SIMPLE) {
|
||||||
msg.conversationId = UUID.nameUUIDFromBytes(subject.toByteArray())
|
conversationService.getSubject(listOf(msg))?.let { subject ->
|
||||||
messageRepo.save(msg)
|
msg.conversationId = UUID.nameUUIDFromBytes(subject.toByteArray())
|
||||||
Thread.yield()
|
messageRepo.save(msg)
|
||||||
|
Thread.yield()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
},
|
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?) =
|
||||||
conversationInit?.isEnabled = newValue as Boolean
|
Preference.OnPreferenceChangeListener { _, newValue ->
|
||||||
true
|
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 {
|
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
|
||||||
|
@ -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 & 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>
|
||||||
|
@ -1,42 +1,95 @@
|
|||||||
<?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.support.v7.preference.SwitchPreferenceCompat
|
android:persistent="false">
|
||||||
android:defaultValue="true"
|
|
||||||
android:key="emulate_conversations"
|
<android.support.v7.preference.SwitchPreferenceCompat
|
||||||
android:summary="@string/emulate_conversations_summary"
|
android:defaultValue="true"
|
||||||
android:title="@string/emulate_conversations" />
|
android:key="emulate_conversations"
|
||||||
<android.support.v7.preference.Preference
|
android:summary="@string/emulate_conversations_summary"
|
||||||
android:defaultValue="true"
|
android:title="@string/emulate_conversations" />
|
||||||
android:key="emulate_conversations_initialize"
|
<android.support.v7.preference.Preference
|
||||||
android:summary="@string/emulate_conversations_summary"
|
android:defaultValue="true"
|
||||||
android:title="@string/emulate_conversations_initialize" />
|
android:key="emulate_conversations_initialize"
|
||||||
<android.support.v7.preference.SwitchPreferenceCompat
|
android:summary="@string/emulate_conversations_summary"
|
||||||
android:defaultValue="true"
|
android:title="@string/emulate_conversations_initialize" />
|
||||||
android:key="request_acknowledgements"
|
|
||||||
android:summary="@string/request_acknowledgements_summary"
|
</android.support.v7.preference.PreferenceScreen>
|
||||||
android:title="@string/request_acknowledgements" />
|
|
||||||
<android.support.v7.preference.EditTextPreference
|
<android.support.v7.preference.PreferenceScreen
|
||||||
android:inputType="textUri"
|
android:key="preference_network_and_performance"
|
||||||
android:key="trusted_node"
|
android:title="@string/preference_group_network_and_performance"
|
||||||
android:summary="@string/trusted_node_summary"
|
android:summary="@string/preference_group_network_and_performance_summary"
|
||||||
android:title="@string/trusted_node" />
|
android:persistent="false">
|
||||||
<android.support.v7.preference.EditTextPreference
|
|
||||||
android:defaultValue="120"
|
<android.support.v7.preference.SwitchPreferenceCompat
|
||||||
android:inputType="number"
|
android:defaultValue="true"
|
||||||
android:key="sync_timeout"
|
android:key="wifi_only"
|
||||||
android:summary="@string/sync_timeout_summary"
|
android:summary="@string/wifi_only_summary"
|
||||||
android:title="@string/sync_timeout" />
|
android:title="@string/wifi_only" />
|
||||||
<android.support.v7.preference.SwitchPreferenceCompat
|
<android.support.v7.preference.SwitchPreferenceCompat
|
||||||
android:defaultValue="false"
|
android:defaultValue="true"
|
||||||
android:dependency="trusted_node"
|
android:key="request_acknowledgements"
|
||||||
android:key="server_pow"
|
android:summary="@string/request_acknowledgements_summary"
|
||||||
android:summary="@string/server_pow_summary"
|
android:title="@string/request_acknowledgements" />
|
||||||
android:title="@string/server_pow" />
|
|
||||||
|
</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.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>
|
||||||
|
Loading…
Reference in New Issue
Block a user