-
Notifications
You must be signed in to change notification settings - Fork 100
Show dynamically time, relative date, weekday, or short date in channel lists #833
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b1fbd2d
Show time, relative date, weekday, or short date for last message in …
laevandus 9d26265
Add ChannelListConfig for relative dates
laevandus 13004e4
Merge branch 'develop' into add/channel-list-date-time-formatting
laevandus 7930820
Merge branch 'develop' into add/channel-list-date-time-formatting
laevandus 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
17 changes: 17 additions & 0 deletions
17
Sources/StreamChatSwiftUI/ChatChannelList/ChannelListConfig.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,17 @@ | ||
// | ||
// Copyright © 2025 Stream.io Inc. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// A configuration for channel lists. | ||
public struct ChannelListConfig { | ||
public init(messageRelativeDateFormatEnabled: Bool = false) { | ||
self.messageRelativeDateFormatEnabled = messageRelativeDateFormatEnabled | ||
} | ||
|
||
/// If true, the timestamp format depends on the time passed. | ||
/// | ||
/// Different date formats are used for today, yesterday, last 7 days, and older dates. | ||
public var messageRelativeDateFormatEnabled: Bool | ||
} |
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
63 changes: 63 additions & 0 deletions
63
Sources/StreamChatSwiftUI/Utils/Common/MessageRelativeDateFormatter.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,63 @@ | ||
// | ||
// Copyright © 2025 Stream.io Inc. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// A formatter that converts message timestamps to a format which depends on the time passed. | ||
public final class MessageRelativeDateFormatter: DateFormatter, @unchecked Sendable { | ||
override public init() { | ||
super.init() | ||
locale = .autoupdatingCurrent | ||
dateStyle = .short | ||
timeStyle = .none | ||
} | ||
|
||
@available(*, unavailable) | ||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
override public func string(from date: Date) -> String { | ||
if calendar.isDateInToday(date) { | ||
return todayFormatter.string(from: date) | ||
} | ||
if calendar.isDateInYesterday(date) { | ||
return yesterdayFormatter.string(from: date) | ||
} | ||
if calendar.isDateInLastWeek(date) { | ||
return weekdayFormatter.string(from: date) | ||
} | ||
|
||
return super.string(from: date) | ||
} | ||
|
||
var todayFormatter: DateFormatter { | ||
InjectedValues[\.utils].dateFormatter | ||
} | ||
|
||
let yesterdayFormatter: DateFormatter = { | ||
let formatter = DateFormatter() | ||
formatter.locale = .autoupdatingCurrent | ||
formatter.dateStyle = .short | ||
formatter.timeStyle = .none | ||
formatter.doesRelativeDateFormatting = true | ||
return formatter | ||
}() | ||
|
||
let weekdayFormatter: DateFormatter = { | ||
let formatter = DateFormatter() | ||
formatter.locale = .autoupdatingCurrent | ||
formatter.setLocalizedDateFormatFromTemplate("EEEE") | ||
return formatter | ||
}() | ||
} | ||
|
||
extension Calendar { | ||
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
63 changes: 63 additions & 0 deletions
63
StreamChatSwiftUITests/Tests/Utils/MessageRelativeDateFormatter_Tests.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,63 @@ | ||
// | ||
// Copyright © 2025 Stream.io Inc. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
@testable import StreamChat | ||
@testable import StreamChatSwiftUI | ||
import XCTest | ||
|
||
final class MessageRelativeDateFormatter_Tests: StreamChatTestCase { | ||
private var formatter: MessageRelativeDateFormatter! | ||
|
||
override func setUp() { | ||
super.setUp() | ||
formatter = MessageRelativeDateFormatter() | ||
formatter.locale = Locale(identifier: "en_UK") | ||
formatter.todayFormatter.locale = Locale(identifier: "en_UK") | ||
formatter.yesterdayFormatter.locale = Locale(identifier: "en_UK") | ||
} | ||
|
||
override func tearDown() { | ||
super.tearDown() | ||
formatter = nil | ||
} | ||
|
||
func test_showingTimeOnly() throws { | ||
let date = try XCTUnwrap(Calendar.current.date(bySettingHour: 1, minute: 2, second: 3, of: Date())) | ||
let result = formatter.string(from: date) | ||
let expected = formatter.todayFormatter.string(from: date) | ||
XCTAssertEqual(expected, result) | ||
XCTAssertEqual("01:02", result) | ||
} | ||
|
||
func test_showingYesterday() throws { | ||
let date = try XCTUnwrap(Calendar.current.date(byAdding: .day, value: -1, to: Date())) | ||
let result = formatter.string(from: date) | ||
let expected = formatter.yesterdayFormatter.string(from: date) | ||
XCTAssertEqual(expected, result) | ||
XCTAssertEqual("Yesterday", result) | ||
} | ||
|
||
func test_showingWeekday() throws { | ||
let date = try XCTUnwrap(Calendar.current.date(byAdding: .day, value: -6, to: Date())) | ||
let result = formatter.string(from: date) | ||
let expected = formatter.weekdayFormatter.string(from: date) | ||
XCTAssertEqual(expected, result) | ||
} | ||
|
||
func test_showingShortDate() throws { | ||
let components = DateComponents( | ||
timeZone: TimeZone(secondsFromGMT: 0), | ||
year: 2025, | ||
month: 1, | ||
day: 15, | ||
hour: 3, | ||
minute: 4, | ||
second: 5 | ||
) | ||
let date = try XCTUnwrap(Calendar.current.date(from: components)) | ||
let result = formatter.string(from: date) | ||
XCTAssertEqual("15/01/2025", result) | ||
} | ||
} |
Oops, something went wrong.
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.