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

UI/UX cleanup, Search improvements #306

Merged
merged 29 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
675aa36
appnav missing when inspector open
aapis Oct 15, 2024
13273c1
bugfix: full search results only render the second time you search
aapis Oct 15, 2024
314d525
Use the same icon for the find page as on mobile
aapis Oct 15, 2024
7b43fba
migrate searchbar into WidgetLibrary.UI
aapis Oct 15, 2024
6888d35
fully deprecate old SearchBar
aapis Oct 15, 2024
f3e6d29
help text doesn't need a highlight state, rounded bottom corners
aapis Oct 15, 2024
c674b1b
soften edge of grouped records view
aapis Oct 15, 2024
4a671cc
removed unnecessary arg
aapis Oct 15, 2024
edf34b9
soften find type selector
aapis Oct 15, 2024
6945880
use calendar colour if we're creating a job, project or company
aapis Oct 15, 2024
44ef124
soften UI
aapis Oct 15, 2024
44cd355
keep the sharp edge in the sidebar
aapis Oct 15, 2024
8d6ba29
moved resourcepath to UI
aapis Oct 15, 2024
6c37114
use UI resourcepath
aapis Oct 15, 2024
99ba950
improve search result group labels
aapis Oct 15, 2024
a14db40
disabled entity links that do not have children
aapis Oct 15, 2024
e871a25
modify search history
aapis Oct 16, 2024
b3c32d8
cleanup/refactoring, added support for placement within the search panel
aapis Oct 16, 2024
58d59e0
UI tweaks
aapis Oct 16, 2024
887e130
when navigating to dashboard, close search widget
aapis Oct 16, 2024
bbf7d74
adding saved search and recent search terms
aapis Oct 16, 2024
62df914
working on search UI/UX
aapis Oct 16, 2024
4c04121
undecided on where this view will live
aapis Oct 16, 2024
5c8cc41
disabling this button disables the cmd-f command on dashboard so buh bye
aapis Oct 16, 2024
b6ba959
allow finding saved search terms
aapis Oct 16, 2024
32f26dc
UI tweaks
aapis Oct 16, 2024
d883e9f
bugfix: timer instantiated 2s after onappear instead of 2s after typing
aapis Oct 16, 2024
0f623f0
standardize entity type rows in search using entity buttons from
aapis Oct 16, 2024
d0a4c73
savedsearch should be in cloud
aapis Oct 16, 2024
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
159 changes: 159 additions & 0 deletions KWCore/Sources/Query/CDSavedSearch.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
//
// CDSavedSearch.swift
// KlockWork
//
// Created by Ryan Priebe on 2024-10-15.
// Copyright © 2024 YegCollective. All rights reserved.
//

import SwiftUI
import KWCore
import CoreData

public class CDSavedSearch: ObservableObject {
/// Context for updating CD objects
public var moc: NSManagedObjectContext?

/// Thread lock
private let lock = NSLock()

/// Create new CDSavedSearch instance
/// - Parameter moc: NSManagedObjectContext
public init(moc: NSManagedObjectContext?) {
self.moc = moc
}

/// Find all saved searches
/// - Returns: [SavedSearch]
public func all() -> [SavedSearch] {
let predicate = NSPredicate(
format: "created < %@",
Date() as CVarArg
)

return self.query(predicate)
}

/// Find SavedSearch objects by term
/// - Parameter term: String
/// - Returns: Optional(SavedSearch
public func find(by term: String) -> SavedSearch? {
let predicate = NSPredicate(
format: "term == %@",
term
)

let results = self.query(predicate)

if let result = results.first {
return result
}

return nil
}

/// Destroy a saved search term
/// - Parameter term: SavedSearch
/// - Returns: Void
public func destroy(_ term: String) -> Void {
if let entity = self.query(NSPredicate(format: "term == %@", term)).first {
self.moc!.delete(entity)

PersistenceController.shared.save()
}
}

/// Create and return a new saved search term
/// - Parameters:
/// - term: String
/// - created: Date
/// - saveByDefault: Bool(true)
/// - Returns: Void
public func createAndReturn(term: String, created: Date) -> SavedSearch {
return self.make(term: term, created: created)
}

/// Create a new saved search term
/// - Parameters:
/// - term: String
/// - created: Date
/// - saveByDefault: Bool(true)
/// - Returns: Void
public func create(term: String, created: Date) -> Void {
let _ = self.make(term: term, created: created)
}

/// Create a new saved search term
/// - Parameters:
/// - term: String
/// - created: Date
/// - saveByDefault: Bool(true)
/// - Returns: SavedSearch
private func make(term: String, created: Date, saveByDefault: Bool = true) -> SavedSearch {
let savedSearch = SavedSearch(context: self.moc!)
savedSearch.term = term
savedSearch.created = created

if saveByDefault {
PersistenceController.shared.save()
}

return savedSearch
}

/// Query companies
/// - Parameter predicate: Query predicate
/// - Parameter sort: [NSSortDescriptor]
/// - Returns: Array<SavedSearch>
private func query(_ predicate: NSPredicate? = nil, _ sort: [NSSortDescriptor] = [NSSortDescriptor(keyPath: \SavedSearch.term?, ascending: true)]) -> [SavedSearch] {
lock.lock()

var results: [SavedSearch] = []
let fetch: NSFetchRequest<SavedSearch> = SavedSearch.fetchRequest()
fetch.sortDescriptors = sort
fetch.returnsDistinctResults = true
if predicate != nil {
fetch.predicate = predicate
}

do {
results = try moc!.fetch(fetch)
} catch {
if predicate != nil {
print("[error] CDSavedSearch.query Unable to find records for predicate \(predicate!.predicateFormat)")
} else {
print("[error] CDSavedSearch.query Unable to find records for query")
}

print(error)
}

lock.unlock()

return results
}

/// Count companies
/// - Parameter predicate: Query predicate
/// - Parameter sort: [NSSortDescriptor]
/// - Returns: Int
private func count(_ predicate: NSPredicate, sort: [NSSortDescriptor] = [NSSortDescriptor(keyPath: \SavedSearch.term?, ascending: true)]) -> Int {
lock.lock()

var count = 0
let fetch: NSFetchRequest<SavedSearch> = SavedSearch.fetchRequest()
fetch.sortDescriptors = sort
fetch.predicate = predicate
fetch.returnsDistinctResults = true

do {
count = try moc!.fetch(fetch).count
} catch {
print("[error] CDSavedSearch.query Unable to find records for predicate \(predicate.predicateFormat)")
}

lock.unlock()

return count
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ extension WidgetLibrary.UI {
let project = CoreDataProjects(moc: self.state.moc).createAndReturn(
name: self.event.title,
abbreviation: StringHelper.abbreviate(self.event.title),
colour: Color.randomStorable(),
colour: Color(self.event.calendar.color).toStored(),
created: Date(),
pid: Int64(Int.random(in: 1...9999999))
)
Expand All @@ -87,7 +87,7 @@ extension WidgetLibrary.UI {
let company = CoreDataCompanies(moc: self.state.moc).createAndReturn(
name: self.event.title,
abbreviation: StringHelper.abbreviate(self.event.title),
colour: Color.randomStorable(),
colour: Color(self.event.calendar.color).toStored(),
created: Date(),
projects: [],
isDefault: false,
Expand All @@ -101,7 +101,7 @@ extension WidgetLibrary.UI {
Button("Job...") {
let job = CoreDataJob(moc: self.state.moc).createAndReturn(
alive: true,
colour: Color.randomStorable(),
colour: Color(self.event.calendar.color).toStored(),
jid: Double(Int.random(in: 1...9999999)),
overview: "Work related to calendar event \"\(self.event.title ?? "Invalid event name")\" on \(self.event.startDate.formatted(date: .abbreviated, time: .omitted)) from \(self.event.startTime()) - \(self.event.endTime())",
shredable: false,
Expand Down
Loading