Ferrite-backup/Ferrite/Views/LibraryViews/BookmarksView.swift
kingbri 2f870b9410 Ferrite: Add bookmarks
Bookmarks are added through search results and can be accessed through
the library. These can be moved and deleted within the list.

Add a RealDebrid instant availability cache for bookmark IA status
to not overwhelm the API. Instant availability results are fresh on
every search results since the cache is cleared.

Also don't require a source API object to be present for the API parser
button in source settings. If a JSON parser exists for a source, allow
the option to be presented.

Signed-off-by: kingbri <bdashore3@proton.me>
2022-09-16 12:33:18 -04:00

67 lines
2.3 KiB
Swift

//
// BookmarksView.swift
// Ferrite
//
// Created by Brian Dashore on 9/2/22.
//
import SwiftUI
struct BookmarksView: View {
@Environment(\.verticalSizeClass) var verticalSizeClass
@EnvironmentObject var navModel: NavigationViewModel
@EnvironmentObject var debridManager: DebridManager
@AppStorage("RealDebrid.Enabled") var realDebridEnabled = false
let backgroundContext = PersistenceController.shared.backgroundContext
var bookmarks: FetchedResults<Bookmark>
@State private var viewTask: Task<Void, Never>?
var body: some View {
ZStack {
if !bookmarks.isEmpty {
List {
ForEach(bookmarks, id: \.self) { bookmark in
SearchResultButtonView(result: bookmark.toSearchResult(), existingBookmark: bookmark)
}
.onDelete { offsets in
for index in offsets {
if let bookmark = bookmarks[safe: index] {
PersistenceController.shared.delete(bookmark, context: backgroundContext)
NotificationCenter.default.post(name: .didDeleteBookmark, object: nil)
}
}
}
.onMove { (source, destination) in
var changedBookmarks = bookmarks.map { $0 }
changedBookmarks.move(fromOffsets: source, toOffset: destination)
for reverseIndex in stride(from: changedBookmarks.count - 1, through: 0, by: -1) {
changedBookmarks[reverseIndex].orderNum = Int16(reverseIndex)
}
PersistenceController.shared.save()
}
}
.listStyle(.insetGrouped)
.onAppear {
if realDebridEnabled {
viewTask = Task {
let hashes = bookmarks.compactMap { $0.magnetHash }
await debridManager.populateDebridHashes(hashes)
}
}
}
.onDisappear {
viewTask?.cancel()
}
}
}
}
}