-
-
Save pitt500/b8ae65ac6c12cd9e0e89122bee49b621 to your computer and use it in GitHub Desktop.
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() | |
} | |
} |
asos-danieltavares
commented
Jul 14, 2022
//
// 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 = 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(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()
}
}
import SwiftUI
struct Todo: Identifiable, Equatable, Hashable {
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 TodoView2: View {
@State private var todos = Todo.samplevar body: some View {
NavigationView {
List {
ForEach(Array(todos.enumerated()), id: .element) { index, item in
HStack {
Button {
todos[index].isComplete.toggle()
sort()
} label: {
Image(systemName: todos[index].isComplete ? "checkmark.square" : "square")
}Text(item.title) } .foregroundColor(item.isComplete ? .gray : nil) } } .toolbar { ToolbarItem(placement: .navigationBarTrailing) { Button { todos.append(Todo(id: UUID())) } label: { Text("Add") } } } .navigationTitle("Todo List") }.animation(.default, value: todos)
}
func sort() {
todos.sort { !$0.isComplete && $1.isComplete }
}}
struct TodoView2_Previews: PreviewProvider {
static var previews: some View {
TodoView2()
}
}
Thank you!
//
// 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.