Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

P1/ Folder name does not reflect the correct date for current timezone. #2670

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand All @@ -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)
Expand All @@ -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)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
Loading