summaryrefslogtreecommitdiff
path: root/Sora/Views/Bookmarks/BookmarksView.swift
blob: b52e7b7b7422b1eb085026316c07bf68bfcc5300 (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
import SwiftUI

struct BookmarksView: View {
  @EnvironmentObject var settings: Settings
  @EnvironmentObject var manager: BooruManager
  @Binding var selectedTab: Int
  @State private var bookmarksSearchText: String = ""

  var filteredBookmarks: [Bookmark] {
    guard !bookmarksSearchText.isEmpty else {
      return settings.bookmarks
    }

    return settings.bookmarks
      .filter { bookmark in
        bookmark.tags.joined(separator: " ").lowercased().contains(bookmarksSearchText.lowercased())
      }
  }

  var body: some View {
    NavigationStack {
      VStack {
        if settings.bookmarks.isEmpty {
          ContentUnavailableView(
            "No Bookmarks",
            systemImage: "bookmark",
            description: Text("Add a bookmark by tapping the bookmark button on a search page.")
          )
        } else {
          List {
            if filteredBookmarks.isEmpty, !bookmarksSearchText.isEmpty {
              Text("No bookmarks match your search")
            }

            ForEach(
              filteredBookmarks,
              id: \.self
            ) { bookmark in
              Button(action: {
                let previousProvider = settings.preferredBooru

                settings.preferredBooru = bookmark.provider
                manager.searchText = bookmark.tags.joined(separator: " ")
                selectedTab = 0

                if previousProvider == settings.preferredBooru {
                  manager.performSearch()
                }
              }) {
                BookmarkListItemView(bookmark: bookmark)
              }
              #if os(macOS)
                .buttonStyle(.plain)
              #endif
            }
            .onDelete(perform: settings.removeBookmark)
          }
          #if os(macOS)
            .listStyle(.plain)
          #endif
        }
      }
    }
    .navigationTitle("Bookmarks")
    .searchable(text: $bookmarksSearchText)
  }
}

#Preview {
  BookmarksView(selectedTab: .constant(1))
    .environmentObject(Settings())
    .environmentObject(BooruManager(.yandere))
}