-
Notifications
You must be signed in to change notification settings - Fork 223
Change timestamp formatting according to the default design #2736
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
Merged
nuno-vieira
merged 5 commits into
develop
from
fix/channel-list-timestamp-logic-according-to-default-design
Aug 18, 2023
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7c2e456
Change channel list message timestamp formatting logic according to d…
nuno-vieira 9ff1510
Fix previous snapshot tests by forcing to render the time only
nuno-vieira 0c673f5
Add test coverage to new channel list timestamp logic
nuno-vieira d4625c1
Merge branch 'develop' into fix/channel-list-timestamp-logic-accordin…
nuno-vieira 8c45224
Update CHANGELOG.md
nuno-vieira File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
Sources/StreamChatUI/Appearance+Formatters/ChannelListMessageTimestampFormatter.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// | ||
// Copyright © 2023 Stream.io Inc. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// The last message timestamp formatter in channel list. | ||
open class ChannelListMessageTimestampFormatter: MessageTimestampFormatter { | ||
/// The formatter to show the time that a message was sent if it was sent today. | ||
public var timeFormatter: DateFormatter = { | ||
let formatter = DateFormatter() | ||
formatter.dateStyle = .none | ||
formatter.timeStyle = .short | ||
formatter.locale = .autoupdatingCurrent | ||
return formatter | ||
}() | ||
|
||
/// The formatter to show "Yesterday" in case the message was sent yesterday. | ||
public var relativeDateFormatter: DateFormatter = { | ||
let formatter = DateFormatter() | ||
formatter.dateStyle = .short | ||
formatter.timeStyle = .none | ||
formatter.doesRelativeDateFormatting = true | ||
formatter.locale = .autoupdatingCurrent | ||
return formatter | ||
}() | ||
|
||
/// The formatter to show the week day that a message was sent. | ||
public var weekDayDateFormatter: DateFormatter = { | ||
let formatter = DateFormatter() | ||
formatter.setLocalizedDateFormatFromTemplate("EEEE") | ||
formatter.locale = .autoupdatingCurrent | ||
return formatter | ||
}() | ||
|
||
/// The formatter to show the date that a message was sent in case it was sent before the last week. | ||
public var dateFormatter: DateFormatter = { | ||
let formatter = DateFormatter() | ||
formatter.dateStyle = .short | ||
formatter.timeStyle = .none | ||
formatter.locale = .autoupdatingCurrent | ||
return formatter | ||
}() | ||
|
||
var calendar: ChannelListMessageTimestampCalendar = Calendar.current | ||
|
||
public init() {} | ||
|
||
open func format(_ date: Date) -> String { | ||
if calendar.isDateInToday(date) { | ||
return timeFormatter.string(from: date) | ||
} | ||
if calendar.isDateInYesterday(date) { | ||
return relativeDateFormatter.string(from: date) | ||
} | ||
if calendar.isDateInLastWeek(date) { | ||
return weekDayDateFormatter.string(from: date) | ||
} | ||
|
||
return dateFormatter.string(from: date) | ||
} | ||
} | ||
|
||
protocol ChannelListMessageTimestampCalendar { | ||
func isDateInToday(_ date: Date) -> Bool | ||
func isDateInYesterday(_ date: Date) -> Bool | ||
func isDateInLastWeek(_ date: Date) -> Bool | ||
} | ||
|
||
extension Calendar: ChannelListMessageTimestampCalendar { | ||
func isDateInLastWeek(_ date: Date) -> Bool { | ||
guard let dateBefore7days = self.date(byAdding: .day, value: -7, to: Date()) else { | ||
return false | ||
} | ||
|
||
return date > dateBefore7days | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.