Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
34 changes: 34 additions & 0 deletions CalenTask/Domain/TaskFactory.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import Foundation
import SwiftData

/// Punto unico di creazione delle task dal calendario (#18): spazio di
/// destinazione, identità, inserimento, salvataggio e notifiche con le stesse
/// regole ovunque.
@MainActor
enum TaskFactory {
/// Crea una task/evento in [start, end) nello spazio di destinazione
/// (`WorkspaceScope.creationTarget`) e la salva. Ritorna la task creata.
@discardableResult
static func createScheduled(
title: String,
kind: TaskKind,
start: Date,
end: Date,
in context: ModelContext
) throws -> TodoTask {
let (workspace, me) = try SeedService.identity(in: context)
let target = WorkspaceScope.creationTarget(in: context, fallback: workspace)
let task = TodoTask(
workspaceID: target.id,
title: title,
kind: kind,
startAt: start,
endAt: max(end, start.addingTimeInterval(15 * 60)),
createdByID: me.id
)
context.insert(task)
try context.save()
NotificationService.shared.sync(task: task)
return task
}
}
15 changes: 2 additions & 13 deletions CalenTask/Features/Calendar/CalendarActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,8 @@ enum CalendarActions {
/// apre l'editor (i dettagli si mettono lì).
static func createTask(start: Date, end: Date, in context: ModelContext, router: AppRouter) {
do {
let (workspace, me) = try SeedService.identity(in: context)
let target = WorkspaceScope.creationTarget(in: context, fallback: workspace)
let task = TodoTask(
workspaceID: target.id,
title: "Nuova attività",
kind: .task,
startAt: start,
endAt: max(end, start.addingTimeInterval(15 * 60)),
createdByID: me.id
)
context.insert(task)
context.saveOrLog()
NotificationService.shared.sync(task: task)
let task = try TaskFactory.createScheduled(
title: "Nuova attività", kind: .task, start: start, end: end, in: context)
#if os(macOS)
router.inspect(taskID: task.id)
#else
Expand Down
16 changes: 4 additions & 12 deletions CalenTask/Features/Calendar/CalendarScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1384,18 +1384,10 @@ struct CalendarScreen: View {

private func createBlock(at slot: Date) {
do {
let (workspace, me) = try SeedService.identity(in: modelContext)
let target = WorkspaceScope.creationTarget(in: modelContext, fallback: workspace)
let event = TodoTask(
workspaceID: target.id,
title: "Nuovo evento",
kind: .event,
startAt: slot,
endAt: slot.addingTimeInterval(TimeInterval(slotDuration * 60)),
createdByID: me.id
)
modelContext.insert(event)
try modelContext.save()
let event = try TaskFactory.createScheduled(
title: "Nuovo evento", kind: .event, start: slot,
end: slot.addingTimeInterval(TimeInterval(slotDuration * 60)),
in: modelContext)
showsSlotResults = false
selectedDay = slot.startOfDay
router.open(taskID: event.id) // dettaglio subito, per dargli un nome
Expand Down
Loading