Skip to content

Commit

Permalink
Folder name does not reflect the correct date for current timezone. (#…
Browse files Browse the repository at this point in the history
…2670)

Task/Issue URL: https://app.asana.com/0/0/1207133700674248/f

**Description**:
P1/ Fix an issue that causes the folder name not to take into account the current TimeZone when generating the folder name
  • Loading branch information
alessandroboron committed Apr 23, 2024
1 parent ce6c788 commit a1bd0e7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
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

0 comments on commit a1bd0e7

Please sign in to comment.