Caps-iOS/Caps/Views/SettingsView.swift

101 lines
3.5 KiB
Swift
Raw Normal View History

2022-06-10 21:20:49 +02:00
import SwiftUI
2022-06-11 11:27:56 +02:00
import SFSafeSymbols
2022-06-10 21:20:49 +02:00
struct SettingsView: View {
2022-06-11 11:27:56 +02:00
2022-06-10 21:20:49 +02:00
@EnvironmentObject
var database: Database
2022-06-11 11:27:56 +02:00
2022-06-10 21:20:49 +02:00
@Binding
var isPresented: Bool
2022-06-11 11:27:56 +02:00
@AppStorage("authKey")
private var serverAuthenticationKey: String = ""
2022-06-24 12:05:38 +02:00
@State
private var imageCacheSize: Int = 0
2022-06-10 21:20:49 +02:00
var body: some View {
2022-06-11 11:27:56 +02:00
NavigationView {
VStack(alignment: .leading, spacing: 3) {
Text("Authentication")
.font(.footnote)
.textCase(.uppercase)
.foregroundColor(.secondary)
.padding(.top)
Group {
FancyTextField(text: $serverAuthenticationKey, icon: .key, placeholder: "Server key")
}.padding(.horizontal)
Text("Statistics")
.font(.footnote)
.textCase(.uppercase)
.foregroundColor(.secondary)
.padding(.top)
Group {
SettingsStatisticRow(label: "Caps", value: "\(database.numberOfCaps)")
SettingsStatisticRow(label: "Total images", value: "\(database.numberOfImages)")
SettingsStatisticRow(label: "Images per cap", value: String(format: "%.1f", database.averageImageCount))
}.padding(.horizontal)
Text("Classifier")
.font(.footnote)
.textCase(.uppercase)
.foregroundColor(.secondary)
.padding(.top)
Group {
SettingsStatisticRow(label: "Version", value: "\(database.classifierVersion)")
SettingsStatisticRow(label: "Recognized caps", value: "\(database.classifierClassCount)")
}.padding(.horizontal)
Text("Storage")
.font(.footnote)
.textCase(.uppercase)
.foregroundColor(.secondary)
.padding(.top)
Group {
2022-06-24 12:05:38 +02:00
SettingsStatisticRow(label: "Image cache", value: byteString(imageCacheSize))
2022-06-11 11:27:56 +02:00
SettingsStatisticRow(label: "Database", value: byteString(database.databaseSize))
SettingsStatisticRow(label: "Classifier", value: byteString(database.classifierSize))
2023-07-28 13:20:12 +02:00
HStack {
Spacer()
Button("Clear image cache", action: clearImageCache)
.padding()
Spacer()
}
2022-06-11 11:27:56 +02:00
}.padding(.horizontal)
2022-06-10 21:20:49 +02:00
Spacer()
}
2022-06-11 11:27:56 +02:00
.padding(.horizontal)
2023-04-17 14:20:25 +02:00
.navigationTitle("Info")
2022-06-24 12:05:38 +02:00
.onAppear(perform: updateImageCacheSize)
}
}
private func updateImageCacheSize() {
2023-07-28 13:20:12 +02:00
imageCacheSize = database.imageCacheSize()
}
private func clearImageCache() {
2022-06-24 12:05:38 +02:00
Task {
2023-07-28 13:20:12 +02:00
database.clearImageCache()
DispatchQueue.main.async {
updateImageCacheSize()
}
2022-06-10 21:20:49 +02:00
}
}
2022-06-11 11:27:56 +02:00
2022-06-10 21:20:49 +02:00
private func hide() {
isPresented = false
}
2022-06-11 11:27:56 +02:00
2022-06-10 21:20:49 +02:00
private func byteString(_ count: Int) -> String {
ByteCountFormatter.string(fromByteCount: Int64(count), countStyle: .file)
}
}
struct SettingsView_Previews: PreviewProvider {
static var previews: some View {
SettingsView(isPresented: .constant(true))
.environmentObject(Database.mock)
2022-06-11 11:27:56 +02:00
//.previewLayout(.fixed(width: 375, height: 410))
2022-06-10 21:20:49 +02:00
}
}