Caps-iOS/Caps/Views/SettingsView.swift
2024-03-01 17:37:10 +01:00

148 lines
5.6 KiB
Swift

import SwiftUI
import SFSafeSymbols
struct SettingsView: View {
@EnvironmentObject
var database: Database
@Binding
var isPresented: Bool
@AppStorage("authKey")
private var serverAuthenticationKey: String = ""
@State
private var imageCacheSize: Int = 0
var body: some View {
NavigationView {
VStack(alignment: .leading, spacing: 3) {
Text("Server")
.font(.footnote)
.textCase(.uppercase)
.foregroundColor(.secondary)
.padding(.top)
Group {
FancyTextField(
text: $database.serverPath,
icon: .globe,
placeholder: "Server url")
.keyboardType(.URL)
}.padding(.horizontal)
Text("Authentication")
.font(.footnote)
.textCase(.uppercase)
.foregroundColor(.secondary)
.padding(.top)
Group {
FancyTextField(text: $serverAuthenticationKey, icon: .key, placeholder: "Server key")
.keyboardType(.asciiCapable)
}.padding(.horizontal)
Text("Statistics")
.font(.footnote)
.textCase(.uppercase)
.foregroundColor(.secondary)
.padding(.top)
Group {
SettingsStatisticRow(label: "Caps", value: "\(database.numberOfCaps)")
SettingsStatisticRow(label: "Images per cap", value: String(format: "%.1f", database.averageImageCount))
SettingsStatisticRow(label: "Total images", value: "\(database.numberOfImages)")
CachedImageCountView(cache: database.images)
SettingsStatisticRow(label: "Cache size", value: byteString(imageCacheSize))
SettingsStatisticRow(label: "Database", value: byteString(database.databaseSize))
if database.pendingImageUploadCount > 0 {
SettingsStatisticRow(label: "Images to upload", value: "\(database.pendingImageUploadCount)")
}
HStack {
Spacer()
Button(action: clearImageCache) {
Label("Clear image cache", systemSymbol: .trash)
}
.padding()
Spacer()
}
}.padding(.horizontal)
Text("Classifier")
.font(.footnote)
.textCase(.uppercase)
.foregroundColor(.secondary)
.padding(.top)
Group {
SettingsStatisticRow(label: "Server Version", value: "\(database.serverClassifierVersion)")
SettingsStatisticRow(label: "Local Version", value: "\(database.localClassifierVersion)")
SettingsStatisticRow(label: "Size", value: byteString(database.classifierSize))
SettingsStatisticRow(label: "Recognized caps", value: "\(database.classifierClassCount)")
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)
}
}.padding(.horizontal)
Spacer()
}
.padding(.horizontal)
.navigationTitle("Info")
.onAppear(perform: updateImageCacheSize)
}
}
private func updateImageCacheSize() {
imageCacheSize = database.imageCacheSize()
}
private func clearImageCache() {
Task {
database.clearImageCache()
DispatchQueue.main.async {
updateImageCacheSize()
}
}
}
private func downloadNewClassifier() {
Task {
// Ensure that correct version is saved
await database.updateServerClassifierVersion()
await database.downloadClassifier()
await database.downloadClassifierClasses()
}
}
private func updateClassifierVersion() {
Task {
await database.updateServerClassifierVersion()
}
}
private func hide() {
isPresented = false
}
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)
//.previewLayout(.fixed(width: 375, height: 410))
}
}