Some debrid services aren't "rich", which means that they don't broadcast whether an instantly available torrent is a batch or a single file. This results in all torrents either having the green badge or red badge based on what hash is given. However, batches need to intercept the download itself which requires the download function to be split into download and unrestrict. In between, there's room for the batch sheet to act. Signed-off-by: kingbri <bdashore3@proton.me>
96 lines
3.3 KiB
Swift
96 lines
3.3 KiB
Swift
//
|
|
// BatchChoiceView.swift
|
|
// Ferrite
|
|
//
|
|
// Created by Brian Dashore on 7/24/22.
|
|
//
|
|
|
|
import SwiftUI
|
|
|
|
struct BatchChoiceView: View {
|
|
@EnvironmentObject var debridManager: DebridManager
|
|
@EnvironmentObject var scrapingModel: ScrapingViewModel
|
|
@EnvironmentObject var navModel: NavigationViewModel
|
|
@EnvironmentObject var pluginManager: PluginManager
|
|
|
|
let backgroundContext = PersistenceController.shared.backgroundContext
|
|
|
|
@AppStorage("Behavior.AutocorrectSearch") var autocorrectSearch = true
|
|
|
|
@State private var searchText: String = ""
|
|
|
|
// TODO: Make this generic for an IA protocol
|
|
var body: some View {
|
|
NavView {
|
|
List {
|
|
ForEach(debridManager.selectedDebridItem?.files ?? [], id: \.self) { file in
|
|
if file.name.lowercased().contains(searchText.lowercased()) || searchText.isEmpty {
|
|
Button(file.name) {
|
|
debridManager.selectedDebridFile = file
|
|
|
|
queueCommonDownload(fileName: file.name)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
.tint(.primary)
|
|
.listStyle(.insetGrouped)
|
|
.inlinedList(inset: -20)
|
|
.searchable(text: $searchText, placement: .navigationBarDrawer(displayMode: .always))
|
|
.autocorrectionDisabled(!autocorrectSearch)
|
|
.textInputAutocapitalization(autocorrectSearch ? .sentences : .never)
|
|
.navigationTitle("Select a file")
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
.toolbar {
|
|
ToolbarItem(placement: .navigationBarTrailing) {
|
|
Button("Done") {
|
|
navModel.currentChoiceSheet = nil
|
|
|
|
Task {
|
|
try? await Task.sleep(seconds: 1)
|
|
|
|
debridManager.clearSelectedDebridItems()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Common function to communicate betwen VMs and queue/display a download
|
|
func queueCommonDownload(fileName: String) {
|
|
debridManager.currentDebridTask = Task {
|
|
if debridManager.requiresUnrestrict {
|
|
await debridManager.unrestrictDownload()
|
|
} else {
|
|
await debridManager.fetchDebridDownload(magnet: navModel.selectedMagnet)
|
|
}
|
|
|
|
if !debridManager.downloadUrl.isEmpty {
|
|
try? await Task.sleep(seconds: 1)
|
|
navModel.selectedBatchTitle = fileName
|
|
|
|
if var selectedHistoryInfo = navModel.selectedHistoryInfo {
|
|
selectedHistoryInfo.url = debridManager.downloadUrl
|
|
selectedHistoryInfo.subName = fileName
|
|
PersistenceController.shared.createHistory(selectedHistoryInfo, performSave: true)
|
|
}
|
|
|
|
pluginManager.runDefaultAction(
|
|
urlString: debridManager.downloadUrl,
|
|
navModel: navModel
|
|
)
|
|
}
|
|
|
|
debridManager.clearSelectedDebridItems()
|
|
}
|
|
|
|
navModel.currentChoiceSheet = nil
|
|
}
|
|
}
|
|
|
|
struct BatchChoiceView_Previews: PreviewProvider {
|
|
static var previews: some View {
|
|
BatchChoiceView()
|
|
}
|
|
}
|