Skip to content

Commit

Permalink
feat: Allow tokens to be pinned to the top (#69)
Browse files Browse the repository at this point in the history
* Added pin token

* Rotate pin
  • Loading branch information
joeldavidw authored Sep 28, 2024
1 parent 9fdf9af commit 34cf48e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
27 changes: 25 additions & 2 deletions Chronos/App/Tabs/Tokens/Row/TokenRowView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ struct TokenRowView: View {

let timer: Publishers.Autoconnect<Timer.TimerPublisher>

let triggerSortAndFilterTokenPairs: () -> Void

var token: Token {
return tokenPair.token
}
Expand All @@ -28,7 +30,8 @@ struct TokenRowView: View {
return tokenPair.encToken
}

let otpService = Container.shared.otpService()
private let otpService = Container.shared.otpService()
private let cryptoService = Container.shared.cryptoService()

var body: some View {
VStack(alignment: .leading, spacing: 8) {
Expand All @@ -42,6 +45,14 @@ struct TokenRowView: View {
.foregroundStyle(.gray)
.lineLimit(1)
}

if token.pinned ?? false {
Spacer()

Image(systemName: "pin.fill")
.font(.system(size: 12))
.rotationEffect(Angle(degrees: 45))
}
}

if stateTapToRevealEnabled && !tokenRevealed {
Expand Down Expand Up @@ -149,6 +160,18 @@ struct TokenRowView: View {
}
.tint(.blue)

Button {
token.pinned = !(token.pinned ?? false)
cryptoService.updateEncryptedToken(encryptedToken: encryptedToken, token: token)
triggerSortAndFilterTokenPairs()
} label: {
VStack(alignment: .center) {
Image(systemName: token.pinned ?? false ? "pin.slash" : "pin")
Text(token.pinned ?? false ? "Unpin" : "Pin")
}
}
.tint(.indigo)

Button {
self.showTokenQRSheet = true
} label: {
Expand All @@ -157,7 +180,7 @@ struct TokenRowView: View {
Text("QR")
}
}
.tint(.indigo)
.tint(.gray)
}
}

Expand Down
13 changes: 12 additions & 1 deletion Chronos/App/Tabs/Tokens/TokensTab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ struct TokensTab: View {
var body: some View {
NavigationStack {
List(tokenPairs) { tokenPair in
TokenRowView(tokenPair: tokenPair, timer: timer)
TokenRowView(tokenPair: tokenPair, timer: timer, triggerSortAndFilterTokenPairs: self.sortAndFilterTokenPairs)
}
.onAppear { Task { await updateTokenPairs() } }
.onChange(of: encryptedTokens) { _, _ in
Expand Down Expand Up @@ -194,6 +194,17 @@ struct TokensTab: View {
tokenPair.token.account.localizedCaseInsensitiveContains(searchQuery)
}
.sorted { token1, token2 in
let pinned1 = token1.token.pinned ?? false
let pinned2 = token2.token.pinned ?? false

if pinned1 != pinned2 {
return pinned1
}

if pinned1 {
return token1.token.issuer.localizedCaseInsensitiveCompare(token2.token.issuer) == .orderedAscending
}

switch sortCriteria {
case .ISSUER_ASC:
return token1.token.issuer.localizedCaseInsensitiveCompare(token2.token.issuer) == .orderedAscending
Expand Down
3 changes: 3 additions & 0 deletions Chronos/Data/Token.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class Token: Codable, Identifiable {

// HOTP
var counter: Int = 0

// Extra Data
var pinned: Bool? = false
}

func validateToken(
Expand Down
1 change: 1 addition & 0 deletions Chronos/Services/CryptoService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ extension CryptoService {
let tokenJson = try JSONDecoder().decode(Token.self, from: Data(decrypted.plainText))
return tokenJson
} catch {
print(error)
return nil
}
}
Expand Down

0 comments on commit 34cf48e

Please sign in to comment.