Updated the ContentView view in the app target to support navigation split view and to show floating new task button on both sidebar and details views when required.
This commit is contained in:
@@ -10,6 +10,7 @@ extension ContentView {
|
||||
///
|
||||
/// 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 {
|
||||
|
||||
@@ -18,22 +19,60 @@ extension ContentView {
|
||||
/// 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
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user