Skip to content

Commit

Permalink
Map ImageComments into ImageCommentsViewModels
Browse files Browse the repository at this point in the history
  • Loading branch information
ullas-jain committed Nov 21, 2023
1 parent c8cc757 commit fd84288
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,39 @@

import Foundation

public struct ImageCommentsViewModel {
public let comments: [ImageCommentViewModel]
}

public struct ImageCommentViewModel: Equatable {
public let message: String
public let date: String
public let username: String

public init(message: String, date: String, username: String) {
self.message = message
self.date = date
self.username = username
}
}

public final class ImageCommentsPresenter {
public static var title: String {
NSLocalizedString("IMAGE_COMMENTS_VIEW_TITLE",
tableName: "ImageComments",
bundle: Bundle(for: Self.self),
comment: "Title for the image comments view")
}

public static func map(_ comments: [ImageComment]) -> ImageCommentsViewModel {
let formatter = RelativeDateTimeFormatter()

return ImageCommentsViewModel(comments: comments.map { comment in
ImageCommentViewModel(
message: comment.message,
date: formatter.localizedString(for: comment.createdAt, relativeTo: Date()),
username: comment.username
)
})
}
}
10 changes: 0 additions & 10 deletions EssentialFeedTests/Feed Cache/Helpers/FeedCacheTestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,4 @@ extension Date {
private var feedCacheMaxAgeInDays: Int {
7
}

private func adding(days: Int) -> Date {
Calendar(identifier: .gregorian).date(byAdding: .day, value: days, to: self)!
}
}

extension Date {
func adding(seconds: TimeInterval) -> Date {
self + seconds
}
}
14 changes: 14 additions & 0 deletions EssentialFeedTests/Helpers/SharedTestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,17 @@ extension HTTPURLResponse {
self.init(url: anyURL(), statusCode: statusCode, httpVersion: nil, headerFields: nil)!
}
}

extension Date {
func adding(seconds: TimeInterval) -> Date {
return self + seconds
}

func adding(minutes: Int) -> Date {
return Calendar(identifier: .gregorian).date(byAdding: .minute, value: minutes, to: self)!
}

func adding(days: Int) -> Date {
return Calendar(identifier: .gregorian).date(byAdding: .day, value: days, to: self)!
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,40 @@ class ImageCommentsPresenterTests: XCTestCase {
XCTAssertEqual(ImageCommentsPresenter.title, localized("IMAGE_COMMENTS_VIEW_TITLE"))
}

func test_map_createsViewModels() {
let now = Date()

let comments = [
ImageComment(
id: UUID(),
message: "a message",
createdAt: now.adding(minutes: -5),
username: "a username"
),
ImageComment(
id: UUID(),
message: "another message",
createdAt: now.adding(days: -1),
username: "another username"
),
]

let viewModel = ImageCommentsPresenter.map(comments)

XCTAssertEqual(viewModel.comments, [
ImageCommentViewModel(
message: "a message",
date: "5 minutes ago",
username: "a username"
),
ImageCommentViewModel(
message: "another message",
date: "1 day ago",
username: "another username"
),
])
}

// MARK: - Helpers

private func localized(_ key: String, file: StaticString = #file, line: UInt = #line) -> String {
Expand Down

0 comments on commit fd84288

Please sign in to comment.