Files
todos/App/Sources/View Models/ContentViewModel.swift
T

79 lines
2.8 KiB
Swift
Raw Normal View History

2026-02-09 17:53:40 +00:00
import SwiftData
import SwiftUI
extension ContentView {
/// View model for managing the logic for the content view.
///
/// This observable class encapsulates the state and business logic for the main content view.
/// It follows the MVVM pattern to separate presentation logic from the view layer.
///
/// The view model manages:
/// - Presentation state for the new task view within a sheet.
/// - Selected task group for the navigation split view.
2026-02-09 17:53:40 +00:00
@Observable
2026-02-10 02:22:41 +00:00
final class ViewModel {
2026-02-09 17:53:40 +00:00
// MARK: Properties
/// Whether the new task view should be shown.
var newTaskShown: Bool
/// The currently selected todo task group in the navigation split view.
///
/// When a user selects a group from the sidebar, this property is updated
/// to reflect the selection. The detail view displays content based on this value.
var group: TodoGroup?
2026-02-09 17:53:40 +00:00
// MARK: Initialization
/// Initializes a new view model instance with default values.
///
/// The view model is initialized with:
/// - New task view hidden
/// - No task group selected
2026-02-09 17:53:40 +00:00
init() {
newTaskShown = false
group = nil
}
// MARK: Computed
/// Determines whether the new task button should be shown in the detail view.
///
/// This prevents showing the add button when viewing completed tasks,
/// as users should not create new tasks in the completed state.
var showNewTaskButtonInDetails: Bool {
group != nil
&& group != .completed
2026-02-09 17:53:40 +00:00
}
// MARK: Functions
/// Shows the new task view by setting the presentation state to true.
///
/// This method triggers the presentation of a sheet where users can create a new task.
/// Called when the floating action button is tapped.
2026-02-09 17:53:40 +00:00
func showNewTask() {
newTaskShown = true
}
/// Determines whether the new task button should be shown in the sidebar.
///
/// The button appears in the sidebar only on compact horizontal size class devices
/// (typically iPhones in portrait mode). This provides a way to create tasks when
/// the sidebar is displayed as a full screen view.
///
/// - Parameter horizontalSizeClass: The current horizontal size class of the environment.
/// - Returns: `true` if the button should be shown in the sidebar, `false` otherwise.
func showNewTaskButtonInSidebar(when horizontalSizeClass: UserInterfaceSizeClass?) -> Bool {
guard let horizontalSizeClass else {
return false
}
return horizontalSizeClass == .compact
}
2026-02-09 17:53:40 +00:00
}
}