Ferrite-backup/Ferrite/Views/SearchResultsView.swift
kingbri e9670ea118 RealDebrid: Add batch torrent support
Batch torrents are torrents that have multiple files bundled within
one torrent file.

RealDebrid does support these, but it is difficult to get them to work.

The main flow requires setting a specific combination in RealDebrid
to allow for link generation. However, this is not intuitive to users
and is bad API design on RealDebrid's part.

Ferrite's implementation presents users with all the possible files
from batches (duplicates deleted) and selects the user-chosen file
to download. That way, only the user chosen file is presented to
play on an external video player.

This still needs work for optimization purposes, but this commit
does produce a working build.

Signed-off-by: kingbri <bdashore3@gmail.com>
2022-08-04 21:33:59 -04:00

101 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(\.colorScheme) var colorScheme
@EnvironmentObject var scrapingModel: ScrapingViewModel
@EnvironmentObject var debridManager: DebridManager
@EnvironmentObject var navigationModel: NavigationViewModel
@AppStorage("RealDebrid.Enabled") var realDebridEnabled = false
var body: some View {
List {
ForEach(scrapingModel.searchResults, id: \.self) { result in
VStack(alignment: .leading) {
Button {
scrapingModel.selectedSearchResult = result
switch debridManager.matchSearchResult(result: result) {
case .full:
Task {
await debridManager.fetchRdDownload(searchResult: result)
navigationModel.currentChoiceSheet = .magnet
}
case .partial:
if debridManager.setSelectedRdResult(result: result) {
navigationModel.currentChoiceSheet = .batch
}
case .none:
navigationModel.currentChoiceSheet = .magnet
}
} label: {
Text(result.title)
.font(.callout)
.fixedSize(horizontal: false, vertical: true)
}
.sheet(item: $navigationModel.currentChoiceSheet) { item in
switch item {
case .magnet:
MagnetChoiceView()
case .batch:
BatchChoiceView()
}
}
.tint(colorScheme == .light ? .black : .white)
.padding(.bottom, 5)
HStack {
Text(result.source)
Spacer()
Text(result.size)
if realDebridEnabled {
Text("RD")
.fontWeight(.bold)
.padding(2)
.background {
switch debridManager.matchSearchResult(result: result) {
case .full:
Color.green
.cornerRadius(4)
.opacity(0.5)
case .partial:
Color.orange
.cornerRadius(4)
.opacity(0.5)
case .none:
Color.red
.cornerRadius(4)
.opacity(0.5)
}
}
}
}
.font(.caption)
}
}
}
.onChange(of: isSearching) { changed in
if !changed {
scrapingModel.searchResults = []
}
}
}
}
struct SearchResultsView_Previews: PreviewProvider {
static var previews: some View {
SearchResultsView()
}
}