- Fix how abrupt search cancellations work - Add a no results prompt if no results are found - Clean up how scraping model results are returned on error - Allow a base URL and dynamic base URL to be provided together Signed-off-by: kingbri <bdashore3@gmail.com>
95 lines
3.6 KiB
Swift
95 lines
3.6 KiB
Swift
//
|
|
// SearchResultsView.swift
|
|
// Ferrite
|
|
//
|
|
// Created by Brian Dashore on 7/11/22.
|
|
//
|
|
|
|
import SwiftUI
|
|
|
|
struct SearchResultsView: View {
|
|
@Environment(\.isSearching) var isSearching
|
|
@Environment(\.dismissSearch) var dismissSearch
|
|
|
|
@EnvironmentObject var scrapingModel: ScrapingViewModel
|
|
@EnvironmentObject var debridManager: DebridManager
|
|
@EnvironmentObject var navModel: NavigationViewModel
|
|
|
|
@AppStorage("RealDebrid.Enabled") var realDebridEnabled = false
|
|
|
|
var body: some View {
|
|
List {
|
|
ForEach(scrapingModel.searchResults, id: \.self) { result in
|
|
if result.source == scrapingModel.filteredSource?.name || scrapingModel.filteredSource == nil {
|
|
VStack(alignment: .leading) {
|
|
Button {
|
|
scrapingModel.selectedSearchResult = result
|
|
|
|
switch debridManager.matchSearchResult(result: result) {
|
|
case .full:
|
|
Task {
|
|
await debridManager.fetchRdDownload(searchResult: result)
|
|
navModel.runDebridAction(action: nil, urlString: debridManager.realDebridDownloadUrl)
|
|
}
|
|
case .partial:
|
|
if debridManager.setSelectedRdResult(result: result) {
|
|
navModel.currentChoiceSheet = .batch
|
|
}
|
|
case .none:
|
|
navModel.runMagnetAction(action: nil, searchResult: result)
|
|
}
|
|
} label: {
|
|
Text(result.title)
|
|
.font(.callout)
|
|
.fixedSize(horizontal: false, vertical: true)
|
|
}
|
|
.tint(.primary)
|
|
.padding(.bottom, 5)
|
|
|
|
SearchResultRDView(result: result)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
.overlay {
|
|
if scrapingModel.searchResults.isEmpty {
|
|
if navModel.showSearchProgress {
|
|
VStack(spacing: 5) {
|
|
ProgressView()
|
|
Text("Loading \(scrapingModel.currentSourceName ?? "")")
|
|
}
|
|
} else if isSearching, scrapingModel.runningSearchTask != nil {
|
|
Text("No results found")
|
|
}
|
|
}
|
|
}
|
|
.onChange(of: navModel.selectedTab) { tab in
|
|
// Cancel the search if tab is switched while search is in progress
|
|
if tab != .search, navModel.showSearchProgress {
|
|
scrapingModel.runningSearchTask?.cancel()
|
|
scrapingModel.runningSearchTask = nil
|
|
dismissSearch()
|
|
}
|
|
}
|
|
.onChange(of: scrapingModel.searchResults) { _ in
|
|
// Cleans up any leftover search results in the event of an abrupt cancellation
|
|
if !isSearching {
|
|
scrapingModel.searchResults = []
|
|
}
|
|
}
|
|
.onChange(of: isSearching) { changed in
|
|
// Clear the results array and cleans up search tasks on cancel
|
|
if !changed {
|
|
scrapingModel.searchResults = []
|
|
scrapingModel.runningSearchTask?.cancel()
|
|
scrapingModel.runningSearchTask = nil
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
struct SearchResultsView_Previews: PreviewProvider {
|
|
static var previews: some View {
|
|
SearchResultsView()
|
|
}
|
|
}
|