From 3907f23fec8b55c8ffc6ba6432f79cc26c860518 Mon Sep 17 00:00:00 2001 From: kingbri Date: Fri, 5 Aug 2022 12:15:56 -0400 Subject: [PATCH] Sources: Add list IDs and source IDs List IDs are used to link a source list with an actual source. Each list entry has a unique ID that a user can compare with a source to see if it's legitimate. Source IDs are just identifiers for sources. Not sure what to do with these yet but they may be useful for the future. Signed-off-by: kingbri --- .../Classes/Source+CoreDataProperties.swift | 4 +- .../SourceList+CoreDataProperties.swift | 1 + .../FerriteDB.xcdatamodel/contents | 5 ++- Ferrite/Models/SourceModels.swift | 1 + Ferrite/ViewModels/SourceManager.swift | 7 +++- .../SettingsViews/SettingsSourceUrlView.swift | 7 +++- Ferrite/Views/SourceListView.swift | 2 +- Ferrite/Views/SourceSettingsView.swift | 39 ++++++++++++++++--- 8 files changed, 55 insertions(+), 11 deletions(-) diff --git a/Ferrite/DataManagement/Classes/Source+CoreDataProperties.swift b/Ferrite/DataManagement/Classes/Source+CoreDataProperties.swift index cbe5a3e..42ef1f8 100644 --- a/Ferrite/DataManagement/Classes/Source+CoreDataProperties.swift +++ b/Ferrite/DataManagement/Classes/Source+CoreDataProperties.swift @@ -16,10 +16,12 @@ extension Source { return NSFetchRequest(entityName: "Source") } + @NSManaged public var id: UUID @NSManaged public var baseUrl: String @NSManaged public var enabled: Bool @NSManaged public var name: String - @NSManaged public var author: String? + @NSManaged public var author: String + @NSManaged public var listId: UUID? @NSManaged public var preferredParser: Int16 @NSManaged public var version: Int16 @NSManaged public var htmlParser: SourceHtmlParser? diff --git a/Ferrite/DataManagement/Classes/SourceList+CoreDataProperties.swift b/Ferrite/DataManagement/Classes/SourceList+CoreDataProperties.swift index fb205bb..db7bb72 100644 --- a/Ferrite/DataManagement/Classes/SourceList+CoreDataProperties.swift +++ b/Ferrite/DataManagement/Classes/SourceList+CoreDataProperties.swift @@ -14,6 +14,7 @@ public extension SourceList { NSFetchRequest(entityName: "SourceList") } + @NSManaged var id: UUID @NSManaged var author: String @NSManaged var name: String @NSManaged var urlString: String diff --git a/Ferrite/DataManagement/FerriteDB.xcdatamodeld/FerriteDB.xcdatamodel/contents b/Ferrite/DataManagement/FerriteDB.xcdatamodeld/FerriteDB.xcdatamodel/contents index 7bf55b5..db4687e 100644 --- a/Ferrite/DataManagement/FerriteDB.xcdatamodeld/FerriteDB.xcdatamodel/contents +++ b/Ferrite/DataManagement/FerriteDB.xcdatamodeld/FerriteDB.xcdatamodel/contents @@ -1,9 +1,11 @@ - + + + @@ -29,6 +31,7 @@ + diff --git a/Ferrite/Models/SourceModels.swift b/Ferrite/Models/SourceModels.swift index a8dad03..ce54637 100644 --- a/Ferrite/Models/SourceModels.swift +++ b/Ferrite/Models/SourceModels.swift @@ -18,6 +18,7 @@ public struct SourceJson: Codable, Hashable { let version: Int16 let baseUrl: String var author: String? + var listId: UUID? let rssParser: SourceRssParserJson? let htmlParser: SourceHtmlParserJson? } diff --git a/Ferrite/ViewModels/SourceManager.swift b/Ferrite/ViewModels/SourceManager.swift index 24aab10..724132b 100644 --- a/Ferrite/ViewModels/SourceManager.swift +++ b/Ferrite/ViewModels/SourceManager.swift @@ -33,6 +33,7 @@ public class SourceManager: ObservableObject { for index in sourceResponse.sources.indices { sourceResponse.sources[index].author = sourceList.author + sourceResponse.sources[index].listId = sourceList.id } tempSourceUrls += sourceResponse.sources @@ -62,10 +63,12 @@ public class SourceManager: ObservableObject { } let newSource = Source(context: backgroundContext) + newSource.id = UUID() newSource.name = sourceJson.name newSource.version = sourceJson.version newSource.baseUrl = sourceJson.baseUrl - newSource.author = sourceJson.author + newSource.author = sourceJson.author ?? "Unknown" + newSource.listId = sourceJson.listId // Adds an RSS parser if present if let rssParserJson = sourceJson.rssParser { @@ -240,6 +243,7 @@ public class SourceManager: ObservableObject { } let newSourceUrl = SourceList(context: backgroundContext) + newSourceUrl.id = UUID() newSourceUrl.urlString = sourceUrl newSourceUrl.name = rawResponse.name newSourceUrl.author = rawResponse.author @@ -248,6 +252,7 @@ public class SourceManager: ObservableObject { return true } catch { + print(error) urlErrorAlertText = error.localizedDescription showUrlErrorAlert.toggle() diff --git a/Ferrite/Views/SettingsViews/SettingsSourceUrlView.swift b/Ferrite/Views/SettingsViews/SettingsSourceUrlView.swift index 11722d4..0f228e7 100644 --- a/Ferrite/Views/SettingsViews/SettingsSourceUrlView.swift +++ b/Ferrite/Views/SettingsViews/SettingsSourceUrlView.swift @@ -20,7 +20,12 @@ struct SettingsSourceListView: View { var body: some View { List { ForEach(sourceUrls, id: \.self) { sourceUrl in - Text(sourceUrl.name) + VStack(alignment: .leading, spacing: 5) { + Text(sourceUrl.name) + Text("ID: \(sourceUrl.id)") + .font(.caption) + .foregroundColor(.gray) + } } .onDelete { offsets in for index in offsets { diff --git a/Ferrite/Views/SourceListView.swift b/Ferrite/Views/SourceListView.swift index 5cdf60a..c1bcef7 100644 --- a/Ferrite/Views/SourceListView.swift +++ b/Ferrite/Views/SourceListView.swift @@ -40,7 +40,7 @@ struct SourceListView: View { .foregroundColor(.secondary) } - Text("by \(source.author ?? "Unknown")") + Text("by \(source.author)") .foregroundColor(.secondary) } } diff --git a/Ferrite/Views/SourceSettingsView.swift b/Ferrite/Views/SourceSettingsView.swift index fcbf77b..b54576e 100644 --- a/Ferrite/Views/SourceSettingsView.swift +++ b/Ferrite/Views/SourceSettingsView.swift @@ -10,10 +10,37 @@ import SwiftUI struct SourceSettingsView: View { @Environment(\.dismiss) var dismiss + @EnvironmentObject var navModel: NavigationViewModel + var body: some View { NavView { Form { - SourceSettingsMethodView() + if let selectedSource = navModel.selectedSource { + Section("Info") { + VStack(alignment: .leading, spacing: 5) { + HStack { + Text(selectedSource.name) + + Text("v\(selectedSource.version)") + } + + Text("by \(selectedSource.author)") + .foregroundColor(.secondary) + + if let listId = selectedSource.listId { + Text("List ID: \(listId)") + .font(.caption) + .foregroundColor(.secondary) + } else { + Text("No list ID found. This source should be removed.") + .font(.caption) + .foregroundColor(.secondary) + } + } + } + + SourceSettingsMethodView(selectedSource: selectedSource) + } } .navigationTitle("Source settings") .toolbar { @@ -28,28 +55,28 @@ struct SourceSettingsView: View { } struct SourceSettingsMethodView: View { - @EnvironmentObject var navModel: NavigationViewModel + @ObservedObject var selectedSource: Source @State private var selectedTempParser: SourcePreferredParser = .none var body: some View { Picker("Fetch method", selection: $selectedTempParser) { - if navModel.selectedSource?.htmlParser != nil { + if selectedSource.htmlParser != nil { Text("Web scraping") .tag(SourcePreferredParser.scraping) } - if navModel.selectedSource?.rssParser != nil { + if selectedSource.rssParser != nil { Text("RSS") .tag(SourcePreferredParser.rss) } } .pickerStyle(.inline) .onAppear { - selectedTempParser = SourcePreferredParser(rawValue: navModel.selectedSource?.preferredParser ?? 0) ?? .none + selectedTempParser = SourcePreferredParser(rawValue: selectedSource.preferredParser) ?? .none } .onChange(of: selectedTempParser) { newMethod in - navModel.selectedSource?.preferredParser = newMethod.rawValue + selectedSource.preferredParser = selectedTempParser.rawValue PersistenceController.shared.save() } }