From a1e45c056f8f040ab5170dfd24adc2624aedcdeb Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Thu, 11 Jun 2026 09:37:28 -0700 Subject: [PATCH] Add iOS Activity Log screen. Create shared view model and use it on iOS and Mac. --- .../ActivityLogWindowController.swift | 114 +++----------- Shared/ActivityLog/ActivityLogViewModel.swift | 143 ++++++++++++++++++ Shared/Localizable.xcstrings | 12 ++ iOS/Settings/ActivityLogView.swift | 127 ++++++++++++++++ iOS/Settings/Settings.storyboard | 17 +++ iOS/Settings/SettingsViewController.swift | 3 + 6 files changed, 327 insertions(+), 89 deletions(-) create mode 100644 Shared/ActivityLog/ActivityLogViewModel.swift create mode 100644 iOS/Settings/ActivityLogView.swift diff --git a/Mac/ActivityLog/ActivityLogWindowController.swift b/Mac/ActivityLog/ActivityLogWindowController.swift index 3fc16769b..a329c98bf 100644 --- a/Mac/ActivityLog/ActivityLogWindowController.swift +++ b/Mac/ActivityLog/ActivityLogWindowController.swift @@ -174,36 +174,9 @@ private extension ActivityLogWindowController { func completionAttributedString(for activity: Activity) -> NSAttributedString { let result = NSMutableAttributedString() - - let date = activity.endDate ?? Date() - appendText("[\(DateFormatter.logTimestamp.string(from: date))] ", color: .secondaryLabelColor, to: result) - - let isFailed = activity.state == .failed - let indicator = isFailed ? "✗ " : "✓ " - appendText(indicator, color: isFailed ? .systemRed : .systemGreen, weight: .bold, to: result) - - let sourceColor = color(for: activity.owner) - appendText("\(activity.owner.displayName): ", color: sourceColor, weight: .medium, to: result) - appendText(plainDescription(for: activity), color: sourceColor, weight: .medium, to: result) - - if let detail = secondaryDetail(for: activity) { - appendText(" \(detail)", color: .secondaryLabelColor, to: result) + for segment in ActivityLogViewModel.segments(for: activity) { + appendText(segment.text, color: nsColor(for: segment.color), weight: fontWeight(for: segment.weight), to: result) } - - if activity.durationIsSignificant, let startDate = activity.startDate, let endDate = activity.endDate { - let duration = endDate.timeIntervalSince(startDate) - appendText(" (\(formattedDuration(duration)))", color: .secondaryLabelColor, to: result) - } - - // e.g. skip reason - if let message = activity.completionMessage { - appendText(" — \(message)", color: .secondaryLabelColor, to: result) - } - - if isFailed, let error = activity.error { - appendText(" — \(error.localizedDescription)", color: .systemRed, to: result) - } - appendText("\n", to: result) return result } @@ -217,69 +190,32 @@ private extension ActivityLogWindowController { result.append(NSAttributedString(string: string, attributes: attributes)) } - /// Feed-content activities show the URL as detail when the feed name is the primary text. - func secondaryDetail(for activity: Activity) -> String? { - switch activity.kind { - case .refreshFeedContent(let feedURL): - return activity.detail == nil ? nil : feedURL - default: - return activity.detail - } - } - - func formattedDuration(_ duration: TimeInterval) -> String { - let posix = Locale(identifier: "en_US_POSIX") - if duration < 10.0 { - return String(format: "%.2fs", locale: posix, duration) - } else if duration < 60.0 { - return String(format: "%.1fs", locale: posix, duration) - } else { - let minutes = Int(duration) / 60 - let seconds = Int(duration) % 60 - return "\(minutes)m \(seconds)s" - } - } - - func plainDescription(for activity: Activity) -> String { - if let simple = activity.kind.simpleDisplayName { - return simple - } - switch activity.kind { - case .refreshFeedContent(let feedURL): - let format = NSLocalizedString("Refreshing feed: %@", comment: "Activity kind — refreshing a feed; %@ is the feed name or URL") - return String(format: format, activity.detail ?? feedURL) - case .findFeed(let urlString): - let format = NSLocalizedString("Finding feed %@", comment: "Activity kind — finding a feed at %@ URL") - return String(format: format, urlString) - case .fetchFeedCandidate(let urlString): - let format = NSLocalizedString("Fetching %@", comment: "Activity kind — fetching a candidate URL during feed finding") - return String(format: format, urlString) - case .downloadFeedImage(let feedURL): - let format = NSLocalizedString("Downloading image %@", comment: "Activity kind — downloading a feed image; %@ is the URL") - return String(format: format, feedURL) - case .downloadFavicon(let faviconURL): - let format = NSLocalizedString("Downloading favicon %@", comment: "Activity kind — downloading a favicon; %@ is the URL") - return String(format: format, faviconURL) - case .downloadAvatar(let avatarURL): - let format = NSLocalizedString("Downloading avatar %@", comment: "Activity kind — downloading an author avatar; %@ is the URL") - return String(format: format, avatarURL) - case .downloadHTMLMetadata(let urlString): - let format = NSLocalizedString("Downloading metadata %@", comment: "Activity kind — downloading HTML metadata; %@ is the URL") - return String(format: format, urlString) - default: - return "" - } - } - - func color(for owner: ActivityOwner) -> NSColor { - switch owner { - case .account(let accountID, _): - guard let account = AccountManager.shared.existingAccount(accountID: accountID) else { + func nsColor(for color: ActivityLogTextColor) -> NSColor { + switch color { + case .primary: + return .labelColor + case .secondary: + return .secondaryLabelColor + case .success: + return .systemGreen + case .failure: + return .systemRed + case .account(let accountID): + guard let accountID, let account = AccountManager.shared.existingAccount(accountID: accountID) else { return .secondaryLabelColor } return account.type.logColor - case .app, .feedFinder, .feedImageDownloader, .faviconDownloader, .avatarDownloader, .htmlMetadataDownloader: - return .secondaryLabelColor + } + } + + func fontWeight(for weight: ActivityLogTextWeight) -> NSFont.Weight { + switch weight { + case .regular: + return .regular + case .medium: + return .medium + case .bold: + return .bold } } } diff --git a/Shared/ActivityLog/ActivityLogViewModel.swift b/Shared/ActivityLog/ActivityLogViewModel.swift new file mode 100644 index 000000000..f34781681 --- /dev/null +++ b/Shared/ActivityLog/ActivityLogViewModel.swift @@ -0,0 +1,143 @@ +// +// ActivityLogViewModel.swift +// NetNewsWire +// +// Created by Brent Simmons on 6/11/26. +// + +import Foundation +import ActivityLog + +/// Platform-neutral formatting for the Activity Log. Builds the styled segments and plain +/// text for one completed activity. Each platform maps the semantic colors and weights to +/// its own color and font types. + +enum ActivityLogTextColor { + case primary + case secondary + case success + case failure + case account(accountID: String?) // resolves to the account's logColor, or secondary +} + +enum ActivityLogTextWeight { + case regular + case medium + case bold +} + +struct ActivityLogTextSegment { + let text: String + let color: ActivityLogTextColor + let weight: ActivityLogTextWeight +} + +@MainActor final class ActivityLogViewModel { + + static func segments(for activity: Activity) -> [ActivityLogTextSegment] { + var result = [ActivityLogTextSegment]() + + let date = activity.endDate ?? Date() + result.append(ActivityLogTextSegment(text: "[\(DateFormatter.logTimestamp.string(from: date))] ", color: .secondary, weight: .regular)) + + let isFailed = activity.state == .failed + let indicator = isFailed ? "✗ " : "✓ " + result.append(ActivityLogTextSegment(text: indicator, color: isFailed ? .failure : .success, weight: .bold)) + + let ownerColor = ownerColor(for: activity.owner) + result.append(ActivityLogTextSegment(text: "\(activity.owner.displayName): ", color: ownerColor, weight: .medium)) + result.append(ActivityLogTextSegment(text: plainDescription(for: activity), color: ownerColor, weight: .medium)) + + if let detail = secondaryDetail(for: activity) { + result.append(ActivityLogTextSegment(text: " \(detail)", color: .secondary, weight: .regular)) + } + + if activity.durationIsSignificant, let startDate = activity.startDate, let endDate = activity.endDate { + let duration = endDate.timeIntervalSince(startDate) + result.append(ActivityLogTextSegment(text: " (\(formattedDuration(duration)))", color: .secondary, weight: .regular)) + } + + // e.g. skip reason + if let message = activity.completionMessage { + result.append(ActivityLogTextSegment(text: " — \(message)", color: .secondary, weight: .regular)) + } + + if isFailed, let error = activity.error { + result.append(ActivityLogTextSegment(text: " — \(error.localizedDescription)", color: .failure, weight: .regular)) + } + + return result + } + + static func plainText(for activity: Activity) -> String { + segments(for: activity).map { $0.text }.joined() + } +} + +// MARK: - Private + +private extension ActivityLogViewModel { + + static func ownerColor(for owner: ActivityOwner) -> ActivityLogTextColor { + switch owner { + case .account(let accountID, _): + return .account(accountID: accountID) + case .app, .feedFinder, .feedImageDownloader, .faviconDownloader, .avatarDownloader, .htmlMetadataDownloader: + return .secondary + } + } + + /// Feed-content activities show the URL as detail when the feed name is the primary text. + static func secondaryDetail(for activity: Activity) -> String? { + switch activity.kind { + case .refreshFeedContent(let feedURL): + return activity.detail == nil ? nil : feedURL + default: + return activity.detail + } + } + + static func formattedDuration(_ duration: TimeInterval) -> String { + let posix = Locale(identifier: "en_US_POSIX") + if duration < 10.0 { + return String(format: "%.2fs", locale: posix, duration) + } else if duration < 60.0 { + return String(format: "%.1fs", locale: posix, duration) + } else { + let minutes = Int(duration) / 60 + let seconds = Int(duration) % 60 + return "\(minutes)m \(seconds)s" + } + } + + static func plainDescription(for activity: Activity) -> String { + if let simple = activity.kind.simpleDisplayName { + return simple + } + switch activity.kind { + case .refreshFeedContent(let feedURL): + let format = NSLocalizedString("Refreshing feed: %@", comment: "Activity kind — refreshing a feed; %@ is the feed name or URL") + return String(format: format, activity.detail ?? feedURL) + case .findFeed(let urlString): + let format = NSLocalizedString("Finding feed %@", comment: "Activity kind — finding a feed at %@ URL") + return String(format: format, urlString) + case .fetchFeedCandidate(let urlString): + let format = NSLocalizedString("Fetching %@", comment: "Activity kind — fetching a candidate URL during feed finding") + return String(format: format, urlString) + case .downloadFeedImage(let feedURL): + let format = NSLocalizedString("Downloading image %@", comment: "Activity kind — downloading a feed image; %@ is the URL") + return String(format: format, feedURL) + case .downloadFavicon(let faviconURL): + let format = NSLocalizedString("Downloading favicon %@", comment: "Activity kind — downloading a favicon; %@ is the URL") + return String(format: format, faviconURL) + case .downloadAvatar(let avatarURL): + let format = NSLocalizedString("Downloading avatar %@", comment: "Activity kind — downloading an author avatar; %@ is the URL") + return String(format: format, avatarURL) + case .downloadHTMLMetadata(let urlString): + let format = NSLocalizedString("Downloading metadata %@", comment: "Activity kind — downloading HTML metadata; %@ is the URL") + return String(format: format, urlString) + default: + return "" + } + } +} diff --git a/Shared/Localizable.xcstrings b/Shared/Localizable.xcstrings index 7e19349d6..b1aae3367 100644 --- a/Shared/Localizable.xcstrings +++ b/Shared/Localizable.xcstrings @@ -66,6 +66,14 @@ }, "Active" : { + }, + "Activity Log" : { + "comment" : "The title of the activity log.", + "isCommentAutoGenerated" : true + }, + "Activity may contain feed URLs and other information you may not want to share publicly." : { + "comment" : "A warning that the activity log may contain sensitive information.", + "isCommentAutoGenerated" : true }, "Add Account" : { "comment" : "Add Account" @@ -697,6 +705,10 @@ "Next Unread Article" : { "comment" : "Next Unread Article" }, + "No Activity Logged" : { + "comment" : "A title for a view that indicates that there is no activity log.", + "isCommentAutoGenerated" : true + }, "No current activity." : { "comment" : "Current Activity empty state" }, diff --git a/iOS/Settings/ActivityLogView.swift b/iOS/Settings/ActivityLogView.swift new file mode 100644 index 000000000..8180feae8 --- /dev/null +++ b/iOS/Settings/ActivityLogView.swift @@ -0,0 +1,127 @@ +// +// ActivityLogView.swift +// NetNewsWire-iOS +// +// Created by Brent Simmons on 6/11/26. +// + +import SwiftUI +import Account +import ActivityLog +import RSCore + +struct ActivityLogView: View { + + @State private var entries = [Activity]() + @State private var plainText = "" + + var body: some View { + Group { + if entries.isEmpty { + ContentUnavailableView("No Activity Logged", systemImage: "checkmark.circle") + } else { + VStack(spacing: 0) { + privacyWarning + Divider() + ScrollView { + Text(buildAttributedString(entries)) + .font(.system(.body, design: .monospaced)) + .textSelection(.enabled) + .frame(maxWidth: .infinity, alignment: .leading) + .padding() + } + } + } + } + .navigationTitle("Activity Log") + .toolbar { + ToolbarItem(placement: .topBarTrailing) { + Button("Copy Contents") { + UIPasteboard.general.string = plainText + } + .disabled(entries.isEmpty) + } + } + .task { + reload() + } + .onReceive(NotificationCenter.default.publisher(for: .activityDidChange)) { _ in + reload() + } + } + + private var privacyWarning: some View { + Text("Activity may contain feed URLs and other information you may not want to share publicly.") + .font(.footnote) + .foregroundStyle(.secondary) + .padding() + } +} + +// MARK: - Private + +private extension ActivityLogView { + + func reload() { + entries = ActivityLog.shared.completedActivities + plainText = buildPlainText(entries) + } + + func buildAttributedString(_ entries: [Activity]) -> AttributedString { + var result = AttributedString() + for entry in entries { + result.append(attributedString(for: entry)) + result.append(AttributedString("\n\n")) + } + return result + } + + func attributedString(for activity: Activity) -> AttributedString { + var result = AttributedString() + for segment in ActivityLogViewModel.segments(for: activity) { + var piece = AttributedString(segment.text) + piece.foregroundColor = color(for: segment.color) + piece.font = .system(.body, design: .monospaced).weight(fontWeight(for: segment.weight)) + result.append(piece) + } + return result + } + + func buildPlainText(_ entries: [Activity]) -> String { + var result = "" + for entry in entries { + result += ActivityLogViewModel.plainText(for: entry) + result += "\n\n" + } + return result + } + + func color(for color: ActivityLogTextColor) -> Color { + switch color { + case .primary: + return .primary + case .secondary: + return .secondary + case .success: + return .green + case .failure: + return .red + case .account(let accountID): + guard let accountID, let account = AccountManager.shared.existingAccount(accountID: accountID) else { + return .secondary + } + return account.type.logColor + } + } + + func fontWeight(for weight: ActivityLogTextWeight) -> Font.Weight { + switch weight { + case .regular: + return .regular + case .medium: + return .medium + case .bold: + return .bold + } + } +} diff --git a/iOS/Settings/Settings.storyboard b/iOS/Settings/Settings.storyboard index dd5f31a06..74967c268 100644 --- a/iOS/Settings/Settings.storyboard +++ b/iOS/Settings/Settings.storyboard @@ -533,6 +533,23 @@ + + + + + + + + + + + diff --git a/iOS/Settings/SettingsViewController.swift b/iOS/Settings/SettingsViewController.swift index 50beeb66b..864392770 100644 --- a/iOS/Settings/SettingsViewController.swift +++ b/iOS/Settings/SettingsViewController.swift @@ -33,6 +33,7 @@ final class SettingsViewController: UITableViewController { case accountStats = 1 case dinosaurs = 2 case cloudKitZoneStats = 3 + case activityLog = 4 } private enum FeedsRow: Int { @@ -282,6 +283,8 @@ final class SettingsViewController: UITableViewController { return UIHostingController(rootView: AccountStatsView()) case .cloudKitZoneStats: return UIHostingController(rootView: CloudKitStatsView()) + case .activityLog: + return UIHostingController(rootView: ActivityLogView()) case .dinosaurs: return UIHostingController(rootView: DinosaursView(dismissAndPresent: { [weak self] dinosaur in guard let self else {