~ knotless Search/
↑↓ move↵ open⇧↵ all results

grain.social/grain-ios

1 collaborator
0
1
main
39 lines · 1.1 KB Raw History
import Foundation

@Observable
@MainActor
final class StoryStripViewModel {
    var authors: [GrainStoryAuthor] = []
    var isLoading = false
    /// Bumped to trigger re-render without changing author order.
    var version: Int = 0

    private let client: XRPCClient

    init(client: XRPCClient) {
        self.client = client
    }

    func load(
        auth: AuthContext? = nil,
        storyStatusCache: StoryStatusCache? = nil,
        viewedStories: ViewedStoryStorage? = nil
    ) async {
        isLoading = true
        do {
            let response = try await client.getStoryAuthors(auth: auth)
            authors = response.authors.filter { $0.storyCount > 0 }
            storyStatusCache?.update(from: response.authors)
            // What was watched on another device arrives with the list.
            viewedStories?.absorb(authors: response.authors)
        } catch {
            // Silently fail — strip just won't show
        }
        isLoading = false
    }

    /// Signal that viewed state changed so views re-evaluate sort order.
    func invalidate() {
        version += 1
    }
}