Multiple servers can be added to Ferrite to playback from any Kodi server that the user wants. This also adds the ability to have friendly names which makes it easier to select what server to play on. Each server shows the user whether it's online or not through Kodi's JSONRPC ping method. Signed-off-by: kingbri <bdashore3@proton.me>
91 lines
3.3 KiB
Swift
91 lines
3.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
|
|
|
|
let backgroundContext = PersistenceController.shared.backgroundContext
|
|
|
|
@Binding var searchText: String
|
|
@Binding var bookmarksEmpty: Bool
|
|
|
|
@State private var viewTask: Task<Void, Never>?
|
|
@State private var bookmarkPredicate: NSPredicate?
|
|
|
|
var body: some View {
|
|
DynamicFetchRequest(
|
|
predicate: bookmarkPredicate,
|
|
sortDescriptors: [NSSortDescriptor(keyPath: \Bookmark.orderNum, ascending: true)]
|
|
) { (bookmarks: FetchedResults<Bookmark>) in
|
|
List {
|
|
if !bookmarks.isEmpty {
|
|
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: bookmark)
|
|
}
|
|
}
|
|
}
|
|
.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)
|
|
.inlinedList(inset: Application.shared.osVersion.majorVersion > 14 ? 15 : -25)
|
|
.backport.onAppear {
|
|
bookmarksEmpty = bookmarks.isEmpty
|
|
|
|
if debridManager.enabledDebrids.count > 0 {
|
|
viewTask = Task {
|
|
let magnets = bookmarks.compactMap {
|
|
if let magnetHash = $0.magnetHash {
|
|
return Magnet(hash: magnetHash, link: $0.magnetLink)
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
await debridManager.populateDebridIA(magnets)
|
|
}
|
|
}
|
|
}
|
|
.onDisappear {
|
|
viewTask?.cancel()
|
|
}
|
|
.onChange(of: bookmarks.count) { newCount in
|
|
bookmarksEmpty = newCount == 0
|
|
}
|
|
}
|
|
.backport.onAppear {
|
|
applyPredicate()
|
|
}
|
|
.onChange(of: searchText) { _ in
|
|
applyPredicate()
|
|
}
|
|
}
|
|
|
|
func applyPredicate() {
|
|
bookmarkPredicate = searchText.isEmpty ? nil : NSPredicate(format: "title CONTAINS[cd] %@", searchText)
|
|
}
|
|
}
|