Debrid: Use universal cached IDs

Different services can send different statuses for if a file is
cached or not. Therefore, make this scoped to the debrid service
rather than expecting everything to state "downloaded".

Also it feels pretty blank if the disclosure groups are gone when
a cloud array is empty, so remove those checks.

Signed-off-by: kingbri <bdashore3@proton.me>
This commit is contained in:
kingbri 2024-06-16 21:23:22 -05:00 committed by Brian Dashore
parent 64378ccc23
commit 217bb5caa4
7 changed files with 18 additions and 14 deletions

View file

@ -11,6 +11,7 @@ class AllDebrid: PollingDebridSource, ObservableObject {
let id = "AllDebrid"
let abbreviation = "AD"
let website = "https://alldebrid.com"
let cachedStatus: [String] = ["Ready"]
var authTask: Task<Void, Error>?
@Published var authProcessing: Bool = false
@ -224,7 +225,7 @@ class AllDebrid: PollingDebridSource, ObservableObject {
func getRestrictedFile(magnet: Magnet, ia: DebridIA?, iaFile: DebridIAFile?) async throws -> (restrictedFile: DebridIAFile?, newIA: DebridIA?) {
let selectedMagnetId: String
if let existingMagnet = cloudMagnets.first(where: { $0.hash == magnet.hash && $0.status == "Ready" }) {
if let existingMagnet = cloudMagnets.first(where: { $0.hash == magnet.hash && cachedStatus.contains($0.status) }) {
selectedMagnetId = existingMagnet.id
} else {
let magnetId = try await addMagnet(magnet: magnet)
@ -317,7 +318,7 @@ class AllDebrid: PollingDebridSource, ObservableObject {
DebridCloudMagnet(
id: String(magnetResponse.id),
fileName: magnetResponse.filename,
status: magnetResponse.status == "Ready" ? "downloaded" : magnetResponse.status,
status: magnetResponse.status,
hash: magnetResponse.hash,
links: magnetResponse.links.map(\.link)
)

View file

@ -14,6 +14,7 @@ class OffCloud: DebridSource, ObservableObject {
let description: String? = "OffCloud is a debrid service that is used for downloads and media playback. " +
"You must pay to access this service. \n\n" +
"This service does not inform if a magnet link is a batch before downloading."
let cachedStatus: [String] = ["downloaded"]
@Published var authProcessing: Bool = false
var isLoggedIn: Bool {
@ -143,12 +144,12 @@ class OffCloud: DebridSource, ObservableObject {
let selectedCloudMagnet: DebridCloudMagnet
// Don't queue a new job if the magnet already exists in the user's account
if let existingCloudMagnet = cloudMagnets.first(where: { $0.hash == magnet.hash && $0.status == "downloaded" }) {
if let existingCloudMagnet = cloudMagnets.first(where: { $0.hash == magnet.hash && cachedStatus.contains($0.status) }) {
selectedCloudMagnet = existingCloudMagnet
} else {
let cloudDownloadResponse = try await offcloudDownload(magnet: magnet)
guard cloudDownloadResponse.status == "downloaded" else {
guard cachedStatus.contains(cloudDownloadResponse.status) else {
throw DebridError.IsCaching
}

View file

@ -11,6 +11,7 @@ class RealDebrid: PollingDebridSource, ObservableObject {
let id = "RealDebrid"
let abbreviation = "RD"
let website = "https://real-debrid.com"
let cachedStatus: [String] = ["downloaded"]
var authTask: Task<Void, Error>?
@Published var authProcessing: Bool = false

View file

@ -13,6 +13,7 @@ class TorBox: DebridSource, ObservableObject {
let website = "https://torbox.app"
let description: String? = "TorBox is a debrid service that is used for downloads and media playback with seeding. " +
"Both free and paid plans are available."
let cachedStatus: [String] = ["cached", "completed"]
@Published var authProcessing: Bool = false
var isLoggedIn: Bool {
@ -155,7 +156,7 @@ class TorBox: DebridSource, ObservableObject {
}
// If the user magnet isn't saved, it's considered as caching
guard filteredCloudMagnet.downloadState == "cached" || filteredCloudMagnet.downloadState == "completed" else {
guard cachedStatus.contains(filteredCloudMagnet.downloadState) else {
throw DebridError.IsCaching
}
@ -245,7 +246,7 @@ class TorBox: DebridSource, ObservableObject {
DebridCloudMagnet(
id: String(cloudMagnet.id),
fileName: cloudMagnet.name,
status: cloudMagnet.downloadState == "cached" || cloudMagnet.downloadState == "completed" ? "downloaded" : cloudMagnet.downloadState,
status: cloudMagnet.downloadState,
hash: cloudMagnet.hash,
links: cloudMagnet.files.map { String($0.id) }
)

View file

@ -14,6 +14,7 @@ protocol DebridSource: AnyObservableObject {
var abbreviation: String { get }
var website: String { get }
var description: String? { get }
var cachedStatus: [String] { get }
// Auth variables
var authProcessing: Bool { get set }
@ -59,6 +60,10 @@ extension DebridSource {
var description: String? {
nil
}
var cachedStatus: [String] {
[]
}
}
protocol PollingDebridSource: DebridSource {

View file

@ -22,7 +22,7 @@ struct CloudMagnetView: View {
searchText.isEmpty ? true : $0.fileName.lowercased().contains(searchText.lowercased())
}, id: \.self) { cloudMagnet in
Button {
if cloudMagnet.status == "downloaded", !cloudMagnet.links.isEmpty {
if debridSource.cachedStatus.contains(cloudMagnet.status), !cloudMagnet.links.isEmpty {
navModel.resultFromCloud = true
navModel.selectedTitle = cloudMagnet.fileName

View file

@ -16,13 +16,8 @@ struct DebridCloudView: View {
var body: some View {
List {
if !debridSource.cloudDownloads.isEmpty {
CloudDownloadView(debridSource: debridSource, searchText: $searchText)
}
if !debridSource.cloudMagnets.isEmpty {
CloudMagnetView(debridSource: debridSource, searchText: $searchText)
}
CloudDownloadView(debridSource: debridSource, searchText: $searchText)
CloudMagnetView(debridSource: debridSource, searchText: $searchText)
}
.listStyle(.plain)
.task {