增加历史token的删除逻辑

This commit is contained in:
anlicheng 2026-05-11 12:05:08 +08:00
parent 78f8752aa7
commit ad45e56728
2 changed files with 62 additions and 17 deletions

View File

@ -201,6 +201,27 @@ class AppContext {
return []
}
func deleteCacheToken(_ token: String) throws {
let normalizedToken = token.trimmingCharacters(in: .whitespacesAndNewlines)
guard !normalizedToken.isEmpty else {
return
}
var items = self.loadCacheTokenHistory()
items.removeAll { $0.token == normalizedToken }
items = Array(items.prefix(maxCachedTokenCount))
let historyData = try JSONEncoder().encode(items)
try KeychainStore.shared.save(historyData, account: cachedTokenHistoryAccount)
if let firstToken = items.first?.token,
let data = firstToken.data(using: .utf8) {
try KeychainStore.shared.save(data, account: cachedTokenAccount)
} else {
try KeychainStore.shared.delete(account: cachedTokenAccount)
}
}
private func saveCacheToken(_ token: String, networkName: String) throws {
let normalizedToken = token.trimmingCharacters(in: .whitespacesAndNewlines)
guard !normalizedToken.isEmpty else {

View File

@ -299,6 +299,7 @@ struct LoginTokenView: View {
private var tokenHistoryPopover: some View {
VStack(alignment: .leading, spacing: 0) {
ForEach(visibleTokenHistory, id: \.token) { item in
HStack(spacing: 4) {
Button(action: {
self.token = item.token
self.dismissTokenHistory()
@ -316,10 +317,22 @@ struct LoginTokenView: View {
Spacer(minLength: 0)
}
.contentShape(Rectangle())
.padding(.horizontal, 10)
.padding(.leading, 10)
.padding(.vertical, 6)
}
.buttonStyle(.plain)
Button(action: {
self.deleteTokenHistoryItem(item)
}) {
Image(systemName: "trash")
.font(.system(size: 11, weight: .medium))
.foregroundColor(.secondary)
.frame(width: 24, height: 24)
}
.buttonStyle(.plain)
}
.padding(.trailing, 6)
}
}
.frame(width: 280)
@ -341,6 +354,17 @@ struct LoginTokenView: View {
self.showTokenHistory = wantsTokenHistory && !visibleTokenHistory.isEmpty
}
private func deleteTokenHistoryItem(_ item: TokenHistoryItem) {
do {
try self.appContext.deleteCacheToken(item.token)
self.tokenHistory = self.appContext.loadCacheTokenHistory()
self.showTokenHistory = wantsTokenHistory && !visibleTokenHistory.isEmpty
} catch {
self.showAlert = true
self.errorMessage = error.localizedDescription
}
}
private func tokenHistoryTitle(_ item: TokenHistoryItem) -> String {
guard let networkName = item.networkName, !networkName.isEmpty else {
return item.token