-
-
Notifications
You must be signed in to change notification settings - Fork 450
/
ConversationsListViewModel.swift
107 lines (93 loc) · 3.42 KB
/
ConversationsListViewModel.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import Models
import Network
import SwiftUI
@MainActor
@Observable class ConversationsListViewModel {
var client: Client?
var isLoadingFirstPage: Bool = true
var isLoadingNextPage: Bool = false
var conversations: [Conversation] = []
var isError: Bool = false
var nextPage: LinkHandler?
var scrollToTopVisible: Bool = false
public init() {}
func fetchConversations() async {
guard let client else { return }
if conversations.isEmpty {
isLoadingFirstPage = true
}
do {
(conversations, nextPage) = try await client.getWithLink(endpoint: Conversations.conversations(maxId: nil))
if nextPage?.maxId == nil {
nextPage = nil
}
isLoadingFirstPage = false
} catch {
isError = true
isLoadingFirstPage = false
}
}
func fetchNextPage() async {
if let maxId = nextPage?.maxId, let client {
do {
isLoadingNextPage = true
var nextMessages: [Conversation] = []
(nextMessages, nextPage) = try await client.getWithLink(endpoint: Conversations.conversations(maxId: maxId))
conversations.append(contentsOf: nextMessages)
if nextPage?.maxId == nil {
nextPage = nil
}
isLoadingNextPage = false
} catch {}
}
}
func markAsRead(conversation: Conversation) async {
guard let client else { return }
_ = try? await client.post(endpoint: Conversations.read(id: conversation.id))
}
func delete(conversation: Conversation) async {
guard let client else { return }
_ = try? await client.delete(endpoint: Conversations.delete(id: conversation.id))
await fetchConversations()
}
func favorite(conversation: Conversation) async {
guard let client, let message = conversation.lastStatus else { return }
let endpoint: Endpoint = if message.favourited ?? false {
Statuses.unfavorite(id: message.id)
} else {
Statuses.favorite(id: message.id)
}
do {
let status: Status = try await client.post(endpoint: endpoint)
updateConversationWithNewLastStatus(conversation: conversation, newLastStatus: status)
} catch {}
}
func bookmark(conversation: Conversation) async {
guard let client, let message = conversation.lastStatus else { return }
let endpoint: Endpoint = if message.bookmarked ?? false {
Statuses.unbookmark(id: message.id)
} else {
Statuses.bookmark(id: message.id)
}
do {
let status: Status = try await client.post(endpoint: endpoint)
updateConversationWithNewLastStatus(conversation: conversation, newLastStatus: status)
} catch {}
}
private func updateConversationWithNewLastStatus(conversation: Conversation, newLastStatus: Status) {
let newConversation = Conversation(id: conversation.id, unread: conversation.unread, lastStatus: newLastStatus, accounts: conversation.accounts)
updateConversations(conversation: newConversation)
}
private func updateConversations(conversation: Conversation) {
if let index = conversations.firstIndex(where: { $0.id == conversation.id }) {
conversations.remove(at: index)
}
conversations.insert(conversation, at: 0)
conversations = conversations.sorted(by: { ($0.lastStatus?.createdAt.asDate ?? Date.now) > ($1.lastStatus?.createdAt.asDate ?? Date.now) })
}
func handleEvent(event: any StreamEvent) {
if let event = event as? StreamEventConversation {
updateConversations(conversation: event.conversation)
}
}
}