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() | |
} | |
} |
//
// ContentView.swift
// testproject
//
// Created by mgsulaiman on 15/07/2022.
//import SwiftUI
struct Todo: Identifiable , Equatable {
let id: UUID
var title: String = "Untitled"
var isComplete: Bool = falsestatic 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.samplevar body: some View { NavigationView { List { ForEach(Array(todos.enumerated()) , id: \.1.id) { index , item in HStack { Button { todos[index].isComplete.toggle() sort() } label: { Image(systemName: item.isComplete ? "checkmark.square" : "square") } Text(item.title) } .foregroundColor(todos[index].isComplete ? .gray : nil) } }.animation(.default, value: todos) .toolbar { ToolbarItem(placement: .navigationBarTrailing) { Button { 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()
}
}
Thank you!
I need to double check the animation behavior tho.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you!