mirror of
https://github.com/Ferrite-iOS/Ferrite.git
synced 2026-01-11 20:10:27 +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"
|
||||
var authTask: Task<Void, Error>?
|
||||
|
||||
var authProcessing: Bool = false
|
||||
@Published var authProcessing: Bool = false
|
||||
var isLoggedIn: Bool {
|
||||
getToken() != nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,14 +23,7 @@ class DebridManager: ObservableObject {
|
|||
// UI Variables
|
||||
@Published var showWebView: Bool = false
|
||||
@Published var showAuthSession: Bool = false
|
||||
|
||||
var hasEnabledDebrids: Bool {
|
||||
debridSources.contains { $0.isLoggedIn }
|
||||
}
|
||||
|
||||
var enabledDebridCount: Int {
|
||||
debridSources.filter(\.isLoggedIn).count
|
||||
}
|
||||
@Published var enabledDebrids: [DebridSource] = []
|
||||
|
||||
@Published var selectedDebridSource: DebridSource? {
|
||||
didSet {
|
||||
|
|
@ -57,6 +50,9 @@ class DebridManager: ObservableObject {
|
|||
@Published var notImplementedMessage: String = ""
|
||||
|
||||
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
|
||||
if let rawPreferredService = UserDefaults.standard.string(forKey: "Debrid.PreferredService") {
|
||||
let debridServiceId: String?
|
||||
|
|
@ -193,7 +189,7 @@ class DebridManager: ObservableObject {
|
|||
debridSource.authProcessing = false
|
||||
}
|
||||
|
||||
if enabledDebridCount == 1 {
|
||||
if enabledDebrids.count == 1 {
|
||||
selectedDebridSource = debridSource
|
||||
}
|
||||
}
|
||||
|
|
@ -201,6 +197,8 @@ class DebridManager: ObservableObject {
|
|||
// Set an API key if manually provided
|
||||
if let apiKey {
|
||||
debridSource.setApiKey(apiKey)
|
||||
enabledDebrids.append(debridSource)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -213,6 +211,7 @@ class DebridManager: ObservableObject {
|
|||
|
||||
if validateAuthUrl(authUrl) {
|
||||
try await pollingSource.authTask?.value
|
||||
enabledDebrids.append(debridSource)
|
||||
} else {
|
||||
throw DebridError.AuthQuery(description: "The authentication URL was invalid")
|
||||
}
|
||||
|
|
@ -278,7 +277,7 @@ class DebridManager: ObservableObject {
|
|||
// Currently handles Premiumize callback
|
||||
func handleAuthCallback(url: URL?, error: Error?) async {
|
||||
defer {
|
||||
if enabledDebridCount == 1 {
|
||||
if enabledDebrids.count == 1 {
|
||||
selectedDebridSource = selectedOAuthDebridSource
|
||||
}
|
||||
|
||||
|
|
@ -296,6 +295,7 @@ class DebridManager: ObservableObject {
|
|||
|
||||
if let callbackUrl = url {
|
||||
try oauthDebridSource.handleAuthCallback(url: callbackUrl)
|
||||
enabledDebrids.append(oauthDebridSource)
|
||||
} else {
|
||||
throw DebridError.AuthQuery(description: "The callback URL was invalid")
|
||||
}
|
||||
|
|
@ -312,6 +312,8 @@ class DebridManager: ObservableObject {
|
|||
if selectedDebridSource?.id == debridSource.id {
|
||||
selectedDebridSource = nil
|
||||
}
|
||||
|
||||
enabledDebrids.removeAll { $0.id == debridSource.id }
|
||||
}
|
||||
|
||||
// MARK: - Debrid fetch UI linked functions
|
||||
|
|
|
|||
|
|
@ -102,7 +102,7 @@ class ScrapingViewModel: ObservableObject {
|
|||
|
||||
cleanedSearchText = searchText.lowercased()
|
||||
|
||||
if await !debridManager.hasEnabledDebrids {
|
||||
if await !debridManager.enabledDebrids.isEmpty {
|
||||
await debridManager.clearIAValues()
|
||||
}
|
||||
|
||||
|
|
@ -136,7 +136,7 @@ class ScrapingViewModel: ObservableObject {
|
|||
var failedSourceNames: [String] = []
|
||||
for await (requestResult, sourceName) in group {
|
||||
if let requestResult {
|
||||
if await debridManager.hasEnabledDebrids {
|
||||
if await !debridManager.enabledDebrids.isEmpty {
|
||||
await debridManager.populateDebridIA(requestResult.magnets)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -56,7 +56,7 @@ struct BookmarksView: View {
|
|||
.frame(height: 15)
|
||||
}
|
||||
.task {
|
||||
if debridManager.hasEnabledDebrids {
|
||||
if !debridManager.enabledDebrids.isEmpty {
|
||||
let magnets = bookmarks.compactMap {
|
||||
if let magnetHash = $0.magnetHash {
|
||||
return Magnet(hash: magnetHash, link: $0.magnetLink)
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ struct LibraryPickerView: View {
|
|||
Text("Bookmarks").tag(NavigationViewModel.LibraryPickerSegment.bookmarks)
|
||||
Text("History").tag(NavigationViewModel.LibraryPickerSegment.history)
|
||||
|
||||
if debridManager.hasEnabledDebrids {
|
||||
if !debridManager.enabledDebrids.isEmpty {
|
||||
Text("Cloud").tag(NavigationViewModel.LibraryPickerSegment.debridCloud)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -60,7 +60,7 @@ struct SearchFilterHeaderView: View {
|
|||
|
||||
// MARK: - Cache status picker
|
||||
|
||||
if debridManager.hasEnabledDebrids {
|
||||
if !debridManager.enabledDebrids.isEmpty {
|
||||
IAFilterView()
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -128,7 +128,7 @@ struct SettingsView: View {
|
|||
}
|
||||
|
||||
Section(header: InlineHeader("Default actions")) {
|
||||
if debridManager.hasEnabledDebrids {
|
||||
if !debridManager.enabledDebrids.isEmpty {
|
||||
NavigationLink {
|
||||
DefaultActionPickerView(
|
||||
actionRequirement: .debrid,
|
||||
|
|
|
|||
Loading…
Reference in a new issue