New task feature #2
@@ -0,0 +1,39 @@
|
|||||||
|
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.
|
||||||
|
@Observable
|
||||||
|
class ViewModel {
|
||||||
|
|
||||||
|
// MARK: Properties
|
||||||
|
|
||||||
|
/// Whether the new task view should be shown.
|
||||||
|
var newTaskShown: Bool
|
||||||
|
|
||||||
|
// MARK: Initialization
|
||||||
|
|
||||||
|
/// Initializes a new view model instance with default values.
|
||||||
|
///
|
||||||
|
/// The view model is initialized with:
|
||||||
|
/// - New task view hidden
|
||||||
|
init() {
|
||||||
|
newTaskShown = false
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: Functions
|
||||||
|
|
||||||
|
/// Shows the new task view by setting the presentation state to true.
|
||||||
|
func showNewTask() {
|
||||||
|
newTaskShown = true
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,63 +1,52 @@
|
|||||||
import SwiftData
|
import SwiftData
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
|
/// The main content view of the app.
|
||||||
|
///
|
||||||
|
/// This view serves as the primary interface for managing todo tasks.
|
||||||
|
///
|
||||||
|
/// The view uses a dedicated view model (`ViewModel`) to manage state and business logic, following the MVVM pattern.
|
||||||
|
/// User interactions are handled through the view model, maintaining separation of concerns.
|
||||||
struct ContentView: View {
|
struct ContentView: View {
|
||||||
|
|
||||||
@Environment(\.modelContext)
|
// MARK: Properties
|
||||||
private var modelContext
|
|
||||||
|
|
||||||
@Query
|
/// The view model managing the state and logic for this view.
|
||||||
private var items: [Todo]
|
@State
|
||||||
|
private var viewModel = ViewModel()
|
||||||
|
|
||||||
|
// MARK: Body
|
||||||
|
|
||||||
|
/// The content and behavior of the view.
|
||||||
|
///
|
||||||
|
/// Displays a navigation view containing:
|
||||||
|
/// - A floating action button for adding new tasks
|
||||||
|
///
|
||||||
|
/// The view presents a sheet when `viewModel.newTaskShown` is true,
|
||||||
|
/// allowing users to create new todo tasks.
|
||||||
var body: some View {
|
var body: some View {
|
||||||
NavigationSplitView {
|
NavigationView {
|
||||||
List {
|
ZStack(alignment: .bottomTrailing) {
|
||||||
ForEach(items) { item in
|
List {}
|
||||||
NavigationLink {
|
|
||||||
Text("Item at \(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard))")
|
|
||||||
} label: {
|
|
||||||
Text(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.onDelete(perform: deleteItems)
|
|
||||||
}
|
|
||||||
#if os(macOS)
|
|
||||||
.navigationSplitViewColumnWidth(min: 180, ideal: 200)
|
|
||||||
#endif
|
|
||||||
.toolbar {
|
|
||||||
#if os(iOS)
|
|
||||||
ToolbarItem(placement: .navigationBarTrailing) {
|
|
||||||
EditButton()
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
ToolbarItem {
|
|
||||||
Button(action: addItem) {
|
|
||||||
Label("Add Item", systemImage: "plus")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} detail: {
|
|
||||||
Text("Select an item")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private func addItem() {
|
Button(action: viewModel.showNewTask) {
|
||||||
withAnimation {
|
Image(systemName: "plus")
|
||||||
let newItem = Todo(title: "New Todo", due: Date())
|
.frame(width: 48, height: 48)
|
||||||
modelContext.insert(newItem)
|
.foregroundStyle(.white)
|
||||||
}
|
}
|
||||||
}
|
.glassEffect(.regular.tint(.blue.opacity(0.8)).interactive())
|
||||||
|
.padding(.trailing, 20)
|
||||||
private func deleteItems(offsets: IndexSet) {
|
|
||||||
withAnimation {
|
|
||||||
for index in offsets {
|
|
||||||
modelContext.delete(items[index])
|
|
||||||
}
|
}
|
||||||
|
.navigationTitle("Todos")
|
||||||
|
}
|
||||||
|
.sheet(isPresented: $viewModel.newTaskShown) {
|
||||||
|
NewTaskView()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MARK: - Previews
|
||||||
|
|
||||||
#Preview {
|
#Preview {
|
||||||
ContentView()
|
ContentView()
|
||||||
.modelContainer(for: Todo.self, inMemory: true)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
import Foundation
|
||||||
|
import SwiftData
|
||||||
|
import Testing
|
||||||
|
|
||||||
|
@testable import Todos
|
||||||
|
|
||||||
|
@Suite("ContentView.ViewModel Tests")
|
||||||
|
struct ContentViewModelTests {
|
||||||
|
|
||||||
|
// MARK: Initialization Tests
|
||||||
|
|
||||||
|
@Test("View model initializes with default values")
|
||||||
|
func initialization() {
|
||||||
|
// GIVEN
|
||||||
|
// WHEN
|
||||||
|
let viewModel = ContentView.ViewModel()
|
||||||
|
|
||||||
|
// THEN
|
||||||
|
#expect(viewModel.newTaskShown == false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: - Functions Tests
|
||||||
|
|
||||||
|
@Test("showNewTask sets showNewTaskView to true")
|
||||||
|
func newTaskShown() {
|
||||||
|
// GIVEN
|
||||||
|
let viewModel = ContentView.ViewModel()
|
||||||
|
|
||||||
|
// WHEN
|
||||||
|
viewModel.showNewTask()
|
||||||
|
|
||||||
|
// THEN
|
||||||
|
#expect(viewModel.newTaskShown == true)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user