summaryrefslogtreecommitdiff
path: root/Sora/Views/Post/PostGridView.swift
blob: 89c762a157334b6ce195104a08652e57853bfcff (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
import SwiftUI
import WaterfallGrid

struct PostGridView: View {
    @EnvironmentObject var settings: Settings
    @ObservedObject var manager: BooruManager
    @Environment(\.isSearching) private var isSearching

    var filteredPosts: [BooruPost] {
        (settings.showNSFWPosts ? manager.posts : manager.posts.filter { $0.rating == "s" || $0.rating == "q" })
            .sorted(by: { $0.id > $1.id })
    }

    var body: some View {
        ScrollViewReader { _ in
            ScrollView {
                if filteredPosts.isEmpty {
                    ProgressView()
                        .padding()
                }

                WaterfallGrid(filteredPosts, id: \.id) { post in
                    Group {
                        #if os(macOS)
                            Button {
                                manager.selectedPost = post
                            } label: {
                                PostView(
                                    post: post,
                                    manager: manager,
                                    posts: filteredPosts
                                )
                            }
                            .buttonStyle(PlainButtonStyle())
                        #else
                            NavigationLink(value: post) {
                                PostView(
                                    post: post,
                                    manager: manager,
                                    posts: filteredPosts
                                )
                            }
                        #endif
                    }
                }
                .gridStyle(columns: settings.columns)
            }
            .searchable(text: $manager.searchText, prompt: "Tags")
            .searchSuggestions {
                if settings.searchSuggestions {
                    SearchSuggestionsView(
                        tags: manager.allTags,
                        searchText: $manager.searchText
                    )
                }
            }
            .onSubmit(of: .search, manager.performSearch)
            .navigationDestination(for: BooruPost.self) { post in
                PostDetailsView(post: post)
            }
            .onChange(of: manager.searchText) { _, _ in
                if manager.searchText.isEmpty, !isSearching {
                    Task {
                        manager.performSearch()
                    }
                }
            }
            .toolbar {
                #if os(macOS)
                    ToolbarItem {
                        Button(action: {
                            Task {
                                await manager.fetchPosts(page: 1, tags: manager.tags, replace: true)
                            }
                        }) {
                            Label("Refresh", systemImage: "arrow.clockwise")
                        }
                    }
                #endif

                if !manager.tags.isEmpty {
                    #if os(macOS)
                        ToolbarItem {
                            PostGridBookmarkButtonView()
                        }
                    #else
                        ToolbarItem(placement: .bottomBar) {
                            PostGridBookmarkButtonView()
                        }
                    #endif
                }
            }
            .navigationTitle("Posts")
            .refreshable {
                await manager.fetchPosts(page: 1, tags: manager.tags, replace: true)
            }
        }
    }
}