-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathNCAssistantChatConversationsModel.swift
More file actions
38 lines (32 loc) · 1.3 KB
/
NCAssistantChatConversationsModel.swift
File metadata and controls
38 lines (32 loc) · 1.3 KB
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
// SPDX-FileCopyrightText: Nextcloud GmbH
// SPDX-FileCopyrightText: 2026 Milen Pivchev
// SPDX-License-Identifier: GPL-3.0-or-later
import Foundation
import NextcloudKit
@Observable class NCAssistantChatConversationsModel {
var conversations: [AssistantConversation] = []
var isLoading: Bool = false
var hasError: Bool = false
private let ncSession: NCSession.Session
init(controller: NCMainTabBarController?) {
self.ncSession = NCSession.shared.getSession(controller: controller)
loadAllSessions()
}
func loadAllSessions() {
Task {
let result = await NextcloudKit.shared.getAssistantChatConversations(account: ncSession.account)
conversations = result.sessions ?? []
}
}
func createNewConversation(title: String? = nil) async -> AssistantConversation? {
let timestamp = Int(Date().timeIntervalSince1970)
let result = await NextcloudKit.shared.createAssistantChatConversation(title: title, timestamp: timestamp, account: ncSession.account)
if result.error == .success, let newConversation = result.conversation?.conversation {
conversations.insert(newConversation, at: 0)
return newConversation
} else {
hasError = true
return nil
}
}
}