summaryrefslogtreecommitdiff
path: root/Hutch/Views/Pastes/PasteListViewModel.swift
blob: ee7d47acd2c8f2387144182ca60eb5181c372225 (plain) (blame)
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
108
109
import Foundation

@Observable
@MainActor
final class PasteListViewModel {
    private(set) var pastes: [Paste] = []
    private(set) var isLoading = false
    private(set) var isLoadingMore = false
    private(set) var isRefreshing = false
    private(set) var isCreatingPaste = false
    var error: String?

    private var cursor: String?
    private var hasMore = true
    private let service: PasteService

    init(service: PasteService) {
        self.service = service
    }

    func loadPastes() async {
        if pastes.isEmpty, let cached = service.loadCachedPastes() {
            pastes = cached.results
            cursor = cached.cursor
            hasMore = cached.cursor != nil
        }

        if pastes.isEmpty {
            isLoading = true
        } else {
            isRefreshing = true
        }
        error = nil
        cursor = nil
        hasMore = true

        do {
            let page = try await service.listPastes(cursor: nil, useCache: true)
            pastes = page.results
            cursor = page.cursor
            hasMore = page.cursor != nil
        } catch {
            if pastes.isEmpty {
                self.error = error.localizedDescription
            }
        }

        isLoading = false
        isRefreshing = false
    }

    func loadMoreIfNeeded(currentItem: Paste) async {
        guard let last = pastes.last,
              last.id == currentItem.id,
              hasMore,
              !isLoadingMore else {
            return
        }

        isLoadingMore = true
        defer { isLoadingMore = false }

        do {
            let page = try await service.listPastes(cursor: cursor, useCache: false)
            pastes.append(contentsOf: page.results)
            cursor = page.cursor
            hasMore = page.cursor != nil
        } catch {
            self.error = error.localizedDescription
        }
    }

    func createPaste(files: [PasteUploadDraft], visibility: Visibility) async -> Paste? {
        guard !isCreatingPaste else { return nil }

        let normalizedFiles = files.filter {
            !$0.contents.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
        }
        guard !normalizedFiles.isEmpty else {
            error = "Add at least one file with text content."
            return nil
        }

        isCreatingPaste = true
        error = nil
        defer { isCreatingPaste = false }

        do {
            let paste = try await service.createPaste(files: normalizedFiles, visibility: visibility)
            upsertPaste(paste)
            return paste
        } catch {
            self.error = error.localizedDescription
            return nil
        }
    }

    func upsertPaste(_ paste: Paste) {
        if let index = pastes.firstIndex(where: { $0.id == paste.id }) {
            pastes[index] = paste
        } else {
            pastes.insert(paste, at: 0)
        }
    }

    func removePaste(id: String) {
        pastes.removeAll { $0.id == id }
    }
}