Created
December 24, 2020 21:17
-
-
Save mrackwitz/58ca40bab556e13690fcf451b698b981 to your computer and use it in GitHub Desktop.
Christmas Todo List App
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 SwiftUI | |
// MARK: - App | |
@main | |
struct MyApp: App { | |
var body: some Scene { | |
WindowGroup { | |
ContentView() | |
} | |
} | |
} | |
// MARK: - Model | |
enum TodoListImplementations: String, Identifiable, CaseIterable { | |
case byOwnId | |
case viaIndex | |
case withDedicatedViews | |
case withDedicatedViewsAndID | |
var id: String { rawValue } | |
} | |
struct ToDoItem: Identifiable { | |
static var ids = AnyIterator((0...).makeIterator()) | |
var id = ToDoItem.ids.next() | |
var name: String | |
} | |
// MARK: - View | |
struct ContentView: View { | |
@State var implementation: TodoListImplementations = .byOwnId | |
@State var items: [ToDoItem] = [ | |
ToDoItem(name: "π― Light candles"), | |
ToDoItem(name: "π Put up tree"), | |
ToDoItem(name: "π Wrap gifts"), | |
ToDoItem(name: "π Close Xcode"), | |
] | |
var body: some View { | |
VStack(alignment: .leading, spacing: 32) { | |
Text("Christmas ToDo List") | |
.font(.title) | |
.multilineTextAlignment(.leading) | |
Picker("Implementation", selection: $implementation) { | |
ForEach(TodoListImplementations.allCases) { impl in | |
Text(impl.rawValue).tag(impl) | |
} | |
} | |
switch implementation { | |
case .byOwnId: | |
ToDoListViewByOwnId(items: $items) | |
case .viaIndex: | |
ToDoListViewByIndex(items: $items) | |
case .withDedicatedViews: | |
ToDoListViewWithDedicatedViews(items: $items) | |
case .withDedicatedViewsAndID: | |
ToDoListViewWithDedicatedViewsAndID(items: $items) | |
} | |
Button(action: reverse) { | |
Text("Reverse") | |
} | |
} | |
.padding(16) | |
} | |
func reverse() { | |
items = items.reversed() | |
} | |
} | |
struct ToDoListViewByOwnId: View { | |
@Binding var items: [ToDoItem] | |
var body: some View { | |
VStack(spacing: 32) { | |
ForEach(items) { item in | |
RowView(item: item) | |
} | |
} | |
} | |
} | |
struct ToDoListViewByIndex: View { | |
@Binding var items: [ToDoItem] | |
var body: some View { | |
VStack(spacing: 32) { | |
ForEach(Array(items.enumerated()), id: \.0) { item in | |
RowView(item: item.1) | |
} | |
} | |
} | |
} | |
struct ToDoListViewWithDedicatedViews: View { | |
@Binding var items: [ToDoItem] | |
var body: some View { | |
VStack(spacing: 32) { | |
RowView(item: items[0]) | |
RowView(item: items[1]) | |
RowView(item: items[2]) | |
RowView(item: items[3]) | |
} | |
} | |
} | |
struct ToDoListViewWithDedicatedViewsAndID: View { | |
@Binding var items: [ToDoItem] | |
var body: some View { | |
VStack(spacing: 32) { | |
RowView(item: items[0]).id(items[0].id) | |
RowView(item: items[1]).id(items[1].id) | |
RowView(item: items[2]).id(items[2].id) | |
RowView(item: items[3]).id(items[3].id) | |
} | |
} | |
} | |
struct RowView: View { | |
var item: ToDoItem | |
@State var isCompleted: Bool = false | |
var body: some View { | |
HStack { | |
Text(item.name) | |
.font(.body) | |
Spacer() | |
Toggle("", isOn: $isCompleted) | |
} | |
.padding(16) | |
.background( | |
RoundedRectangle(cornerRadius: 16) | |
.fill(Color.black.opacity(0.1)) | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment