summaryrefslogtreecommitdiff
path: root/Sora/Views/Post/Grid/PostGridSearchHistoryView.swift
blob: 30f7808897533f67d438484bcbd915fab0064c48 (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
import SwiftUI

struct PostGridSearchHistoryView: View {
  @EnvironmentObject private var manager: BooruManager
  @EnvironmentObject private var settings: SettingsManager
  @State private var searchText: String = ""
  @Binding var selectedTab: Int
  @Binding var isPresented: Bool
  @State private var isShowingRemoveAllConfirmation = false

  var filteredHistory: [BooruSearchQuery] {
    guard !searchText.isEmpty else {
      return settings.searchHistory
    }

    return settings.searchHistory
      .filter { query in
        query.tags
          .joined(separator: " ")
          .lowercased()
          .contains(searchText.lowercased())
      }
  }

  var body: some View {
    NavigationStack {
      VStack {
        if settings.searchHistory.isEmpty {
          ContentUnavailableView(
            "No History",
            systemImage: "magnifyingglass",
            description: Text("Recent searches will appear here.")
          )
        } else {
          List {
            if filteredHistory.isEmpty, !searchText.isEmpty {
              Text("No matching history found")
            }

            ForEach(
              filteredHistory.sorted { $0.date > $1.date },
              id: \.id
            ) { query in
              Button(action: {
                let previousProvider = settings.preferredBooru

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

                isPresented.toggle()

                if previousProvider == settings.preferredBooru {
                  manager.performSearch()
                }
              }) {
                GenericItemView(
                  item: query,
                  removeAction: settings.removeSearchHistoryEntry
                )
              }
              #if os(macOS)
                .buttonStyle(.plain)
              #endif
            }
            .onDelete(perform: settings.removeSearchHistoryEntry)
          }
        }
      }
    }
    .navigationTitle("Search History")
    .searchable(text: $searchText)
    .toolbar {
      ToolbarItem {
        Button(action: {
          isShowingRemoveAllConfirmation = true
        }) {
          Label("Remove All Search History", systemImage: "trash")
        }
      }
    }
    .confirmationDialog(
      "Are you sure you want to remove all searches? This action cannot be undone.",
      isPresented: $isShowingRemoveAllConfirmation
    ) {
      Button("Remove All Searches") {
        settings.bookmarks.removeAll()
      }
    }
  }
}

#Preview {
  PostGridSearchHistoryView(
    selectedTab: .constant(0),
    isPresented: .constant(true)
  )
  .environmentObject(SettingsManager())
  .environmentObject(BooruManager(.safebooru))
}