2026-03-21 20:35:03 -07:00
//
2026-03-24 07:41:43 -07:00
// CloudKitStatsViewController.swift
2026-03-21 20:35:03 -07:00
// NetNewsWire
//
// Created by Brent Simmons on 3/20/26.
//
import AppKit
import Account
import RSWeb
2026-03-24 10:50:54 -07:00
final class CloudKitStatsViewController : NSViewController {
2026-03-21 20:35:03 -07:00
private let model = CloudKitStatsViewModel ()
2026-03-24 10:50:54 -07:00
private let toolbarView = CloudKitStatsToolbarView ()
private let contentAreaView = NSView ()
private var currentChild : NSViewController ?
2026-03-21 20:35:03 -07:00
private var hasAppeared = false
override func loadView () {
2026-03-24 10:50:54 -07:00
let containerView = NSView ( frame : . zero )
2026-03-21 20:35:03 -07:00
2026-03-24 10:50:54 -07:00
contentAreaView . translatesAutoresizingMaskIntoConstraints = false
2026-03-21 20:35:03 -07:00
2026-03-24 10:50:54 -07:00
containerView . addSubview ( contentAreaView )
containerView . addSubview ( toolbarView )
2026-03-24 07:41:43 -07:00
2026-03-21 20:35:03 -07:00
NSLayoutConstraint . activate ([
2026-03-24 12:58:37 -07:00
containerView . widthAnchor . constraint ( equalToConstant : CloudKitStatsLayout . containerWidth ),
2026-03-24 10:50:54 -07:00
contentAreaView . topAnchor . constraint ( equalTo : containerView . topAnchor ),
contentAreaView . leadingAnchor . constraint ( equalTo : containerView . leadingAnchor , constant : CloudKitStatsLayout . horizontalPadding ),
contentAreaView . trailingAnchor . constraint ( equalTo : containerView . trailingAnchor , constant : - CloudKitStatsLayout . horizontalPadding ),
2026-03-24 12:58:37 -07:00
contentAreaView . bottomAnchor . constraint ( equalTo : toolbarView . topAnchor , constant : - CloudKitStatsLayout . sectionSpacing ),
2026-03-21 20:35:03 -07:00
2026-03-24 10:50:54 -07:00
toolbarView . leadingAnchor . constraint ( equalTo : containerView . leadingAnchor ),
toolbarView . trailingAnchor . constraint ( equalTo : containerView . trailingAnchor ),
toolbarView . bottomAnchor . constraint ( equalTo : containerView . bottomAnchor )
2026-03-21 20:35:03 -07:00
])
self . view = containerView
2026-03-24 12:58:37 -07:00
model . onChange = { [ weak self ] in
self ?. updateUI ()
}
switchToScan ()
2026-03-21 20:35:03 -07:00
}
override func viewDidAppear () {
super . viewDidAppear ()
if ! hasAppeared {
hasAppeared = true
model . fetch ()
}
updateUI ()
}
}
2026-03-24 10:50:54 -07:00
// MARK: - Actions
2026-03-21 20:35:03 -07:00
2026-03-24 10:50:54 -07:00
extension CloudKitStatsViewController {
2026-03-21 20:35:03 -07:00
2026-03-24 10:50:54 -07:00
@objc func refreshScan ( _ sender : Any ?) {
model . fetch ()
2026-03-21 20:35:03 -07:00
}
2026-03-24 10:50:54 -07:00
@objc func cancelScan ( _ sender : Any ?) {
model . cancelFetch ()
2026-03-21 20:35:03 -07:00
}
2026-03-24 10:50:54 -07:00
@objc func cancelCleanUp ( _ sender : Any ?) {
model . cancelCleanUp ()
2026-03-21 20:35:03 -07:00
}
2026-03-24 10:50:54 -07:00
@objc func returnToPreviousScanResults ( _ sender : Any ?) {
model . cleanUpStatus = . idle
updateUI ()
2026-03-21 20:35:03 -07:00
}
2026-03-24 10:50:54 -07:00
@objc func showHelp ( _ sender : Any ?) {
2026-03-24 14:34:23 -07:00
if let url = URL ( string : "https://netnewswire.com/help/optimize-icloud.html" ) {
2026-03-21 20:35:03 -07:00
MacWebBrowser . openURL ( url )
}
}
2026-03-24 10:50:54 -07:00
@objc func shareStats ( _ sender : Any ?) {
2026-03-21 20:35:03 -07:00
guard let button = sender as ? NSButton else {
return
}
2026-03-24 07:41:43 -07:00
let text = model . cleanUpStatus . isActive ? model . cleanUpStatsText : model . statsText
let picker = NSSharingServicePicker ( items : [ text ])
2026-03-21 20:35:03 -07:00
picker . show ( relativeTo : button . bounds , of : button , preferredEdge : . minY )
}
2026-03-24 10:50:54 -07:00
@objc func cleanUp ( _ sender : Any ?) {
2026-03-24 07:41:43 -07:00
let plan = model . cleanUpPlan
2026-03-24 12:58:37 -07:00
guard model . cleanUpPlanIsStale || ! plan . isEmpty else {
2026-03-24 07:41:43 -07:00
return
}
let alert = NSAlert ()
alert . alertStyle = . warning
2026-03-24 10:50:54 -07:00
alert . messageText = NSLocalizedString ( "Clean Up iCloud Records" , comment : "Clean up alert title" )
2026-03-24 12:58:37 -07:00
alert . informativeText = model . cleanUpPlanIsStale ? staleCleanUpConfirmationText () : cleanUpConfirmationText ( plan )
2026-06-06 14:43:31 -07:00
alert . addButton ( withTitle : NSLocalizedString ( "Clean Up" , comment : "Clean Up button" ))
2026-03-24 10:50:54 -07:00
alert . addButton ( withTitle : NSLocalizedString ( "Cancel" , comment : "Cancel button" ))
2026-03-24 07:41:43 -07:00
guard let window = view . window else {
return
}
alert . beginSheetModal ( for : window ) { [ weak self ] response in
guard response == . alertFirstButtonReturn else {
return
}
self ?. model . cleanUp ()
}
}
2026-03-24 10:50:54 -07:00
}
2026-03-24 07:41:43 -07:00
2026-03-24 10:50:54 -07:00
// MARK: - Private
private extension CloudKitStatsViewController {
// MARK: - Child View Controller Management
func switchToCleanUp () {
let incoming = CloudKitStatsCleanUpViewController ( model : model )
switchToChild ( incoming )
incoming . updateUI ()
2026-03-24 07:41:43 -07:00
}
2026-03-24 10:50:54 -07:00
func switchToScan () {
let incoming = CloudKitStatsScanViewController ( model : model )
switchToChild ( incoming )
incoming . updateUI ()
2026-03-24 07:41:43 -07:00
}
2026-03-24 10:50:54 -07:00
func switchToChild ( _ incoming : NSViewController ) {
addChild ( incoming )
incoming . view . translatesAutoresizingMaskIntoConstraints = false
let constraints = [
incoming . view . topAnchor . constraint ( equalTo : contentAreaView . topAnchor ),
incoming . view . leadingAnchor . constraint ( equalTo : contentAreaView . leadingAnchor ),
incoming . view . trailingAnchor . constraint ( equalTo : contentAreaView . trailingAnchor ),
incoming . view . bottomAnchor . constraint ( equalTo : contentAreaView . bottomAnchor )
]
2026-03-24 12:58:37 -07:00
contentAreaView . addSubview ( incoming . view )
NSLayoutConstraint . activate ( constraints )
2026-03-24 10:50:54 -07:00
if let outgoing = currentChild {
NSAnimationContext . runAnimationGroup { context in
context . duration = CloudKitStatsLayout . animationDuration
context . allowsImplicitAnimation = true
transition ( from : outgoing , to : incoming , options : . crossfade ) {
Task { @ MainActor in
outgoing . removeFromParent ()
}
}
}
}
currentChild = incoming
}
// MARK: - UI Update
func updateUI () {
let shouldShowCleanUp = model . cleanUpStatus . isActive
let isShowingCleanUp = currentChild is CloudKitStatsCleanUpViewController
if shouldShowCleanUp != isShowingCleanUp {
if shouldShowCleanUp {
switchToCleanUp ()
} else {
switchToScan ()
}
} else if let child = currentChild as ? CloudKitStatsScanViewController {
child . updateUI ()
} else if let child = currentChild as ? CloudKitStatsCleanUpViewController {
child . updateUI ()
}
updateToolbar ()
}
func updateToolbar () {
let isCleaning = model . cleanUpStatus . isCleaning
let isCleanUpDone = model . cleanUpStatus . isCompleted || model . cleanUpStatus . isCanceled || model . cleanUpStatus . cleanUpError != nil
if isCleaning {
toolbarView . cleanUpButton . isEnabled = false
toolbarView . shareButton . isEnabled = true
} else if isCleanUpDone {
toolbarView . cleanUpButton . isEnabled = true
toolbarView . shareButton . isEnabled = true
} else {
let enableShare = model . fetchStatus . isCompleted || model . fetchStatus . fetchError != nil
toolbarView . shareButton . isEnabled = enableShare
toolbarView . cleanUpButton . isEnabled = model . canCleanUp
}
2026-03-24 07:41:43 -07:00
}
func cleanUpConfirmationText ( _ plan : CloudKitCleanUpPlan ) -> String {
var lines = [ String ]()
if plan . readContentCount > 0 {
2026-03-24 10:50:54 -07:00
lines . append ( CloudKitStatsLayout . formattedCount ( plan . readContentCount , singular : NSLocalizedString ( "read content record" , comment : "Singular label for read content records" ), plural : NSLocalizedString ( "read content records" , comment : "Plural label for read content records" )))
2026-03-24 07:41:43 -07:00
}
if plan . unreadContentCount > 0 {
2026-03-24 10:50:54 -07:00
lines . append ( CloudKitStatsLayout . formattedCount ( plan . unreadContentCount , singular : NSLocalizedString ( "unread content record" , comment : "Singular label for unread content records" ), plural : NSLocalizedString ( "unread content records" , comment : "Plural label for unread content records" )))
2026-03-24 07:41:43 -07:00
}
let listText = lines . map { "• " + $0 }. joined ( separator : " \n " )
2026-03-26 09:39:03 -07:00
return NSLocalizedString ( "This will delete:" , comment : "Clean up confirmation prefix" ) + " \n " + listText + " \n\n " + NSLocalizedString ( "This may take several minutes." , comment : "Clean up confirmation suffix" )
2026-03-21 20:35:03 -07:00
}
2026-03-24 12:58:37 -07:00
func staleCleanUpConfirmationText () -> String {
2026-03-29 10:30:14 -07:00
let syncUnreadContent = AccountManager . shared . syncArticleContentForUnreadArticles
2026-03-24 12:58:37 -07:00
if syncUnreadContent {
2026-03-26 09:39:03 -07:00
return NSLocalizedString ( "This will delete any read content records. \n\n This may take several minutes." , comment : "Clean up confirmation when sync unread is on and plan is stale" )
2026-03-24 12:58:37 -07:00
} else {
2026-03-26 09:39:03 -07:00
return NSLocalizedString ( "This will delete any not-starred content records. \n\n This may take several minutes." , comment : "Clean up confirmation when plan is stale" )
2026-03-24 12:58:37 -07:00
}
}
2026-03-21 20:35:03 -07:00
}