diff --git a/EssentialFeed/Image Comments Presentation/ImageCommentsPresenter.swift b/EssentialFeed/Image Comments Presentation/ImageCommentsPresenter.swift index f188fc5c..559358e9 100644 --- a/EssentialFeed/Image Comments Presentation/ImageCommentsPresenter.swift +++ b/EssentialFeed/Image Comments Presentation/ImageCommentsPresenter.swift @@ -7,6 +7,22 @@ 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", @@ -14,4 +30,16 @@ public final class ImageCommentsPresenter { 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 + ) + }) + } } diff --git a/EssentialFeedTests/Feed Cache/Helpers/FeedCacheTestHelpers.swift b/EssentialFeedTests/Feed Cache/Helpers/FeedCacheTestHelpers.swift index fbbc38e5..4b4013d4 100644 --- a/EssentialFeedTests/Feed Cache/Helpers/FeedCacheTestHelpers.swift +++ b/EssentialFeedTests/Feed Cache/Helpers/FeedCacheTestHelpers.swift @@ -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 - } } diff --git a/EssentialFeedTests/Helpers/SharedTestHelpers.swift b/EssentialFeedTests/Helpers/SharedTestHelpers.swift index c0205748..d83c8fd9 100644 --- a/EssentialFeedTests/Helpers/SharedTestHelpers.swift +++ b/EssentialFeedTests/Helpers/SharedTestHelpers.swift @@ -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)! + } +} diff --git a/EssentialFeedTests/Image Comments Presentation/ImageCommentsPresenterTests.swift b/EssentialFeedTests/Image Comments Presentation/ImageCommentsPresenterTests.swift index 4183d812..d9a91c81 100644 --- a/EssentialFeedTests/Image Comments Presentation/ImageCommentsPresenterTests.swift +++ b/EssentialFeedTests/Image Comments Presentation/ImageCommentsPresenterTests.swift @@ -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 {