Read and more actions
This commit is contained in:
parent
fa9a089a94
commit
0afaa3a821
@ -3,6 +3,7 @@
|
|||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
struct ContactRow: View {
|
struct ContactRow: View {
|
||||||
|
@Environment(AccountManager.self) private var accountManager: AccountManager
|
||||||
@EnvironmentObject private var navigator: Navigator
|
@EnvironmentObject private var navigator: Navigator
|
||||||
|
|
||||||
var cont: MessageContact = .placeholder()
|
var cont: MessageContact = .placeholder()
|
||||||
@ -71,6 +72,18 @@ struct ContactRow: View {
|
|||||||
}
|
}
|
||||||
.contentShape(Rectangle())
|
.contentShape(Rectangle())
|
||||||
.onTapGesture {
|
.onTapGesture {
|
||||||
|
guard let client = accountManager.getClient() else { return }
|
||||||
|
|
||||||
|
if cont.unread {
|
||||||
|
Task {
|
||||||
|
do {
|
||||||
|
_ = try await client.post(endpoint: Conversations.read(id: cont.id))
|
||||||
|
} catch {
|
||||||
|
print(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
navigator.navigate(to: cont.lastStatus == nil ? .account(acc: cont.accounts[0]) : .post(status: cont.lastStatus!))
|
navigator.navigate(to: cont.lastStatus == nil ? .account(acc: cont.accounts[0]) : .post(status: cont.lastStatus!))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,19 +7,78 @@ struct ContactsView: View {
|
|||||||
@EnvironmentObject private var navigator: Navigator
|
@EnvironmentObject private var navigator: Navigator
|
||||||
|
|
||||||
@State private var contacts: [MessageContact] = []
|
@State private var contacts: [MessageContact] = []
|
||||||
|
@State private var fetching: Bool = false
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
ScrollView(.vertical, showsIndicators: false) {
|
ScrollView(.vertical, showsIndicators: false) {
|
||||||
LazyVStack(alignment: .leading) {
|
LazyVStack(alignment: .leading) {
|
||||||
ForEach(contacts) { contact in
|
ForEach(contacts) { contact in
|
||||||
ContactRow(cont: contact)
|
ContactRow(cont: contact)
|
||||||
|
.contextMenu {
|
||||||
|
if contact.lastStatus != nil {
|
||||||
|
ControlGroup {
|
||||||
|
Button {
|
||||||
|
guard let client = accountManager.getClient() else { return }
|
||||||
|
Task {
|
||||||
|
do {
|
||||||
|
let endpoint = contact.lastStatus!.favourited ?? false ? Statuses.unfavorite(id: contact.lastStatus!.id) : Statuses.favorite(id: contact.lastStatus!.id)
|
||||||
|
|
||||||
|
_ = try await client.post(endpoint: endpoint)
|
||||||
|
} catch {
|
||||||
|
print(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} label: {
|
||||||
|
Label(contact.lastStatus!.favourited ?? false ? "status.action.unlike" : "status.action.like", systemImage: contact.lastStatus!.favourited ?? false ? "heart.slash.fill" : "heart")
|
||||||
|
}
|
||||||
|
|
||||||
|
Button {
|
||||||
|
guard let client = accountManager.getClient() else { return }
|
||||||
|
Task {
|
||||||
|
do {
|
||||||
|
let endpoint = contact.lastStatus!.bookmarked ?? false ? Statuses.unbookmark(id: contact.lastStatus!.id) : Statuses.bookmark(id: contact.lastStatus!.id)
|
||||||
|
|
||||||
|
_ = try await client.post(endpoint: endpoint)
|
||||||
|
} catch {
|
||||||
|
print(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} label: {
|
||||||
|
Label(contact.lastStatus!.bookmarked ?? false ? "status.action.unbookmark" : "status.action.bookmark", systemImage: contact.lastStatus!.bookmarked ?? false ? "bookmark.slash.fill" : "bookmark")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Divider()
|
||||||
|
}
|
||||||
|
|
||||||
|
if contact.unread {
|
||||||
|
Button {
|
||||||
|
guard let client = accountManager.getClient() else { return }
|
||||||
|
Task {
|
||||||
|
do {
|
||||||
|
_ = try await client.post(endpoint: Conversations.read(id: contact.id))
|
||||||
|
} catch {
|
||||||
|
print(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} label: {
|
||||||
|
Label("activity.messages.read", systemImage: "eye")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.background(Color.appBackground)
|
.background(Color.appBackground)
|
||||||
.navigationTitle(Text("activity.messages"))
|
.navigationTitle(Text("activity.messages"))
|
||||||
.task {
|
.onAppear {
|
||||||
await fetchContacts()
|
guard !fetching else { return }
|
||||||
|
|
||||||
|
fetching = true
|
||||||
|
Task {
|
||||||
|
await fetchContacts()
|
||||||
|
}
|
||||||
|
fetching = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,4 +102,24 @@ struct ContactsView: View {
|
|||||||
print(error)
|
print(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func fetchContact(convId: String) async -> MessageContact? {
|
||||||
|
guard let client = accountManager.getClient() else { return nil }
|
||||||
|
|
||||||
|
do {
|
||||||
|
let msgs: [MessageContact] = try await client.get(endpoint: Conversations.conversations(maxId: convId))
|
||||||
|
guard !msgs.isEmpty else { return nil } // Could not find conversation
|
||||||
|
|
||||||
|
if contacts.contains(where: { $0.id == (msgs.first?.id ?? "") }) {
|
||||||
|
let index: Int = contacts.firstIndex(where: { $0.id == (msgs.first?.id ?? "") }) ?? -1
|
||||||
|
contacts[index] = msgs.first ?? .placeholder()
|
||||||
|
}
|
||||||
|
|
||||||
|
return msgs.first
|
||||||
|
} catch {
|
||||||
|
print(error)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user