Created
June 6, 2023 00:38
-
-
Save zwaldowski/ea62eb4d05ab6f6a38df277d28ffa4ac to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftData | |
import SwiftUI | |
@main | |
struct Much_TodoApp: App { | |
var body: some Scene { | |
WindowGroup { | |
ContentView() | |
} | |
.modelContainer(for: TodoItem.self) | |
} | |
} | |
struct ContentView: View { | |
@Environment(\.modelContext) private var modelContext | |
@Query(sort: \.created, order: .reverse) private var items: [TodoItem] | |
var body: some View { | |
NavigationStack { | |
List { | |
ForEach(items) { (@Bindable item) in | |
LabeledContent { | |
Toggle("Completed", isOn: $item.isDone) | |
.labelsHidden() | |
} label: { | |
TextField("Item Title", text: $item.text, prompt: Text("New Item")) | |
Text(item.created, format: .dateTime.month().day().hour().minute()) | |
} | |
} | |
} | |
.navigationTitle("Much Todo") | |
.toolbar { | |
ToolbarItem { | |
Button { | |
withAnimation { | |
modelContext.insert(TodoItem()) | |
} | |
} label: { | |
Label("Add Item", systemImage: "plus") | |
} | |
} | |
} | |
} | |
} | |
} | |
@Model | |
class TodoItem { | |
var created: Date = Date.now | |
var text: String = "" | |
var isDone: Bool = false | |
init() {} | |
} | |
#Preview { | |
ContentView() | |
.modelContainer(for: TodoItem.self, inMemory: true) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment