Skip to content

Commit

Permalink
Merge pull request #303 from aapis/feature/1.13/cal-event-actions
Browse files Browse the repository at this point in the history
New feature: Create entities for a calendar event
  • Loading branch information
aapis authored Oct 15, 2024
2 parents fb8208c + bda7fd9 commit 586acf4
Show file tree
Hide file tree
Showing 18 changed files with 285 additions and 448 deletions.
2 changes: 1 addition & 1 deletion KWCore/Sources/Query/CoreDataCompanies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public class CoreDataCompanies: ObservableObject {
/// - Returns: Company|nil
public func findDefault() -> Company? {
let predicate = NSPredicate(
format: "isDefault == true"
format: "isDefault == true && alive == true"
)

let results = query(predicate)
Expand Down
6 changes: 3 additions & 3 deletions KWCore/Sources/Query/CoreDataJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ public class CoreDataJob: ObservableObject {
/// - project: Optional(Project)
/// - saveByDefault: Bool(true) - Save immediately after creating the obejct, or not
/// - Returns: Void
public func create(alive: Bool, colour: [Double], created: Date = Date(), jid: Double, overview: String?, shredable: Bool, title: String?, uri: String, project: Project?, saveByDefault: Bool = true) -> Void {
public func create(alive: Bool, colour: [Double], created: Date = Date(), jid: Double, overview: String?, shredable: Bool, title: String?, uri: String, project: Project? = nil, saveByDefault: Bool = true) -> Void {
let _ = self.make(
alive: alive,
colour: colour,
Expand All @@ -452,7 +452,7 @@ public class CoreDataJob: ObservableObject {
/// - project: Optional(Project)
/// - saveByDefault: Bool(true) - Save immediately after creating the obejct, or not
/// - Returns: Void
public func createAndReturn(alive: Bool, colour: [Double], created: Date = Date(), jid: Double, overview: String?, shredable: Bool, title: String?, uri: String, project: Project?, saveByDefault: Bool = true) -> Job {
public func createAndReturn(alive: Bool, colour: [Double], created: Date = Date(), jid: Double, overview: String?, shredable: Bool, title: String?, uri: String, project: Project? = nil, saveByDefault: Bool = true) -> Job {
return self.make(
alive: alive,
colour: colour,
Expand All @@ -479,7 +479,7 @@ public class CoreDataJob: ObservableObject {
/// - project: Optional(Project)
/// - saveByDefault: Bool(true)
/// - Returns: Void
private func make(alive: Bool, colour: [Double], created: Date = Date(), id: UUID = UUID(), jid: Double, lastUpdate: Date = Date(), overview: String?, shredable: Bool, title: String?, uri: String, project: Project?, saveByDefault: Bool = true) -> Job {
private func make(alive: Bool, colour: [Double], created: Date = Date(), id: UUID = UUID(), jid: Double, lastUpdate: Date = Date(), overview: String?, shredable: Bool, title: String?, uri: String, project: Project? = nil, saveByDefault: Bool = true) -> Job {
let newJob = Job(context: self.moc!)
newJob.alive = alive
newJob.colour = colour
Expand Down
40 changes: 29 additions & 11 deletions KWCore/Sources/Query/CoreDataNotes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -255,24 +255,42 @@ public class CoreDataNotes {

/// Find notes by Job
/// - Parameter job: Job
/// - Returns: [Note]]
public func find(by job: Job) -> [Note] {
let predicate = NSPredicate(
format: "ANY mJob == %@",
job
)
/// - Returns: [Note]
public func find(by job: Job, allowKilled: Bool = false) -> [Note] {
var predicate: NSPredicate
if !allowKilled {
predicate = NSPredicate(
format: "ANY mJob == %@",
job
)
} else {
predicate = NSPredicate(
format: "alive == true && ANY mJob == %@",
job
)
}

return query(predicate)
}

/// Find notes by Job
/// - Parameter project: Project
/// - Returns: [Note]
public func find(by project: Project) -> [Note] {
let predicate = NSPredicate(
format: "ANY mJob.project == %@",
project
)
public func find(by project: Project, allowKilled: Bool = false) -> [Note] {
var predicate: NSPredicate

if !allowKilled {
predicate = NSPredicate(
format: "mJob.project == %@",
project
)
} else {
predicate = NSPredicate(
format: "alive == true && mJob.project == %@",
project
)
}


return query(predicate)
}
Expand Down
4 changes: 2 additions & 2 deletions KWCore/Sources/Query/CoreDataTasks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ public class CoreDataTasks {
/// - job: Job
/// - saveByDefault: Bool
/// - Returns: LogTask
public func create(cancelledDate: Date? = nil, completedDate: Date? = nil, content: String, created: Date, due: Date, lastUpdate: Date? = Date(), job: Job?, saveByDefault: Bool = true) -> Void {
public func create(cancelledDate: Date? = nil, completedDate: Date? = nil, content: String, created: Date, due: Date, lastUpdate: Date? = Date(), job: Job? = nil, saveByDefault: Bool = true) -> Void {
let _ = self.make(cancelledDate: cancelledDate, completedDate: completedDate, content: content, created: created, due: due, lastUpdate: lastUpdate, job: job, saveByDefault: saveByDefault)
}

Expand All @@ -452,7 +452,7 @@ public class CoreDataTasks {
/// - job: Job
/// - saveByDefault: Bool
/// - Returns: LogTask
public func createAndReturn(cancelledDate: Date? = nil, completedDate: Date? = nil, content: String, created: Date, due: Date, lastUpdate: Date? = Date(), job: Job?, saveByDefault: Bool = true) -> LogTask {
public func createAndReturn(cancelledDate: Date? = nil, completedDate: Date? = nil, content: String, created: Date, due: Date, lastUpdate: Date? = Date(), job: Job? = nil, saveByDefault: Bool = true) -> LogTask {
return self.make(cancelledDate: cancelledDate, completedDate: completedDate, content: content, created: created, due: due, lastUpdate: lastUpdate, job: job, saveByDefault: saveByDefault)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ extension WidgetLibrary.UI {
orientation: .right,
action: {self.state.session.search.inspectingEvent = self.event},
showBorder: false,
showButton: false
showButton: false,
contextMenu: AnyView(self.contextMenu)
)
.background(self.hasEventPassed ? Theme.lightWhite : Color(event.calendar.color))
.foregroundStyle(self.hasEventPassed ? Theme.lightBase : Theme.base)
Expand All @@ -38,6 +39,89 @@ extension WidgetLibrary.UI {
}
}
}

@ViewBuilder var contextMenu: some View {
Menu("Create for event") {
Button("Task...") {
let task = CoreDataTasks(moc: self.state.moc).createAndReturn(
content: self.event.title,
created: Date(),
due: self.event.startDate,
saveByDefault: false
)

self.state.session.task = task
self.state.to(.taskDetail)
}

Button("Note...") {
let note = CoreDataNotes(moc: self.state.moc).createAndReturn(
alive: true,
body: "# Notes for \(self.event.title ?? "Invalid event name")\n\n",
lastUpdate: Date(),
postedDate: Date(),
starred: false,
title: "Notes for \(self.event.title ?? "Invalid event name")"
)

self.state.session.note = note
self.state.to(.noteDetail)
}

if let defaultCompany = CoreDataCompanies(moc: PersistenceController.shared.container.viewContext).findDefault() {
Button("Project...") {
let project = CoreDataProjects(moc: self.state.moc).createAndReturn(
name: self.event.title,
abbreviation: StringHelper.abbreviate(self.event.title),
colour: Color.randomStorable(),
created: Date(),
pid: Int64(Int.random(in: 1...9999999))
)

self.state.session.company = defaultCompany
self.state.session.project = project
self.state.to(.projectDetail)
}

Button("Company...") {
let company = CoreDataCompanies(moc: self.state.moc).createAndReturn(
name: self.event.title,
abbreviation: StringHelper.abbreviate(self.event.title),
colour: Color.randomStorable(),
created: Date(),
projects: [],
isDefault: false,
pid: Int64(Int.random(in: 1...9999999))
)

self.state.session.company = company
self.state.to(.companyDetail)
}

Button("Job...") {
let job = CoreDataJob(moc: self.state.moc).createAndReturn(
alive: true,
colour: Color.randomStorable(),
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,
title: self.event.title,
uri: "https://",
project: defaultCompany.defaultProject
)

self.state.session.company = defaultCompany
self.state.session.project = defaultCompany.defaultProject
self.state.session.job = job
self.state.to(.jobs)
}
}
}
Divider()
Button("Inspect") {
self.state.session.search.inspectingEvent = self.event
}
}
}
}
}
Expand Down
4 changes: 0 additions & 4 deletions KlockWork.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@
537628A02966518B00DE8ECF /* ToolbarTabs.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5376289F2966518B00DE8ECF /* ToolbarTabs.swift */; };
537628A22966525500DE8ECF /* ToolbarButtons.swift in Sources */ = {isa = PBXBuildFile; fileRef = 537628A12966525500DE8ECF /* ToolbarButtons.swift */; };
537628A8296659DC00DE8ECF /* PersistenceController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 537628A7296659DC00DE8ECF /* PersistenceController.swift */; };
537628AC29665B4E00DE8ECF /* NoteView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 537628AB29665B4E00DE8ECF /* NoteView.swift */; };
537628B429665F4E00DE8ECF /* Data.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 537628B229665F4E00DE8ECF /* Data.xcdatamodeld */; };
537885742B44CB3300825B78 /* TaskDashboardByProject.swift in Sources */ = {isa = PBXBuildFile; fileRef = 537885732B44CB3300825B78 /* TaskDashboardByProject.swift */; };
537885762B44CBAF00825B78 /* Column.swift in Sources */ = {isa = PBXBuildFile; fileRef = 537885752B44CBAF00825B78 /* Column.swift */; };
Expand Down Expand Up @@ -436,7 +435,6 @@
5376289F2966518B00DE8ECF /* ToolbarTabs.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ToolbarTabs.swift; sourceTree = "<group>"; };
537628A12966525500DE8ECF /* ToolbarButtons.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ToolbarButtons.swift; sourceTree = "<group>"; };
537628A7296659DC00DE8ECF /* PersistenceController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PersistenceController.swift; sourceTree = "<group>"; };
537628AB29665B4E00DE8ECF /* NoteView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NoteView.swift; sourceTree = "<group>"; };
537628B329665F4E00DE8ECF /* Note.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = Note.xcdatamodel; sourceTree = "<group>"; };
537885732B44CB3300825B78 /* TaskDashboardByProject.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TaskDashboardByProject.swift; sourceTree = "<group>"; };
537885752B44CBAF00825B78 /* Column.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = Column.swift; path = KlockWork/Views/Entities/Tasks/Column.swift; sourceTree = SOURCE_ROOT; };
Expand Down Expand Up @@ -1036,7 +1034,6 @@
children = (
53E918752B43DB0000912C6F /* Templates */,
5371BA292A7B278D00DEEC21 /* Sidebars */,
537628AB29665B4E00DE8ECF /* NoteView.swift */,
53999C1E29669C4E00125E65 /* NoteCreate.swift */,
53152CE3296BC8A100A14E43 /* NoteDashboard.swift */,
53F5895F2998335A00843B32 /* NoteRow.swift */,
Expand Down Expand Up @@ -1742,7 +1739,6 @@
53C3419029996C070071B855 /* FancyRandomJobColourPicker.swift in Sources */,
537368D124B93DB600193E88 /* Today.swift in Sources */,
53C000AD2B41490F00D5EC04 /* NoteTemplates.swift in Sources */,
537628AC29665B4E00DE8ECF /* NoteView.swift in Sources */,
53B0BE8829726400007CB663 /* GenericToolbar.swift in Sources */,
53E24C412A844BC800EB21FD /* EKEvent.swift in Sources */,
5334F2722B2B95240079D2E7 /* FancyJobActiveToggle.swift in Sources */,
Expand Down
12 changes: 12 additions & 0 deletions KlockWork/Extensions/Models/Company.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,16 @@ extension Company {

var pageType: Page { .companies }
var pageDetailType: Page { .companyDetail }

var defaultProject: Project? {
if let company = CoreDataCompanies(moc: PersistenceController.shared.container.viewContext).findDefault() {
if let projects = company.projects?.allObjects as? [Project] {
if projects.count > 0 {
return projects.sorted(by: {$0.created ?? Date() > $1.created ?? Date()}).first
}
}
}

return nil
}
}
6 changes: 3 additions & 3 deletions KlockWork/Utils/Navigation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -576,17 +576,17 @@ extension Navigation {
public let all: [HistoryPage] = [
HistoryPage(page: .dashboard, view: AnyView(Dashboard()), sidebar: AnyView(DashboardSidebar()), title: "Dashboard"),
HistoryPage(page: .planning, view: AnyView(Planning()), sidebar: AnyView(DefaultPlanningSidebar()), title: "Planning"),
HistoryPage(page: .today, view: AnyView(Today()), sidebar: AnyView(TodaySidebar()), title: "Today", navButtons: [.CLIFilter, .CLIMode, .resetUserChoices]),
HistoryPage(page: .today, view: AnyView(Today()), sidebar: AnyView(TodaySidebar()), title: "Today", navButtons: [.resetUserChoices, .CLIFilter, .CLIMode]),
HistoryPage(page: .recordDetail, view: AnyView(RecordDetail()), sidebar: AnyView(TodaySidebar()), title: "Record"),
HistoryPage(page: .companies, view: AnyView(CompanyDashboard()), sidebar: AnyView(DefaultCompanySidebar()), title: "Companies & Projects", navButtons: [.createCompany, .createProject]),
HistoryPage(page: .companies, view: AnyView(CompanyDashboard()), sidebar: AnyView(DefaultCompanySidebar()), title: "Companies & Projects", navButtons: [.resetUserChoices, .createCompany, .createProject]),
HistoryPage(page: .companyDetail, view: AnyView(CompanyView()), sidebar: AnyView(DefaultCompanySidebar()), title: "Company"),
HistoryPage(page: .jobs, view: AnyView(JobDashboardRedux()), sidebar: AnyView(JobDashboardSidebar()), title: "Jobs", navButtons: [.resetUserChoices, .createJob]),
HistoryPage(page: .notes, view: AnyView(NoteDashboard()), sidebar: AnyView(NoteDashboardSidebar()), title: "Notes", navButtons: [.resetUserChoices, .createNote]),
HistoryPage(page: .noteDetail, view: AnyView(NoteCreate()), sidebar: AnyView(NoteCreateSidebar()), title: "Note detail"),
HistoryPage(page: .tasks, view: AnyView(TaskDashboard()), sidebar: AnyView(TaskDashboardSidebar()), title: "Tasks", navButtons: [.resetUserChoices, .createTask]),
HistoryPage(page: .terms, view: AnyView(TermsDashboard()), sidebar: AnyView(TermsDashboardSidebar()), title: "Terms", navButtons: [.resetUserChoices, .createDefinition]),
HistoryPage(page: .definitionDetail, view: AnyView(DefinitionDetail()), sidebar: AnyView(TermsDashboardSidebar()), title: "Definition detail"),
HistoryPage(page: .taskDetail, view: AnyView(TaskDetail()), sidebar: AnyView(TermsDashboardSidebar()), title: "Task detail"),
HistoryPage(page: .noteDetail, view: AnyView(NoteView()), sidebar: AnyView(NoteCreateSidebar()), title: "Note detail"),
HistoryPage(page: .people, view: AnyView(PeopleDashboard()), sidebar: AnyView(PeopleDashboardSidebar()), title: "People", navButtons: [.resetUserChoices, .createPerson]),
HistoryPage(page: .peopleDetail, view: AnyView(PeopleDetail()), sidebar: AnyView(PeopleDashboardSidebar()), title: "Person"),
HistoryPage(page: .projectDetail, view: AnyView(CompanyView()), sidebar: AnyView(DefaultCompanySidebar()), title: "Project"),
Expand Down
Loading

0 comments on commit 586acf4

Please sign in to comment.