This PR contains the work done to fix the behavior of the keyboard dismissal for the *New task* and *Task list* views. Reviewed-on: rock-n-code/todos-app#12 Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
128 lines
3.9 KiB
Swift
128 lines
3.9 KiB
Swift
import SwiftData
|
|
import SwiftUI
|
|
|
|
/// A view for creating new todo tasks with an intuitive form interface.
|
|
///
|
|
/// This view presents a modal sheet that allows users to create new todo tasks by providing
|
|
/// task details through a structured form. The view integrates with SwiftData for persistence
|
|
/// and implements smart keyboard focus management for an enhanced input experience.
|
|
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
|
|
|
|
/// Tracks which text field currently has keyboard focus.
|
|
@FocusState
|
|
private var inputFocus: InputFieldFocus?
|
|
|
|
/// 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.
|
|
///
|
|
/// Creates a new `NewTaskViewModel` with default values:
|
|
/// - Empty title
|
|
/// - Empty note
|
|
/// - Due date set to current date
|
|
///
|
|
/// The view model is stored as `@State` to ensure it persists across view updates
|
|
/// and maintains its state throughout the view's lifecycle.
|
|
init() {
|
|
_viewModel = State(initialValue: .init())
|
|
}
|
|
|
|
// MARK: Body
|
|
|
|
/// The content and behavior of the view.
|
|
///
|
|
/// Constructs a form interface wrapped in the custom content container that provides
|
|
/// tap-to-dismiss keyboard functionality.
|
|
var body: some View {
|
|
Content(inputFocus: $inputFocus) {
|
|
Form {
|
|
Section("view.new-task.section.task") {
|
|
TextField(
|
|
"view.new-task.placeholder.title",
|
|
text: $viewModel.title,
|
|
axis: .vertical
|
|
)
|
|
.focused(
|
|
$inputFocus,
|
|
equals: .title
|
|
)
|
|
.onNewlineFocus(
|
|
$viewModel.title,
|
|
to: .note,
|
|
on: $inputFocus
|
|
)
|
|
|
|
TextField(
|
|
"view.new-task.placeholder.note",
|
|
text: $viewModel.note,
|
|
axis: .vertical
|
|
)
|
|
.focused(
|
|
$inputFocus,
|
|
equals: .note
|
|
)
|
|
.onEmptyNewlineResignFocus(
|
|
text: $viewModel.note,
|
|
from: $inputFocus
|
|
)
|
|
}
|
|
.autocapitalization(.sentences)
|
|
.autocorrectionDisabled(false)
|
|
.keyboardType(.default)
|
|
|
|
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",
|
|
traits: .emptyContainer
|
|
) {
|
|
NavigationStack {
|
|
NewTaskView()
|
|
}
|
|
}
|