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. @Observable final class ViewModel { // 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? // 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 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 } // 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. 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 } } }