Created
July 14, 2022 16:47
-
-
Save pitt500/b8ae65ac6c12cd9e0e89122bee49b621 to your computer and use it in GitHub Desktop.
This file contains 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 | |
struct Todo: Identifiable { | |
let id: UUID | |
var title: String = "Untitled" | |
var isComplete: Bool = false | |
static var sample: [Todo] { | |
[ | |
Todo( | |
id: UUID(), | |
title: "Milk", | |
isComplete: false | |
), | |
Todo( | |
id: UUID(), | |
title: "Buy eggs", | |
isComplete: false | |
), | |
Todo( | |
id: UUID(), | |
title: "Clean room", | |
isComplete: true | |
) | |
] | |
} | |
} | |
struct ContentView: View { | |
@State private var todos = Todo.sample | |
var body: some View { | |
NavigationView { | |
List { | |
ForEach(todos.indices, id: \.self) { index in | |
HStack { | |
Button { | |
todos[index].isComplete.toggle() | |
withAnimation { | |
sort() | |
} | |
} label: { | |
Image(systemName: todos[index].isComplete ? "checkmark.square" : "square") | |
} | |
Text(todos[index].title) | |
} | |
.foregroundColor(todos[index].isComplete ? .gray : nil) | |
} | |
} | |
.toolbar { | |
ToolbarItem(placement: .navigationBarTrailing) { | |
Button { | |
withAnimation { | |
todos.append(Todo(id: UUID())) | |
} | |
} label: { | |
Text("Add") | |
} | |
} | |
} | |
.navigationTitle("Todo List") | |
} | |
} | |
func sort() { | |
todos.sort { !$0.isComplete && $1.isComplete } | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you!
I need to double check the animation behavior tho.