From 7d4316ad3ba2f773f1016ce44ddf2e8daada950d Mon Sep 17 00:00:00 2001 From: Pat Nakajima Date: Tue, 6 Feb 2024 12:23:19 -0600 Subject: [PATCH] cleanup --- Sources/XMTPiOS/Extensions/Date.swift | 4 ++ Sources/XMTPiOS/Group.swift | 3 ++ .../Views/ConversationListView.swift | 42 ++++++++++++++++++- .../Views/NewConversationView.swift | 9 ++++ 4 files changed, 56 insertions(+), 2 deletions(-) diff --git a/Sources/XMTPiOS/Extensions/Date.swift b/Sources/XMTPiOS/Extensions/Date.swift index 5db07bf1..a2aa8b29 100644 --- a/Sources/XMTPiOS/Extensions/Date.swift +++ b/Sources/XMTPiOS/Extensions/Date.swift @@ -11,4 +11,8 @@ extension Date { var millisecondsSinceEpoch: Double { timeIntervalSince1970 * 1000 } + + init(millisecondsSinceEpoch: Int64) { + self.init(timeIntervalSince1970: TimeInterval(millisecondsSinceEpoch / 1_000_000_000)) + } } diff --git a/Sources/XMTPiOS/Group.swift b/Sources/XMTPiOS/Group.swift index 69e412b4..0660a15e 100644 --- a/Sources/XMTPiOS/Group.swift +++ b/Sources/XMTPiOS/Group.swift @@ -44,6 +44,9 @@ public struct Group: Identifiable, Equatable, Hashable { } } + public var createdAt: Date { + Date(millisecondsSinceEpoch: ffiGroup.createdAtNs()) + } public func addMembers(addresses: [String]) async throws { try await ffiGroup.addMembers(accountAddresses: addresses) diff --git a/XMTPiOSExample/XMTPiOSExample/Views/ConversationListView.swift b/XMTPiOSExample/XMTPiOSExample/Views/ConversationListView.swift index a44e0526..e737d746 100644 --- a/XMTPiOSExample/XMTPiOSExample/Views/ConversationListView.swift +++ b/XMTPiOSExample/XMTPiOSExample/Views/ConversationListView.swift @@ -17,9 +17,37 @@ struct ConversationListView: View { var body: some View { List { - ForEach(conversations, id: \.id) { item in + ForEach(conversations.sorted(by: { $0.createdAt > $1.createdAt }), id: \.id) { item in NavigationLink(value: item) { - Text(item.id) + HStack { + switch item { + case .conversation: + Image(systemName: "person.fill") + .resizable() + .scaledToFit() + .frame(width: 16, height: 16) + .foregroundStyle(.secondary) + case .group: + Image(systemName: "person.3.fill") + .resizable() + .scaledToFit() + .frame(width: 16, height: 16) + .foregroundStyle(.secondary) + } + + VStack(alignment: .leading) { + switch item { + case .conversation(let conversation): + Text(abbreviate(address: conversation.peerAddress)) + case .group(let group): + Text(group.members.map { abbreviate(address: $0) }.joined(separator: ", ")) + } + + Text(item.createdAt.formatted()) + .font(.caption) + .foregroundStyle(.secondary) + } + } } } } @@ -73,6 +101,16 @@ struct ConversationListView: View { } } + func abbreviate(address: String) -> String { + if address.count > 6 { + let start = address.index(address.startIndex, offsetBy: 6) + let end = address.index(address.endIndex, offsetBy: -5) + return address.replacingCharacters(in: start ... end, with: "...") + } else { + return address + } + } + func loadConversations() async { do { let conversations = try await client.conversations.list().map { diff --git a/XMTPiOSExample/XMTPiOSExample/Views/NewConversationView.swift b/XMTPiOSExample/XMTPiOSExample/Views/NewConversationView.swift index 279d9eec..7586683a 100644 --- a/XMTPiOSExample/XMTPiOSExample/Views/NewConversationView.swift +++ b/XMTPiOSExample/XMTPiOSExample/Views/NewConversationView.swift @@ -27,6 +27,15 @@ enum ConversationOrGroup: Identifiable, Hashable { return group.members.joined(separator: ",") } } + + var createdAt: Date { + switch self { + case .conversation(let conversation): + return conversation.createdAt + case .group(let group): + return group.createdAt + } + } } struct NewConversationView: View {