Empty Cache even if Cache is not enabled

This commit is contained in:
Nindi Gill 2022-12-11 22:12:31 +11:00
parent 524cf3d105
commit 33a866ef0f

View file

@ -10,7 +10,7 @@ import SwiftUI
struct SettingsInstallersCacheView: View { struct SettingsInstallersCacheView: View {
@Binding var cacheDownloads: Bool @Binding var cacheDownloads: Bool
@Binding var cacheDirectory: String @Binding var cacheDirectory: String
@State private var cacheSize: String = "" @State private var cacheSize: UInt64 = 0
@State private var buttonClicked: Bool = false @State private var buttonClicked: Bool = false
@State private var openPanel: NSOpenPanel = NSOpenPanel() @State private var openPanel: NSOpenPanel = NSOpenPanel()
@ -32,26 +32,26 @@ struct SettingsInstallersCacheView: View {
PathControl(path: $cacheDirectory) PathControl(path: $cacheDirectory)
.disabled(true) .disabled(true)
HStack(alignment: .firstTextBaseline) { HStack(alignment: .firstTextBaseline) {
FooterText("Cache directory currently contains \(cacheSize) of data.") FooterText("Cache directory currently contains \(cacheSize.bytesString()) of data.")
Spacer() Spacer()
Button("Empty Cache...") { Button("Empty Cache...") {
buttonClicked.toggle() buttonClicked.toggle()
} }
.disabled(cacheSize == 0)
} }
.disabled(!cacheDownloads)
} }
.onAppear { .onAppear {
getCacheSize() retrieveCacheSize()
} }
.onChange(of: cacheDirectory) { _ in .onChange(of: cacheDirectory) { _ in
getCacheSize() retrieveCacheSize()
} }
.alert(isPresented: $buttonClicked) { .alert(isPresented: $buttonClicked) {
Alert( Alert(
title: Text("Empty Cache Directory?"), 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(), primaryButton: .cancel(),
secondaryButton: .destructive(Text("Empty")) { emptyCache() ; getCacheSize() } secondaryButton: .destructive(Text("Empty")) { emptyCache() ; retrieveCacheSize() }
) )
} }
} }
@ -75,7 +75,7 @@ struct SettingsInstallersCacheView: View {
cacheDirectory = url.path cacheDirectory = url.path
} }
private func getCacheSize() { private func retrieveCacheSize() {
let url: URL = URL(fileURLWithPath: cacheDirectory) let url: URL = URL(fileURLWithPath: cacheDirectory)
var isDirectory: ObjCBool = false var isDirectory: ObjCBool = false
@ -85,8 +85,7 @@ struct SettingsInstallersCacheView: View {
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true) try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)
} }
let size: UInt64 = try FileManager.default.sizeOfDirectory(at: url) cacheSize = try FileManager.default.sizeOfDirectory(at: url)
cacheSize = size.bytesString()
} catch { } catch {
print(error.localizedDescription) print(error.localizedDescription)
} }