Fix source searching #8
4 changed files with 133 additions and 83 deletions
|
|
@ -15,6 +15,7 @@
|
||||||
0C32FB532890D19D002BD219 /* AboutView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C32FB522890D19D002BD219 /* AboutView.swift */; };
|
0C32FB532890D19D002BD219 /* AboutView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C32FB522890D19D002BD219 /* AboutView.swift */; };
|
||||||
0C32FB552890D1BF002BD219 /* UIApplication.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C32FB542890D1BF002BD219 /* UIApplication.swift */; };
|
0C32FB552890D1BF002BD219 /* UIApplication.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C32FB542890D1BF002BD219 /* UIApplication.swift */; };
|
||||||
0C32FB572890D1F2002BD219 /* ListRowViews.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C32FB562890D1F2002BD219 /* ListRowViews.swift */; };
|
0C32FB572890D1F2002BD219 /* ListRowViews.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C32FB562890D1F2002BD219 /* ListRowViews.swift */; };
|
||||||
|
0C360C5C28C7DF1400884ED3 /* DynamicFetchRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C360C5B28C7DF1400884ED3 /* DynamicFetchRequest.swift */; };
|
||||||
0C4CFC462897030D00AD9FAD /* Regex in Frameworks */ = {isa = PBXBuildFile; productRef = 0C4CFC452897030D00AD9FAD /* Regex */; };
|
0C4CFC462897030D00AD9FAD /* Regex in Frameworks */ = {isa = PBXBuildFile; productRef = 0C4CFC452897030D00AD9FAD /* Regex */; };
|
||||||
0C4CFC4D28970C8B00AD9FAD /* SourceComplexQuery+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C4CFC4728970C8B00AD9FAD /* SourceComplexQuery+CoreDataClass.swift */; };
|
0C4CFC4D28970C8B00AD9FAD /* SourceComplexQuery+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C4CFC4728970C8B00AD9FAD /* SourceComplexQuery+CoreDataClass.swift */; };
|
||||||
0C4CFC4E28970C8B00AD9FAD /* SourceComplexQuery+CoreDataProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C4CFC4828970C8B00AD9FAD /* SourceComplexQuery+CoreDataProperties.swift */; };
|
0C4CFC4E28970C8B00AD9FAD /* SourceComplexQuery+CoreDataProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C4CFC4828970C8B00AD9FAD /* SourceComplexQuery+CoreDataProperties.swift */; };
|
||||||
|
|
@ -91,6 +92,7 @@
|
||||||
0C32FB522890D19D002BD219 /* AboutView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AboutView.swift; sourceTree = "<group>"; };
|
0C32FB522890D19D002BD219 /* AboutView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AboutView.swift; sourceTree = "<group>"; };
|
||||||
0C32FB542890D1BF002BD219 /* UIApplication.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIApplication.swift; sourceTree = "<group>"; };
|
0C32FB542890D1BF002BD219 /* UIApplication.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIApplication.swift; sourceTree = "<group>"; };
|
||||||
0C32FB562890D1F2002BD219 /* ListRowViews.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ListRowViews.swift; sourceTree = "<group>"; };
|
0C32FB562890D1F2002BD219 /* ListRowViews.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ListRowViews.swift; sourceTree = "<group>"; };
|
||||||
|
0C360C5B28C7DF1400884ED3 /* DynamicFetchRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DynamicFetchRequest.swift; sourceTree = "<group>"; };
|
||||||
0C4CFC4728970C8B00AD9FAD /* SourceComplexQuery+CoreDataClass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SourceComplexQuery+CoreDataClass.swift"; sourceTree = "<group>"; };
|
0C4CFC4728970C8B00AD9FAD /* SourceComplexQuery+CoreDataClass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SourceComplexQuery+CoreDataClass.swift"; sourceTree = "<group>"; };
|
||||||
0C4CFC4828970C8B00AD9FAD /* SourceComplexQuery+CoreDataProperties.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SourceComplexQuery+CoreDataProperties.swift"; sourceTree = "<group>"; };
|
0C4CFC4828970C8B00AD9FAD /* SourceComplexQuery+CoreDataProperties.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SourceComplexQuery+CoreDataProperties.swift"; sourceTree = "<group>"; };
|
||||||
0C57D4CB289032ED008534E8 /* SearchResultRDView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchResultRDView.swift; sourceTree = "<group>"; };
|
0C57D4CB289032ED008534E8 /* SearchResultRDView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchResultRDView.swift; sourceTree = "<group>"; };
|
||||||
|
|
@ -255,6 +257,7 @@
|
||||||
0CB6516428C5A5D700DCA721 /* InlinedList.swift */,
|
0CB6516428C5A5D700DCA721 /* InlinedList.swift */,
|
||||||
0CB6516928C5B4A600DCA721 /* InlineHeader.swift */,
|
0CB6516928C5B4A600DCA721 /* InlineHeader.swift */,
|
||||||
0CDCB91728C662640098B513 /* EmptyInstructionView.swift */,
|
0CDCB91728C662640098B513 /* EmptyInstructionView.swift */,
|
||||||
|
0C360C5B28C7DF1400884ED3 /* DynamicFetchRequest.swift */,
|
||||||
);
|
);
|
||||||
path = CommonViews;
|
path = CommonViews;
|
||||||
sourceTree = "<group>";
|
sourceTree = "<group>";
|
||||||
|
|
@ -458,6 +461,7 @@
|
||||||
0CBC7705288DE7F40054BE44 /* PersistenceController.swift in Sources */,
|
0CBC7705288DE7F40054BE44 /* PersistenceController.swift in Sources */,
|
||||||
0C31133D28B1ABFA004DCB0D /* SourceJsonParser+CoreDataProperties.swift in Sources */,
|
0C31133D28B1ABFA004DCB0D /* SourceJsonParser+CoreDataProperties.swift in Sources */,
|
||||||
0CA0545B288EEA4E00850554 /* SourceListEditorView.swift in Sources */,
|
0CA0545B288EEA4E00850554 /* SourceListEditorView.swift in Sources */,
|
||||||
|
0C360C5C28C7DF1400884ED3 /* DynamicFetchRequest.swift in Sources */,
|
||||||
0C84F4872895BFED0074B7C9 /* SourceList+CoreDataProperties.swift in Sources */,
|
0C84F4872895BFED0074B7C9 /* SourceList+CoreDataProperties.swift in Sources */,
|
||||||
0C794B6B289DACF100DD1CC8 /* SourceCatalogView.swift in Sources */,
|
0C794B6B289DACF100DD1CC8 /* SourceCatalogView.swift in Sources */,
|
||||||
0CA148E9288903F000DE2211 /* MainView.swift in Sources */,
|
0CA148E9288903F000DE2211 /* MainView.swift in Sources */,
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@
|
||||||
|
|
||||||
import CoreData
|
import CoreData
|
||||||
import Foundation
|
import Foundation
|
||||||
import UIKit
|
import SwiftUI
|
||||||
|
|
||||||
public class SourceManager: ObservableObject {
|
public class SourceManager: ObservableObject {
|
||||||
var toastModel: ToastViewModel?
|
var toastModel: ToastViewModel?
|
||||||
|
|
@ -52,6 +52,22 @@ public class SourceManager: ObservableObject {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func fetchUpdatedSources(installedSources: FetchedResults<Source>) -> [SourceJson] {
|
||||||
|
var updatedSources: [SourceJson] = []
|
||||||
|
|
||||||
|
for source in installedSources {
|
||||||
|
if let availableSource = availableSources.first(where: {
|
||||||
|
source.listId == $0.listId && source.name == $0.name && source.author == $0.author
|
||||||
|
}),
|
||||||
|
availableSource.version > source.version
|
||||||
|
{
|
||||||
|
updatedSources.append(availableSource)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return updatedSources
|
||||||
|
}
|
||||||
|
|
||||||
// Checks if the current app version is supported by the source
|
// Checks if the current app version is supported by the source
|
||||||
func checkAppVersion(minVersion: String?) -> Bool {
|
func checkAppVersion(minVersion: String?) -> Bool {
|
||||||
// If there's no min version, assume that every version is supported
|
// If there's no min version, assume that every version is supported
|
||||||
|
|
|
||||||
26
Ferrite/Views/CommonViews/DynamicFetchRequest.swift
Normal file
26
Ferrite/Views/CommonViews/DynamicFetchRequest.swift
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
//
|
||||||
|
// DynamicFetchRequest.swift
|
||||||
|
// Ferrite
|
||||||
|
//
|
||||||
|
// Created by Brian Dashore on 9/6/22.
|
||||||
|
//
|
||||||
|
|
||||||
|
import CoreData
|
||||||
|
import SwiftUI
|
||||||
|
|
||||||
|
struct DynamicFetchRequest<T: NSManagedObject, Content: View>: View {
|
||||||
|
@FetchRequest var fetchRequest: FetchedResults<T>
|
||||||
|
|
||||||
|
let content: (FetchedResults<T>) -> Content
|
||||||
|
|
||||||
|
var body: some View {
|
||||||
|
content(fetchRequest)
|
||||||
|
}
|
||||||
|
|
||||||
|
init(predicate: NSPredicate?,
|
||||||
|
@ViewBuilder content: @escaping (FetchedResults<T>) -> Content)
|
||||||
|
{
|
||||||
|
_fetchRequest = FetchRequest<T>(sortDescriptors: [], predicate: predicate)
|
||||||
|
self.content = content
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -22,24 +22,6 @@ struct SourcesView: View {
|
||||||
sortDescriptors: []
|
sortDescriptors: []
|
||||||
) var sources: FetchedResults<Source>
|
) var sources: FetchedResults<Source>
|
||||||
|
|
||||||
private var updatedSources: [SourceJson] {
|
|
||||||
var tempSources: [SourceJson] = []
|
|
||||||
|
|
||||||
for source in sources {
|
|
||||||
guard let availableSource = sourceManager.availableSources.first(where: {
|
|
||||||
source.listId == $0.listId && source.name == $0.name && source.author == $0.author
|
|
||||||
}) else {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if availableSource.version > source.version {
|
|
||||||
tempSources.append(availableSource)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return tempSources
|
|
||||||
}
|
|
||||||
|
|
||||||
@State private var checkedForSources = false
|
@State private var checkedForSources = false
|
||||||
@State private var isEditing = false
|
@State private var isEditing = false
|
||||||
|
|
||||||
|
|
@ -47,92 +29,114 @@ struct SourcesView: View {
|
||||||
@State private var searchText: String = ""
|
@State private var searchText: String = ""
|
||||||
@State private var filteredUpdatedSources: [SourceJson] = []
|
@State private var filteredUpdatedSources: [SourceJson] = []
|
||||||
@State private var filteredAvailableSources: [SourceJson] = []
|
@State private var filteredAvailableSources: [SourceJson] = []
|
||||||
|
@State private var sourcePredicate: NSPredicate?
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
NavView {
|
NavView {
|
||||||
ZStack {
|
DynamicFetchRequest(predicate: sourcePredicate) { (installedSources: FetchedResults<Source>) in
|
||||||
if !checkedForSources {
|
ZStack {
|
||||||
ProgressView()
|
if !checkedForSources {
|
||||||
} else if sources.isEmpty, sourceManager.availableSources.isEmpty {
|
ProgressView()
|
||||||
EmptyInstructionView(title: "No Sources", message: "Add a source list in Settings")
|
} else if sources.isEmpty, sourceManager.availableSources.isEmpty {
|
||||||
} else {
|
EmptyInstructionView(title: "No Sources", message: "Add a source list in Settings")
|
||||||
List {
|
} else {
|
||||||
if !filteredUpdatedSources.isEmpty {
|
List {
|
||||||
Section(header: InlineHeader("Updates")) {
|
if !filteredUpdatedSources.isEmpty {
|
||||||
ForEach(filteredUpdatedSources, id: \.self) { source in
|
Section(header: InlineHeader("Updates")) {
|
||||||
SourceUpdateButtonView(updatedSource: source)
|
ForEach(filteredUpdatedSources, id: \.self) { source in
|
||||||
|
SourceUpdateButtonView(updatedSource: source)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if !sources.isEmpty {
|
if !installedSources.isEmpty {
|
||||||
Section(header: InlineHeader("Installed")) {
|
Section(header: InlineHeader("Installed")) {
|
||||||
ForEach(sources, id: \.self) { source in
|
ForEach(installedSources, id: \.self) { source in
|
||||||
InstalledSourceView(installedSource: source)
|
InstalledSourceView(installedSource: source)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if !filteredAvailableSources.isEmpty, sourceManager.availableSources.contains(where: { availableSource in
|
if !filteredAvailableSources.isEmpty {
|
||||||
!sources.contains(
|
Section(header: InlineHeader("Catalog")) {
|
||||||
where: {
|
ForEach(filteredAvailableSources, id: \.self) { availableSource in
|
||||||
availableSource.name == $0.name &&
|
if !installedSources.contains(where: {
|
||||||
availableSource.listId == $0.listId &&
|
|
||||||
availableSource.author == $0.author
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}) {
|
|
||||||
Section(header: InlineHeader("Catalog")) {
|
|
||||||
ForEach(filteredAvailableSources, id: \.self) { availableSource in
|
|
||||||
if !sources.contains(
|
|
||||||
where: {
|
|
||||||
availableSource.name == $0.name &&
|
availableSource.name == $0.name &&
|
||||||
availableSource.listId == $0.listId &&
|
availableSource.listId == $0.listId &&
|
||||||
availableSource.author == $0.author
|
availableSource.author == $0.author
|
||||||
|
}) {
|
||||||
|
SourceCatalogButtonView(availableSource: availableSource)
|
||||||
}
|
}
|
||||||
) {
|
|
||||||
SourceCatalogButtonView(availableSource: availableSource)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
.conditionalId(UUID())
|
||||||
|
.listStyle(.insetGrouped)
|
||||||
}
|
}
|
||||||
.conditionalId(UUID())
|
|
||||||
.listStyle(.insetGrouped)
|
|
||||||
}
|
}
|
||||||
}
|
.sheet(isPresented: $navModel.showSourceSettings) {
|
||||||
.sheet(isPresented: $navModel.showSourceSettings) {
|
SourceSettingsView()
|
||||||
SourceSettingsView()
|
.environmentObject(navModel)
|
||||||
.environmentObject(navModel)
|
|
||||||
}
|
|
||||||
.onAppear {
|
|
||||||
filteredUpdatedSources = updatedSources
|
|
||||||
viewTask = Task {
|
|
||||||
await sourceManager.fetchSourcesFromUrl()
|
|
||||||
filteredAvailableSources = sourceManager.availableSources
|
|
||||||
checkedForSources = true
|
|
||||||
}
|
}
|
||||||
}
|
.onAppear {
|
||||||
.onDisappear {
|
viewTask = Task {
|
||||||
viewTask?.cancel()
|
await sourceManager.fetchSourcesFromUrl()
|
||||||
}
|
filteredAvailableSources = sourceManager.availableSources.filter { availableSource in
|
||||||
.navigationTitle("Sources")
|
!installedSources.contains(where: {
|
||||||
.navigationSearchBar {
|
availableSource.name == $0.name &&
|
||||||
SearchBar("Search", text: $searchText, isEditing: $isEditing)
|
availableSource.listId == $0.listId &&
|
||||||
.showsCancelButton(isEditing)
|
availableSource.author == $0.author
|
||||||
.onCancel {
|
})
|
||||||
searchText = ""
|
}
|
||||||
|
|
||||||
|
var updatedSources: [SourceJson] = []
|
||||||
|
for source in installedSources {
|
||||||
|
if let availableSource = sourceManager.availableSources.first(where: {
|
||||||
|
source.listId == $0.listId && source.name == $0.name && source.author == $0.author
|
||||||
|
}),
|
||||||
|
availableSource.version > source.version
|
||||||
|
{
|
||||||
|
updatedSources.append(availableSource)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
filteredUpdatedSources = sourceManager.fetchUpdatedSources(installedSources: installedSources)
|
||||||
|
checkedForSources = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.onChange(of: searchText) { _ in
|
.onDisappear {
|
||||||
filteredAvailableSources = sourceManager.availableSources.filter { searchText.isEmpty ? true : $0.name.contains(searchText) }
|
viewTask?.cancel()
|
||||||
filteredUpdatedSources = updatedSources.filter { searchText.isEmpty ? true : $0.name.contains(searchText) }
|
}
|
||||||
if #available(iOS 15.0, *) {
|
.onChange(of: searchText) { _ in
|
||||||
if searchText.isEmpty {
|
sourcePredicate = searchText.isEmpty ? nil : NSPredicate(format: "name CONTAINS[cd] %@", searchText)
|
||||||
sources.nsPredicate = nil
|
}
|
||||||
} else {
|
.onReceive(installedSources.publisher.count()) { _ in
|
||||||
sources.nsPredicate = NSPredicate(format: "name CONTAINS[cd] %@", searchText)
|
filteredAvailableSources = sourceManager.availableSources.filter { availableSource in
|
||||||
|
let sourceExists = installedSources.contains(where: {
|
||||||
|
availableSource.name == $0.name &&
|
||||||
|
availableSource.listId == $0.listId &&
|
||||||
|
availableSource.author == $0.author
|
||||||
|
})
|
||||||
|
|
||||||
|
if searchText.isEmpty {
|
||||||
|
return !sourceExists
|
||||||
|
} else {
|
||||||
|
return !sourceExists && availableSource.name.lowercased().contains(searchText.lowercased())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
filteredUpdatedSources = sourceManager.fetchUpdatedSources(installedSources: installedSources).filter {
|
||||||
|
searchText.isEmpty ? true : $0.name.lowercased().contains(searchText.lowercased())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.navigationTitle("Sources")
|
||||||
|
.navigationSearchBar {
|
||||||
|
SearchBar("Search", text: $searchText, isEditing: $isEditing)
|
||||||
|
.showsCancelButton(isEditing)
|
||||||
|
.onCancel {
|
||||||
|
searchText = ""
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue