ReadKeep/readeck/Data/TokenProvider.swift
Ilyas Hallak e88693363b Refactor authentication, settings, and UI; add search and improve bookmark image handling
- Refactor authentication flow to require endpoint for login and decouple token saving
- Add and integrate search functionality for bookmarks
- Simplify and improve server settings setup (remove connection test, direct save & login)
- Update sidebar/tab navigation to include search and improve structure
- Show placeholder image in BookmarkCardView if no image is available, ensuring consistent layout
- Improve BookmarkDetailView header and meta info display
- Add utility for domain extraction from URLs
- General code cleanup and minor UI/UX improvements
2025-07-03 21:45:53 +02:00

60 lines
1.6 KiB
Swift

import Foundation
protocol TokenProvider {
func getToken() async -> String?
func getEndpoint() async -> String?
func setToken(_ token: String) async
func clearToken() async
}
class CoreDataTokenProvider: TokenProvider {
private let settingsRepository = SettingsRepository()
private var cachedSettings: Settings?
private var isLoaded = false
private func loadSettingsIfNeeded() async {
guard !isLoaded else { return }
do {
cachedSettings = try await settingsRepository.loadSettings()
isLoaded = true
} catch {
print("Failed to load settings: \(error)")
cachedSettings = nil
}
}
func getToken() async -> String? {
await loadSettingsIfNeeded()
return cachedSettings?.token
}
func getEndpoint() async -> String? {
await loadSettingsIfNeeded()
// Basis-URL ohne /api Suffix, da es in der API-Klasse hinzugefügt wird
return cachedSettings?.endpoint
}
func setToken(_ token: String) async {
await loadSettingsIfNeeded()
do {
try await settingsRepository.saveToken(token)
if cachedSettings != nil {
cachedSettings!.token = token
}
} catch {
print("Failed to save token: \(error)")
}
}
func clearToken() async {
do {
try await settingsRepository.clearSettings()
cachedSettings = nil
} catch {
print("Failed to clear settings: \(error)")
}
}
}