Ferrite-backup/Ferrite/Views/ContentView.swift
kingbri 2322d3af67 Debrid: Decentralize and add AllDebrid support
AllDebrid is another debrid provider. Add support to Ferrite in
addition to RealDebrid.

The overall debrid login backend has changed to accomodate for a more
agnostic app structure where more services can be added as needed.

Also add some cosmetic changes to search so filters can be added while
searching for a phrase.

Signed-off-by: kingbri <bdashore3@proton.me>
2022-11-27 18:18:09 -05:00

126 lines
4.7 KiB
Swift

//
// ContentView.swift
// Ferrite
//
// Created by Brian Dashore on 7/1/22.
//
import SwiftUI
import SwiftUIX
struct ContentView: View {
@EnvironmentObject var scrapingModel: ScrapingViewModel
@EnvironmentObject var debridManager: DebridManager
@EnvironmentObject var navModel: NavigationViewModel
@EnvironmentObject var sourceManager: SourceManager
@FetchRequest(
entity: Source.entity(),
sortDescriptors: []
) var sources: FetchedResults<Source>
@State private var selectedSource: Source? {
didSet {
scrapingModel.filteredSource = selectedSource
}
}
var body: some View {
NavView {
VStack(spacing: 10) {
HStack(spacing: 6) {
Text("Filter")
.foregroundColor(.secondary)
Menu {
Button {
selectedSource = nil
} label: {
Text("None")
if selectedSource == nil {
Image(systemName: "checkmark")
}
}
ForEach(sources, id: \.self) { source in
if let name = source.name, source.enabled {
Button {
selectedSource = source
} label: {
if selectedSource == source {
Label(name, systemImage: "checkmark")
} else {
Text(name)
}
}
}
}
} label: {
Text(selectedSource?.name ?? "Source")
.padding(.trailing, -3)
Image(systemName: "chevron.down")
}
.foregroundColor(.primary)
.animation(.none)
Spacer()
}
.padding(.vertical, 5)
.padding(.horizontal, 20)
SearchResultsView()
}
.navigationTitle("Search")
.navigationBarTitleDisplayMode(navModel.isEditingSearch || navModel.isSearching ? .inline : .automatic)
.navigationSearchBar {
SearchBar("Search",
text: $scrapingModel.searchText,
isEditing: $navModel.isEditingSearch,
onCommit: {
scrapingModel.searchResults = []
scrapingModel.runningSearchTask = Task {
navModel.isSearching = true
navModel.showSearchProgress = true
let sources = sourceManager.fetchInstalledSources()
await scrapingModel.scanSources(sources: sources)
if debridManager.enabledDebrids.count > 0, !scrapingModel.searchResults.isEmpty {
debridManager.realDebridIAValues = []
debridManager.allDebridIAValues = []
await debridManager.populateDebridHashes(
scrapingModel.searchResults.compactMap(\.magnetHash)
)
}
navModel.showSearchProgress = false
}
})
.showsCancelButton(navModel.isEditingSearch || navModel.isSearching)
.onCancel {
scrapingModel.searchResults = []
scrapingModel.runningSearchTask?.cancel()
scrapingModel.runningSearchTask = nil
navModel.isSearching = false
scrapingModel.searchText = ""
}
}
.introspectSearchController { searchController in
searchController.hidesNavigationBarDuringPresentation = false
}
.toolbar {
ToolbarItemGroup(placement: .navigationBarTrailing) {
DebridChoiceView()
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}