Skip to content

Instantly share code, notes, and snippets.

@pitt500
Created July 14, 2022 16:47
Show Gist options
  • Save pitt500/b8ae65ac6c12cd9e0e89122bee49b621 to your computer and use it in GitHub Desktop.
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()
}
}
@mgsulaiman
Copy link

mgsulaiman commented Jul 15, 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()
    }
}

@pitt500
Copy link
Author

pitt500 commented Jul 18, 2022

import SwiftUI

struct Todo: Identifiable, Equatable, Hashable {
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 TodoView2: View {
@State private var todos = Todo.sample

var 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!

@pitt500
Copy link
Author

pitt500 commented Jul 18, 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()
}
}

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