Caps-iOS/Caps/Views/SettingsView.swift

137 lines
5.0 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 {
2023-10-24 14:51:08 +02:00
SettingsStatisticRow(label: "Server Version", value: "\(database.serverClassifierVersion)")
SettingsStatisticRow(label: "Local Version", value: "\(database.localClassifierVersion)")
2022-06-11 11:27:56 +02:00
SettingsStatisticRow(label: "Recognized caps", value: "\(database.classifierClassCount)")
2023-10-24 14:51:08 +02:00
HStack {
Spacer()
Button(action: updateClassifierVersion) {
Label("Refresh", systemSymbol: .arrowCounterclockwise)
}
.disabled(database.classifierDownloadProgress != nil)
.padding()
if database.localClassifierVersion != database.serverClassifierVersion {
Button(action: downloadNewClassifier) {
Label("Download", systemSymbol: .squareAndArrowDown)
}
.disabled(database.classifierDownloadProgress != nil)
.padding()
}
Spacer()
}
if let progress = database.classifierDownloadProgress {
ClassifierDownloadView(progress: progress)
}
2022-06-11 11:27:56 +02:00
}.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()
2023-10-24 14:51:08 +02:00
Button(action: clearImageCache) {
Label("Clear image cache", systemSymbol: .trash)
}
2023-07-28 13:20:12 +02:00
.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
2023-10-24 14:51:08 +02:00
private func downloadNewClassifier() {
Task {
// Ensure that correct version is saved
await database.updateServerClassifierVersion()
await database.downloadClassifier()
}
}
private func updateClassifierVersion() {
Task {
await database.updateServerClassifierVersion()
}
}
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
}
}