Skip to content

Commit

Permalink
plans can now be browsed using the date selector
Browse files Browse the repository at this point in the history
  • Loading branch information
aapis committed Aug 31, 2023
1 parent 8122911 commit 0dc18e2
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 60 deletions.
2 changes: 1 addition & 1 deletion DLPrototype/DLPrototype.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ struct DLPrototype: App {
// NSApp.presentationOptions.remove(.titled)

nav.title = "\(appName ?? "DLPrototype") \(version ?? "0").\(build ?? "0")"
nav.session.plan = CoreDataPlan(moc: persistenceController.container.viewContext).forToday().first
nav.session.plan = CoreDataPlan(moc: persistenceController.container.viewContext).forDate(nav.session.date).first

if let plan = nav.session.plan {
nav.planning.jobs = plan.jobs as! Set<Job>
Expand Down
15 changes: 13 additions & 2 deletions DLPrototype/Models/CoreData/CoreDataPlan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class CoreDataPlan {
public func forToday() -> [Plan] {
let (start, _) = DateHelper.startAndEndOf(Date())
let predicate = NSPredicate(
format: "created >= (%@)",
format: "created >= %@",
start as CVarArg
)

Expand All @@ -48,13 +48,24 @@ public final class CoreDataPlan {
public func countForToday() -> Int {
let (start, _) = DateHelper.startAndEndOf(Date())
let predicate = NSPredicate(
format: "created >= (%@)",
format: "created >= %@",
start as CVarArg
)

return count(predicate)
}

public func forDate(_ date: Date) -> [Plan] {
let (start, end) = DateHelper.startAndEndOf(date)
let predicate = NSPredicate(
format: "created > %@ && created <= %@",
start as CVarArg,
end as CVarArg
)

return query(predicate)
}

private func query(_ predicate: NSPredicate) -> [Plan] {
lock.lock()

Expand Down
82 changes: 64 additions & 18 deletions DLPrototype/Utils/Navigation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,43 +169,89 @@ extension Navigation {
return count
}

func finalize() -> Plan {
let plan = Plan(context: PersistenceController.shared.container.viewContext)
plan.id = id
plan.created = Date.now
plan.jobs = NSSet(set: jobs)
plan.tasks = NSSet(set: tasks)
plan.notes = NSSet(set: notes)
func finalize(_ date: Date) -> Plan {
if let existingPlan = CoreDataPlan(moc: moc).forDate(date).first {
return update(existingPlan)
}

return create(for: date)
}

func reset(_ date: Date) -> Void {
if let plan = CoreDataPlan(moc: moc).forDate(date).first {
moc.delete(plan)
}

PersistenceController.shared.save()
}

return plan
mutating func empty(_ date: Date) -> Void {
if let plan = CoreDataPlan(moc: moc).forDate(date).first {
plan.jobs = []
plan.tasks = []
plan.notes = []
} else {
jobs = []
tasks = []
notes = []
}

PersistenceController.shared.save()
}

func reset() -> Void {
// let moc = PersistenceController.shared.container.viewContext
let plans = CoreDataPlan(moc: moc).forToday()
mutating func clean() -> Void {
let plans = CoreDataPlan(moc: moc).all()

if plans.count > 0 {
for plan in plans {
moc.delete(plan)
}
}

jobs = []
tasks = []
notes = []

PersistenceController.shared.save()
}

func clean() -> Void {
let moc = PersistenceController.shared.container.viewContext
let plans = CoreDataPlan(moc: moc).all()
mutating func load(_ plan: Plan?) -> Void {
if let pl = plan {
id = pl.id!

var sJobs: Set<Job> = []
for o in pl.jobs!.allObjects as! [Job] {sJobs.insert(o)}
jobs = sJobs

if plans.count > 0 {
for plan in plans {
moc.delete(plan)
}
var sTasks: Set<LogTask> = []
for o in pl.tasks!.allObjects as! [LogTask] {sTasks.insert(o)}
tasks = sTasks

var sNotes: Set<Note> = []
for o in pl.notes!.allObjects as! [Note] {sNotes.insert(o)}
notes = sNotes
}
}

private func create(for date: Date) -> Plan {
let plan = Plan(context: moc)
plan.id = id
plan.created = date
plan.jobs = NSSet(set: jobs)
plan.tasks = NSSet(set: tasks)
plan.notes = NSSet(set: notes)

PersistenceController.shared.save()
return plan
}

private func update(_ plan: Plan) -> Plan {
plan.jobs = NSSet(set: jobs)
plan.tasks = NSSet(set: tasks)
plan.notes = NSSet(set: notes)

PersistenceController.shared.save()
return plan
}
}
}
Expand Down
1 change: 1 addition & 0 deletions DLPrototype/Utils/ViewUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class ViewUpdater: ObservableObject {
"job.dashboard": UUID(),
"dashboard.header": UUID(),
"sidebar": UUID(),
"planning.daily": UUID(),
]

public func get(_ key: String) -> UUID {
Expand Down
16 changes: 13 additions & 3 deletions DLPrototype/Views/Planning/Menu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ extension Planning {
.onChange(of: nav.planning.tasks, perform: actionOnChangeTasks)
.onChange(of: nav.planning.jobs, perform: actionOnChangeJobs)
.onChange(of: nav.planning.notes, perform: actionOnChangeNotes)
.onChange(of: nav.session.date, perform: actionOnChangeDate)
}

var CountPills: some View {
Expand Down Expand Up @@ -105,13 +106,13 @@ extension Planning {

extension Planning.Menu {
private func actionFinalizePlan() -> Void {
nav.planning.reset()
let plan = nav.planning.finalize()
nav.planning.empty(nav.session.date)
let plan = nav.planning.finalize(nav.session.date)
nav.session.plan = plan
}

private func actionResetPlan() -> Void {
nav.planning.reset()
nav.planning.empty(nav.session.date)
nav.planning = Navigation.Planning(moc: nav.planning.moc)
nav.session.plan = nil
nav.session.gif = .normal
Expand Down Expand Up @@ -176,4 +177,13 @@ extension Planning.Menu {

ClipboardHelper.copy(dailyPlan)
}

private func actionOnChangeDate(_ date: Date) -> Void {
if let plan = CoreDataPlan(moc: nav.planning.moc).forDate(date).first {
nav.session.plan = plan
nav.planning.load(nav.session.plan)
} else {
nav.planning.empty(nav.session.date)
}
}
}
54 changes: 26 additions & 28 deletions DLPrototype/Views/Planning/Planning.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,39 @@ struct Planning: View {
private let maxItems: Int = 6
private let title: String = "Planning"
private let description: String = "Use the daily plan to organize your day. Feature plans allow you to define projects of any scope!"
private let buttons: [ToolbarButton] = [
ToolbarButton(
id: 0,
helpText: "What are you working on today?",
label: AnyView(
HStack {
Image(systemName: "calendar")
.font(.title2)
Text("Daily")
}
),
contents: AnyView(Planning.Today())
),
ToolbarButton(
id: 1,
helpText: "Feature planning",
label: AnyView(
HStack {
Image(systemName: "list.bullet.below.rectangle")
.font(.title2)
Text("Feature")
}
),
contents: AnyView(Planning.Feature())
)
]

static public let tooManyJobs: Int = 5
static public let tooManyTasks: Int = 8

@EnvironmentObject public var nav: Navigation

@State private var jobs: [Job] = []
@State private var buttons: [ToolbarButton] = []

var body: some View {
VStack(alignment: .leading) {
Expand Down Expand Up @@ -48,33 +73,6 @@ extension Planning {

private func actionOnAppear() -> Void {
actionOnChangeJobs(nav.planning.jobs)

buttons = [
ToolbarButton(
id: 0,
helpText: "What are you working on today?",
label: AnyView(
HStack {
Image(systemName: "calendar")
.font(.title2)
Text("Daily")
}
),
contents: AnyView(Planning.Today())
),
ToolbarButton(
id: 1,
helpText: "Feature planning",
label: AnyView(
HStack {
Image(systemName: "list.bullet.below.rectangle")
.font(.title2)
Text("Feature")
}
),
contents: AnyView(Planning.Feature())
)
]
}

private func actionOnChangeJobs(_ newJobs: Set<Job>) -> Void {
Expand Down
4 changes: 2 additions & 2 deletions DLPrototype/Views/Planning/Rows/Planning.Group.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ extension Planning {
nav.planning.jobs.remove(job)

if nav.planning.jobs.count == 0 {
nav.planning.reset()
nav.planning.reset(nav.session.date)
nav.session.plan = nil
nav.session.gif = .normal
} else {
let plan = nav.planning.finalize()
let plan = nav.planning.finalize(nav.session.date)
nav.session.plan = plan
}
} label: {
Expand Down
8 changes: 7 additions & 1 deletion DLPrototype/Views/Planning/Tabs/Planning.Today.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import SwiftUI

extension Planning {
struct Today: View {
@Environment(\.managedObjectContext) var moc
@EnvironmentObject public var nav: Navigation
@EnvironmentObject public var updater: ViewUpdater

var body: some View {
VStack(alignment: .leading, spacing: 1) {
Expand All @@ -25,7 +27,7 @@ extension Planning {
}
} else {
HStack {
Text("Add jobs using the sidebar widget then select the tasks you'd like to focus on (turn it on by clicking the sidebar's moon icon). This list saves automatically.")
Text("Add jobs using the sidebar widget then select the tasks you'd like to focus. This list saves automatically.")
.foregroundColor(.gray)
Spacer()
}
Expand All @@ -34,10 +36,14 @@ extension Planning {
}
}
}
.onAppear(perform: actionOnAppear)
.id(updater.get("planning.daily"))
}
}
}

extension Planning.Today {
private func actionOnAppear() -> Void {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@ struct CreateEntitiesWidget: View {
Theme.base.opacity(0.5)
if doesPlanExist {
FancyButtonv2(
text: gif == .focus ? "Disable planning focus" : "Enable global filter to only show you the things you need to see",
text: gif == .focus ? "Disable the global filter, show all items" : "Show only the items in your daily plan",
action: actionOnChangeFocus,
icon: gif == .focus ? "moon.fill" : "moon",
icon: gif == .focus ? "circle.hexagongrid.fill" : "circle.hexagongrid",
showLabel: false,
size: .small,
type: .tsWhite // TODO: don't use star!
type: .tsWhite
)
.mask(Circle())
} else {
FancyButtonv2(
text: "You need to create a plan first, click here!",
icon: "moon.zzz",
icon: "hexagon",
showLabel: false,
size: .small,
type: nav.parent == .planning ? .secondary : .standard,
Expand All @@ -68,6 +68,10 @@ struct CreateEntitiesWidget: View {
}
}
.mask(Circle())
if !doesPlanExist && gif == .normal {
Image(systemName: "questionmark.circle.fill")
.position(x: 38, y: 38)
}
}
.frame(width: 46, height: 46)

Expand Down Expand Up @@ -199,7 +203,7 @@ extension CreateEntitiesWidget {
}

private func findPlan() -> Void {
let plans = CoreDataPlan(moc: moc).forToday()
let plans = CoreDataPlan(moc: moc).forDate(nav.session.date)
if plans.count > 0 {
if let plan = plans.first {
doesPlanExist = !plan.isEmpty()
Expand Down

0 comments on commit 0dc18e2

Please sign in to comment.