MutedUntil setting in Settings dialog

This commit is contained in:
Philipp Heckel 2022-01-01 13:42:00 +01:00
parent 1c6dd84543
commit 1ce42048b5
5 changed files with 64 additions and 11 deletions

View file

@ -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")

View file

@ -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()
}
}

View file

@ -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)

View file

@ -16,8 +16,7 @@
<string name="channel_subscriber_notification_text">You are subscribed to instant delivery topics</string>
<string name="channel_subscriber_notification_text_one">You are subscribed to one instant delivery topic</string>
<string name="channel_subscriber_notification_text_two">You are subscribed to two instant delivery topics</string>
<string name="channel_subscriber_notification_text_three">You are subscribed to three instant delivery topics
</string>
<string name="channel_subscriber_notification_text_three">You are subscribed to three instant delivery topics</string>
<string name="channel_subscriber_notification_text_four">You are subscribed to four instant delivery topics</string>
<string name="channel_subscriber_notification_text_more">You are subscribed to %1$d instant delivery topics</string>
@ -147,19 +146,26 @@
<!-- Settings -->
<string name="settings_title">Settings</string>
<string name="settings_notifications_header">Notifications</string>
<string name="settings_notifications_header_summary">General settings for all subscribed topics</string>
<string name="settings_notifications_muted_until_title">Pause notifications</string>
<string name="settings_notifications_muted_until_enabled">All notifications will be displayed</string>
<string name="settings_notifications_muted_until_disabled_forever">Notifications muted until re-enabled</string>
<string name="settings_notifications_muted_until_disabled_until">Notifications muted until %1$s</string>
<string name="settings_unified_push_header">UnifiedPush</string>
<string name="settings_unified_push_header_summary">Allows other apps to use ntfy as a message distributor. Find out more at unifiedpush.org.</string>
<string name="settings_unified_push_enabled_title">Enabled</string>
<string name="settings_unified_push_enabled_title">Enable distributor</string>
<string name="settings_unified_push_enabled_summary_on">Apps can use ntfy as distributor</string>
<string name="settings_unified_push_enabled_summary_off">Apps cannot use ntfy as distributor</string>
<string name="settings_unified_push_base_url_title">Server URL</string>
<string name="settings_unified_push_base_url_default_summary">%1$s (default)</string>
<string name="settings_about_header">About</string>
<string name="settings_about_version">Version</string>
<string name="settings_about_version_title">Version</string>
<string name="settings_about_version_format">ntfy %1$s (%2$s)</string>
<string name="settings_about_version_copied_to_clipboard_message">Copied to clipboard</string>
<!-- Preferences IDs -->
<string name="pref_notifications_muted_until">MutedUntil</string>
<string name="pref_unified_push_enabled">UnifiedPushEnabled</string>
<string name="pref_unified_push_base_url">UnifiedPushBaseURL</string>
<string name="pref_version">Version</string>

View file

@ -1,5 +1,14 @@
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
app:title="@string/settings_title">
<PreferenceCategory
app:title="@string/settings_notifications_header"
app:summary="@string/settings_notifications_header_summary"
app:layout="@layout/preference_category_material_edited">
<Preference
app:key="@string/pref_notifications_muted_until"
app:title="@string/settings_notifications_muted_until_title"/>
</PreferenceCategory>
<PreferenceCategory
app:title="@string/settings_unified_push_header"
app:summary="@string/settings_unified_push_header_summary"
@ -16,6 +25,6 @@
<PreferenceCategory app:title="@string/settings_about_header">
<Preference
app:key="@string/pref_version"
app:title="@string/settings_about_version"/>
app:title="@string/settings_about_version_title"/>
</PreferenceCategory>
</PreferenceScreen>