-
Notifications
You must be signed in to change notification settings - Fork 1
feat: upcoming event page time filter pill #252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
cdb46ee
93693b3
12f4050
11c4a77
7516aa8
d22f36f
3df7d47
3e7cabc
ac0e865
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,18 +36,23 @@ object PreferenceUtils { | |
| internal fun formatSnoozePreset(value: Long): String { | ||
| val seconds = value / 1000L | ||
|
|
||
| if (seconds % (3600L * 24) == 0L) { | ||
| val days = seconds / (3600L * 24) | ||
| if (seconds % Consts.WEEK_IN_SECONDS == 0L) { | ||
| val weeks = seconds / Consts.WEEK_IN_SECONDS | ||
| return "${weeks}w" | ||
| } | ||
|
|
||
| if (seconds % Consts.DAY_IN_SECONDS == 0L) { | ||
| val days = seconds / Consts.DAY_IN_SECONDS | ||
| return "${days}d" | ||
| } | ||
|
|
||
| if (seconds % 3600L == 0L) { | ||
| val hours = seconds / 3600L | ||
| if (seconds % Consts.HOUR_IN_SECONDS == 0L) { | ||
| val hours = seconds / Consts.HOUR_IN_SECONDS | ||
| return "${hours}h" | ||
| } | ||
|
|
||
| if (seconds % 60L == 0L) { | ||
| val minutes = seconds / 60L | ||
| if (seconds % Consts.MINUTE_IN_SECONDS == 0L) { | ||
| val minutes = seconds / Consts.MINUTE_IN_SECONDS | ||
| return "${minutes}m" | ||
| } | ||
|
|
||
|
|
@@ -70,9 +75,10 @@ object PreferenceUtils { | |
| val seconds = | ||
| when (unit) { | ||
| "s" -> num | ||
| "m" -> num * Consts.MINUTE_IN_SECONDS; | ||
| "h" -> num * Consts.HOUR_IN_SECONDS; | ||
| "d" -> num * Consts.DAY_IN_SECONDS; | ||
| "m" -> num * Consts.MINUTE_IN_SECONDS | ||
| "h" -> num * Consts.HOUR_IN_SECONDS | ||
| "d" -> num * Consts.DAY_IN_SECONDS | ||
| "w" -> num * Consts.WEEK_IN_SECONDS | ||
| else -> throw Exception("Unknown unit ${unit}") | ||
| } | ||
| seconds * 1000L | ||
|
|
@@ -108,4 +114,30 @@ object PreferenceUtils { | |
|
|
||
| fun formatPattern(pattern: LongArray): String = | ||
| pattern.map { p -> formatSnoozePreset(p) }.joinToString(", ") | ||
|
|
||
| /** | ||
| * Format a millisecond duration as a human-readable label (e.g., "8 hours", "3 days", "1 week"). | ||
| * Used for display in bottom sheets and chips. | ||
| */ | ||
| fun formatPresetHumanReadable(millis: Long): String { | ||
| val seconds = millis / 1000L | ||
|
|
||
| if (seconds % Consts.WEEK_IN_SECONDS == 0L) { | ||
| val weeks = seconds / Consts.WEEK_IN_SECONDS | ||
| return if (weeks == 1L) "1 week" else "$weeks weeks" | ||
| } | ||
| if (seconds % Consts.DAY_IN_SECONDS == 0L) { | ||
| val days = seconds / Consts.DAY_IN_SECONDS | ||
| return if (days == 1L) "1 day" else "$days days" | ||
| } | ||
| if (seconds % Consts.HOUR_IN_SECONDS == 0L) { | ||
| val hours = seconds / Consts.HOUR_IN_SECONDS | ||
| return if (hours == 1L) "1 hour" else "$hours hours" | ||
| } | ||
| if (seconds % Consts.MINUTE_IN_SECONDS == 0L) { | ||
| val minutes = seconds / Consts.MINUTE_IN_SECONDS | ||
| return if (minutes == 1L) "1 minute" else "$minutes minutes" | ||
| } | ||
| return "$seconds seconds" | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Human-readable time strings are hardcoded in EnglishLow Severity
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| // | ||
| // Calendar Notifications Plus | ||
| // Copyright (C) 2025 William Harris (wharris+cnplus@upscalews.com) | ||
| // | ||
| // This program is free software; you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation; either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License | ||
| // along with this program; if not, write to the Free Software Foundation, | ||
| // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| // | ||
|
|
||
| package com.github.quarck.calnotify.prefs | ||
|
|
||
| import android.app.AlertDialog | ||
| import android.content.Context | ||
| import android.os.Bundle | ||
| import android.util.AttributeSet | ||
| import android.view.View | ||
| import android.widget.EditText | ||
| import android.widget.TextView | ||
| import androidx.preference.DialogPreference | ||
| import androidx.preference.PreferenceDialogFragmentCompat | ||
| import com.github.quarck.calnotify.R | ||
| import com.github.quarck.calnotify.Settings | ||
|
|
||
| class UpcomingTimePresetPreferenceX @JvmOverloads constructor( | ||
| context: Context, | ||
| attrs: AttributeSet? = null, | ||
| defStyleAttr: Int = androidx.preference.R.attr.dialogPreferenceStyle, | ||
| defStyleRes: Int = 0 | ||
| ) : DialogPreference(context, attrs, defStyleAttr, defStyleRes) { | ||
|
|
||
| var presetValue: String = Settings.DEFAULT_UPCOMING_TIME_PRESETS | ||
| private set | ||
|
|
||
| init { | ||
| dialogLayoutResource = R.layout.dialog_upcoming_time_presets | ||
| positiveButtonText = context.getString(android.R.string.ok) | ||
| negativeButtonText = context.getString(android.R.string.cancel) | ||
| } | ||
|
|
||
| fun persistPreset(value: String) { | ||
| presetValue = value | ||
| persistString(value) | ||
| notifyChanged() | ||
| } | ||
|
|
||
| override fun onSetInitialValue(defaultValue: Any?) { | ||
| presetValue = getPersistedString((defaultValue as? String) ?: Settings.DEFAULT_UPCOMING_TIME_PRESETS) | ||
| } | ||
|
|
||
| override fun onGetDefaultValue(a: android.content.res.TypedArray, index: Int): Any? { | ||
| return a.getString(index) | ||
| } | ||
|
|
||
| class Dialog : PreferenceDialogFragmentCompat() { | ||
| private var edit: EditText? = null | ||
|
|
||
| override fun onBindDialogView(view: View) { | ||
| super.onBindDialogView(view) | ||
|
|
||
| val pref = preference as UpcomingTimePresetPreferenceX | ||
|
|
||
| val label = view.findViewById<TextView>(R.id.text_label_upcoming_presets) | ||
| label?.text = getString(R.string.dialog_upcoming_time_presets_label, Settings.MAX_LOOKAHEAD_DAYS.toInt(), Settings.MAX_UPCOMING_TIME_PRESETS) | ||
|
|
||
| edit = view.findViewById(R.id.edit_text_upcoming_time_presets) | ||
| edit?.setText(pref.presetValue) | ||
| } | ||
|
|
||
| override fun onDialogClosed(positiveResult: Boolean) { | ||
| if (positiveResult) { | ||
| val value = edit?.text?.toString() | ||
|
|
||
| if (value != null) { | ||
| val presets = PreferenceUtils.parseSnoozePresets(value) | ||
| if (presets != null) { | ||
| // Filter out negative values | ||
| val validPresets = presets.filter { it > 0 } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dialog validates positivity but not max lookahead constraintMedium Severity The dialog's Additional Locations (1) |
||
| val newValue = if (validPresets.isEmpty()) { | ||
| Settings.DEFAULT_UPCOMING_TIME_PRESETS | ||
| } else { | ||
| value.split(',') | ||
| .map { it.trim() } | ||
| .filter { it.isNotEmpty() } | ||
| .joinToString(", ") | ||
| } | ||
|
|
||
| val pref = preference as UpcomingTimePresetPreferenceX | ||
| if (pref.callChangeListener(newValue)) { | ||
| pref.persistPreset(newValue) | ||
| } | ||
|
|
||
| if (validPresets.size > Settings.MAX_UPCOMING_TIME_PRESETS) { | ||
| showFormattedMessage(R.string.error_too_many_upcoming_presets, Settings.MAX_UPCOMING_TIME_PRESETS) | ||
| } | ||
| } else { | ||
| showMessage(R.string.error_cannot_parse_preset) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun showMessage(id: Int) { | ||
| val context = requireContext() | ||
| AlertDialog.Builder(context) | ||
| .setMessage(context.getString(id)) | ||
| .setCancelable(false) | ||
| .setPositiveButton(android.R.string.ok) { _, _ -> } | ||
| .create() | ||
| .show() | ||
| } | ||
|
|
||
| private fun showFormattedMessage(id: Int, vararg args: Any) { | ||
| val context = requireContext() | ||
| AlertDialog.Builder(context) | ||
| .setMessage(context.getString(id, *args)) | ||
| .setCancelable(false) | ||
| .setPositiveButton(android.R.string.ok) { _, _ -> } | ||
| .create() | ||
| .show() | ||
| } | ||
|
|
||
| companion object { | ||
| fun newInstance(key: String): Dialog { | ||
| val fragment = Dialog() | ||
| val args = Bundle(1) | ||
| args.putString(ARG_KEY, key) | ||
| fragment.arguments = args | ||
| return fragment | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preset preference class duplicates existing SnoozePresetPreferenceXLow Severity
|
||


Uh oh!
There was an error while loading. Please reload this page.