diff --git a/Mac/AppDefaults.swift b/Mac/AppDefaults.swift index 3720ba64d..fd5ee3e85 100644 --- a/Mac/AppDefaults.swift +++ b/Mac/AppDefaults.swift @@ -15,17 +15,21 @@ enum FontSize: Int { case veryLarge = 3 } -/// What the status bar’s count text shows. Clicking the text switches between them. +/// What the status bar’s count text shows. Clicking the text cycles through them. enum StatusBarCountMode: Int { case unreadCount = 0 case foldersAndFeeds = 1 + case lastRefresh = 2 - var toggled: StatusBarCountMode { + /// The mode after this one when clicking. Skips the unread count when unread counts are hidden. + func next(includingUnreadCount: Bool) -> StatusBarCountMode { switch self { + case .lastRefresh: + includingUnreadCount ? .unreadCount : .foldersAndFeeds case .unreadCount: .foldersAndFeeds case .foldersAndFeeds: - .unreadCount + .lastRefresh } } } @@ -336,7 +340,7 @@ final class AppDefaults: Sendable { var statusBarCountMode: StatusBarCountMode { get { - StatusBarCountMode(rawValue: AppDefaults.int(for: Key.statusBarCountMode)) ?? .unreadCount + StatusBarCountMode(rawValue: AppDefaults.int(for: Key.statusBarCountMode)) ?? .lastRefresh } set { AppDefaults.setInt(for: Key.statusBarCountMode, newValue.rawValue) @@ -403,6 +407,7 @@ final class AppDefaults: Sendable { Key.useColumnLayout: false, Key.showStatusBar: Self.showStatusBarDefault, Key.showStatusBarThemeMenu: true, + Key.statusBarCountMode: StatusBarCountMode.lastRefresh.rawValue, "NSScrollViewShouldScrollUnderTitlebar": false, Key.refreshInterval: RefreshInterval.every2Hours.rawValue, Key.showDebugMenu: showDebugMenu, diff --git a/Mac/MainWindow/StatusBar/MainWindowStatusBarView.swift b/Mac/MainWindow/StatusBar/MainWindowStatusBarView.swift index c9ab1eb50..17b92cb17 100644 --- a/Mac/MainWindow/StatusBar/MainWindowStatusBarView.swift +++ b/Mac/MainWindow/StatusBar/MainWindowStatusBarView.swift @@ -214,6 +214,19 @@ final class MainWindowStatusBarView: NSView { CoalescingQueue.standard.add(self, #selector(updateCountLabel)) } + // The last refresh time shows the date only when it’s not today, so it changes at midnight. + @objc func handleCalendarDayChanged(_ notification: Notification) { + updateCountLabel() + } + + @objc func handleCurrentLocaleDidChange(_ notification: Notification) { + updateCountLabel() + } + + @objc func handleSystemTimeZoneDidChange(_ notification: Notification) { + updateCountLabel() + } + @objc func handleArticleThemeNamesDidChange(_ notification: Notification) { updateThemePopUpButton() } @@ -239,11 +252,7 @@ final class MainWindowStatusBarView: NSView { } @objc func toggleCountMode(_ sender: Any?) { - // With unread counts hidden, the folders and feeds count is the only mode. - guard Self.showsUnreadCounts else { - return - } - AppDefaults.shared.statusBarCountMode = AppDefaults.shared.statusBarCountMode.toggled + AppDefaults.shared.statusBarCountMode = effectiveCountMode.next(includingUnreadCount: Self.showsUnreadCounts) } } @@ -344,6 +353,9 @@ private extension MainWindowStatusBarView { NotificationCenter.default.addObserver(self, selector: #selector(handleAccountChildrenDidChange(_:)), name: .UserDidDeleteAccount, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(handleAccountChildrenDidChange(_:)), name: .AccountStateDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(handleProgressInfoDidChange(_:)), name: .progressInfoDidChange, object: CombinedRefreshProgress.shared) + NotificationCenter.default.addObserver(self, selector: #selector(handleCalendarDayChanged(_:)), name: .NSCalendarDayChanged, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(handleCurrentLocaleDidChange(_:)), name: NSLocale.currentLocaleDidChangeNotification, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(handleSystemTimeZoneDidChange(_:)), name: .NSSystemTimeZoneDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(handleArticleThemeNamesDidChange(_:)), name: .ArticleThemeNamesDidChangeNotification, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(handleCurrentArticleThemeDidChange(_:)), name: .CurrentArticleThemeDidChangeNotification, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(handleUserDefaultsDidChange(_:)), name: UserDefaults.didChangeNotification, object: nil) @@ -409,13 +421,24 @@ private extension MainWindowStatusBarView { AppDefaults.shared.unreadCountDisplay == .count } - /// The saved mode, except that hidden unread counts (hidden or a dot) mean folders and feeds. + /// The saved mode, except that the unread count becomes the last refresh time when unread counts are hidden (hidden or a dot). var effectiveCountMode: StatusBarCountMode { - Self.showsUnreadCounts ? AppDefaults.shared.statusBarCountMode : .foldersAndFeeds + let mode = AppDefaults.shared.statusBarCountMode + if mode == .unreadCount && !Self.showsUnreadCounts { + return .lastRefresh + } + return mode } func countText() -> String { switch effectiveCountMode { + case .lastRefresh: + guard let lastRefreshDate = AccountManager.shared.lastRefreshCompletedDate else { + return NSLocalizedString("Not updated yet", comment: "Status bar: no refresh has completed yet") + } + let formatString = NSLocalizedString("Updated %@", comment: "Updated") + return String(format: formatString, Self.lastRefreshDateString(lastRefreshDate)) + case .unreadCount: let formatString = NSLocalizedString("%d unread", comment: "Unread") return String.localizedStringWithFormat(formatString, AccountManager.shared.unreadCount) @@ -429,6 +452,14 @@ private extension MainWindowStatusBarView { } } + /// Just the time when it’s today, otherwise the date and time — in the user’s locale. + static func lastRefreshDateString(_ date: Date) -> String { + if Calendar.current.isDateInToday(date) { + return date.formatted(date: .omitted, time: .shortened) + } + return date.formatted(date: .abbreviated, time: .shortened) + } + // MARK: - Link /// The link’s contextual menu. Copy copies the whole link, with its scheme.