Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display the URL cache size #604

Merged
merged 3 commits into from
Oct 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions Demo/Sources/Settings/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,35 @@ import AVFoundation
import Player
import SwiftUI

private struct UrlCacheView: View {
@State private var urlCacheSize: String = ""

var body: some View {
HStack {
Button("Clear URL cache", action: clearUrlCache)
#if os(iOS)
.buttonStyle(.borderless)
#endif
Spacer()
Text(urlCacheSize)
.font(.footnote)
.foregroundColor(.red)
}
.onAppear {
updateCacheSize()
}
defagos marked this conversation as resolved.
Show resolved Hide resolved
}

private func updateCacheSize() {
urlCacheSize = ByteCountFormatter.string(fromByteCount: Int64(URLCache.shared.currentDiskUsage), countStyle: .binary)
}

private func clearUrlCache() {
URLCache.shared.removeAllCachedResponses()
updateCacheSize()
}
}

struct SettingsView: View {
@AppStorage(UserDefaults.presenterModeEnabledKey)
private var isPresenterModeEnabled = false
Expand Down Expand Up @@ -104,7 +133,7 @@ struct SettingsView: View {
private func debuggingSection() -> some View {
Section {
Button("Simulate memory warning", action: simulateMemoryWarning)
Button("Clear URL cache", action: clearUrlCache)
UrlCacheView()
} header: {
Text("Debugging")
} footer: {
Expand All @@ -130,10 +159,6 @@ struct SettingsView: View {
private func simulateMemoryWarning() {
UIApplication.shared.perform(Selector(("_performMemoryWarning")))
}

private func clearUrlCache() {
URLCache.shared.removeAllCachedResponses()
}
}

#Preview {
Expand Down