ios: date/time formatting now respects locale settings (#420)

This commit is contained in:
Evgeny Poberezkin
2022-03-09 22:35:33 +00:00
committed by GitHub
parent 6aa9f208ee
commit e272048f24
5 changed files with 12 additions and 11 deletions

View File

@@ -529,7 +529,7 @@ struct ChatItem: Identifiable, Decodable {
var id: Int64 { get { meta.itemId } }
var timestampText: String { get { meta.timestampText } }
var timestampText: Text { get { meta.timestampText } }
func isRcvNew() -> Bool {
if case .rcvNew = meta.itemStatus { return true }
@@ -570,7 +570,7 @@ struct CIMeta: Decodable {
var itemStatus: CIStatus
var createdAt: Date
var timestampText: String { get { SimpleX.timestampText(itemTs) } }
var timestampText: Text { get { SimpleX.timestampText(itemTs) } }
static func getSample(_ id: Int64, _ ts: Date, _ text: String, _ status: CIStatus = .sndNew) -> CIMeta {
CIMeta(
@@ -583,13 +583,14 @@ struct CIMeta: Decodable {
}
}
let msgTimeFormat = Date.FormatStyle.dateTime.hour().minute()
let msgDateFormat = Date.FormatStyle.dateTime.day(.twoDigits).month(.twoDigits)
func timestampText(_ date: Date) -> String {
func timestampText(_ date: Date) -> Text {
let now = Calendar.current.dateComponents([.day, .hour], from: .now)
let dc = Calendar.current.dateComponents([.day, .hour], from: date)
return now.day == dc.day || ((now.day ?? 0) - (dc.day ?? 0) == 1 && (dc.hour ?? 0) >= 18 && (now.hour ?? 0) < 12)
? date.formatted(date: .omitted, time: .shortened)
: String(date.formatted(date: .numeric, time: .omitted).prefix(5))
let recent = now.day == dc.day || ((now.day ?? 0) - (dc.day ?? 0) == 1 && (dc.hour ?? 0) >= 18 && (now.hour ?? 0) < 12)
return Text(date, format: recent ? msgTimeFormat : msgDateFormat)
}
enum CIStatus: Decodable {

View File

@@ -25,7 +25,7 @@ struct CIMetaView: View {
default: EmptyView()
}
Text(chatItem.timestampText)
chatItem.timestampText
.font(.caption)
.foregroundColor(.secondary)
}

View File

@@ -55,8 +55,8 @@ struct TextItemView: View {
}
}
private func reserveSpaceForMeta(_ meta: String) -> Text {
Text(" \(meta)")
private func reserveSpaceForMeta(_ meta: Text) -> Text {
(Text(" ") + meta)
.font(.caption)
.foregroundColor(.clear)
}

View File

@@ -39,7 +39,7 @@ struct ChatPreviewView: View {
.foregroundColor(chat.chatInfo.ready ? .primary : .secondary)
.frame(maxHeight: .infinity, alignment: .topLeading)
Spacer()
Text(cItem?.timestampText ?? timestampText(chat.chatInfo.createdAt))
(cItem?.timestampText ?? timestampText(chat.chatInfo.createdAt))
.font(.subheadline)
.frame(minWidth: 60, alignment: .trailing)
.foregroundColor(.secondary)

View File

@@ -28,7 +28,7 @@ struct ContactRequestView: View {
.padding(.top, 4)
.frame(maxHeight: .infinity, alignment: .topLeading)
Spacer()
Text(timestampText(contactRequest.createdAt))
timestampText(contactRequest.createdAt)
.font(.subheadline)
.padding(.trailing, 8)
.padding(.top, 4)