增加历史token的删除逻辑
This commit is contained in:
parent
78f8752aa7
commit
ad45e56728
@ -201,6 +201,27 @@ class AppContext {
|
|||||||
return []
|
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 {
|
private func saveCacheToken(_ token: String, networkName: String) throws {
|
||||||
let normalizedToken = token.trimmingCharacters(in: .whitespacesAndNewlines)
|
let normalizedToken = token.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||||
guard !normalizedToken.isEmpty else {
|
guard !normalizedToken.isEmpty else {
|
||||||
|
|||||||
@ -299,27 +299,40 @@ struct LoginTokenView: View {
|
|||||||
private var tokenHistoryPopover: some View {
|
private var tokenHistoryPopover: some View {
|
||||||
VStack(alignment: .leading, spacing: 0) {
|
VStack(alignment: .leading, spacing: 0) {
|
||||||
ForEach(visibleTokenHistory, id: \.token) { item in
|
ForEach(visibleTokenHistory, id: \.token) { item in
|
||||||
Button(action: {
|
HStack(spacing: 4) {
|
||||||
self.token = item.token
|
Button(action: {
|
||||||
self.dismissTokenHistory()
|
self.token = item.token
|
||||||
}) {
|
self.dismissTokenHistory()
|
||||||
HStack(spacing: 8) {
|
}) {
|
||||||
Image(systemName: item.token == normalizedToken ? "checkmark" : "key.fill")
|
HStack(spacing: 8) {
|
||||||
.foregroundColor(.secondary)
|
Image(systemName: item.token == normalizedToken ? "checkmark" : "key.fill")
|
||||||
.frame(width: 14)
|
.foregroundColor(.secondary)
|
||||||
|
.frame(width: 14)
|
||||||
|
|
||||||
Text(tokenHistoryTitle(item))
|
Text(tokenHistoryTitle(item))
|
||||||
.font(.system(size: 12))
|
.font(.system(size: 12))
|
||||||
.lineLimit(1)
|
.lineLimit(1)
|
||||||
.truncationMode(.middle)
|
.truncationMode(.middle)
|
||||||
|
|
||||||
Spacer(minLength: 0)
|
Spacer(minLength: 0)
|
||||||
|
}
|
||||||
|
.contentShape(Rectangle())
|
||||||
|
.padding(.leading, 10)
|
||||||
|
.padding(.vertical, 6)
|
||||||
}
|
}
|
||||||
.contentShape(Rectangle())
|
.buttonStyle(.plain)
|
||||||
.padding(.horizontal, 10)
|
|
||||||
.padding(.vertical, 6)
|
Button(action: {
|
||||||
|
self.deleteTokenHistoryItem(item)
|
||||||
|
}) {
|
||||||
|
Image(systemName: "trash")
|
||||||
|
.font(.system(size: 11, weight: .medium))
|
||||||
|
.foregroundColor(.secondary)
|
||||||
|
.frame(width: 24, height: 24)
|
||||||
|
}
|
||||||
|
.buttonStyle(.plain)
|
||||||
}
|
}
|
||||||
.buttonStyle(.plain)
|
.padding(.trailing, 6)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.frame(width: 280)
|
.frame(width: 280)
|
||||||
@ -341,6 +354,17 @@ struct LoginTokenView: View {
|
|||||||
self.showTokenHistory = wantsTokenHistory && !visibleTokenHistory.isEmpty
|
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 {
|
private func tokenHistoryTitle(_ item: TokenHistoryItem) -> String {
|
||||||
guard let networkName = item.networkName, !networkName.isEmpty else {
|
guard let networkName = item.networkName, !networkName.isEmpty else {
|
||||||
return item.token
|
return item.token
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user