Files
luto/Luto/UI/MainView.swift

111 lines
3.7 KiB
Swift
Raw Permalink Normal View History

2023-07-13 11:56:17 +02:00
//
// ContentView.swift
// Luto
//
// Created by Pierre Boulc'h on 04/07/2023.
//
import SwiftUI
import AppKit
struct MainView: View {
2023-07-13 13:02:39 +02:00
private enum FocusStateField: Hashable{
case titleField
case descriptionField
}
2023-07-19 13:01:33 +02:00
@ObservedObject var viewModel: TaskViewModel
2023-07-13 11:56:17 +02:00
@State var taskTitle = ""
@State var taskDescription = ""
@State var showAdditionnalFields = false
2023-07-13 13:02:39 +02:00
@FocusState private var focusState: FocusStateField?
2023-07-13 11:56:17 +02:00
var body: some View {
VStack {
Text("Mes tâches")
2023-07-13 13:02:39 +02:00
ScrollView {
LazyVStack(alignment: .leading) {
2023-07-19 13:01:33 +02:00
ForEach(Array(viewModel.allTask.enumerated()), id: \.offset) { index, task in
2023-07-13 13:02:39 +02:00
HStack {
VStack(alignment: .leading) {
Text("\(task.title)").font(.system(size: 16))
2023-07-19 13:01:33 +02:00
if !task.body.isEmpty {
Text("\(task.body)")
2023-07-13 13:02:39 +02:00
}
}
.padding(EdgeInsets(top: 0, leading: 12, bottom: 0, trailing: 8))
Spacer()
Button(action: {
removeTask(index: index)
}, label: {
Image(systemName: "trash")
}).buttonStyle(PlainButtonStyle()).padding(12)
.foregroundColor(.accentColor)
}.border(width: 5, edges: [.leading], color: .accentColor)
}
2023-07-13 11:56:17 +02:00
}
}
Spacer()
2023-07-13 13:02:39 +02:00
Divider()
2023-07-13 11:56:17 +02:00
HStack {
2023-07-13 13:02:39 +02:00
TextField("Titre de la tâche ...", text: $taskTitle).focused($focusState, equals: .titleField).onChange(of: focusState) { newFocusState in
if newFocusState != .titleField {
2023-07-13 11:56:17 +02:00
withAnimation {
showAdditionnalFields = true
2023-07-13 13:02:39 +02:00
focusState = .descriptionField
2023-07-13 11:56:17 +02:00
}
}
}.onSubmit {
addTask()
2023-07-19 13:01:33 +02:00
}.onAppear {
focusState = .titleField
2023-07-13 11:56:17 +02:00
}
Button(action: {
addTask()
}, label: {
Image(systemName: "plus")
}).buttonStyle(PlainButtonStyle()).padding(12)
.background(Color.accentColor)
.foregroundColor(.black)
.cornerRadius(24)
}.textFieldStyle(OvalTextFieldStyle())
if showAdditionnalFields {
2023-07-13 13:02:39 +02:00
TextField("Description ...", text: $taskDescription).focused($focusState, equals: .descriptionField).textFieldStyle(OvalTextFieldStyle()).padding(EdgeInsets(top: 8, leading: 0, bottom: 0, trailing: 0)).onSubmit {
addTask()
}
2023-07-19 13:01:33 +02:00
} else {
Button(action: {
showAdditionnalFields = true
}, label: {
Image("arrow-down").tint(Color.white)
}).buttonStyle(PlainButtonStyle())
2023-07-13 11:56:17 +02:00
}
}
.padding()
.frame(width: 500, height: 500)
}
func addTask() {
if !taskTitle.isEmpty {
withAnimation {
2023-07-19 13:01:33 +02:00
viewModel.addTask(name: taskTitle, body: taskDescription)
2023-07-13 11:56:17 +02:00
}
taskTitle = ""
2023-07-13 13:02:39 +02:00
taskDescription = ""
showAdditionnalFields = false
focusState = .titleField
2023-07-13 11:56:17 +02:00
}
}
func removeTask(index: Int) {
2023-07-19 13:01:33 +02:00
withAnimation {
viewModel.deleteTask(at: IndexSet([index]))
2023-07-13 11:56:17 +02:00
}
}
}