From 1ce42048b59622e7d5cff8abc38e5ee70efcd4b1 Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Sat, 1 Jan 2022 13:42:00 +0100 Subject: [PATCH] MutedUntil setting in Settings dialog --- .../java/io/heckel/ntfy/ui/MainActivity.kt | 2 +- .../io/heckel/ntfy/ui/NotificationFragment.kt | 10 +++-- .../io/heckel/ntfy/ui/SettingsActivity.kt | 38 ++++++++++++++++++- app/src/main/res/values/strings.xml | 14 +++++-- app/src/main/res/xml/main_preferences.xml | 11 +++++- 5 files changed, 64 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/io/heckel/ntfy/ui/MainActivity.kt b/app/src/main/java/io/heckel/ntfy/ui/MainActivity.kt index 8297a95..b571331 100644 --- a/app/src/main/java/io/heckel/ntfy/ui/MainActivity.kt +++ b/app/src/main/java/io/heckel/ntfy/ui/MainActivity.kt @@ -169,7 +169,7 @@ class MainActivity : AppCompatActivity(), ActionMode.Callback, AddFragment.Subsc private fun startNotificationMutedChecker() { lifecycleScope.launch(Dispatchers.IO) { - delay(1000) // Just to be sure we've initialized all the things, we wait a bit ... + delay(5000) // Just to be sure we've initialized all the things, we wait a bit ... while (isActive) { Log.d(DetailActivity.TAG, "Checking global and subscription-specific 'muted until' timestamp") diff --git a/app/src/main/java/io/heckel/ntfy/ui/NotificationFragment.kt b/app/src/main/java/io/heckel/ntfy/ui/NotificationFragment.kt index a09dd8f..a0f3bb2 100644 --- a/app/src/main/java/io/heckel/ntfy/ui/NotificationFragment.kt +++ b/app/src/main/java/io/heckel/ntfy/ui/NotificationFragment.kt @@ -4,6 +4,7 @@ import android.app.AlertDialog import android.app.Dialog import android.content.Context import android.os.Bundle +import android.util.Log import android.widget.RadioButton import androidx.fragment.app.DialogFragment import androidx.lifecycle.lifecycleScope @@ -16,8 +17,9 @@ import kotlinx.coroutines.launch import java.util.* class NotificationFragment : DialogFragment() { + var settingsListener: NotificationSettingsListener? = null + private lateinit var repository: Repository - private lateinit var settingsListener: NotificationSettingsListener private lateinit var muteFor30minButton: RadioButton private lateinit var muteFor1hButton: RadioButton private lateinit var muteFor2hButton: RadioButton @@ -31,7 +33,9 @@ class NotificationFragment : DialogFragment() { override fun onAttach(context: Context) { super.onAttach(context) - settingsListener = activity as NotificationSettingsListener + if (settingsListener == null) { + settingsListener = activity as NotificationSettingsListener + } } override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { @@ -85,7 +89,7 @@ class NotificationFragment : DialogFragment() { private fun onClick(mutedUntilTimestamp: Long) { lifecycleScope.launch(Dispatchers.Main) { delay(150) // Another hack: Let the animation finish before dismissing the window - settingsListener.onNotificationMutedUntilChanged(mutedUntilTimestamp) + settingsListener?.onNotificationMutedUntilChanged(mutedUntilTimestamp) dismiss() } } diff --git a/app/src/main/java/io/heckel/ntfy/ui/SettingsActivity.kt b/app/src/main/java/io/heckel/ntfy/ui/SettingsActivity.kt index 7a3992d..c1a709d 100644 --- a/app/src/main/java/io/heckel/ntfy/ui/SettingsActivity.kt +++ b/app/src/main/java/io/heckel/ntfy/ui/SettingsActivity.kt @@ -8,12 +8,14 @@ import android.text.TextUtils import android.util.Log import android.widget.Toast import androidx.appcompat.app.AppCompatActivity +import androidx.fragment.app.FragmentManager import androidx.preference.* import androidx.preference.Preference.OnPreferenceClickListener import io.heckel.ntfy.BuildConfig import io.heckel.ntfy.R import io.heckel.ntfy.app.Application import io.heckel.ntfy.data.Repository +import io.heckel.ntfy.util.formatDateShort class SettingsActivity : AppCompatActivity() { private val repository by lazy { (application as Application).repository } @@ -27,7 +29,7 @@ class SettingsActivity : AppCompatActivity() { if (savedInstanceState == null) { supportFragmentManager .beginTransaction() - .replace(R.id.settings_layout, SettingsFragment(repository)) + .replace(R.id.settings_layout, SettingsFragment(repository, supportFragmentManager)) .commit() } @@ -38,7 +40,7 @@ class SettingsActivity : AppCompatActivity() { supportActionBar?.setDisplayHomeAsUpEnabled(true) } - class SettingsFragment(val repository: Repository) : PreferenceFragmentCompat() { + class SettingsFragment(val repository: Repository, private val supportFragmentManager: FragmentManager) : PreferenceFragmentCompat() { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { setPreferencesFromResource(R.xml.main_preferences, rootKey) @@ -46,6 +48,38 @@ class SettingsActivity : AppCompatActivity() { // preferenceDataStore is overridden to use the repository. This is convenient, because // everybody has access to the repository. + // Notifications muted until (global) + val mutedUntilPrefId = context?.getString(R.string.pref_notifications_muted_until) ?: return + val mutedUntilSummary = { s: Long -> + when (s) { + 0L -> getString(R.string.settings_notifications_muted_until_enabled) + 1L -> getString(R.string.settings_notifications_muted_until_disabled_forever) + else -> { + val formattedDate = formatDateShort(s) + getString(R.string.settings_notifications_muted_until_disabled_until, formattedDate) + } + } + } + val mutedUntil: Preference? = findPreference(mutedUntilPrefId) + mutedUntil?.preferenceDataStore = object : PreferenceDataStore() { } // Dummy store to protect from accidentally overwriting + mutedUntil?.summary = mutedUntilSummary(repository.getGlobalMutedUntil()) + mutedUntil?.onPreferenceClickListener = OnPreferenceClickListener { + if (repository.getGlobalMutedUntil() > 0) { + repository.setGlobalMutedUntil(0) + mutedUntil?.summary = mutedUntilSummary(0) + } else { + val notificationFragment = NotificationFragment() + notificationFragment.settingsListener = object : NotificationFragment.NotificationSettingsListener { + override fun onNotificationMutedUntilChanged(mutedUntilTimestamp: Long) { + repository.setGlobalMutedUntil(mutedUntilTimestamp) + mutedUntil?.summary = mutedUntilSummary(mutedUntilTimestamp) + } + } + notificationFragment.show(supportFragmentManager, NotificationFragment.TAG) + } + true + } + // UnifiedPush Enabled val upEnabledPrefId = context?.getString(R.string.pref_unified_push_enabled) ?: return val upEnabled: SwitchPreference? = findPreference(upEnabledPrefId) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d209226..4d97b73 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -16,8 +16,7 @@ You are subscribed to instant delivery topics You are subscribed to one instant delivery topic You are subscribed to two instant delivery topics - You are subscribed to three instant delivery topics - + You are subscribed to three instant delivery topics You are subscribed to four instant delivery topics You are subscribed to %1$d instant delivery topics @@ -147,19 +146,26 @@ Settings + Notifications + General settings for all subscribed topics + Pause notifications + All notifications will be displayed + Notifications muted until re-enabled + Notifications muted until %1$s UnifiedPush Allows other apps to use ntfy as a message distributor. Find out more at unifiedpush.org. - Enabled + Enable distributor Apps can use ntfy as distributor Apps cannot use ntfy as distributor Server URL %1$s (default) About - Version + Version ntfy %1$s (%2$s) Copied to clipboard + MutedUntil UnifiedPushEnabled UnifiedPushBaseURL Version diff --git a/app/src/main/res/xml/main_preferences.xml b/app/src/main/res/xml/main_preferences.xml index 30a196c..c404af4 100644 --- a/app/src/main/res/xml/main_preferences.xml +++ b/app/src/main/res/xml/main_preferences.xml @@ -1,5 +1,14 @@ + + + + app:title="@string/settings_about_version_title"/>