Extract isEnabled key as resource

This commit is contained in:
Jonathan Klee 2024-09-20 13:42:47 +02:00
parent 5684aba36d
commit 78bf83acf4
5 changed files with 11 additions and 4 deletions

View file

@ -94,9 +94,11 @@ class SubscriberService : Service() {
override fun onDestroy() { override fun onDestroy() {
Log.d(TAG, "Subscriber service has been destroyed") Log.d(TAG, "Subscriber service has been destroyed")
stopService() stopService()
if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("isEnabled", false)) { val preferenceKey = getString(R.string.eos_preference_key_is_enabled)
if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean(preferenceKey, false)) {
sendBroadcast(Intent(this, AutoRestartReceiver::class.java)) sendBroadcast(Intent(this, AutoRestartReceiver::class.java))
} }
super.onDestroy() super.onDestroy()
} }

View file

@ -6,6 +6,7 @@ import androidx.preference.PreferenceManager
import androidx.work.* import androidx.work.*
import io.heckel.ntfy.app.Application import io.heckel.ntfy.app.Application
import io.heckel.ntfy.util.Log import io.heckel.ntfy.util.Log
import io.heckel.ntfy.R
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@ -47,7 +48,8 @@ class SubscriberServiceManager(private val context: Context) {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
val app = context.applicationContext as Application val app = context.applicationContext as Application
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(app) val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(app)
val action = if (sharedPreferences.getBoolean("isEnabled", false)) { val preferenceKey = context.getString(R.string.eos_preference_key_is_enabled)
val action = if (sharedPreferences.getBoolean(preferenceKey, false)) {
SubscriberService.Action.START SubscriberService.Action.START
} else { } else {
SubscriberService.Action.STOP SubscriberService.Action.STOP

View file

@ -17,7 +17,9 @@ class PreferencesFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.settings_preferences, rootKey) setPreferencesFromResource(R.xml.settings_preferences, rootKey)
val preference: SwitchPreferenceCompat? = findPreference("isEnabled")
val preference: SwitchPreferenceCompat? =
findPreference(getString(R.string.eos_preference_key_is_enabled))
preference?.setOnPreferenceChangeListener { _, newValue -> preference?.setOnPreferenceChangeListener { _, newValue ->
val isChecked = newValue as Boolean val isChecked = newValue as Boolean

View file

@ -393,6 +393,7 @@
<string name="user_dialog_button_save">Save</string> <string name="user_dialog_button_save">Save</string>
<!-- /e/OS integration preferences --> <!-- /e/OS integration preferences -->
<string name="eos_preference_key_is_enabled" translatable="false">isEnabled</string>
<string name="eos_settings_title" translatable="false">UnifiedPush</string> <string name="eos_settings_title" translatable="false">UnifiedPush</string>
<string name="eos_settings_enable_title" translatable="true">Enable UnifiedPush distributor</string> <string name="eos_settings_enable_title" translatable="true">Enable UnifiedPush distributor</string>
<string name="eos_settings_enable_description" translatable="true">It allows 3rd party applications to receive UnifiedPush notifications</string> <string name="eos_settings_enable_description" translatable="true">It allows 3rd party applications to receive UnifiedPush notifications</string>

View file

@ -3,7 +3,7 @@
<SwitchPreferenceCompat <SwitchPreferenceCompat
app:icon="@drawable/ic_notification" app:icon="@drawable/ic_notification"
app:key="isEnabled" app:key="@string/eos_preference_key_is_enabled"
app:defaultValue="false" app:defaultValue="false"
app:title="@string/eos_settings_enable_title" app:title="@string/eos_settings_enable_title"
app:summary="@string/eos_settings_enable_description" /> app:summary="@string/eos_settings_enable_description" />