diff --git a/DuckDuckGo/Bookmarks/ViewModel/BookmarkAllTabsDialogViewModel.swift b/DuckDuckGo/Bookmarks/ViewModel/BookmarkAllTabsDialogViewModel.swift index 92c02692ee..daf6018403 100644 --- a/DuckDuckGo/Bookmarks/ViewModel/BookmarkAllTabsDialogViewModel.swift +++ b/DuckDuckGo/Bookmarks/ViewModel/BookmarkAllTabsDialogViewModel.swift @@ -62,16 +62,15 @@ final class BookmarkAllTabsDialogViewModel: BookmarkAllTabsDialogEditing { websites: [WebsiteInfo], foldersStore: BookmarkFoldersStore, bookmarkManager: BookmarkManager = LocalBookmarkManager.shared, - dateProvider: () -> Date = Date.init + dateFormatterConfigurationProvider: () -> DateFormatterConfiguration = DateFormatterConfiguration.defaultConfiguration ) { self.websites = websites self.foldersStore = foldersStore self.bookmarkManager = bookmarkManager - let dateString = Self.dateFormatter.string(from: dateProvider()) - folderName = String(format: UserText.Bookmarks.Dialog.Value.folderName, dateString, websites.count) folders = .init(bookmarkManager.list) selectedFolder = foldersStore.lastBookmarkAllTabsFolderIdUsed.flatMap(bookmarkManager.getBookmarkFolder(withId:)) + folderName = Self.folderName(configuration: dateFormatterConfigurationProvider(), websitesNumber: websites.count) bind() } @@ -96,6 +95,12 @@ final class BookmarkAllTabsDialogViewModel: BookmarkAllTabsDialogEditing { private extension BookmarkAllTabsDialogViewModel { + static func folderName(configuration: DateFormatterConfiguration, websitesNumber: Int) -> String { + Self.dateFormatter.timeZone = configuration.timeZone + let dateString = Self.dateFormatter.string(from: configuration.date) + return String(format: UserText.Bookmarks.Dialog.Value.folderName, dateString, websitesNumber) + } + func bind() { folderCancellable = bookmarkManager.listPublisher .receive(on: DispatchQueue.main) @@ -105,3 +110,18 @@ private extension BookmarkAllTabsDialogViewModel { } } + +// MARK: - DateConfiguration + +extension BookmarkAllTabsDialogViewModel { + + struct DateFormatterConfiguration { + let date: Date + let timeZone: TimeZone + + static func defaultConfiguration() -> DateFormatterConfiguration { + .init(date: Date(), timeZone: .current) + } + } + +} diff --git a/UnitTests/Bookmarks/ViewModels/BookmarkAllTabsDialogViewModelTests.swift b/UnitTests/Bookmarks/ViewModels/BookmarkAllTabsDialogViewModelTests.swift index c9a2a419dc..dd05e42238 100644 --- a/UnitTests/Bookmarks/ViewModels/BookmarkAllTabsDialogViewModelTests.swift +++ b/UnitTests/Bookmarks/ViewModels/BookmarkAllTabsDialogViewModelTests.swift @@ -117,11 +117,19 @@ final class BookmarkAllTabsDialogViewModelTests: XCTestCase { // MARK: - State - func testWhenInitThenFolderNameIsSetToCurrentDateAndNumberOfWebsites() { + func testWhenInitThenFolderNameIsSetToCurrentDateAndNumberOfWebsites() throws { // GIVEN let date = Date(timeIntervalSince1970: 1712902304) // 12th of April 2024 + let gmtTimeZone = try XCTUnwrap(TimeZone(identifier: "GMT")) let websitesInfo = WebsiteInfo.makeWebsitesInfo(url: .duckDuckGo, occurrences: 5) - let sut = BookmarkAllTabsDialogViewModel(websites: websitesInfo, foldersStore: foldersStoreMock, bookmarkManager: bookmarkManager, dateProvider: { date }) + let sut = BookmarkAllTabsDialogViewModel( + websites: websitesInfo, + foldersStore: foldersStoreMock, + bookmarkManager: bookmarkManager, + dateFormatterConfigurationProvider: { + BookmarkAllTabsDialogViewModel.DateFormatterConfiguration(date: date, timeZone: gmtTimeZone) + } + ) // WHEN let result = sut.folderName @@ -130,6 +138,28 @@ final class BookmarkAllTabsDialogViewModelTests: XCTestCase { XCTAssertEqual(result, String(format: UserText.Bookmarks.Dialog.Value.folderName, "2024-04-12", websitesInfo.count)) } + func testWhenInitAndTimeZoneIsPDTThenFolderNameIsSetToCurrentDateAndNumberOfWebsites() throws { + // GIVEN + let date = Date(timeIntervalSince1970: 1712902304) // 12th of April 2024 (GMT) + let pdtTimeZone = try XCTUnwrap(TimeZone(identifier: "America/Los_Angeles")) + let expectedDate = "2024-04-11" // Expected date in PDT TimeZone + let websitesInfo = WebsiteInfo.makeWebsitesInfo(url: .duckDuckGo, occurrences: 5) + let sut = BookmarkAllTabsDialogViewModel( + websites: websitesInfo, + foldersStore: foldersStoreMock, + bookmarkManager: bookmarkManager, + dateFormatterConfigurationProvider: { + BookmarkAllTabsDialogViewModel.DateFormatterConfiguration(date: date, timeZone: pdtTimeZone) + } + ) + + // WHEN + let result = sut.folderName + + // THEN + XCTAssertEqual(result, String(format: UserText.Bookmarks.Dialog.Value.folderName, expectedDate, websitesInfo.count)) + } + func testWhenInitThenFoldersAreSetFromBookmarkList() { // GIVEN let folder = BookmarkFolder(id: "1", title: #function)