Debrid: Remove ID storage

Storing an ID reference is redundant. Store a class reference
instead.

Signed-off-by: kingbri <bdashore3@proton.me>
This commit is contained in:
kingbri 2024-06-05 13:05:26 -04:00
parent 46e66ab457
commit 4e6cfee608
14 changed files with 45 additions and 50 deletions

View file

@ -9,9 +9,9 @@ import Foundation
// TODO: Fix errors // TODO: Fix errors
public class AllDebrid: PollingDebridSource, ObservableObject { public class AllDebrid: PollingDebridSource, ObservableObject {
public let id = DebridInfo( public let id = "AllDebrid"
name: "AllDebrid", abbreviation: "AD", website: "https://alldebrid.com" public let abbreviation = "AD"
) public let website = "https://alldebrid.com"
public var authTask: Task<Void, Error>? public var authTask: Task<Void, Error>?
public var authProcessing: Bool = false public var authProcessing: Bool = false
@ -178,7 +178,7 @@ public class AllDebrid: PollingDebridSource, ObservableObject {
return DebridIA( return DebridIA(
magnet: Magnet(hash: magnetResp.hash, link: magnetResp.magnet), magnet: Magnet(hash: magnetResp.hash, link: magnetResp.magnet),
source: self.id.name, source: self.id,
expiryTimeStamp: Date().timeIntervalSince1970 + 300, expiryTimeStamp: Date().timeIntervalSince1970 + 300,
files: files files: files
) )
@ -292,7 +292,7 @@ public class AllDebrid: PollingDebridSource, ObservableObject {
cloudTorrents = rawResponse.magnets.map { magnetResponse in cloudTorrents = rawResponse.magnets.map { magnetResponse in
DebridCloudTorrent( DebridCloudTorrent(
torrentId: String(magnetResponse.id), torrentId: String(magnetResponse.id),
source: self.id.name, source: self.id,
fileName: magnetResponse.filename, fileName: magnetResponse.filename,
status: magnetResponse.status, status: magnetResponse.status,
hash: magnetResponse.hash, hash: magnetResponse.hash,
@ -325,7 +325,7 @@ public class AllDebrid: PollingDebridSource, ObservableObject {
// The link is also the ID // The link is also the ID
cloudDownloads = rawResponse.links.map { link in cloudDownloads = rawResponse.links.map { link in
DebridCloudDownload( DebridCloudDownload(
downloadId: link.link, source: self.id.name, fileName: link.filename, link: link.link downloadId: link.link, source: self.id, fileName: link.filename, link: link.link
) )
} }

View file

@ -8,10 +8,9 @@
import Foundation import Foundation
public class Premiumize: OAuthDebridSource, ObservableObject { public class Premiumize: OAuthDebridSource, ObservableObject {
public let id = DebridInfo( public let id = "Premiumize"
name: "Premiumize", abbreviation: "PM", website: "https://premiumize.me" public let abbreviation = "PM"
) public let website = "https://premiumize.me"
@Published public var authProcessing: Bool = false @Published public var authProcessing: Bool = false
public var isLoggedIn: Bool { public var isLoggedIn: Bool {
getToken() != nil getToken() != nil
@ -195,7 +194,7 @@ public class Premiumize: OAuthDebridSource, ObservableObject {
return DebridIA( return DebridIA(
magnet: magnet, magnet: magnet,
source: id.name, source: id,
expiryTimeStamp: Date().timeIntervalSince1970 + 300, expiryTimeStamp: Date().timeIntervalSince1970 + 300,
files: files files: files
) )
@ -300,7 +299,7 @@ public class Premiumize: OAuthDebridSource, ObservableObject {
// The "link" is the ID for Premiumize // The "link" is the ID for Premiumize
cloudDownloads = rawResponse.files.map { file in cloudDownloads = rawResponse.files.map { file in
DebridCloudDownload(downloadId: file.id, source: self.id.name, fileName: file.name, link: file.id) DebridCloudDownload(downloadId: file.id, source: self.id, fileName: file.name, link: file.id)
} }
return cloudDownloads return cloudDownloads

View file

@ -8,9 +8,9 @@
import Foundation import Foundation
public class RealDebrid: PollingDebridSource, ObservableObject { public class RealDebrid: PollingDebridSource, ObservableObject {
public let id = DebridInfo( public let id = "RealDebrid"
name: "RealDebrid", abbreviation: "RD", website: "https://real-debrid.com" public let abbreviation = "RD"
) public let website = "https://real-debrid.com"
public var authTask: Task<Void, Error>? public var authTask: Task<Void, Error>?
@Published public var authProcessing: Bool = false @Published public var authProcessing: Bool = false
@ -287,7 +287,7 @@ public class RealDebrid: PollingDebridSource, ObservableObject {
IAValues.append( IAValues.append(
DebridIA( DebridIA(
magnet: Magnet(hash: hash, link: nil), magnet: Magnet(hash: hash, link: nil),
source: id.name, source: id,
expiryTimeStamp: Date().timeIntervalSince1970 + 300, expiryTimeStamp: Date().timeIntervalSince1970 + 300,
files: files files: files
) )
@ -296,7 +296,7 @@ public class RealDebrid: PollingDebridSource, ObservableObject {
IAValues.append( IAValues.append(
DebridIA( DebridIA(
magnet: Magnet(hash: hash, link: nil), magnet: Magnet(hash: hash, link: nil),
source: id.name, source: id,
expiryTimeStamp: Date().timeIntervalSince1970 + 300, expiryTimeStamp: Date().timeIntervalSince1970 + 300,
files: [] files: []
) )
@ -427,7 +427,7 @@ public class RealDebrid: PollingDebridSource, ObservableObject {
cloudTorrents = rawResponse.map { response in cloudTorrents = rawResponse.map { response in
DebridCloudTorrent( DebridCloudTorrent(
torrentId: response.id, torrentId: response.id,
source: self.id.name, source: self.id,
fileName: response.filename, fileName: response.filename,
status: response.status, status: response.status,
hash: response.hash, hash: response.hash,
@ -453,7 +453,7 @@ public class RealDebrid: PollingDebridSource, ObservableObject {
let data = try await performRequest(request: &request, requestName: #function) let data = try await performRequest(request: &request, requestName: #function)
let rawResponse = try jsonDecoder.decode([UserDownloadsResponse].self, from: data) let rawResponse = try jsonDecoder.decode([UserDownloadsResponse].self, from: data)
cloudDownloads = rawResponse.map { response in cloudDownloads = rawResponse.map { response in
DebridCloudDownload(downloadId: response.id, source: self.id.name, fileName: response.filename, link: response.download) DebridCloudDownload(downloadId: response.id, source: self.id, fileName: response.filename, link: response.download)
} }
return cloudDownloads return cloudDownloads

View file

@ -7,12 +7,6 @@
import Foundation import Foundation
public struct DebridInfo: Hashable, Sendable {
let name: String
let abbreviation: String
let website: String
}
public struct DebridIA: Hashable, Sendable { public struct DebridIA: Hashable, Sendable {
let magnet: Magnet let magnet: Magnet
let source: String let source: String

View file

@ -9,7 +9,10 @@ import Foundation
public protocol DebridSource: AnyObservableObject { public protocol DebridSource: AnyObservableObject {
// ID of the service // ID of the service
var id: DebridInfo { get } //var id: DebridInfo { get }
var id: String { get }
var abbreviation: String { get }
var website: String { get }
// Auth variables // Auth variables
var authProcessing: Bool { get set } var authProcessing: Bool { get set }

View file

@ -26,11 +26,13 @@ public class DebridManager: ObservableObject {
debridSources.contains { $0.isLoggedIn } debridSources.contains { $0.isLoggedIn }
} }
@Published var selectedDebridId: DebridInfo? @Published var selectedDebridSource: DebridSource?
/*
func debridSourceFromName(_ name: String? = nil) -> DebridSource? { func debridSourceFromName(_ name: String? = nil) -> DebridSource? {
debridSources.first { $0.id.name == name ?? selectedDebridId?.name } debridSources.first { $0.id.name == name ?? selectedDebridId?.name }
} }
*/
// Service agnostic variables // Service agnostic variables
@Published var enabledDebrids: Set<DebridType> = [] { @Published var enabledDebrids: Set<DebridType> = [] {
@ -113,8 +115,7 @@ public class DebridManager: ObservableObject {
// If a UserDefaults integer isn't set, it's usually 0 // If a UserDefaults integer isn't set, it's usually 0
let rawPreferredService = UserDefaults.standard.integer(forKey: "Debrid.PreferredService") let rawPreferredService = UserDefaults.standard.integer(forKey: "Debrid.PreferredService")
let legacyPreferredService = DebridType(rawValue: rawPreferredService) let legacyPreferredService = DebridType(rawValue: rawPreferredService)
let preferredDebridSource = self.debridSourceFromName(legacyPreferredService?.toString()) selectedDebridSource = self.debridSources.first { $0.id == legacyPreferredService?.toString() }
selectedDebridId = preferredDebridSource?.id
// If a user has one logged in service, automatically set the preferred service to that one // If a user has one logged in service, automatically set the preferred service to that one
/* /*
@ -265,10 +266,8 @@ public class DebridManager: ObservableObject {
return .none return .none
} }
let selectedSource = debridSourceFromName() if let selectedDebridSource,
let match = selectedDebridSource.IAValues.first(where: { magnetHash == $0.magnet.hash })
if let selectedSource,
let match = selectedSource.IAValues.first(where: { magnetHash == $0.magnet.hash })
{ {
return match.files.count > 1 ? .partial : .full return match.files.count > 1 ? .partial : .full
} else { } else {
@ -282,7 +281,7 @@ public class DebridManager: ObservableObject {
return false return false
} }
switch selectedDebridId?.name { switch selectedDebridSource?.id {
case .some("RealDebrid"): case .some("RealDebrid"):
if let realDebridItem = realDebrid.IAValues.first(where: { magnetHash == $0.magnet.hash }) { if let realDebridItem = realDebrid.IAValues.first(where: { magnetHash == $0.magnet.hash }) {
selectedRealDebridItem = realDebridItem selectedRealDebridItem = realDebridItem

View file

@ -16,7 +16,7 @@ struct DebridLabelView: View {
var body: some View { var body: some View {
Tag( Tag(
name: debridSource.id.abbreviation, name: debridSource.abbreviation,
color: tagColor, color: tagColor,
horizontalPadding: 5, horizontalPadding: 5,
verticalPadding: 3 verticalPadding: 3

View file

@ -15,11 +15,11 @@ struct SelectedDebridFilterView<Content: View>: View {
var body: some View { var body: some View {
Menu { Menu {
Button { Button {
debridManager.selectedDebridId = nil debridManager.selectedDebridSource = nil
} label: { } label: {
Text("None") Text("None")
if debridManager.selectedDebridId == nil { if debridManager.selectedDebridSource == nil {
Image(systemName: "checkmark") Image(systemName: "checkmark")
} }
} }
@ -27,11 +27,11 @@ struct SelectedDebridFilterView<Content: View>: View {
ForEach(debridManager.debridSources, id: \.id) { debridSource in ForEach(debridManager.debridSources, id: \.id) { debridSource in
if debridSource.isLoggedIn { if debridSource.isLoggedIn {
Button { Button {
debridManager.selectedDebridId = debridSource.id debridManager.selectedDebridSource = debridSource
} label: { } label: {
Text(debridSource.id.name) Text(debridSource.id)
if debridManager.selectedDebridId == debridSource.id { if debridManager.selectedDebridSource?.id == debridSource.id {
Image(systemName: "checkmark") Image(systemName: "checkmark")
} }
} }
@ -40,6 +40,6 @@ struct SelectedDebridFilterView<Content: View>: View {
} label: { } label: {
label label
} }
.id(debridManager.selectedDebridId) .id(debridManager.selectedDebridSource?.id)
} }
} }

View file

@ -53,7 +53,7 @@ struct SearchFilterHeaderView: View {
SelectedDebridFilterView { SelectedDebridFilterView {
FilterLabelView( FilterLabelView(
name: debridManager.selectedDebridId?.name, name: debridManager.selectedDebridSource?.id,
fallbackName: "Debrid" fallbackName: "Debrid"
) )
} }

View file

@ -30,7 +30,7 @@ struct SearchResultInfoView: View {
Text(size) Text(size)
} }
if let debridSource = debridManager.debridSourceFromName() { if let debridSource = debridManager.selectedDebridSource {
DebridLabelView(debridSource: debridSource, magnet: result.magnet) DebridLabelView(debridSource: debridSource, magnet: result.magnet)
} }
} }

View file

@ -18,9 +18,9 @@ struct SettingsDebridInfoView: View {
List { List {
Section(header: InlineHeader("Description")) { Section(header: InlineHeader("Description")) {
VStack(alignment: .leading, spacing: 10) { VStack(alignment: .leading, spacing: 10) {
Text("\(debridSource.id.name) is a debrid service that is used for unrestricting downloads and media playback. You must pay to access the service.") Text("\(debridSource.id) is a debrid service that is used for unrestricting downloads and media playback. You must pay to access the service.")
Link("Website", destination: URL(string: debridSource.id.website) ?? URL(string: "https://kingbri.dev/ferrite")!) Link("Website", destination: URL(string: debridSource.website) ?? URL(string: "https://kingbri.dev/ferrite")!)
} }
} }
@ -72,7 +72,7 @@ struct SettingsDebridInfoView: View {
} }
} }
.listStyle(.insetGrouped) .listStyle(.insetGrouped)
.navigationTitle(debridSource.id.name) .navigationTitle(debridSource.id)
.navigationBarTitleDisplayMode(.inline) .navigationBarTitleDisplayMode(.inline)
} }
} }

View file

@ -53,7 +53,7 @@ struct LibraryView: View {
EmptyInstructionView(title: "No History", message: "Start watching to build history") EmptyInstructionView(title: "No History", message: "Start watching to build history")
} }
case .debridCloud: case .debridCloud:
if debridManager.selectedDebridId == nil { if debridManager.selectedDebridSource == nil {
EmptyInstructionView(title: "Cloud Unavailable", message: "Listing is not available for this service") EmptyInstructionView(title: "Cloud Unavailable", message: "Listing is not available for this service")
} }
} }
@ -69,7 +69,7 @@ struct LibraryView: View {
switch navModel.libraryPickerSelection { switch navModel.libraryPickerSelection {
case .bookmarks, .debridCloud: case .bookmarks, .debridCloud:
SelectedDebridFilterView { SelectedDebridFilterView {
Text(debridManager.selectedDebridId?.abbreviation ?? "Debrid") Text(debridManager.selectedDebridSource?.abbreviation ?? "Debrid")
} }
.transaction { .transaction {
$0.animation = .none $0.animation = .none

View file

@ -51,7 +51,7 @@ struct SettingsView: View {
SettingsDebridInfoView(debridSource: debridSource) SettingsDebridInfoView(debridSource: debridSource)
} label: { } label: {
HStack { HStack {
Text(debridSource.id.name) Text(debridSource.id)
Spacer() Spacer()
Text(debridSource.isLoggedIn ? "Enabled" : "Disabled") Text(debridSource.isLoggedIn ? "Enabled" : "Disabled")
.foregroundColor(.secondary) .foregroundColor(.secondary)

View file

@ -23,7 +23,7 @@ struct BatchChoiceView: View {
var body: some View { var body: some View {
NavView { NavView {
List { List {
switch debridManager.selectedDebridId?.name { switch debridManager.selectedDebridSource?.id {
case .some("RealDebrid"): case .some("RealDebrid"):
ForEach(debridManager.selectedRealDebridItem?.files ?? [], id: \.self) { file in ForEach(debridManager.selectedRealDebridItem?.files ?? [], id: \.self) { file in
if file.name.lowercased().contains(searchText.lowercased()) || searchText.isEmpty { if file.name.lowercased().contains(searchText.lowercased()) || searchText.isEmpty {