Ferrite-backup/Ferrite/Views/SourceListView.swift
kingbri 940f8337a5 Ferrite: Overhaul sources
Sources are now completely changed to use a more flexible API. This
uses a fully native source system, so there will be 0 overhead on
resource usage and performance.

JSON objects specify what is fetched and displayed by Ferrite when
searching torrents.

Sources now include sizes, seeders, and leechers for any site that
specifies them.

The versioning and repo naming framework has been added, but will be
displayed in another update.

API support will be included in another update.

Signed-off-by: kingbri <bdashore3@gmail.com>
2022-08-31 00:41:38 -04:00

80 lines
2.6 KiB
Swift

//
// SourceListView.swift
// Ferrite
//
// Created by Brian Dashore on 7/24/22.
//
import SwiftUI
struct SourceListView: View {
@EnvironmentObject var sourceManager: SourceManager
let backgroundContext = PersistenceController.shared.backgroundContext
@FetchRequest(
entity: Source.entity(),
sortDescriptors: []
) var sources: FetchedResults<Source>
@State private var availableSourceLength = 0
var body: some View {
NavView {
List {
if !sources.isEmpty {
Section("Installed") {
ForEach(sources, id: \.self) { source in
Toggle(isOn: Binding<Bool>(
get: { source.enabled },
set: {
source.enabled = $0
PersistenceController.shared.save()
}
)) {
Text(source.name)
}
}
.onDelete { offsets in
for index in offsets {
if let source = sources[safe: index] {
PersistenceController.shared.delete(source, context: backgroundContext)
}
}
}
}
}
if sourceManager.availableSources.contains(where: { avail in
!sources.contains(where: { avail.name == $0.name })
}) {
Section("Catalog") {
ForEach(sourceManager.availableSources, id: \.self) { availableSource in
if !sources.contains(where: { availableSource.name == $0.name }) {
HStack {
Text(availableSource.name)
Spacer()
Button("Install") {
sourceManager.installSource(sourceJson: availableSource)
}
}
}
}
}
}
}
.task {
await sourceManager.fetchSourcesFromUrl()
}
.navigationTitle("Sources")
}
}
}
struct SourceListView_Previews: PreviewProvider {
static var previews: some View {
SourceListView()
}
}