Implemented the NewTaskView view with its respective view model.

This commit is contained in:
2026-02-09 18:44:15 +01:00
parent 7c63e0d8f8
commit 1ec6aa5297
5 changed files with 497 additions and 9 deletions
+72
View File
@@ -0,0 +1,72 @@
{
"sourceLanguage" : "en",
"strings" : {
"view.new-task.navigation.title" : {
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Create a new todo"
}
}
}
},
"view.new-task.placeholder.due-date" : {
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Select a due date for you task"
}
}
}
},
"view.new-task.placeholder.note" : {
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Add extra notes to your task (if needed)"
}
}
}
},
"view.new-task.placeholder.title" : {
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Give a title to your task"
}
}
}
},
"view.new-task.section.due-date" : {
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Due Date"
}
}
}
},
"view.new-task.section.task" : {
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Task"
}
}
}
}
},
"version" : "1.1"
}
@@ -0,0 +1,81 @@
import SwiftData
import SwiftUI
extension NewTaskView {
/// View model for managing the logic for creating todo tasks.
///
/// This observable class encapsulates the state and business logic for creating new todo tasks.
/// It follows the MVVM pattern to separate presentation logic from the view layer.
///
/// The view model manages:
/// - User input for task title, note, and due date
/// - Form validation (ensuring title is not empty)
/// - Saving the todo to SwiftData model context
@Observable
class ViewModel {
// MARK: Properties
/// The title of the todo task.
///
/// This is a required field. The save button is disabled when this is empty.
var title: String
/// A note providing additional details about the task, if required.
///
/// If left empty, the note will be stored as `nil` in the model.
var note: String
/// The due date for the todo task.
///
/// Defaults to the current date when the view model is initialized.
var due: Date
// MARK: Initialization
/// Initializes a new view model instance with default values.
///
/// The view model is initialized with:
/// - Empty title
/// - Empty note
/// - Current date as due date
init() {
title = ""
note = ""
due = .init()
}
// MARK: Computed
/// Whether the confirmation button should be enabled.
///
/// Returns `true` when the title is not empty after trimming whitespace, `false` otherwise.
var canConfirm: Bool {
!title.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
}
// MARK: Functions
/// Saves a new todo task to the SwiftData model context.
///
/// This function creates a new `Todo` instance with the current property values and inserts it into the provided model context.
/// The title and note are trimmed of leading and trailing whitespace before saving.
/// The `note` property value is only included if it's not empty after trimming.
///
/// - Parameter modelContext: The SwiftData model context to insert a new todo task.
func save(to modelContext: ModelContext) {
let trimmedTitle = title.trimmingCharacters(in: .whitespacesAndNewlines)
let trimmedNote = note.trimmingCharacters(in: .whitespacesAndNewlines)
let todo = Todo(
title: trimmedTitle,
note: trimmedNote.isEmpty ? nil : trimmedNote,
due: due
)
modelContext.insert(todo)
}
}
}
+105
View File
@@ -0,0 +1,105 @@
import SwiftData
import SwiftUI
/// A view for creating new todo tasks.
///
/// This view provides a form interface for users to input todo task details including:
/// - *Title* (required)
/// - *Note* (optional)
/// - *Due date* (required)
///
/// The view uses a dedicated view model (`ViewModel`) to manage state and business logic, following the MVVM pattern.
/// When the user saves, the todo is inserted into the SwiftData model context and the view is dismissed afterwards.
struct NewTaskView: View {
// MARK: Properties
/// Environment value for dismissing this view.
@Environment(\.dismiss)
private var dismiss
/// SwiftData model context for saving todo tasks.
@Environment(\.modelContext)
private var modelContext
/// The view model managing the state and logic for this view.
@State
private var viewModel: ViewModel
// MARK: Initialization
/// Initializes a new task view with a fresh view model instance.
init() {
_viewModel = State(initialValue: .init())
}
// MARK: Body
/// The content and behavior of the view.
///
/// Displays a navigation stack containing a form with sections for:
/// - Task details (title and note fields)
/// - Due date picker with graphical style
///
/// The toolbar includes:
/// - Cancel button to dismiss without saving
/// - Confirm button to persist the todo and dismiss (disabled when title is empty)
var body: some View {
NavigationStack {
Form {
Section("view.new-task.section.task") {
TextField(
"view.new-task.placeholder.title",
text: $viewModel.title
)
.autocapitalization(.none)
.autocorrectionDisabled(false)
.keyboardType(.asciiCapable)
TextField(
"view.new-task.placeholder.note",
text: $viewModel.note,
axis: .vertical
)
.autocapitalization(.none)
.autocorrectionDisabled(false)
.keyboardType(.asciiCapable)
.lineLimit(3 ... 6)
}
Section("view.new-task.section.due-date") {
DatePicker(
"view.new-task.placeholder.due-date",
selection: $viewModel.due,
displayedComponents: [.date]
)
.datePickerStyle(.graphical)
}
}
.navigationTitle("view.new-task.navigation.title")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button(role: .cancel) {
dismiss()
}
}
ToolbarItem(placement: .confirmationAction) {
Button(role: .confirm) {
viewModel.save(to: modelContext)
dismiss()
}
.disabled(!viewModel.canConfirm)
}
}
}
}
}
// MARK: - Previews
#Preview("New todo task") {
NewTaskView()
.modelContainer(for: Todo.self, inMemory: true)
}