Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search 2.0 #192

Merged
merged 19 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions DLPrototype.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
533E40652970DA640007785A /* ViewUpdater.swift in Sources */ = {isa = PBXBuildFile; fileRef = 533E40642970DA640007785A /* ViewUpdater.swift */; };
533E40682970DD170007785A /* AutoFixJobs.swift in Sources */ = {isa = PBXBuildFile; fileRef = 533E40672970DD170007785A /* AutoFixJobs.swift */; };
533E406A2970F5610007785A /* SearchHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 533E40692970F5610007785A /* SearchHelper.swift */; };
5345C9CD2B31315F004A4188 /* SearchLanguage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5345C9CC2B31315F004A4188 /* SearchLanguage.swift */; };
534B7C2B2B3220E40017C155 /* DispatchQueue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 534B7C2A2B3220E40017C155 /* DispatchQueue.swift */; };
53521E54296FBDAE002E7F21 /* CoreDataNoteVersions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53521E53296FBDAE002E7F21 /* CoreDataNoteVersions.swift */; };
53529A07299F38C0000D2FCB /* JobPickerUsing.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53529A06299F38C0000D2FCB /* JobPickerUsing.swift */; };
5354C8E42AA03C1C001C1779 /* Planning.Row.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5354C8E32AA03C1C001C1779 /* Planning.Row.swift */; };
Expand Down Expand Up @@ -277,6 +279,8 @@
533E40642970DA640007785A /* ViewUpdater.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewUpdater.swift; sourceTree = "<group>"; };
533E40672970DD170007785A /* AutoFixJobs.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AutoFixJobs.swift; sourceTree = "<group>"; };
533E40692970F5610007785A /* SearchHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchHelper.swift; sourceTree = "<group>"; };
5345C9CC2B31315F004A4188 /* SearchLanguage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchLanguage.swift; sourceTree = "<group>"; };
534B7C2A2B3220E40017C155 /* DispatchQueue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DispatchQueue.swift; sourceTree = "<group>"; };
53521E53296FBDAE002E7F21 /* CoreDataNoteVersions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoreDataNoteVersions.swift; sourceTree = "<group>"; };
53529A06299F38C0000D2FCB /* JobPickerUsing.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JobPickerUsing.swift; sourceTree = "<group>"; };
5354C8E32AA03C1C001C1779 /* Planning.Row.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Planning.Row.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -513,6 +517,7 @@
53218D4624B7F6EB0088ABE9 /* DLPrototype */ = {
isa = PBXGroup;
children = (
5345C9CB2B313140004A4188 /* Parsers */,
53790C0E2A74D53000D3FFD4 /* Validators */,
535C0E3329F729160053AD6A /* ML */,
533E40662970DD080007785A /* Utils */,
Expand Down Expand Up @@ -626,6 +631,14 @@
path = Utils;
sourceTree = "<group>";
};
5345C9CB2B313140004A4188 /* Parsers */ = {
isa = PBXGroup;
children = (
5345C9CC2B31315F004A4188 /* SearchLanguage.swift */,
);
path = Parsers;
sourceTree = "<group>";
};
5354C8EB2AA03C85001C1779 /* Rows */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -1023,6 +1036,7 @@
53152CC829690E7300A14E43 /* LosslessStringConvertible.swift */,
5363B67F2A6DE87000C2FBB8 /* View.swift */,
53E24C402A844BC800EB21FD /* EKEvent.swift */,
534B7C2A2B3220E40017C155 /* DispatchQueue.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -1229,6 +1243,7 @@
53152CC929690E7300A14E43 /* LosslessStringConvertible.swift in Sources */,
53B0BE8A2972711A007CB663 /* FancyDivider.swift in Sources */,
53EDDFA22963502A008D34C7 /* LogRowEmpty.swift in Sources */,
5345C9CD2B31315F004A4188 /* SearchLanguage.swift in Sources */,
5371BA342A7B2B8F00DEEC21 /* JobRowPlain.swift in Sources */,
533AB3F2296CA34600E91A9F /* ManageRecords.swift in Sources */,
5334F2702B2B83190079D2E7 /* FancyColourPicker.swift in Sources */,
Expand Down Expand Up @@ -1319,6 +1334,7 @@
53C3418C299960310071B855 /* JobDashboard.swift in Sources */,
539C96EA27EE7AFF007DA058 /* CalendarThisWeek.swift in Sources */,
5334F26E2B2A5CC50079D2E7 /* FancyLabel.swift in Sources */,
534B7C2B2B3220E40017C155 /* DispatchQueue.swift in Sources */,
535C0E3529F729320053AD6A /* Keywords.swift in Sources */,
53F5896229983EEB00843B32 /* TaskResult.swift in Sources */,
5354C8ED2AA03C9D001C1779 /* Tasks.Row.swift in Sources */,
Expand Down
22 changes: 22 additions & 0 deletions DLPrototype/Extensions/DispatchQueue.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// DispatchQueue.swift
// DLPrototype
//
// Created by Ryan Priebe on 2023-12-19.
// Copyright © 2023 YegCollective. All rights reserved.
//

import Foundation

extension DispatchQueue {
static func background(delay: Double = 0.0, background: (()->Void)? = nil, completion: (() -> Void)? = nil) {
DispatchQueue.global(qos: .background).async {
background?()
if let completion = completion {
DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: {
completion()
})
}
}
}
}
1 change: 1 addition & 0 deletions DLPrototype/Extensions/Models/Job.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ extension Job {
return Int(exactly: jid.rounded(.toNearestOrEven)) ?? 0
}

// @TODO: this seems to return a much lighter shade of the actual colour, fix that
func colour_from_stored() -> Color {
if let c = colour {
return Color.fromStored(c)
Expand Down
5 changes: 5 additions & 0 deletions DLPrototype/MainMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ struct MainMenu: Commands {
nav.parent = .companies
}
.keyboardShortcut("p", modifiers: [.command, .shift])
Button("Company") {
nav.view = AnyView(CompanyCreate())
nav.parent = .companies
}
.keyboardShortcut("c", modifiers: [.command, .shift])
Button("Job") {
nav.view = AnyView(JobCreate())
nav.parent = .jobs
Expand Down
14 changes: 13 additions & 1 deletion DLPrototype/Models/CoreData/CoreDataJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,24 +116,36 @@ public class CoreDataJob: ObservableObject {
return all
}

public func all(_ stillAlive: Bool? = true) -> [Job] {
public func all(_ stillAlive: Bool? = true, fetchLimit: Int? = nil, resultLimit: Int? = nil) -> [Job] {
var all: [Job] = []
let fetch: NSFetchRequest<Job> = Job.fetchRequest()
fetch.sortDescriptors = [NSSortDescriptor(keyPath: \Job.jid, ascending: false)]

if let lim = fetchLimit {
fetch.fetchLimit = lim
}

if stillAlive! {
fetch.predicate = NSPredicate(format: "alive == true")
}

do {
all = try moc!.fetch(fetch)

if let resLim = resultLimit {
all = Array(all.prefix(upTo: resLim))
}
} catch {
print("Couldn't retrieve all jobs")
}

return all
}

public func startsWith(_ id: String) -> [Job] {
return self.all().filter {$0.alive == true && $0.jid.string.starts(with: id)}
}

public func owned() -> [Job] {
var all: [Job] = []
let fetch: NSFetchRequest<Job> = Job.fetchRequest()
Expand Down
209 changes: 209 additions & 0 deletions DLPrototype/Parsers/SearchLanguage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
//
// SearchLanguageParser.swift
// DLPrototype
//
// Created by Ryan Priebe on 2023-12-18.
// Copyright © 2023 YegCollective. All rights reserved.
//

import SwiftUI

// MARK: Structs
public struct SearchLanguage {}

extension SearchLanguage {
public struct Results {
var components: Set<SearchLanguage.Component> = []
var moc: NSManagedObjectContext
}

public struct Component: Hashable, Equatable {
var id: UUID = UUID()
var species: Species
var column: Column
var command: Command
var value: Value
var isValid: Bool = false
}

public class Parser {
var components: Set<SearchLanguage.Component> = []
var with: String = ""
private let pattern = /^@(.*?)\.(.*?)=(\d+)/
}
}

extension SearchLanguage.Component {
public struct Species {
var name: String
}

public struct Column {
var name: String
}

public enum Command {
case equals, add, sub, mult, div

var symbol: String {
switch self {
case .equals:
return "="
case .add:
return "+"
case .sub:
return "-"
case .mult:
return "x"
case .div:
return "/"
}
}
}

public struct Value {
var int: Int? = 0
}
}

extension SearchLanguage.Results {
public enum SpeciesType {
case job, task, record, company, person, project

var colour: Color {
switch self {
case .job: return Theme.cGreen
case .task: return Theme.cRed
case .record: return Theme.cOrange
case .company: return Theme.cPurple
case .project: return Theme.cYellow
case .person: return Theme.rowColour
}
}

var name: String {
switch self {
case .job: return "Jobs"
case .task: return "Tasks"
case .record: return "Records"
case .company: return "Companies"
case .project: return "Projects"
case .person: return "People"
}
}
}

struct Result: Identifiable {
var id: UUID = UUID()
var label: SpeciesType
var set: [NSManagedObject]
var count: Int
}
}

// MARK: Method definitions
extension SearchLanguage.Parser {
convenience init(with: String) {
self.init()
self.with = with
}

public func parse() -> Self {
let matches = with.matches(of: pattern)

for match in matches {
let component = SearchLanguage.Component(
species: SearchLanguage.Component.Species(name: String(match.1).capitalized),
column: SearchLanguage.Component.Column(name: String(match.2)),
command: SearchLanguage.Component.Command.equals,
value: SearchLanguage.Component.Value(int: Int(match.3)!)
)

if component.isValid {
components.insert(component)
}
}

return self
}
}

extension SearchLanguage.Results {
func find() -> [Result] {
var resultSet: [Result] = []
var jobs: [Job] = []
var tasks: [LogTask] = []
var projects: [Project] = []
var companies: [Company] = []

for component in components {
switch component.species.name {
case "Job":
if let match = job(id: component.value) {
jobs.append(match)

tasks = match.tasks?.allObjects as! [LogTask]

if let project = match.project {
projects.append(project)

if let company = project.company {
companies.append(company)
}
}
}
default:
print("DERPO unimplemented Species \(component.species.name)")
}

if jobs.count > 0 {
resultSet.append(Result(label: SpeciesType.job, set: jobs, count: jobs.count))
}

if tasks.count > 0 {
resultSet.append(Result(label: SpeciesType.task, set: tasks, count: tasks.count))
}

if projects.count > 0 {
resultSet.append(Result(label: SpeciesType.project, set: projects, count: projects.count))
}

if companies.count > 0 {
resultSet.append(Result(label: SpeciesType.company, set: companies, count: companies.count))
}
}

return resultSet
}

private func job(id: SearchLanguage.Component.Value) -> Job? {
if let int = id.int {
if let species = CoreDataJob(moc: moc).byId(Double(int)) {
return species
}
}

return nil
}
}

extension SearchLanguage.Component {
init(species: Species, column: Column, command: Command, value: Value) {
self.species = species
self.column = column
self.command = command
self.value = value

if !self.species.name.isEmpty && !self.column.name.isEmpty && self.value.int != nil {
self.isValid = true
}
}

static public func == (lhs: SearchLanguage.Component, rhs: SearchLanguage.Component) -> Bool {
return lhs.species.name == rhs.species.name
}

public func hash(into hasher: inout Hasher) {
hasher.combine(species.name)
}
}
27 changes: 27 additions & 0 deletions DLPrototype/Utils/Navigation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ extension Navigation {
var date: Date = Date()
var idate: IdentifiableDay = IdentifiableDay()
var gif: Planning.GlobalInterfaceFilter = .normal
var search: Search = Search(moc: PersistenceController.shared.container.viewContext)
}
}

Expand All @@ -155,6 +156,32 @@ extension Navigation.Session {
}
}

extension Navigation.Session {
public struct Search {
var id: UUID = UUID()
var text: String? = nil
var components: Set<SearchLanguage.Component> = []
var moc: NSManagedObjectContext
var hasResults: Bool = false
var inspectingEntity: NSManagedObject? = nil
}
}

extension Navigation.Session.Search {
mutating func results() -> [SearchLanguage.Results.Result] {
hasResults = true

return SearchLanguage.Results(components: components, moc: moc).find()
}

mutating func reset() -> Void {
components = []
text = nil
hasResults = false
inspectingEntity = nil
}
}

extension Navigation {
public struct Planning {
var id: UUID = UUID()
Expand Down
Loading