Ferrite-backup/Ferrite/Views/SourceListView.swift
kingbri 064a68fbb2 Sources: Add RSS, descriptions, and settings
RSS parsing has been added as a method to parse source since they're
easier on the website's end to parse.

Source settings have been added. The only current setting is the fetch
mode which selects which parser/scraper to use. By default, if an RSS
parser is found, it's selected.

A source now has info shown regarding versioning and authorship. A source
list's repository name and author string are now required.

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

111 lines
4.3 KiB
Swift

//
// SourceListView.swift
// Ferrite
//
// Created by Brian Dashore on 7/24/22.
//
import SwiftUI
struct SourceListView: View {
@EnvironmentObject var sourceManager: SourceManager
@EnvironmentObject var navModel: NavigationViewModel
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()
}
)) {
VStack(alignment: .leading, spacing: 5) {
HStack {
Text(source.name)
Text("v\(source.version)")
.foregroundColor(.secondary)
}
Text("by \(source.author ?? "Unknown")")
.foregroundColor(.secondary)
}
}
.contextMenu {
Button {
navModel.selectedSource = source
navModel.showSourceSettings.toggle()
} label: {
Text("Settings")
Image(systemName: "gear")
}
Button {
PersistenceController.shared.delete(source, context: backgroundContext)
} label: {
Text("Remove")
Image(systemName: "trash")
}
}
}
.sheet(isPresented: $navModel.showSourceSettings) {
SourceSettingsView()
}
}
}
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 {
VStack(alignment: .leading, spacing: 5) {
HStack {
Text(availableSource.name)
Text("v\(availableSource.version)")
.foregroundColor(.secondary)
}
Text("by \(availableSource.author ?? "Unknown")")
.foregroundColor(.secondary)
}
Spacer()
Button("Install") {
sourceManager.installSource(sourceJson: availableSource)
}
}
}
}
}
}
}
.task {
await sourceManager.fetchSourcesFromUrl()
}
.navigationTitle("Sources")
}
}
}
struct SourceListView_Previews: PreviewProvider {
static var previews: some View {
SourceListView()
}
}