From 33a866ef0f0f34ee3298e0102b93f5e6c836bd8a Mon Sep 17 00:00:00 2001 From: Nindi Gill Date: Sun, 11 Dec 2022 22:12:31 +1100 Subject: [PATCH] Empty Cache even if Cache is not enabled --- .../SettingsInstallersCacheView.swift | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/Mist/Views/Settings/SettingsInstallersCacheView.swift b/Mist/Views/Settings/SettingsInstallersCacheView.swift index ae0066c..d61fcc3 100644 --- a/Mist/Views/Settings/SettingsInstallersCacheView.swift +++ b/Mist/Views/Settings/SettingsInstallersCacheView.swift @@ -10,7 +10,7 @@ import SwiftUI struct SettingsInstallersCacheView: View { @Binding var cacheDownloads: Bool @Binding var cacheDirectory: String - @State private var cacheSize: String = "" + @State private var cacheSize: UInt64 = 0 @State private var buttonClicked: Bool = false @State private var openPanel: NSOpenPanel = NSOpenPanel() @@ -32,26 +32,26 @@ struct SettingsInstallersCacheView: View { PathControl(path: $cacheDirectory) .disabled(true) HStack(alignment: .firstTextBaseline) { - FooterText("Cache directory currently contains \(cacheSize) of data.") + FooterText("Cache directory currently contains \(cacheSize.bytesString()) of data.") Spacer() Button("Empty Cache...") { buttonClicked.toggle() } + .disabled(cacheSize == 0) } - .disabled(!cacheDownloads) } .onAppear { - getCacheSize() + retrieveCacheSize() } .onChange(of: cacheDirectory) { _ in - getCacheSize() + retrieveCacheSize() } .alert(isPresented: $buttonClicked) { Alert( title: Text("Empty Cache Directory?"), - message: Text("Emptying the cache directory will free up \(cacheSize)."), + message: Text("Emptying the cache directory will free up \(cacheSize.bytesString())."), primaryButton: .cancel(), - secondaryButton: .destructive(Text("Empty")) { emptyCache() ; getCacheSize() } + secondaryButton: .destructive(Text("Empty")) { emptyCache() ; retrieveCacheSize() } ) } } @@ -75,7 +75,7 @@ struct SettingsInstallersCacheView: View { cacheDirectory = url.path } - private func getCacheSize() { + private func retrieveCacheSize() { let url: URL = URL(fileURLWithPath: cacheDirectory) var isDirectory: ObjCBool = false @@ -85,8 +85,7 @@ struct SettingsInstallersCacheView: View { try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true) } - let size: UInt64 = try FileManager.default.sizeOfDirectory(at: url) - cacheSize = size.bytesString() + cacheSize = try FileManager.default.sizeOfDirectory(at: url) } catch { print(error.localizedDescription) }