Skip to content

Commit

Permalink
Avoid rendering trailing spaces for 6-digit codes (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
KatherineInCode authored May 7, 2024
1 parent edff0c7 commit f1dc721
Show file tree
Hide file tree
Showing 16 changed files with 191 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public struct TOTPCodeModel: Equatable, Sendable {
code.enumerated().map { index, character in
guard (index + 1) % 3 == 0 else { return "\(character)" }
return "\(character) "
}.joined()
}
.joined()
.trimmingCharacters(in: .whitespacesAndNewlines)
}

/// The period of the code.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Foundation
import XCTest

@testable import AuthenticatorShared

final class TOTPCodeModelTests: AuthenticatorTestCase {
// MARK: Tests

/// `displayCode` groups digits correctly
func test_displayCode_spaces() {
XCTAssertEqual(model(for: "12345").displayCode, "123 45")
XCTAssertEqual(model(for: "123456").displayCode, "123 456")
XCTAssertEqual(model(for: "1234567").displayCode, "123 456 7")
XCTAssertEqual(model(for: "12345678").displayCode, "123 456 78")
XCTAssertEqual(model(for: "123456789").displayCode, "123 456 789")
XCTAssertEqual(model(for: "1234567890").displayCode, "123 456 789 0")
}

// MARK: Private Methods

func model(for code: String) -> TOTPCodeModel {
TOTPCodeModel(code: code, codeGenerationDate: Date(), period: 30)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import Foundation

#if DEBUG

extension ItemListSection {
static func digitsFixture(accountNames: Bool = false) -> ItemListSection { // swiftlint:disable:this function_body_length line_length
ItemListSection(
id: "Digits",
items: [
ItemListItem(
id: "5",
name: "Five",
accountName: accountNames ? "[email protected]" : nil,
itemType: .totp(
model: ItemListTotpItem(
itemView: .fixture(),
totpCode: TOTPCodeModel(
code: "12345",
codeGenerationDate: Date(),
period: 30
)
)
)
),
ItemListItem(
id: "6",
name: "Six",
accountName: accountNames ? "[email protected]" : nil,
itemType: .totp(
model: ItemListTotpItem(
itemView: .fixture(),
totpCode: TOTPCodeModel(
code: "123456",
codeGenerationDate: Date(),
period: 30
)
)
)
),
ItemListItem(
id: "7",
name: "Seven",
accountName: accountNames ? "[email protected]" : nil,
itemType: .totp(
model: ItemListTotpItem(
itemView: .fixture(),
totpCode: TOTPCodeModel(
code: "1234567",
codeGenerationDate: Date(),
period: 30
)
)
)
),
ItemListItem(
id: "8",
name: "Eight",
accountName: accountNames ? "[email protected]" : nil,
itemType: .totp(
model: ItemListTotpItem(
itemView: .fixture(),
totpCode: TOTPCodeModel(
code: "12345678",
codeGenerationDate: Date(),
period: 30
)
)
)
),
ItemListItem(
id: "9",
name: "Nine",
accountName: accountNames ? "[email protected]" : nil,
itemType: .totp(
model: ItemListTotpItem(
itemView: .fixture(),
totpCode: TOTPCodeModel(
code: "123456789",
codeGenerationDate: Date(),
period: 30
)
)
)
),
ItemListItem(
id: "10",
name: "Ten",
accountName: accountNames ? "[email protected]" : nil,
itemType: .totp(
model: ItemListTotpItem(
itemView: .fixture(),
totpCode: TOTPCodeModel(
code: "1234567890",
codeGenerationDate: Date(),
period: 30
)
)
)
),
],
name: "Digits"
)
}
}

#endif
13 changes: 13 additions & 0 deletions AuthenticatorShared/UI/Vault/ItemList/ItemList/ItemListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,19 @@ struct ItemListView_Previews: PreviewProvider {
timeProvider: PreviewTimeProvider()
)
}.previewDisplayName("3 Search Results")

NavigationView {
ItemListView(
store: Store(
processor: StateProcessor(
state: ItemListState(
loadingState: .data([ItemListSection.digitsFixture(accountNames: true)])
)
)
),
timeProvider: PreviewTimeProvider()
)
}.previewDisplayName("Digits")
}
}
#endif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,49 @@ struct ItemListItemRowView: View {
timeProvider: PreviewTimeProvider()
)
}

struct ItemListItemRow_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
VStack(spacing: 4) {
ForEach(ItemListSection.digitsFixture(accountNames: false).items) { item in
ItemListItemRowView(
store: Store(
processor: StateProcessor(
state: ItemListItemRowState(
item: item,
hasDivider: true,
showWebIcons: true
)
)
),
timeProvider: PreviewTimeProvider()
)
}
}
}.previewDisplayName(
"Digits without account"
)
NavigationView {
VStack(spacing: 4) {
ForEach(ItemListSection.digitsFixture(accountNames: true).items) { item in
ItemListItemRowView(
store: Store(
processor: StateProcessor(
state: ItemListItemRowState(
item: item,
hasDivider: true,
showWebIcons: true
)
)
),
timeProvider: PreviewTimeProvider()
)
}
}
}.previewDisplayName(
"Digits with account"
)
}
}
#endif

0 comments on commit f1dc721

Please sign in to comment.