- Connect SettingsView font selection to WebView CSS rendering - Add dynamic font size and family mapping in WebView - Implement CSS custom properties for responsive font scaling - Add font family fallback stacks for cross-platform compatibility - Update WebView to use Settings object for typography configuration
158 lines
5.9 KiB
Swift
158 lines
5.9 KiB
Swift
import Foundation
|
|
import CoreData
|
|
|
|
struct Settings {
|
|
var endpoint: String? = nil
|
|
var username: String? = nil
|
|
var password: String? = nil
|
|
var token: String? = nil
|
|
|
|
var fontFamily: FontFamily? = nil
|
|
var fontSize: FontSize? = nil
|
|
|
|
var isLoggedIn: Bool {
|
|
token != nil && !token!.isEmpty
|
|
}
|
|
|
|
mutating func setToken(_ newToken: String) {
|
|
token = newToken
|
|
}
|
|
}
|
|
|
|
protocol PSettingsRepository {
|
|
func saveSettings(_ settings: Settings) async throws
|
|
func loadSettings() async throws -> Settings?
|
|
func clearSettings() async throws
|
|
func saveToken(_ token: String) async throws
|
|
}
|
|
|
|
class SettingsRepository: PSettingsRepository {
|
|
private let coreDataManager = CoreDataManager.shared
|
|
|
|
func saveSettings(_ settings: Settings) async throws {
|
|
let context = coreDataManager.context
|
|
|
|
return try await withCheckedThrowingContinuation { continuation in
|
|
context.perform {
|
|
do {
|
|
// Vorhandene Einstellungen löschen
|
|
let fetchRequest: NSFetchRequest<SettingEntity> = SettingEntity.fetchRequest()
|
|
if let existingSettings = try context.fetch(fetchRequest).first {
|
|
|
|
if let endpoint = settings.endpoint, !endpoint.isEmpty {
|
|
existingSettings.endpoint = endpoint
|
|
}
|
|
|
|
if let username = settings.username, !username.isEmpty {
|
|
existingSettings.username = username
|
|
}
|
|
|
|
if let password = settings.password, !password.isEmpty {
|
|
existingSettings.password = password
|
|
}
|
|
|
|
if let token = settings.token, !token.isEmpty {
|
|
existingSettings.token = token
|
|
}
|
|
|
|
if let fontFamily = settings.fontFamily {
|
|
existingSettings.fontFamily = fontFamily.rawValue
|
|
}
|
|
|
|
if let fontSize = settings.fontSize {
|
|
existingSettings.fontSize = fontSize.rawValue
|
|
}
|
|
|
|
try context.save()
|
|
}
|
|
|
|
continuation.resume()
|
|
} catch {
|
|
continuation.resume(throwing: error)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func loadSettings() async throws -> Settings? {
|
|
let context = coreDataManager.context
|
|
|
|
return try await withCheckedThrowingContinuation { continuation in
|
|
context.perform {
|
|
do {
|
|
let fetchRequest: NSFetchRequest<SettingEntity> = SettingEntity.fetchRequest()
|
|
fetchRequest.fetchLimit = 1
|
|
|
|
let settingEntities = try context.fetch(fetchRequest)
|
|
|
|
if let settingEntity = settingEntities.first {
|
|
let settings = Settings(
|
|
endpoint: settingEntity.endpoint ?? "",
|
|
username: settingEntity.username ?? "",
|
|
password: settingEntity.password ?? "",
|
|
token: settingEntity.token,
|
|
fontFamily: FontFamily(rawValue: settingEntity.fontFamily ?? FontFamily.system.rawValue),
|
|
fontSize: FontSize(rawValue: settingEntity.fontSize ?? FontSize.medium.rawValue)
|
|
)
|
|
continuation.resume(returning: settings)
|
|
} else {
|
|
continuation.resume(returning: nil)
|
|
}
|
|
} catch {
|
|
continuation.resume(throwing: error)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func clearSettings() async throws {
|
|
let context = coreDataManager.context
|
|
|
|
return try await withCheckedThrowingContinuation { continuation in
|
|
context.perform {
|
|
do {
|
|
let fetchRequest: NSFetchRequest<SettingEntity> = SettingEntity.fetchRequest()
|
|
let settingEntities = try context.fetch(fetchRequest)
|
|
|
|
for settingEntity in settingEntities {
|
|
context.delete(settingEntity)
|
|
}
|
|
|
|
try context.save()
|
|
continuation.resume()
|
|
} catch {
|
|
continuation.resume(throwing: error)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func saveToken(_ token: String) async throws {
|
|
let context = coreDataManager.context
|
|
|
|
return try await withCheckedThrowingContinuation { continuation in
|
|
context.perform {
|
|
do {
|
|
let fetchRequest: NSFetchRequest<SettingEntity> = SettingEntity.fetchRequest()
|
|
fetchRequest.fetchLimit = 1
|
|
|
|
let settingEntities = try context.fetch(fetchRequest)
|
|
|
|
if let settingEntity = settingEntities.first {
|
|
settingEntity.token = token
|
|
} else {
|
|
// Fallback: Neue Einstellung erstellen (sollte normalerweise nicht passieren)
|
|
let settingEntity = SettingEntity(context: context)
|
|
settingEntity.token = token
|
|
}
|
|
|
|
try context.save()
|
|
continuation.resume()
|
|
} catch {
|
|
continuation.resume(throwing: error)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|