Skip to content

Instantly share code, notes, and snippets.

@yuraist
Created February 12, 2020 11:46
Show Gist options
  • Save yuraist/e015bf00bdae5e997cd03f950980e1e1 to your computer and use it in GitHub Desktop.
Save yuraist/e015bf00bdae5e997cd03f950980e1e1 to your computer and use it in GitHub Desktop.
import SwiftUI
class Task: ObservableObject, Identifiable {
let id = UUID()
var text: String
var done: Bool
init(text: String, done: Bool = false) {
self.text = text
self.done = done
}
}
class TasksViewModel: ObservableObject {
@Published var list = [
Task(text: "This class 'SwiftUI.AccessibilityNode' is not a known"),
Task(text: "This class 'SwiftUI.AccessibilityNode' is not a known"),
Task(text: "This class 'SwiftUI.AccessibilityNode' is not a known"),
Task(text: "This class 'SwiftUI.AccessibilityNode' is not a known"),
Task(text: "This class 'SwiftUI.AccessibilityNode' is not a known")
]
}
struct TasksView: View {
@ObservedObject var tasks = TasksViewModel()
let checkmarkImageName = "checkmark.circle.fill"
let circleImageName = "circle"
var body: some View {
NavigationView() {
VStack {
ScrollView {
VStack {
ForEach(tasks.list) { task in
Button(action: {
task.done.toggle()
print(task.done)
}) {
HStack {
Image(systemName: task.done ? self.checkmarkImageName : self.circleImageName)
.font(.system(size: 22))
.foregroundColor(Color(UIColor.systemGray2))
.padding(.trailing, 8)
Text(task.text)
.foregroundColor(task.done ? Color(UIColor.systemGray2) : .black)
.strikethrough(task.done, color: Color(UIColor.systemGray2))
Spacer()
}
.padding()
.padding(.vertical, 8)
.frame(maxWidth: .infinity)
.background(Color(UIColor.systemGray6))
.cornerRadius(10)
.padding(.horizontal)
}
}
}
}
Button(action: {}) {
HStack {
Image(systemName: "plus")
Text("Add a task")
}
.font(.body)
.foregroundColor(.black)
.padding()
.frame(maxWidth: .infinity)
.background(Color(UIColor.systemGray6))
.cornerRadius(10)
.padding([.horizontal, .bottom])
}
}
.navigationBarTitle(Text("Tasks"))
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
TasksView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment