summaryrefslogtreecommitdiff
path: root/Sora/Views/Post/Grid/SearchHistory
diff options
context:
space:
mode:
Diffstat (limited to 'Sora/Views/Post/Grid/SearchHistory')
-rw-r--r--Sora/Views/Post/Grid/SearchHistory/PostGridSearchHistoryItemView.swift39
-rw-r--r--Sora/Views/Post/Grid/SearchHistory/PostGridSearchHistoryView.swift73
2 files changed, 112 insertions, 0 deletions
diff --git a/Sora/Views/Post/Grid/SearchHistory/PostGridSearchHistoryItemView.swift b/Sora/Views/Post/Grid/SearchHistory/PostGridSearchHistoryItemView.swift
new file mode 100644
index 0000000..5e0b7ce
--- /dev/null
+++ b/Sora/Views/Post/Grid/SearchHistory/PostGridSearchHistoryItemView.swift
@@ -0,0 +1,39 @@
+import SwiftUI
+
+struct PostGridSearchHistoryItemView: View {
+ @EnvironmentObject var settings: SettingsManager
+ var query: BooruSearchQuery
+
+ var body: some View {
+ #if os(macOS)
+ HStack {
+ VStack(alignment: .leading) {
+ Text(query.tags.joined(separator: ", ").lowercased())
+
+ Spacer()
+
+ Text("On \(query.searchedAt.formatted()) from \(query.provider.rawValue)")
+ .font(.caption)
+ .foregroundStyle(Color.secondary)
+ }
+
+ Spacer()
+
+ Button {
+ settings.removeSearchHistoryEntry(withID: query.id)
+ } label: {
+ Image(systemName: "trash")
+ }
+ }
+ .padding()
+ #else
+ VStack(alignment: .leading) {
+ Text(query.tags.joined(separator: ", ").lowercased())
+
+ Text("On \(query.searchedAt.formatted()) from \(query.provider.rawValue)")
+ .font(.caption)
+ .foregroundStyle(Color.secondary)
+ }
+ #endif
+ }
+}
diff --git a/Sora/Views/Post/Grid/SearchHistory/PostGridSearchHistoryView.swift b/Sora/Views/Post/Grid/SearchHistory/PostGridSearchHistoryView.swift
new file mode 100644
index 0000000..be2d2ea
--- /dev/null
+++ b/Sora/Views/Post/Grid/SearchHistory/PostGridSearchHistoryView.swift
@@ -0,0 +1,73 @@
+import SwiftUI
+
+struct PostGridSearchHistoryView: View {
+ @EnvironmentObject private var manager: BooruManager
+ @EnvironmentObject private var settings: SettingsManager
+ @State private var searchText: String = ""
+ @Binding var selectedTab: Int
+
+ 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, id: \.id) { query in
+ Button(action: {
+ let previousProvider = settings.preferredBooru
+
+ settings.preferredBooru = query.provider
+ manager.searchText = query.tags.joined(separator: " ")
+ selectedTab = 0
+
+ if previousProvider == settings.preferredBooru {
+ manager.performSearch()
+ }
+ }) {
+ PostGridSearchHistoryItemView(query: query)
+ }
+ #if os(macOS)
+ .buttonStyle(.plain)
+ #endif
+ }
+ .onDelete(perform: settings.removeSearchHistoryEntry)
+ }
+ #if os(macOS)
+ .listStyle(.plain)
+ #endif
+ }
+ }
+ }
+ .navigationTitle("Search History")
+ .searchable(text: $searchText)
+ }
+}
+
+#Preview {
+ PostGridSearchHistoryView(selectedTab: .constant(0))
+ .environmentObject(SettingsManager())
+ .environmentObject(BooruManager(.safebooru))
+}