2026-03-21 20:42:01 -07:00
//
2026-03-21 20:55:51 -07:00
// CloudKitStatsView.swift
2026-03-21 20:42:01 -07:00
// NetNewsWire-iOS
//
// Created by Brent Simmons on 3/20/26.
//
import SwiftUI
2026-06-10 12:28:41 -07:00
import RSCore
2026-03-21 20:42:01 -07:00
import Account
2026-03-21 20:55:51 -07:00
struct CloudKitStatsView : View {
2026-03-21 20:42:01 -07:00
2026-03-21 20:55:51 -07:00
private static let model = CloudKitStatsViewModel ()
2026-03-25 13:02:00 -07:00
private static let helpURL = URL ( string : "https://netnewswire.com/help/optimize-icloud.html" ) !
2026-03-21 20:55:51 -07:00
private let model = CloudKitStatsView . model
2026-03-21 20:42:01 -07:00
2026-03-25 13:02:00 -07:00
@ State private var showCleanUpConfirmation = false
2026-03-26 08:38:39 -07:00
@ State private var showHelp = false
2026-03-25 13:02:00 -07:00
2026-03-21 20:42:01 -07:00
var body : some View {
List {
2026-03-25 13:02:00 -07:00
if model . cleanUpStatus . isActive {
cleanUpStatusSection
cleanUpResultsSection
cleanUpNavigationSection
} else {
statusSection
if let fetchError = model . fetchStatus . fetchError {
Section {
Text ( fetchError . localizedDescription )
. foregroundStyle (. red )
}
} else {
statusRecordsSection
contentRecordsSection
}
if model . canCleanUp {
Section {
Button ( NSLocalizedString ( "Clean Up…" , comment : "Clean up button" )) {
showCleanUpConfirmation = true
}
2026-03-26 09:40:05 -07:00
} footer : {
helpLinkFooter
}
} else {
Section {
} footer : {
helpLinkFooter
2026-03-25 13:02:00 -07:00
}
2026-03-21 20:42:01 -07:00
}
}
}
2026-06-06 14:43:31 -07:00
. navigationTitle ( NSLocalizedString ( "iCloud Storage Stats" , comment : "iCloud Storage Stats window title" ))
2026-03-21 20:42:01 -07:00
. toolbar {
ToolbarItem ( placement : . topBarTrailing ) {
if model . fetchStatus . isFetching {
2026-03-25 13:02:00 -07:00
Button ( NSLocalizedString ( "Cancel" , comment : "Cancel button" )) {
2026-03-21 20:42:01 -07:00
model . cancelFetch ()
}
2026-03-25 13:02:00 -07:00
} else if model . cleanUpStatus . isCleaning {
Button ( NSLocalizedString ( "Cancel" , comment : "Cancel button" )) {
model . cancelCleanUp ()
2026-03-21 20:42:01 -07:00
}
2026-03-25 13:02:00 -07:00
} else {
Menu {
ShareLink ( item : model . cleanUpStatus . isActive ? model . cleanUpStatsText : model . statsText ) {
Label ( NSLocalizedString ( "Share Stats" , comment : "Share stats menu item" ), systemImage : "square.and.arrow.up" )
}
Link ( destination : Self . helpURL ) {
2026-06-09 11:25:32 -07:00
Label ( NSLocalizedString ( "Help" , comment : "Help" ), systemImage : "questionmark.circle" )
2026-03-25 13:02:00 -07:00
}
} label : {
2026-06-10 10:48:40 -07:00
Label ( NSLocalizedString ( "More" , comment : "More" ), systemImage : "ellipsis.circle" )
2026-03-25 13:02:00 -07:00
}
. disabled ( ! model . fetchStatus . isCompleted && ! model . cleanUpStatus . isActive )
2026-03-21 20:42:01 -07:00
}
}
}
2026-03-25 13:02:00 -07:00
. alert ( NSLocalizedString ( "Clean Up iCloud Records" , comment : "Clean up alert title" ), isPresented : $ showCleanUpConfirmation ) {
2026-06-06 14:43:31 -07:00
Button ( NSLocalizedString ( "Clean Up" , comment : "Clean Up button" ), role : . destructive ) {
2026-03-25 13:02:00 -07:00
model . cleanUp ()
}
Button ( NSLocalizedString ( "Cancel" , comment : "Cancel button" ), role : . cancel ) {
}
} message : {
Text ( cleanUpConfirmationMessage ())
}
2026-03-26 08:38:39 -07:00
. sheet ( isPresented : $ showHelp ) {
SafariView ( url : Self . helpURL )
}
2026-03-21 20:42:01 -07:00
. onAppear {
if case . idle = model . fetchStatus {
model . fetch ()
}
}
. onReceive ( NotificationCenter . default . publisher ( for : UIApplication . didEnterBackgroundNotification )) { _ in
if model . fetchStatus . isFetching {
model . cancelFetch ()
}
2026-03-25 13:02:00 -07:00
if model . cleanUpStatus . isCleaning {
model . cancelCleanUp ()
}
2026-03-21 20:42:01 -07:00
}
}
2026-03-25 13:02:00 -07:00
// MARK: - Scan Sections
2026-03-21 20:42:01 -07:00
@ ViewBuilder private var statusSection : some View {
Section {
switch model . fetchStatus {
case . idle :
EmptyView ()
case . fetching :
HStack ( spacing : 6 ) {
ProgressView ()
. controlSize (. small )
2026-06-06 14:43:31 -07:00
Text ( NSLocalizedString ( "Scanning iCloud storage" , comment : "Scan status text while fetching" ))
2026-03-21 20:42:01 -07:00
. foregroundStyle (. secondary )
}
2026-03-25 13:02:00 -07:00
. id ( "fetching" )
2026-03-21 20:42:01 -07:00
case . completed :
HStack ( spacing : 4 ) {
Image ( systemName : "checkmark.circle.fill" )
. foregroundStyle (. green )
2026-06-06 14:43:31 -07:00
Text ( NSLocalizedString ( "Scan completed." , comment : "Scan status text when completed" ))
2026-03-21 20:42:01 -07:00
. foregroundStyle (. secondary )
Spacer ()
2026-06-10 10:48:40 -07:00
Button ( NSLocalizedString ( "Refresh" , comment : "Refresh" )) {
2026-03-21 20:42:01 -07:00
model . fetch ()
}
}
case . canceled :
2026-06-06 14:43:31 -07:00
statusRow ( NSLocalizedString ( "Canceled." , comment : "Scan status text when canceled" ))
2026-03-25 13:02:00 -07:00
case . error :
statusRow ( NSLocalizedString ( "Scan failed." , comment : "Scan failed text" ))
}
}
}
private var statusRecordsSection : some View {
Section {
2026-06-06 14:43:31 -07:00
statsRow ( NSLocalizedString ( "Status Records" , comment : "Status records section header" ), model . stats . statusCount , isHeader : true )
2026-06-09 11:25:32 -07:00
iconStatsRow ( NSLocalizedString ( "Starred" , comment : "Starred" ), systemImage : "star.fill" , iconColor : . yellow , model . stats . starredStatusCount , iconBaselineOffset : 1 )
iconStatsRow ( NSLocalizedString ( "Unread" , comment : "Unread" ), systemImage : "circle.fill" , iconColor : . accentColor , model . stats . unreadStatusCount )
2026-06-06 14:43:31 -07:00
statsRow ( NSLocalizedString ( "Read" , comment : "Read row label" ), model . stats . readStatusCount )
2026-03-25 13:02:00 -07:00
}
}
2026-03-26 09:40:05 -07:00
private var helpLinkFooter : some View {
Button ( NSLocalizedString ( "How to Optimize iCloud Syncing" , comment : "Help link" )) {
showHelp = true
}
. font (. subheadline )
. frame ( maxWidth : . infinity )
. padding (. top , 8 )
}
2026-03-25 13:02:00 -07:00
private var contentRecordsSection : some View {
Section {
statsRow ( NSLocalizedString ( "Article Content Records" , comment : "Article content records header" ), model . stats . articleCount , isHeader : true )
2026-06-09 11:25:32 -07:00
iconStatsRow ( NSLocalizedString ( "Starred" , comment : "Starred" ), systemImage : "star.fill" , iconColor : . yellow , model . stats . starredArticleCount , iconBaselineOffset : 1 )
iconStatsRow ( NSLocalizedString ( "Unread" , comment : "Unread" ), systemImage : "circle.fill" , iconColor : . accentColor , model . stats . unreadArticleCount , isWarning : ! syncUnreadContent )
2026-06-06 14:43:31 -07:00
statsRow ( NSLocalizedString ( "Read" , comment : "Read row label" ), model . stats . readArticleCount , isWarning : true )
2026-03-25 13:02:00 -07:00
}
}
// MARK: - Clean Up Sections
@ ViewBuilder private var cleanUpStatusSection : some View {
Section {
if model . cleanUpStatus . cleanUpError != nil {
Text ( NSLocalizedString ( "Cleanup failed to complete, but you may be able to clean up more if you wait a few minutes and try again." , comment : "Cleanup error message" ))
. foregroundStyle (. red )
2026-06-10 10:48:40 -07:00
Button ( NSLocalizedString ( "Refresh" , comment : "Refresh" )) {
2026-03-25 13:02:00 -07:00
model . fetch ()
}
} else if let progress = model . cleanUpStatus . progress {
if model . cleanUpStatus . isCanceled {
ProgressView ( value : fractionComplete ( progress ))
Text ( NSLocalizedString ( "Cleanup canceled." , comment : "Cleanup status text when canceled" ))
2026-03-21 20:42:01 -07:00
. foregroundStyle (. secondary )
2026-03-25 13:02:00 -07:00
} else if model . cleanUpStatus . isCompleted {
ProgressView ( value : 1.0 )
Text ( NSLocalizedString ( "iCloud storage cleanup completed." , comment : "Cleanup phase text when completed" ))
. foregroundStyle (. secondary )
} else {
ProgressView ( value : fractionComplete ( progress ))
Text ( cleanUpPhaseText ( progress . phase ))
. foregroundStyle (. secondary )
}
}
}
}
@ ViewBuilder private var cleanUpResultsSection : some View {
if let progress = model . cleanUpStatus . progress {
Section {
if progress . readContentDeleted > 0 || progress . phase == . deletingReadContent {
2026-06-06 14:43:31 -07:00
statsRow ( NSLocalizedString ( "Read Content Deleted" , comment : "Cleanup stat row label" ), progress . readContentDeleted )
2026-03-25 13:02:00 -07:00
}
if progress . unreadContentDeleted > 0 || progress . phase == . deletingUnreadContent {
2026-06-06 14:43:31 -07:00
statsRow ( NSLocalizedString ( "Unread Content Deleted" , comment : "Cleanup stat row label" ), progress . unreadContentDeleted )
2026-03-25 13:02:00 -07:00
}
}
}
}
@ ViewBuilder private var cleanUpNavigationSection : some View {
if model . cleanUpStatus . isCompleted || model . cleanUpStatus . isCanceled {
Section {
2026-06-06 14:43:31 -07:00
Button ( NSLocalizedString ( "Return to Previous Scan Results" , comment : "Return to previous scan results button" )) {
2026-03-25 13:02:00 -07:00
withAnimation (. easeInOut ( duration : 0.25 )) {
model . cleanUpStatus = . idle
2026-03-21 20:42:01 -07:00
}
}
2026-03-25 13:02:00 -07:00
Button ( NSLocalizedString ( "Refresh Scan" , comment : "Refresh scan button" )) {
withAnimation (. easeInOut ( duration : 0.25 )) {
2026-03-21 20:42:01 -07:00
model . fetch ()
}
}
}
}
}
2026-03-25 13:02:00 -07:00
// MARK: - Shared Helpers
private var syncUnreadContent : Bool {
2026-03-29 10:30:14 -07:00
AccountManager . shared . syncArticleContentForUnreadArticles
2026-03-21 20:42:01 -07:00
}
2026-03-26 08:38:39 -07:00
private func iconStatsRow ( _ label : String , systemImage : String , iconColor : Color , _ count : Int , iconBaselineOffset : CGFloat = 0 , isWarning : Bool = false ) -> some View {
HStack {
HStack ( spacing : 4 ) {
Text ( label )
Image ( systemName : systemImage )
. foregroundStyle ( iconColor )
. imageScale (. small )
. baselineOffset ( iconBaselineOffset )
}
Spacer ()
Text ( formattedNumber ( count ))
. monospacedDigit ()
. foregroundStyle ( countColor ( isWarning : isWarning , count : count ))
}
}
2026-03-25 13:02:00 -07:00
private func statsRow ( _ label : String , _ count : Int , isHeader : Bool = false , isWarning : Bool = false ) -> some View {
2026-03-21 20:42:01 -07:00
HStack {
Text ( label )
. fontWeight ( isHeader ? . semibold : . regular )
Spacer ()
2026-03-25 13:02:00 -07:00
Text ( formattedNumber ( count ))
2026-03-21 20:42:01 -07:00
. monospacedDigit ()
2026-03-25 13:02:00 -07:00
. foregroundStyle ( countColor ( isWarning : isWarning , count : count ))
2026-03-21 20:42:01 -07:00
}
}
2026-03-25 13:02:00 -07:00
private func statusRow ( _ text : String ) -> some View {
HStack ( spacing : 4 ) {
Text ( text )
. foregroundStyle (. secondary )
Spacer ()
2026-06-10 10:48:40 -07:00
Button ( NSLocalizedString ( "Refresh" , comment : "Refresh" )) {
2026-03-25 13:02:00 -07:00
model . fetch ()
}
}
}
private func countColor ( isWarning : Bool , count : Int ) -> AnyShapeStyle {
if model . fetchStatus . isFetching || model . cleanUpStatus . isCleaning {
return AnyShapeStyle (. secondary )
}
if isWarning && count > 0 {
return AnyShapeStyle (. orange )
}
return AnyShapeStyle (. primary )
}
// MARK: - Private Helpers
private func cleanUpConfirmationMessage () -> String {
if model . cleanUpPlanIsStale {
return staleCleanUpConfirmationText ()
}
return cleanUpConfirmationText ( model . cleanUpPlan )
}
private func cleanUpConfirmationText ( _ plan : CloudKitCleanUpPlan ) -> String {
var lines = [ String ]()
if plan . readContentCount > 0 {
lines . append ( 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" )))
}
if plan . unreadContentCount > 0 {
lines . append ( 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" )))
}
let listText = lines . map { "• " + $0 }. joined ( separator : " \n " )
2026-03-26 09:40:05 -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-25 13:02:00 -07:00
}
private func staleCleanUpConfirmationText () -> String {
if syncUnreadContent {
2026-03-26 09:40:05 -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-25 13:02:00 -07:00
} else {
2026-03-26 09:40:05 -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-25 13:02:00 -07:00
}
}
private func cleanUpPhaseText ( _ phase : CloudKitCleanUpPhase ) -> String {
switch phase {
case . deletingStaleStatus :
2026-03-25 21:20:46 -07:00
return ""
2026-03-25 13:02:00 -07:00
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 . completed :
return NSLocalizedString ( "iCloud storage cleanup completed." , comment : "Cleanup phase text when completed" )
}
}
private func fractionComplete ( _ progress : CloudKitCleanUpProgress ) -> Double {
let totalCount = model . cleanUpPlan . totalCount
guard totalCount > 0 else {
return 0
}
return Double ( progress . totalDeleted ) / Double ( totalCount )
}
private func formattedNumber ( _ value : Int ) -> String {
NumberFormatter . localizedString ( from : NSNumber ( value : value ), number : . decimal )
}
private func formattedCount ( _ count : Int , singular : String , plural : String ) -> String {
let label = count == 1 ? singular : plural
return " \( formattedNumber ( count )) \( label ) "
}
2026-03-21 20:42:01 -07:00
}