Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nakajima committed Feb 6, 2024
1 parent 809c6e0 commit 7d4316a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Sources/XMTPiOS/Extensions/Date.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ extension Date {
var millisecondsSinceEpoch: Double {
timeIntervalSince1970 * 1000
}

init(millisecondsSinceEpoch: Int64) {
self.init(timeIntervalSince1970: TimeInterval(millisecondsSinceEpoch / 1_000_000_000))
}
}
3 changes: 3 additions & 0 deletions Sources/XMTPiOS/Group.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
42 changes: 40 additions & 2 deletions XMTPiOSExample/XMTPiOSExample/Views/ConversationListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}
}
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions XMTPiOSExample/XMTPiOSExample/Views/NewConversationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 7d4316a

Please sign in to comment.