Refactor CloudKitStats into separate view controllers and views.

This commit is contained in:
Brent Simmons committed 2026-03-24 10:50:54 -07:00
1 parent a81400b357
commit 06ecca3be3
10 files changed
+1060 -896

No files matched your search

@@ -0,0 +1,156 @@
//
// CloudKitStatsCleanUpContentView.swift
// NetNewsWire
//
// Created by Brent Simmons on 3/24/26.
//
import AppKit
final class CloudKitStatsCleanUpContentView: NSView {
let staleStatusDeletedLabel = NSTextField(labelWithString: "0")
let readContentDeletedLabel = NSTextField(labelWithString: "0")
let unreadContentDeletedLabel = NSTextField(labelWithString: "0")
let orphanedContentDeletedLabel = NSTextField(labelWithString: "0")
let staleStatusDeletedRow: NSView
let readContentDeletedRow: NSView
let unreadContentDeletedRow: NSView
let orphanedContentDeletedRow: NSView
let errorTextField = NSTextField(wrappingLabelWithString: "")
let refreshButton = NSButton()
let navigationButtonGroup = NSView()
let returnToPreviousResultsButton = NSButton()
let refreshScanButton = NSButton()
private let statsStackView = NSStackView()
init() {
staleStatusDeletedRow = Self.makeStatRow(NSLocalizedString("Stale Status Deleted", comment: "Cleanup stat row label"), valueLabel: staleStatusDeletedLabel)
readContentDeletedRow = Self.makeStatRow(NSLocalizedString("Read Content Deleted", comment: "Cleanup stat row label"), valueLabel: readContentDeletedLabel)
unreadContentDeletedRow = Self.makeStatRow(NSLocalizedString("Unread Content Deleted", comment: "Cleanup stat row label"), valueLabel: unreadContentDeletedLabel)
orphanedContentDeletedRow = Self.makeStatRow(NSLocalizedString("Orphaned Content Deleted", comment: "Cleanup stat row label"), valueLabel: orphanedContentDeletedLabel)
super.init(frame: .zero)
translatesAutoresizingMaskIntoConstraints = false
configureStatsStack()
configureErrorViews()
configureNavigationButtons()
addSubview(statsStackView)
addSubview(navigationButtonGroup)
let availableSpace = NSLayoutGuide()
addLayoutGuide(availableSpace)
NSLayoutConstraint.activate([
statsStackView.topAnchor.constraint(equalTo: topAnchor),
statsStackView.leadingAnchor.constraint(equalTo: leadingAnchor),
statsStackView.trailingAnchor.constraint(equalTo: trailingAnchor),
availableSpace.topAnchor.constraint(equalTo: statsStackView.bottomAnchor),
availableSpace.bottomAnchor.constraint(equalTo: bottomAnchor),
navigationButtonGroup.centerYAnchor.constraint(equalTo: availableSpace.centerYAnchor),
navigationButtonGroup.centerXAnchor.constraint(equalTo: centerXAnchor)
])
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
// MARK: - Private
private extension CloudKitStatsCleanUpContentView {
func configureStatsStack() {
statsStackView.translatesAutoresizingMaskIntoConstraints = false
statsStackView.orientation = .vertical
statsStackView.alignment = .leading
statsStackView.spacing = CloudKitStatsLayout.rowSpacing
statsStackView.addArrangedSubview(staleStatusDeletedRow)
statsStackView.addArrangedSubview(readContentDeletedRow)
statsStackView.addArrangedSubview(unreadContentDeletedRow)
statsStackView.addArrangedSubview(orphanedContentDeletedRow)
statsStackView.addArrangedSubview(errorTextField)
statsStackView.addArrangedSubview(refreshButton)
NSLayoutConstraint.activate([
staleStatusDeletedRow.widthAnchor.constraint(equalTo: statsStackView.widthAnchor),
readContentDeletedRow.widthAnchor.constraint(equalTo: statsStackView.widthAnchor),
unreadContentDeletedRow.widthAnchor.constraint(equalTo: statsStackView.widthAnchor),
orphanedContentDeletedRow.widthAnchor.constraint(equalTo: statsStackView.widthAnchor),
errorTextField.widthAnchor.constraint(equalTo: statsStackView.widthAnchor)
])
}
func configureErrorViews() {
errorTextField.textColor = .secondaryLabelColor
errorTextField.font = .systemFont(ofSize: NSFont.systemFontSize)
errorTextField.isHidden = true
refreshButton.bezelStyle = .rounded
refreshButton.title = NSLocalizedString("Refresh", comment: "Refresh button")
refreshButton.translatesAutoresizingMaskIntoConstraints = false
refreshButton.isHidden = true
}
func configureNavigationButtons() {
returnToPreviousResultsButton.bezelStyle = .rounded
returnToPreviousResultsButton.title = NSLocalizedString("Return to Previous Scan Results", comment: "Return to previous scan results button")
returnToPreviousResultsButton.translatesAutoresizingMaskIntoConstraints = false
refreshScanButton.bezelStyle = .rounded
refreshScanButton.title = NSLocalizedString("Refresh Scan", comment: "Refresh scan button")
refreshScanButton.translatesAutoresizingMaskIntoConstraints = false
navigationButtonGroup.translatesAutoresizingMaskIntoConstraints = false
navigationButtonGroup.isHidden = true
navigationButtonGroup.addSubview(returnToPreviousResultsButton)
navigationButtonGroup.addSubview(refreshScanButton)
NSLayoutConstraint.activate([
returnToPreviousResultsButton.topAnchor.constraint(equalTo: navigationButtonGroup.topAnchor),
returnToPreviousResultsButton.leadingAnchor.constraint(equalTo: navigationButtonGroup.leadingAnchor),
returnToPreviousResultsButton.trailingAnchor.constraint(equalTo: navigationButtonGroup.trailingAnchor),
refreshScanButton.topAnchor.constraint(equalTo: returnToPreviousResultsButton.bottomAnchor, constant: CloudKitStatsLayout.rowSpacing),
refreshScanButton.leadingAnchor.constraint(equalTo: navigationButtonGroup.leadingAnchor),
refreshScanButton.trailingAnchor.constraint(equalTo: navigationButtonGroup.trailingAnchor),
refreshScanButton.bottomAnchor.constraint(equalTo: navigationButtonGroup.bottomAnchor)
])
}
static func makeStatRow(_ title: String, valueLabel: NSTextField) -> NSView {
let container = NSView()
container.translatesAutoresizingMaskIntoConstraints = false
let titleLabel = NSTextField(labelWithString: title)
titleLabel.translatesAutoresizingMaskIntoConstraints = false
valueLabel.translatesAutoresizingMaskIntoConstraints = false
CloudKitStatsLayout.configureValueLabel(valueLabel)
container.addSubview(titleLabel)
container.addSubview(valueLabel)
NSLayoutConstraint.activate([
titleLabel.leadingAnchor.constraint(equalTo: container.leadingAnchor),
titleLabel.topAnchor.constraint(equalTo: container.topAnchor),
titleLabel.bottomAnchor.constraint(equalTo: container.bottomAnchor),
valueLabel.trailingAnchor.constraint(equalTo: container.trailingAnchor),
valueLabel.lastBaselineAnchor.constraint(equalTo: titleLabel.lastBaselineAnchor)
])
return container
}
}
@@ -0,0 +1,59 @@
//
// CloudKitStatsCleanUpStatusView.swift
// NetNewsWire
//
// Created by Brent Simmons on 3/24/26.
//
import AppKit
final class CloudKitStatsCleanUpStatusView: NSView {
let progressBar = NSProgressIndicator()
let cancelButton = NSButton()
let phaseTextField = NSTextField(labelWithString: "")
init() {
super.init(frame: .zero)
translatesAutoresizingMaskIntoConstraints = false
progressBar.style = .bar
progressBar.isIndeterminate = false
progressBar.minValue = 0
progressBar.maxValue = 1.0
progressBar.doubleValue = 0
progressBar.translatesAutoresizingMaskIntoConstraints = false
progressBar.setAccessibilityLabel(NSLocalizedString("Cleanup progress", comment: "Progress bar accessibility label"))
cancelButton.bezelStyle = .rounded
cancelButton.title = NSLocalizedString("Cancel", comment: "Cancel button")
cancelButton.translatesAutoresizingMaskIntoConstraints = false
cancelButton.widthAnchor.constraint(equalToConstant: CloudKitStatsLayout.buttonWidth).isActive = true
phaseTextField.textColor = .secondaryLabelColor
phaseTextField.font = .systemFont(ofSize: NSFont.systemFontSize)
phaseTextField.translatesAutoresizingMaskIntoConstraints = false
addSubview(progressBar)
addSubview(cancelButton)
addSubview(phaseTextField)
NSLayoutConstraint.activate([
progressBar.leadingAnchor.constraint(equalTo: leadingAnchor),
progressBar.centerYAnchor.constraint(equalTo: cancelButton.centerYAnchor),
cancelButton.leadingAnchor.constraint(equalTo: progressBar.trailingAnchor, constant: CloudKitStatsLayout.buttonTextGap),
cancelButton.trailingAnchor.constraint(equalTo: trailingAnchor),
cancelButton.topAnchor.constraint(equalTo: topAnchor),
phaseTextField.topAnchor.constraint(equalTo: cancelButton.bottomAnchor, constant: CloudKitStatsLayout.rowSpacing),
phaseTextField.leadingAnchor.constraint(equalTo: leadingAnchor),
phaseTextField.trailingAnchor.constraint(equalTo: trailingAnchor),
phaseTextField.bottomAnchor.constraint(equalTo: bottomAnchor)
])
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
@@ -0,0 +1,153 @@
//
// CloudKitStatsCleanUpViewController.swift
// NetNewsWire
//
// Created by Brent Simmons on 3/24/26.
//
import AppKit
import Account
final class CloudKitStatsCleanUpViewController: NSViewController {
private let model: CloudKitStatsViewModel
private let statusView = CloudKitStatsCleanUpStatusView()
private let contentView = CloudKitStatsCleanUpContentView()
init(model: CloudKitStatsViewModel) {
self.model = model
super.init(nibName: nil, bundle: nil)
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) is not supported")
}
override func loadView() {
let containerView = NSView()
containerView.translatesAutoresizingMaskIntoConstraints = false
statusView.cancelButton.target = nil
statusView.cancelButton.action = #selector(CloudKitStatsViewController.cancelCleanUp(_:))
contentView.refreshButton.target = nil
contentView.refreshButton.action = #selector(CloudKitStatsViewController.refreshScan(_:))
contentView.refreshScanButton.target = nil
contentView.refreshScanButton.action = #selector(CloudKitStatsViewController.refreshScan(_:))
contentView.returnToPreviousResultsButton.target = nil
contentView.returnToPreviousResultsButton.action = #selector(CloudKitStatsViewController.returnToPreviousScanResults(_:))
containerView.addSubview(statusView)
containerView.addSubview(contentView)
NSLayoutConstraint.activate([
statusView.topAnchor.constraint(equalTo: containerView.topAnchor, constant: CloudKitStatsLayout.sectionSpacing),
statusView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
statusView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
contentView.topAnchor.constraint(equalTo: statusView.bottomAnchor, constant: CloudKitStatsLayout.sectionSpacing),
contentView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
contentView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
contentView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor)
])
self.view = containerView
}
// MARK: - API
func updateUI() {
updateCleanUpView()
}
}
// MARK: - Private
private extension CloudKitStatsCleanUpViewController {
// MARK: - UI Update
func updateCleanUpView() {
if model.cleanUpStatus.cleanUpError != nil {
updateCleanUpViewForError()
return
}
let isCanceled = model.cleanUpStatus.isCanceled
guard let progress = model.cleanUpStatus.progress else {
return
}
contentView.errorTextField.isHidden = true
contentView.refreshButton.isHidden = true
statusView.isHidden = false
let plan = model.cleanUpPlan
// Progress bar: visible during cleaning, hidden when canceled, visible at 100% when completed
if isCanceled {
statusView.progressBar.isHidden = true
statusView.cancelButton.isHidden = true
statusView.phaseTextField.isHidden = false
statusView.phaseTextField.stringValue = NSLocalizedString("Canceled.", comment: "Cleanup status text when canceled")
} else if model.cleanUpStatus.isCompleted {
statusView.progressBar.isHidden = false
statusView.progressBar.doubleValue = 1.0
statusView.cancelButton.isEnabled = false
statusView.phaseTextField.isHidden = false
statusView.phaseTextField.stringValue = cleanUpPhaseText(progress.phase)
} else {
statusView.progressBar.isHidden = false
statusView.cancelButton.isHidden = false
statusView.cancelButton.isEnabled = true
statusView.phaseTextField.isHidden = false
statusView.phaseTextField.stringValue = cleanUpPhaseText(progress.phase)
if plan.totalCount > 0 {
statusView.progressBar.doubleValue = Double(progress.totalDeleted) / Double(plan.totalCount)
} else {
statusView.progressBar.doubleValue = 0
}
}
updateStatRow(contentView.staleStatusDeletedRow, label: contentView.staleStatusDeletedLabel, value: progress.staleStatusDeleted, planned: plan.staleStatusCount)
updateStatRow(contentView.readContentDeletedRow, label: contentView.readContentDeletedLabel, value: progress.readContentDeleted, planned: plan.readContentCount)
updateStatRow(contentView.unreadContentDeletedRow, label: contentView.unreadContentDeletedLabel, value: progress.unreadContentDeleted, planned: plan.unreadContentCount)
updateStatRow(contentView.orphanedContentDeletedRow, label: contentView.orphanedContentDeletedLabel, value: progress.orphanedContentDeleted, planned: plan.orphanedContentCount)
contentView.navigationButtonGroup.isHidden = !(isCanceled || model.cleanUpStatus.isCompleted)
}
func updateCleanUpViewForError() {
statusView.isHidden = true
contentView.staleStatusDeletedRow.isHidden = true
contentView.readContentDeletedRow.isHidden = true
contentView.unreadContentDeletedRow.isHidden = true
contentView.orphanedContentDeletedRow.isHidden = true
contentView.navigationButtonGroup.isHidden = true
contentView.errorTextField.isHidden = false
contentView.refreshButton.isHidden = false
contentView.errorTextField.stringValue = NSLocalizedString("Cleanup failed to complete, but you may be able to clean up more if you wait a few minutes and try again.\n\nClick Refresh to see your updated stats.", comment: "Cleanup error message")
}
func updateStatRow(_ row: NSView, label: NSTextField, value: Int, planned: Int) {
row.isHidden = planned == 0
label.stringValue = CloudKitStatsLayout.formattedNumber(value)
}
func cleanUpPhaseText(_ phase: CloudKitCleanUpPhase) -> String {
switch phase {
case .deletingStaleStatus:
return NSLocalizedString("Deleting stale status records…", comment: "Cleanup phase text")
case .deletingReadContent:
return NSLocalizedString("Deleting read content records…", comment: "Cleanup phase text")
case .deletingUnreadContent:
return NSLocalizedString("Deleting unread content records…", comment: "Cleanup phase text")
case .deletingOrphanedContent:
return NSLocalizedString("Deleting orphaned content records…", comment: "Cleanup phase text")
case .completed:
return NSLocalizedString("iCloud storage cleanup completed.", comment: "Cleanup phase text when completed")
}
}
}
@@ -0,0 +1,77 @@
//
// CloudKitStatsLayout.swift
// NetNewsWire
//
// Created by Brent Simmons on 3/24/26.
//
import AppKit
enum CloudKitStatsLayout {
static let containerWidth: CGFloat = 400
static let windowHeight: CGFloat = 450
static let buttonWidth: CGFloat = 80
static let sectionSpacing: CGFloat = 14
static let horizontalPadding: CGFloat = 20
static let rowSpacing: CGFloat = 6
static let iconSize: CGFloat = 12
static let iconLabelGap: CGFloat = 4
static let spinnerTextGap: CGFloat = 6
static let buttonTextGap: CGFloat = 8
static let fetchingAlpha: CGFloat = 0.4
static let animationDuration: CGFloat = 0.25
static let starColor = Assets.Colors.star
@MainActor static func makeDivider() -> NSView {
let divider = NSBox()
divider.boxType = .separator
divider.translatesAutoresizingMaskIntoConstraints = false
return divider
}
@MainActor static func makeLabelWithIcon(_ text: String, symbolName: String, color: NSColor, iconOffset: CGFloat = 0) -> NSView {
let container = NSView()
container.translatesAutoresizingMaskIntoConstraints = false
let icon = NSImageView()
icon.translatesAutoresizingMaskIntoConstraints = false
icon.image = NSImage(systemSymbolName: symbolName, accessibilityDescription: text)
icon.contentTintColor = color
icon.setContentHuggingPriority(.required, for: .horizontal)
let label = NSTextField(labelWithString: text)
label.translatesAutoresizingMaskIntoConstraints = false
container.addSubview(icon)
container.addSubview(label)
NSLayoutConstraint.activate([
label.leadingAnchor.constraint(equalTo: container.leadingAnchor),
label.topAnchor.constraint(equalTo: container.topAnchor),
label.bottomAnchor.constraint(equalTo: container.bottomAnchor),
icon.leadingAnchor.constraint(equalTo: label.trailingAnchor, constant: iconLabelGap),
icon.centerYAnchor.constraint(equalTo: label.centerYAnchor, constant: iconOffset),
icon.widthAnchor.constraint(equalToConstant: iconSize),
icon.heightAnchor.constraint(equalToConstant: iconSize),
icon.trailingAnchor.constraint(equalTo: container.trailingAnchor)
])
return container
}
@MainActor static func configureValueLabel(_ label: NSTextField) {
label.font = NSFont.monospacedDigitSystemFont(ofSize: NSFont.systemFontSize, weight: .regular)
label.alignment = .right
}
static func formattedNumber(_ value: Int) -> String {
NumberFormatter.localizedString(from: NSNumber(value: value), number: .decimal)
}
static func formattedCount(_ count: Int, singular: String, plural: String) -> String {
let label = count == 1 ? singular : plural
return "\(formattedNumber(count)) \(label)"
}
}
@@ -0,0 +1,82 @@
//
// CloudKitStatsToolbarView.swift
// NetNewsWire
//
// Created by Brent Simmons on 3/24/26.
//
import AppKit
final class CloudKitStatsToolbarView: NSView {
let helpButton: NSButton
let shareButton: NSButton
let cleanUpButton: NSButton
init() {
helpButton = NSButton()
helpButton.bezelStyle = .helpButton
helpButton.title = ""
helpButton.translatesAutoresizingMaskIntoConstraints = false
helpButton.setAccessibilityLabel(NSLocalizedString("Help — opens in browser", comment: "Help button accessibility label"))
helpButton.target = nil
helpButton.action = #selector(CloudKitStatsViewController.showHelp(_:))
shareButton = NSButton()
shareButton.bezelStyle = .rounded
shareButton.image = NSImage(systemSymbolName: "square.and.arrow.up", accessibilityDescription: NSLocalizedString("Share", comment: "Share button accessibility description"))
shareButton.translatesAutoresizingMaskIntoConstraints = false
shareButton.target = nil
shareButton.action = #selector(CloudKitStatsViewController.shareStats(_:))
cleanUpButton = NSButton()
cleanUpButton.bezelStyle = .rounded
cleanUpButton.title = NSLocalizedString("Clean Up", comment: "Clean Up button title")
cleanUpButton.translatesAutoresizingMaskIntoConstraints = false
cleanUpButton.target = nil
cleanUpButton.action = #selector(CloudKitStatsViewController.cleanUp(_:))
super.init(frame: .zero)
translatesAutoresizingMaskIntoConstraints = false
let divider = CloudKitStatsLayout.makeDivider()
let background = NSVisualEffectView()
background.translatesAutoresizingMaskIntoConstraints = false
background.blendingMode = .withinWindow
background.material = .titlebar
addSubview(divider)
addSubview(background)
addSubview(helpButton)
addSubview(shareButton)
addSubview(cleanUpButton)
NSLayoutConstraint.activate([
divider.topAnchor.constraint(equalTo: topAnchor),
divider.leadingAnchor.constraint(equalTo: leadingAnchor),
divider.trailingAnchor.constraint(equalTo: trailingAnchor),
background.topAnchor.constraint(equalTo: divider.bottomAnchor),
background.leadingAnchor.constraint(equalTo: leadingAnchor),
background.trailingAnchor.constraint(equalTo: trailingAnchor),
background.bottomAnchor.constraint(equalTo: bottomAnchor),
helpButton.topAnchor.constraint(equalTo: divider.bottomAnchor, constant: CloudKitStatsLayout.sectionSpacing),
helpButton.leadingAnchor.constraint(equalTo: leadingAnchor, constant: CloudKitStatsLayout.horizontalPadding),
helpButton.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -CloudKitStatsLayout.sectionSpacing),
shareButton.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -CloudKitStatsLayout.horizontalPadding),
shareButton.centerYAnchor.constraint(equalTo: helpButton.centerYAnchor),
cleanUpButton.centerXAnchor.constraint(equalTo: centerXAnchor),
cleanUpButton.centerYAnchor.constraint(equalTo: helpButton.centerYAnchor),
cleanUpButton.widthAnchor.constraint(equalToConstant: CloudKitStatsLayout.buttonWidth)
])
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
File diff suppressed because it is too large. Load diff
@@ -5,22 +5,22 @@
// Created by Brent Simmons on 3/20/26.
//
@preconcurrency import AppKit
import AppKit
@MainActor final class CloudKitStatsWindowController: NSWindowController {
final class CloudKitStatsWindowController: NSWindowController {
private static let windowSize = NSSize(width: 400, height: 450)
private static let windowSize = NSSize(width: CloudKitStatsLayout.containerWidth, height: CloudKitStatsLayout.windowHeight)
private static let frameAutosaveName = "CloudKitStats"
private var hasBeenShown = false
init() {
let window = NSWindow(contentRect: NSRect(origin: .zero, size: Self.windowSize), styleMask: [.titled, .closable], backing: .buffered, defer: true)
window.title = "iCloud Storage Stats"
window.title = NSLocalizedString("iCloud Storage Stats", comment: "iCloud Storage Stats window title")
window.isReleasedWhenClosed = false
window.contentViewController = CloudKitStatsViewController()
window.representedURL = URL(string: "https://icloud.com")
window.representedURL = URL(string: "https://icloud.com/")
if let iconButton = window.standardWindowButton(.documentIconButton) {
iconButton.image = NSImage(systemSymbolName: "icloud", accessibilityDescription: "iCloud")
}
@@ -0,0 +1,160 @@
//
// CloudKitStatsScanContentView.swift
// NetNewsWire
//
// Created by Brent Simmons on 3/24/26.
//
import AppKit
final class CloudKitStatsScanContentView: NSView {
// MARK: - Status Records
let statusRecordCountLabel = NSTextField(labelWithString: "0")
let starredCountLabel = NSTextField(labelWithString: "0")
let unreadCountLabel = NSTextField(labelWithString: "0")
let readCountLabel = NSTextField(labelWithString: "0")
let staleCountLabel = NSTextField(labelWithString: "0")
// MARK: - Article Content Records
let totalContentCountLabel = NSTextField(labelWithString: "0")
let starredContentCountLabel = NSTextField(labelWithString: "0")
let unreadContentCountLabel = NSTextField(labelWithString: "0")
let readContentCountLabel = NSTextField(labelWithString: "0")
let orphanedContentCountLabel = NSTextField(labelWithString: "0")
// MARK: - Section containers (for alpha animation)
let statusSectionView = NSView()
let articleSectionView = NSView()
init() {
super.init(frame: .zero)
translatesAutoresizingMaskIntoConstraints = false
statusSectionView.translatesAutoresizingMaskIntoConstraints = false
articleSectionView.translatesAutoresizingMaskIntoConstraints = false
buildSectionRows(statusSectionView, rows: [
.header(NSLocalizedString("Status Records", comment: "Status records section header")),
.data(labelView: NSTextField(labelWithString: NSLocalizedString("Total", comment: "Total row label")), valueLabel: statusRecordCountLabel),
.data(labelView: CloudKitStatsLayout.makeLabelWithIcon(NSLocalizedString("Starred", comment: "Starred row label"), symbolName: "star.fill", color: CloudKitStatsLayout.starColor), valueLabel: starredCountLabel),
.data(labelView: CloudKitStatsLayout.makeLabelWithIcon(NSLocalizedString("Unread", comment: "Unread row label"), symbolName: "circle.fill", color: .controlAccentColor, iconOffset: 0.5), valueLabel: unreadCountLabel),
.data(labelView: NSTextField(labelWithString: NSLocalizedString("Read", comment: "Read row label")), valueLabel: readCountLabel),
.data(labelView: NSTextField(labelWithString: NSLocalizedString("Stale", comment: "Stale row label")), valueLabel: staleCountLabel)
])
buildSectionRows(articleSectionView, rows: [
.header(NSLocalizedString("Article Content Records", comment: "Article content records section header")),
.data(labelView: NSTextField(labelWithString: NSLocalizedString("Total", comment: "Total row label")), valueLabel: totalContentCountLabel),
.data(labelView: CloudKitStatsLayout.makeLabelWithIcon(NSLocalizedString("Starred", comment: "Starred row label"), symbolName: "star.fill", color: CloudKitStatsLayout.starColor), valueLabel: starredContentCountLabel),
.data(labelView: CloudKitStatsLayout.makeLabelWithIcon(NSLocalizedString("Unread", comment: "Unread row label"), symbolName: "circle.fill", color: .controlAccentColor, iconOffset: 0.5), valueLabel: unreadContentCountLabel),
.data(labelView: NSTextField(labelWithString: NSLocalizedString("Read", comment: "Read row label")), valueLabel: readContentCountLabel),
.data(labelView: NSTextField(labelWithString: NSLocalizedString("Orphaned", comment: "Orphaned row label")), valueLabel: orphanedContentCountLabel)
])
let divider = CloudKitStatsLayout.makeDivider()
addSubview(statusSectionView)
addSubview(divider)
addSubview(articleSectionView)
NSLayoutConstraint.activate([
statusSectionView.topAnchor.constraint(equalTo: topAnchor),
statusSectionView.leadingAnchor.constraint(equalTo: leadingAnchor),
statusSectionView.trailingAnchor.constraint(equalTo: trailingAnchor),
divider.topAnchor.constraint(equalTo: statusSectionView.bottomAnchor, constant: CloudKitStatsLayout.sectionSpacing),
divider.leadingAnchor.constraint(equalTo: leadingAnchor),
divider.trailingAnchor.constraint(equalTo: trailingAnchor),
articleSectionView.topAnchor.constraint(equalTo: divider.bottomAnchor, constant: CloudKitStatsLayout.sectionSpacing),
articleSectionView.leadingAnchor.constraint(equalTo: leadingAnchor),
articleSectionView.trailingAnchor.constraint(equalTo: trailingAnchor),
articleSectionView.bottomAnchor.constraint(equalTo: bottomAnchor)
])
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
// MARK: - Private
private extension CloudKitStatsScanContentView {
enum SectionRow {
case header(String)
case data(labelView: NSView, valueLabel: NSTextField)
}
func buildSectionRows(_ container: NSView, rows: [SectionRow]) {
var constraints = [NSLayoutConstraint]()
var previousAnchor = container.topAnchor
var previousSpacing: CGFloat = 0
var dataRowViews = [NSView]()
for row in rows {
switch row {
case .header(let title):
let headerLabel = NSTextField(labelWithString: title)
headerLabel.font = .boldSystemFont(ofSize: NSFont.systemFontSize)
headerLabel.translatesAutoresizingMaskIntoConstraints = false
container.addSubview(headerLabel)
constraints.append(contentsOf: [
headerLabel.topAnchor.constraint(equalTo: previousAnchor, constant: previousSpacing),
headerLabel.leadingAnchor.constraint(equalTo: container.leadingAnchor)
])
previousAnchor = headerLabel.bottomAnchor
previousSpacing = CloudKitStatsLayout.rowSpacing
case .data(let labelView, let valueLabel):
labelView.translatesAutoresizingMaskIntoConstraints = false
valueLabel.translatesAutoresizingMaskIntoConstraints = false
CloudKitStatsLayout.configureValueLabel(valueLabel)
container.addSubview(labelView)
container.addSubview(valueLabel)
// For icon rows, the labelView is a container — find the
// text field inside it so baseline alignment is correct.
let baselineView: NSView
if let textField = labelView as? NSTextField {
baselineView = textField
} else if let textField = labelView.subviews.compactMap({ $0 as? NSTextField }).first {
baselineView = textField
} else {
baselineView = labelView
}
constraints.append(contentsOf: [
labelView.topAnchor.constraint(equalTo: previousAnchor, constant: previousSpacing),
labelView.leadingAnchor.constraint(equalTo: container.leadingAnchor),
valueLabel.trailingAnchor.constraint(equalTo: container.trailingAnchor),
valueLabel.lastBaselineAnchor.constraint(equalTo: baselineView.lastBaselineAnchor)
])
dataRowViews.append(labelView)
previousAnchor = labelView.bottomAnchor
previousSpacing = CloudKitStatsLayout.rowSpacing
}
}
// Make all data rows the same height (tallest wins).
if let firstRow = dataRowViews.first {
for row in dataRowViews.dropFirst() {
constraints.append(row.heightAnchor.constraint(equalTo: firstRow.heightAnchor))
}
}
constraints.append(previousAnchor.constraint(equalTo: container.bottomAnchor))
NSLayoutConstraint.activate(constraints)
}
}
@@ -0,0 +1,76 @@
//
// CloudKitStatsScanStatusView.swift
// NetNewsWire
//
// Created by Brent Simmons on 3/24/26.
//
import AppKit
final class CloudKitStatsScanStatusView: NSView {
let spinner = NSProgressIndicator()
let statusIcon = NSImageView()
let statusTextField = NSTextField(labelWithString: "")
let actionButton = NSButton()
private(set) var statusTextLeadingToSpinner: NSLayoutConstraint!
private(set) var statusTextLeadingToContainer: NSLayoutConstraint!
init() {
super.init(frame: .zero)
translatesAutoresizingMaskIntoConstraints = false
spinner.style = .spinning
spinner.controlSize = .small
spinner.translatesAutoresizingMaskIntoConstraints = false
spinner.setAccessibilityLabel(NSLocalizedString("Scanning", comment: "Spinner accessibility label"))
spinner.startAnimation(nil)
statusIcon.translatesAutoresizingMaskIntoConstraints = false
statusIcon.image = NSImage(systemSymbolName: "checkmark.circle.fill", accessibilityDescription: NSLocalizedString("Completed", comment: "Status icon accessibility description"))
statusIcon.contentTintColor = .systemGreen
statusIcon.isHidden = true
statusTextField.translatesAutoresizingMaskIntoConstraints = false
statusTextField.textColor = .secondaryLabelColor
statusTextField.font = .systemFont(ofSize: NSFont.systemFontSize)
statusTextField.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
actionButton.translatesAutoresizingMaskIntoConstraints = false
actionButton.bezelStyle = .rounded
actionButton.title = NSLocalizedString("Cancel", comment: "Cancel button")
actionButton.widthAnchor.constraint(equalToConstant: CloudKitStatsLayout.buttonWidth).isActive = true
addSubview(spinner)
addSubview(statusIcon)
addSubview(statusTextField)
addSubview(actionButton)
statusTextLeadingToSpinner = statusTextField.leadingAnchor.constraint(equalTo: spinner.trailingAnchor, constant: CloudKitStatsLayout.spinnerTextGap)
statusTextLeadingToContainer = statusTextField.leadingAnchor.constraint(equalTo: leadingAnchor)
NSLayoutConstraint.activate([
spinner.leadingAnchor.constraint(equalTo: leadingAnchor),
spinner.centerYAnchor.constraint(equalTo: statusTextField.centerYAnchor),
statusIcon.leadingAnchor.constraint(equalTo: leadingAnchor),
statusIcon.centerYAnchor.constraint(equalTo: statusTextField.centerYAnchor),
statusTextLeadingToSpinner,
actionButton.topAnchor.constraint(equalTo: topAnchor),
actionButton.bottomAnchor.constraint(equalTo: bottomAnchor),
statusTextField.bottomAnchor.constraint(lessThanOrEqualTo: bottomAnchor),
actionButton.leadingAnchor.constraint(greaterThanOrEqualTo: statusTextField.trailingAnchor, constant: CloudKitStatsLayout.buttonTextGap),
actionButton.trailingAnchor.constraint(equalTo: trailingAnchor),
actionButton.lastBaselineAnchor.constraint(equalTo: statusTextField.lastBaselineAnchor)
])
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
@@ -0,0 +1,171 @@
//
// CloudKitStatsScanViewController.swift
// NetNewsWire
//
// Created by Brent Simmons on 3/24/26.
//
import AppKit
import CloudKit
import Account
import CloudKitSync
final class CloudKitStatsScanViewController: NSViewController {
private let model: CloudKitStatsViewModel
private let statusView = CloudKitStatsScanStatusView()
private let contentView = CloudKitStatsScanContentView()
private var hasShownErrorAlert = false
// MARK: - Init
init(model: CloudKitStatsViewModel) {
self.model = model
super.init(nibName: nil, bundle: nil)
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) is not supported")
}
// MARK: - NSViewController
override func loadView() {
let containerView = NSView()
containerView.translatesAutoresizingMaskIntoConstraints = false
let topDivider = CloudKitStatsLayout.makeDivider()
containerView.addSubview(statusView)
containerView.addSubview(topDivider)
containerView.addSubview(contentView)
NSLayoutConstraint.activate([
statusView.topAnchor.constraint(equalTo: containerView.topAnchor, constant: CloudKitStatsLayout.sectionSpacing),
statusView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
statusView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
topDivider.topAnchor.constraint(equalTo: statusView.bottomAnchor, constant: CloudKitStatsLayout.sectionSpacing),
topDivider.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: -CloudKitStatsLayout.horizontalPadding),
topDivider.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: CloudKitStatsLayout.horizontalPadding),
contentView.topAnchor.constraint(equalTo: topDivider.bottomAnchor, constant: CloudKitStatsLayout.sectionSpacing),
contentView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
contentView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
contentView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor)
])
self.view = containerView
}
// MARK: - API
func updateUI() {
NSAnimationContext.runAnimationGroup { context in
context.duration = CloudKitStatsLayout.animationDuration
context.allowsImplicitAnimation = true
updateStatusBar()
updateStatsValues()
}
}
}
// MARK: - Private
private extension CloudKitStatsScanViewController {
// MARK: - UI Update
func updateStatusBar() {
switch model.fetchStatus {
case .idle:
break
case .fetching:
hasShownErrorAlert = false
statusView.spinner.isHidden = false
statusView.spinner.isIndeterminate = true
statusView.spinner.style = .spinning
statusView.spinner.startAnimation(nil)
statusView.statusIcon.isHidden = true
statusView.statusTextField.stringValue = NSLocalizedString("Scanning iCloud storage", comment: "Scan status text while fetching")
statusView.actionButton.title = NSLocalizedString("Cancel", comment: "Cancel button")
statusView.actionButton.keyEquivalent = "\u{1b}"
statusView.actionButton.action = #selector(CloudKitStatsViewController.cancelScan(_:))
statusView.statusTextLeadingToSpinner.isActive = true
statusView.statusTextLeadingToContainer.isActive = false
case .completed:
statusView.spinner.isHidden = true
statusView.spinner.stopAnimation(nil)
statusView.statusIcon.isHidden = false
statusView.statusTextField.stringValue = NSLocalizedString("Scan completed.", comment: "Scan status text when completed")
statusView.actionButton.title = NSLocalizedString("Refresh", comment: "Refresh button")
statusView.actionButton.keyEquivalent = ""
statusView.actionButton.action = #selector(CloudKitStatsViewController.refreshScan(_:))
statusView.statusTextLeadingToSpinner.isActive = true
statusView.statusTextLeadingToContainer.isActive = false
case .error:
statusView.spinner.isHidden = true
statusView.spinner.stopAnimation(nil)
statusView.statusIcon.isHidden = true
statusView.statusTextField.stringValue = NSLocalizedString("Scan failed. Numbers are incomplete.", comment: "Scan status text on error")
statusView.actionButton.title = NSLocalizedString("Refresh", comment: "Refresh button")
statusView.actionButton.keyEquivalent = ""
statusView.actionButton.action = #selector(CloudKitStatsViewController.refreshScan(_:))
statusView.statusTextLeadingToSpinner.isActive = false
statusView.statusTextLeadingToContainer.isActive = true
showErrorAlertIfNeeded()
case .canceled:
statusView.spinner.isHidden = true
statusView.spinner.stopAnimation(nil)
statusView.statusIcon.isHidden = true
statusView.statusTextField.stringValue = NSLocalizedString("Canceled.", comment: "Scan status text when canceled")
statusView.actionButton.title = NSLocalizedString("Refresh", comment: "Refresh button")
statusView.actionButton.keyEquivalent = ""
statusView.actionButton.action = #selector(CloudKitStatsViewController.refreshScan(_:))
statusView.statusTextLeadingToSpinner.isActive = false
statusView.statusTextLeadingToContainer.isActive = true
}
}
func updateStatsValues() {
let stats = model.stats
contentView.statusRecordCountLabel.stringValue = CloudKitStatsLayout.formattedNumber(stats.statusCount)
contentView.starredCountLabel.stringValue = CloudKitStatsLayout.formattedNumber(stats.starredStatusCount)
contentView.unreadCountLabel.stringValue = CloudKitStatsLayout.formattedNumber(stats.unreadStatusCount)
contentView.readCountLabel.stringValue = CloudKitStatsLayout.formattedNumber(stats.readStatusCount)
contentView.staleCountLabel.stringValue = CloudKitStatsLayout.formattedNumber(stats.staleStatusCount)
contentView.totalContentCountLabel.stringValue = CloudKitStatsLayout.formattedNumber(stats.articleCount)
contentView.starredContentCountLabel.stringValue = CloudKitStatsLayout.formattedNumber(stats.starredArticleCount)
contentView.unreadContentCountLabel.stringValue = CloudKitStatsLayout.formattedNumber(stats.unreadArticleCount)
contentView.readContentCountLabel.stringValue = CloudKitStatsLayout.formattedNumber(stats.readArticleCount)
contentView.orphanedContentCountLabel.stringValue = CloudKitStatsLayout.formattedNumber(stats.orphanedArticleCount)
let isFetching = model.fetchStatus.isFetching
let statusSectionDone = !isFetching || stats.articleCount > 0
contentView.statusSectionView.animator().alphaValue = statusSectionDone ? 1.0 : CloudKitStatsLayout.fetchingAlpha
contentView.articleSectionView.animator().alphaValue = isFetching ? CloudKitStatsLayout.fetchingAlpha : 1.0
}
func showErrorAlertIfNeeded() {
guard let fetchError = model.fetchStatus.fetchError, !hasShownErrorAlert else {
return
}
hasShownErrorAlert = true
let displayError: Error
if fetchError is CKError {
displayError = CloudKitError(fetchError)
} else {
displayError = fetchError
}
let alert = NSAlert()
alert.alertStyle = .warning
alert.messageText = NSLocalizedString("Couldn’t complete the iCloud scan because of an error:", comment: "Scan error alert title")
alert.informativeText = displayError.localizedDescription
alert.addButton(withTitle: NSLocalizedString("OK", comment: "OK button"))
alert.runModal()
}
}