Add iOS Activity Log screen. Create shared view model and use it on iOS and Mac.

This commit is contained in:
Brent Simmons committed 2026-06-11 09:37:28 -07:00
1 parent 7c69ab0587
commit a1e45c056f
6 files changed
+327 -89

No files matched your search

@@ -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
}
}
}
@@ -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 ""
}
}
}
+12
View File
@@ -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"
},
+127
View File
@@ -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
}
}
}
+17
View File
@@ -533,6 +533,23 @@
</subviews>
</tableViewCellContentView>
</tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" accessoryType="disclosureIndicator" indentationWidth="10" textLabel="aLg-lb-4bR" style="IBUITableViewCellStyleDefault" id="aLg-mN-7rY" customClass="VibrantTableViewCell" customModule="NetNewsWire" customModuleProvider="target">
<rect key="frame" x="20" y="1463" width="374" height="51"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="aLg-mN-7rY" id="aLg-cV-2mN">
<rect key="frame" x="0.0" y="0.0" width="343.5" height="51"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" insetsLayoutMarginsFromSafeArea="NO" text="Activity Log" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontForContentSizeCategory="YES" adjustsFontSizeToFit="NO" id="aLg-lb-4bR">
<rect key="frame" x="20" y="0.0" width="315.5" height="51"/>
<autoresizingMask key="autoresizingMask"/>
<fontDescription key="fontDescription" style="UICTFontTextStyleBody"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
</tableViewCellContentView>
</tableViewCell>
</cells>
</tableViewSection>
<tableViewSection headerTitle="Help" id="CS8-fJ-ghn">
@@ -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 {