This PR contains the work done to improve the app layout for max iPhones and iPads by levering on the navigation split view. Plus, the floating button to add todo tasks was improved and integrated better into the layout and a critical bug occurring on max iPhones and iPads was fixed. Reviewed-on: rock-n-code/todos-app#10 Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
79 lines
2.8 KiB
Swift
79 lines
2.8 KiB
Swift
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
|
|
}
|
|
|
|
}
|
|
}
|