mirror of
https://github.com/Ferrite-iOS/Ferrite.git
synced 2026-04-21 00:42:07 +00:00
Debrid: Fix UI updates with auth
If a debrid is authorized, a Published variable needs to be notified since SwiftUI can't read computed properties on the fly (they are getters). Therefore, it's better to maintain a single source of truth of which services are logged in. Signed-off-by: kingbri <bdashore3@proton.me>
This commit is contained in:
parent
d512d8b88d
commit
646c22c9be
7 changed files with 19 additions and 17 deletions
|
|
@ -13,7 +13,7 @@ class AllDebrid: PollingDebridSource, ObservableObject {
|
||||||
let website = "https://alldebrid.com"
|
let website = "https://alldebrid.com"
|
||||||
var authTask: Task<Void, Error>?
|
var authTask: Task<Void, Error>?
|
||||||
|
|
||||||
var authProcessing: Bool = false
|
@Published var authProcessing: Bool = false
|
||||||
var isLoggedIn: Bool {
|
var isLoggedIn: Bool {
|
||||||
getToken() != nil
|
getToken() != nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,14 +23,7 @@ class DebridManager: ObservableObject {
|
||||||
// UI Variables
|
// UI Variables
|
||||||
@Published var showWebView: Bool = false
|
@Published var showWebView: Bool = false
|
||||||
@Published var showAuthSession: Bool = false
|
@Published var showAuthSession: Bool = false
|
||||||
|
@Published var enabledDebrids: [DebridSource] = []
|
||||||
var hasEnabledDebrids: Bool {
|
|
||||||
debridSources.contains { $0.isLoggedIn }
|
|
||||||
}
|
|
||||||
|
|
||||||
var enabledDebridCount: Int {
|
|
||||||
debridSources.filter(\.isLoggedIn).count
|
|
||||||
}
|
|
||||||
|
|
||||||
@Published var selectedDebridSource: DebridSource? {
|
@Published var selectedDebridSource: DebridSource? {
|
||||||
didSet {
|
didSet {
|
||||||
|
|
@ -57,6 +50,9 @@ class DebridManager: ObservableObject {
|
||||||
@Published var notImplementedMessage: String = ""
|
@Published var notImplementedMessage: String = ""
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
|
// Update the UI for debrid services that are enabled
|
||||||
|
enabledDebrids = debridSources.filter { $0.isLoggedIn }
|
||||||
|
|
||||||
// Set the preferred service. Contains migration logic for earlier versions
|
// Set the preferred service. Contains migration logic for earlier versions
|
||||||
if let rawPreferredService = UserDefaults.standard.string(forKey: "Debrid.PreferredService") {
|
if let rawPreferredService = UserDefaults.standard.string(forKey: "Debrid.PreferredService") {
|
||||||
let debridServiceId: String?
|
let debridServiceId: String?
|
||||||
|
|
@ -193,7 +189,7 @@ class DebridManager: ObservableObject {
|
||||||
debridSource.authProcessing = false
|
debridSource.authProcessing = false
|
||||||
}
|
}
|
||||||
|
|
||||||
if enabledDebridCount == 1 {
|
if enabledDebrids.count == 1 {
|
||||||
selectedDebridSource = debridSource
|
selectedDebridSource = debridSource
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -201,6 +197,8 @@ class DebridManager: ObservableObject {
|
||||||
// Set an API key if manually provided
|
// Set an API key if manually provided
|
||||||
if let apiKey {
|
if let apiKey {
|
||||||
debridSource.setApiKey(apiKey)
|
debridSource.setApiKey(apiKey)
|
||||||
|
enabledDebrids.append(debridSource)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -213,6 +211,7 @@ class DebridManager: ObservableObject {
|
||||||
|
|
||||||
if validateAuthUrl(authUrl) {
|
if validateAuthUrl(authUrl) {
|
||||||
try await pollingSource.authTask?.value
|
try await pollingSource.authTask?.value
|
||||||
|
enabledDebrids.append(debridSource)
|
||||||
} else {
|
} else {
|
||||||
throw DebridError.AuthQuery(description: "The authentication URL was invalid")
|
throw DebridError.AuthQuery(description: "The authentication URL was invalid")
|
||||||
}
|
}
|
||||||
|
|
@ -278,7 +277,7 @@ class DebridManager: ObservableObject {
|
||||||
// Currently handles Premiumize callback
|
// Currently handles Premiumize callback
|
||||||
func handleAuthCallback(url: URL?, error: Error?) async {
|
func handleAuthCallback(url: URL?, error: Error?) async {
|
||||||
defer {
|
defer {
|
||||||
if enabledDebridCount == 1 {
|
if enabledDebrids.count == 1 {
|
||||||
selectedDebridSource = selectedOAuthDebridSource
|
selectedDebridSource = selectedOAuthDebridSource
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -296,6 +295,7 @@ class DebridManager: ObservableObject {
|
||||||
|
|
||||||
if let callbackUrl = url {
|
if let callbackUrl = url {
|
||||||
try oauthDebridSource.handleAuthCallback(url: callbackUrl)
|
try oauthDebridSource.handleAuthCallback(url: callbackUrl)
|
||||||
|
enabledDebrids.append(oauthDebridSource)
|
||||||
} else {
|
} else {
|
||||||
throw DebridError.AuthQuery(description: "The callback URL was invalid")
|
throw DebridError.AuthQuery(description: "The callback URL was invalid")
|
||||||
}
|
}
|
||||||
|
|
@ -312,6 +312,8 @@ class DebridManager: ObservableObject {
|
||||||
if selectedDebridSource?.id == debridSource.id {
|
if selectedDebridSource?.id == debridSource.id {
|
||||||
selectedDebridSource = nil
|
selectedDebridSource = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enabledDebrids.removeAll { $0.id == debridSource.id }
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Debrid fetch UI linked functions
|
// MARK: - Debrid fetch UI linked functions
|
||||||
|
|
|
||||||
|
|
@ -102,7 +102,7 @@ class ScrapingViewModel: ObservableObject {
|
||||||
|
|
||||||
cleanedSearchText = searchText.lowercased()
|
cleanedSearchText = searchText.lowercased()
|
||||||
|
|
||||||
if await !debridManager.hasEnabledDebrids {
|
if await !debridManager.enabledDebrids.isEmpty {
|
||||||
await debridManager.clearIAValues()
|
await debridManager.clearIAValues()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -136,7 +136,7 @@ class ScrapingViewModel: ObservableObject {
|
||||||
var failedSourceNames: [String] = []
|
var failedSourceNames: [String] = []
|
||||||
for await (requestResult, sourceName) in group {
|
for await (requestResult, sourceName) in group {
|
||||||
if let requestResult {
|
if let requestResult {
|
||||||
if await debridManager.hasEnabledDebrids {
|
if await !debridManager.enabledDebrids.isEmpty {
|
||||||
await debridManager.populateDebridIA(requestResult.magnets)
|
await debridManager.populateDebridIA(requestResult.magnets)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -56,7 +56,7 @@ struct BookmarksView: View {
|
||||||
.frame(height: 15)
|
.frame(height: 15)
|
||||||
}
|
}
|
||||||
.task {
|
.task {
|
||||||
if debridManager.hasEnabledDebrids {
|
if !debridManager.enabledDebrids.isEmpty {
|
||||||
let magnets = bookmarks.compactMap {
|
let magnets = bookmarks.compactMap {
|
||||||
if let magnetHash = $0.magnetHash {
|
if let magnetHash = $0.magnetHash {
|
||||||
return Magnet(hash: magnetHash, link: $0.magnetLink)
|
return Magnet(hash: magnetHash, link: $0.magnetLink)
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ struct LibraryPickerView: View {
|
||||||
Text("Bookmarks").tag(NavigationViewModel.LibraryPickerSegment.bookmarks)
|
Text("Bookmarks").tag(NavigationViewModel.LibraryPickerSegment.bookmarks)
|
||||||
Text("History").tag(NavigationViewModel.LibraryPickerSegment.history)
|
Text("History").tag(NavigationViewModel.LibraryPickerSegment.history)
|
||||||
|
|
||||||
if debridManager.hasEnabledDebrids {
|
if !debridManager.enabledDebrids.isEmpty {
|
||||||
Text("Cloud").tag(NavigationViewModel.LibraryPickerSegment.debridCloud)
|
Text("Cloud").tag(NavigationViewModel.LibraryPickerSegment.debridCloud)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -60,7 +60,7 @@ struct SearchFilterHeaderView: View {
|
||||||
|
|
||||||
// MARK: - Cache status picker
|
// MARK: - Cache status picker
|
||||||
|
|
||||||
if debridManager.hasEnabledDebrids {
|
if !debridManager.enabledDebrids.isEmpty {
|
||||||
IAFilterView()
|
IAFilterView()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -128,7 +128,7 @@ struct SettingsView: View {
|
||||||
}
|
}
|
||||||
|
|
||||||
Section(header: InlineHeader("Default actions")) {
|
Section(header: InlineHeader("Default actions")) {
|
||||||
if debridManager.hasEnabledDebrids {
|
if !debridManager.enabledDebrids.isEmpty {
|
||||||
NavigationLink {
|
NavigationLink {
|
||||||
DefaultActionPickerView(
|
DefaultActionPickerView(
|
||||||
actionRequirement: .debrid,
|
actionRequirement: .debrid,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue