From 3e5a3c81b5c6019d5c33ff43c511a68b923e84f1 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 28 Nov 2022 14:05:35 -0500 Subject: [PATCH] Add cache size info to Advanced prefs --- Tusker.xcodeproj/project.pbxproj | 4 +++ Tusker/Caching/DiskCache.swift | 4 +++ Tusker/Caching/ImageCache.swift | 4 +++ Tusker/Caching/ImageDataCache.swift | 2 +- Tusker/Extensions/FileManager+Size.swift | 35 +++++++++++++++++++ .../Preferences/AdvancedPrefsView.swift | 35 +++++++++++++++++-- 6 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 Tusker/Extensions/FileManager+Size.swift diff --git a/Tusker.xcodeproj/project.pbxproj b/Tusker.xcodeproj/project.pbxproj index f56bdcd2..dd59bd7f 100644 --- a/Tusker.xcodeproj/project.pbxproj +++ b/Tusker.xcodeproj/project.pbxproj @@ -48,6 +48,7 @@ D61F758A2932E1FC00C0B37F /* SwipeActionsPrefsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D61F75892932E1FC00C0B37F /* SwipeActionsPrefsView.swift */; }; D61F758D2933C69C00C0B37F /* StatusUpdatedNotificationTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D61F758B2933C69C00C0B37F /* StatusUpdatedNotificationTableViewCell.swift */; }; D61F758E2933C69C00C0B37F /* StatusUpdatedNotificationTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = D61F758C2933C69C00C0B37F /* StatusUpdatedNotificationTableViewCell.xib */; }; + D61F759029353B4300C0B37F /* FileManager+Size.swift in Sources */ = {isa = PBXBuildFile; fileRef = D61F758F29353B4300C0B37F /* FileManager+Size.swift */; }; D620483423D3801D008A63EF /* LinkTextView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D620483323D3801D008A63EF /* LinkTextView.swift */; }; D620483623D38075008A63EF /* ContentTextView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D620483523D38075008A63EF /* ContentTextView.swift */; }; D620483823D38190008A63EF /* StatusContentTextView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D620483723D38190008A63EF /* StatusContentTextView.swift */; }; @@ -412,6 +413,7 @@ D61F75892932E1FC00C0B37F /* SwipeActionsPrefsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwipeActionsPrefsView.swift; sourceTree = ""; }; D61F758B2933C69C00C0B37F /* StatusUpdatedNotificationTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusUpdatedNotificationTableViewCell.swift; sourceTree = ""; }; D61F758C2933C69C00C0B37F /* StatusUpdatedNotificationTableViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = StatusUpdatedNotificationTableViewCell.xib; sourceTree = ""; }; + D61F758F29353B4300C0B37F /* FileManager+Size.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "FileManager+Size.swift"; sourceTree = ""; }; D620483323D3801D008A63EF /* LinkTextView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinkTextView.swift; sourceTree = ""; }; D620483523D38075008A63EF /* ContentTextView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentTextView.swift; sourceTree = ""; }; D620483723D38190008A63EF /* StatusContentTextView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusContentTextView.swift; sourceTree = ""; }; @@ -1166,6 +1168,7 @@ D62E9984279CA23900C26176 /* URLSession+Development.swift */, D6ADB6ED28EA74E8009924AB /* UIView+Configure.swift */, D63CC70F2911F1E4000E19DE /* UIScrollView+Top.swift */, + D61F758F29353B4300C0B37F /* FileManager+Size.swift */, ); path = Extensions; sourceTree = ""; @@ -1935,6 +1938,7 @@ D6C143DA253510F4007DC240 /* ComposeEmojiTextField.swift in Sources */, D6DD2A3F273C1F4900386A6C /* ComposeAttachmentImage.swift in Sources */, D6DD2A45273D6C5700386A6C /* GIFImageView.swift in Sources */, + D61F759029353B4300C0B37F /* FileManager+Size.swift in Sources */, 0427033822B30F5F000D31B6 /* BehaviorPrefsView.swift in Sources */, D627943923A553B600D38C68 /* UnbookmarkStatusActivity.swift in Sources */, D64D0AAD2128D88B005A6F37 /* LocalData.swift in Sources */, diff --git a/Tusker/Caching/DiskCache.swift b/Tusker/Caching/DiskCache.swift index 9420ae15..aaf8b86f 100644 --- a/Tusker/Caching/DiskCache.swift +++ b/Tusker/Caching/DiskCache.swift @@ -122,6 +122,10 @@ class DiskCache { } } + func getSizeInBytes() -> Int64? { + return fileManager.recursiveSize(url: URL(fileURLWithPath: path, isDirectory: true)) + } + } extension DiskCache { diff --git a/Tusker/Caching/ImageCache.swift b/Tusker/Caching/ImageCache.swift index e9bfa3da..f7398260 100644 --- a/Tusker/Caching/ImageCache.swift +++ b/Tusker/Caching/ImageCache.swift @@ -110,6 +110,10 @@ class ImageCache { try cache.removeAll() } + func getDiskSizeInBytes() -> Int64? { + return cache.disk?.getSizeInBytes() + } + typealias Request = URLSessionDataTask } diff --git a/Tusker/Caching/ImageDataCache.swift b/Tusker/Caching/ImageDataCache.swift index 818421fa..e08e3104 100644 --- a/Tusker/Caching/ImageDataCache.swift +++ b/Tusker/Caching/ImageDataCache.swift @@ -11,7 +11,7 @@ import UIKit class ImageDataCache { private let memory: MemoryCache - private let disk: DiskCache? + let disk: DiskCache? private let storeOriginalDataInMemory: Bool private let desiredPixelSize: CGSize? diff --git a/Tusker/Extensions/FileManager+Size.swift b/Tusker/Extensions/FileManager+Size.swift new file mode 100644 index 00000000..8b59bc10 --- /dev/null +++ b/Tusker/Extensions/FileManager+Size.swift @@ -0,0 +1,35 @@ +// +// FileManager+Size.swift +// Tusker +// +// Created by Shadowfacts on 11/28/22. +// Copyright © 2022 Shadowfacts. All rights reserved. +// + +import Foundation + +extension FileManager { + func recursiveSize(url: URL) -> Int64? { + if (try? url.resourceValues(forKeys: [.isRegularFileKey]).isRegularFile) == true { + return size(url: url) + } else { + guard let enumerator = enumerator(at: url, includingPropertiesForKeys: [.isRegularFileKey, .fileSizeKey, .totalFileAllocatedSizeKey]) else { + return nil + } + var total: Int64 = 0 + for case let url as URL in enumerator { + total += size(url: url) ?? 0 + } + return total + } + } +} + +private func size(url: URL) -> Int64? { + guard let resourceValues = try? url.resourceValues(forKeys: [.isRegularFileKey, .fileSizeKey, .totalFileAllocatedSizeKey]), + resourceValues.isRegularFile ?? false, + let size = resourceValues.fileSize ?? resourceValues.totalFileAllocatedSize else { + return nil + } + return Int64(size) +} diff --git a/Tusker/Screens/Preferences/AdvancedPrefsView.swift b/Tusker/Screens/Preferences/AdvancedPrefsView.swift index bac9e53f..f691a658 100644 --- a/Tusker/Screens/Preferences/AdvancedPrefsView.swift +++ b/Tusker/Screens/Preferences/AdvancedPrefsView.swift @@ -11,7 +11,9 @@ import CoreData struct AdvancedPrefsView : View { @ObservedObject var preferences = Preferences.shared - + @State private var imageCacheSize: Int64 = 0 + @State private var mastodonCacheSize: Int64 = 0 + var body: some View { List { formattingSection @@ -64,13 +66,42 @@ struct AdvancedPrefsView : View { } var cachingSection: some View { - Section(header: Text("Caching"), footer: Text("Clearing caches will restart the app.")) { + Section { Button(action: clearCache) { Text("Clear Mastodon Cache") }.foregroundColor(.red) Button(action: clearImageCaches) { Text("Clear Image Caches") }.foregroundColor(.red) + } header: { + Text("Caching") + } footer: { + var s: AttributedString = "Clearing caches will restart the app." + if imageCacheSize != 0 { + s += AttributedString("\nImage cache size: \(ByteCountFormatter().string(fromByteCount: imageCacheSize))") + } + if mastodonCacheSize != 0 { + s += AttributedString("\nMastodon cache size: \(ByteCountFormatter().string(fromByteCount: mastodonCacheSize))") + } + return Text(s) + }.task { + imageCacheSize = [ + ImageCache.avatars, + .headers, + .attachments, + .emojis, + ].map { + $0.getDiskSizeInBytes() ?? 0 + }.reduce(0, +) + mastodonCacheSize = LocalData.shared.accounts.map { + let descriptions = MastodonController.getForAccount($0).persistentContainer.persistentStoreDescriptions + return descriptions.map { + guard let url = $0.url else { + return 0 + } + return FileManager.default.recursiveSize(url: url) ?? 0 + }.reduce(0, +) + }.reduce(0, +) } }