Caps-iOS/Caps/Views/SettingsView.swift

148 lines
5.6 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) {
2024-03-01 17:37:10 +01:00
Text("Server")
.font(.footnote)
.textCase(.uppercase)
.foregroundColor(.secondary)
.padding(.top)
Group {
FancyTextField(
text: $database.serverPath,
icon: .globe,
placeholder: "Server url")
.keyboardType(.URL)
}.padding(.horizontal)
2022-06-11 11:27:56 +02:00
Text("Authentication")
.font(.footnote)
.textCase(.uppercase)
.foregroundColor(.secondary)
.padding(.top)
Group {
FancyTextField(text: $serverAuthenticationKey, icon: .key, placeholder: "Server key")
2024-03-01 17:37:10 +01:00
.keyboardType(.asciiCapable)
2022-06-11 11:27:56 +02:00
}.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))
2023-10-24 15:44:13 +02:00
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()
}
2022-06-11 11:27:56 +02:00
}.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)")
2023-10-24 15:44:13 +02:00
SettingsStatisticRow(label: "Size", value: byteString(database.classifierSize))
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)
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()
2023-10-24 15:44:13 +02:00
await database.downloadClassifierClasses()
2023-10-24 14:51:08 +02:00
}
}
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
}
}