Skip to content

Commit

Permalink
Merge pull request #440 from adevinta/tech/displayedText
Browse files Browse the repository at this point in the history
[Tech] Update DisplayedTextViewModel: Add a model, deprecate some var
  • Loading branch information
robergro authored Sep 14, 2023
2 parents 4616a8c + dd45054 commit 59898fe
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 20 deletions.
11 changes: 11 additions & 0 deletions core/Sources/Common/DisplayedText/Enum/DisplayedTextType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,15 @@ enum DisplayedTextType: CaseIterable {
case text
/// Attributed text is displayed on label
case attributedText

// MARK: - Properties

var containsText: Bool {
switch self {
case .none:
return false
case .text, .attributedText:
return true
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// DisplayedTextTypeTests.swift
// SparkCoreTests
//
// Created by robin.lemaire on 14/09/2023.
// Copyright © 2023 Adevinta. All rights reserved.
//

import XCTest
@testable import SparkCore

final class DisplayedTextTypeTests: XCTestCase {

// MARK: - Tests

func test_containsText_when_type_is_none() {
// GIVEN
let type: DisplayedTextType = .none

// WHEN
let containsText = type.containsText

// THEN
XCTAssertFalse(containsText)
}

func test_containsText_when_type_is_text() {
// GIVEN
let type: DisplayedTextType = .text

// WHEN
let containsText = type.containsText

// THEN
XCTAssertTrue(containsText)
}

func test_containsText_when_type_is_attributedText() {
// GIVEN
let type: DisplayedTextType = .attributedText

// WHEN
let containsText = type.containsText

// THEN
XCTAssertTrue(containsText)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// DisplayedText+ExtensionTests.swift
// SparkCoreTests
//
// Created by robin.lemaire on 14/09/2023.
// Copyright © 2023 Adevinta. All rights reserved.
//

@testable import SparkCore

extension DisplayedText {

// MARK: - Properties

static func mocked(
text: String = "My text"
) -> Self {
return .init(
text: text
)
}
}
37 changes: 37 additions & 0 deletions core/Sources/Common/DisplayedText/Model/DisplayedText.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// DisplayedText.swift
// SparkCore
//
// Created by robin.lemaire on 13/09/2023.
// Copyright © 2023 Adevinta. All rights reserved.
//

struct DisplayedText: Equatable {

// MARK: - Properties

let text: String?
let attributedText: AttributedStringEither?

// MARK: - Initialization

init?(text: String?, attributedText: AttributedStringEither?) {
// Both values cannot be nil
guard text != nil || attributedText != nil else {
return nil
}

self.text = text
self.attributedText = attributedText
}

init(text: String) {
self.text = text
self.attributedText = nil
}

init(attributedText: AttributedStringEither) {
self.text = nil
self.attributedText = attributedText
}
}
119 changes: 119 additions & 0 deletions core/Sources/Common/DisplayedText/Model/DisplayedTextTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
//
// DisplayedTextTests.swift
// SparkCoreTests
//
// Created by robin.lemaire on 14/09/2023.
// Copyright © 2023 Adevinta. All rights reserved.
//

import XCTest
@testable import SparkCore

final class DisplayedTextTests: XCTestCase {

// MARK: - Optional Init

func test_optional_init_with_only_text() {
// GIVEN
let textMock = "My text"

// WHEN
let displayedText = DisplayedText(
text: textMock,
attributedText: nil
)

// THEN
XCTAssertEqual(
displayedText?.text,
textMock,
"Wrong text"
)
XCTAssertNil(
displayedText?.attributedText,
"Wrong attributedText"
)
}

func test_optional_init_with_only_attributedText() {
// GIVEN
let attributedMock: AttributedStringEither = .left(.init(string: "Holà"))

// WHEN
let displayedText = DisplayedText(
text: nil,
attributedText: attributedMock
)

// THEN
XCTAssertNil(
displayedText?.text,
"Wrong text"
)
XCTAssertEqual(
displayedText?.attributedText,
attributedMock,
"Wrong attributedText"
)
}

func test_optional_init_without_text_and_attributedText() {
// GIVEN / WHEN
let displayedText = DisplayedText(
text: nil,
attributedText: nil
)

// THEN
XCTAssertNil(
displayedText,
"Wrong displayedText"
)
}

// MARK: - Text Init

func test_init_with_text() {
// GIVEN
let textMock = "My text"

// WHEN
let displayedText = DisplayedText(
text: textMock
)

// THEN
XCTAssertEqual(
displayedText.text,
textMock,
"Wrong text"
)
XCTAssertNil(
displayedText.attributedText,
"Wrong attributedText"
)
}

// MARK: - AttributedText Init

func test_init_with_attributedText() {
// GIVEN
let attributedMock: AttributedStringEither = .left(.init(string: "Holà"))

// WHEN
let displayedText = DisplayedText(
attributedText: attributedMock
)

// THEN
XCTAssertNil(
displayedText.text,
"Wrong text"
)
XCTAssertEqual(
displayedText.attributedText,
attributedMock,
"Wrong attributedText"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@

// sourcery: AutoMockable
protocol DisplayedTextViewModel {
@available(*, deprecated, message: "use displayedText.text")
var text: String? { get }
@available(*, deprecated, message: "use displayedText.attributedText")
var attributedText: AttributedStringEither? { get }
var displayedTextType: DisplayedTextType { get }
var displayedText: DisplayedText? { get }

var containsText: Bool { get }

/// Update the text only if the value changed.
/// Return true if value changed.
Expand All @@ -27,9 +32,17 @@ final class DisplayedTextViewModelDefault: DisplayedTextViewModel {

// MARK: - Internal Properties

@available(*, deprecated, message: "use displayedText.text")
private(set) var text: String?
@available(*, deprecated, message: "use displayedText.attributedText")
private(set) var attributedText: AttributedStringEither?

private(set) var displayedTextType: DisplayedTextType
private(set) var displayedText: DisplayedText?

var containsText: Bool {
self.displayedTextType.containsText
}

// MARK: - Private Properties

Expand All @@ -46,6 +59,10 @@ final class DisplayedTextViewModelDefault: DisplayedTextViewModel {
) {
self.text = text
self.attributedText = attributedText
self.displayedText = .init(
text: text,
attributedText: attributedText
)
self.displayedTextType = getDisplayedTextTypeUseCase.execute(
text: text,
attributedText: attributedText
Expand All @@ -60,11 +77,12 @@ final class DisplayedTextViewModelDefault: DisplayedTextViewModel {
func textChanged(_ text: String?) -> Bool {
// Displayed text changed ?
if self.getDidDisplayedTextChangeUseCase.execute(
currentText: self.text,
currentText: self.displayedText?.text,
newText: text,
displayedTextType: self.displayedTextType
) {
self.text = text
self.displayedText = text.map { .init(text: $0) }
self.displayedTextType = self.getDisplayedTextTypeUseCase.execute(
text: text
)
Expand All @@ -78,14 +96,15 @@ final class DisplayedTextViewModelDefault: DisplayedTextViewModel {
func attributedTextChanged(_ attributedText: AttributedStringEither?) -> Bool {
// Displayed attributed text changed ?
if self.getDidDisplayedTextChangeUseCase.execute(
currentAttributedText: self.attributedText,
currentAttributedText: self.displayedText?.attributedText,
newAttributedText: attributedText,
displayedTextType: self.displayedTextType
) {
self.attributedText = attributedText
self.displayedTextType = self.getDisplayedTextTypeUseCase.execute(
attributedText: attributedText
)
self.displayedText = attributedText.map { .init(attributedText: $0) }

return true
}
Expand Down
Loading

0 comments on commit 59898fe

Please sign in to comment.