That is my projects link https://github.com/m3rtkoksal/TaskManager
This is my TaskListView
This is my NewTaskView
Unfortunately when I tap one of the items in scrollview again I see an empty NewTaskView. I should see NewTaskView with selectedTask datas instead.
That is my NewTaskView
struct NewTaskView: View {
@Environment(\.presentationMode) private var presentationMode
@StateObject private var obser = observer()
@State var taskTitle = ""
@State var taskFrom = ""
@State var taskFromDate = Date()
@State var taskToDate = Date()
@State var taskTo = ""
@State var taskNote = ""
@EnvironmentObject var task: SelectedTask
var body: some View {
NavigationView {
VStack(alignment: .leading) {
Group {
Text("Task Title")
TextField("Title", text:$taskTitle)
Divider()
Text("From")
DatePicker("", selection: $taskFromDate, in: Date()..., displayedComponents: [.date, .hourAndMinute])
.labelsHidden()
.onReceive(Just(taskFromDate)) { data in
taskFrom = getDate(date: taskFromDate)
}
Divider()
} .padding(.horizontal, 10)
.font(Font.custom("SFCompactDisplay-Bold", size: 25))
.foregroundColor(.gray)
Group {
Text("To")
DatePicker("", selection: $taskToDate, in: Date()..., displayedComponents: [.date, .hourAndMinute])
.labelsHidden()
.onReceive(Just(taskToDate)) { data in
taskTo = getDate(date: taskToDate)
}
Divider()
Text("Note")
TextField("Note", text:$taskNote)
}.padding(.horizontal, 10)
.font(Font.custom("SFCompactDisplay-Bold", size: 25))
.foregroundColor(.gray)
Button(action: {
let taskDictionary = [
"title" : self.taskTitle,
"dateTo": self.taskTo,
"dateFrom" : self.taskFrom,
"text": self.taskNote
]
let docRef = Firestore.firestore().document("tasks/\(UUID().uuidString)")
docRef.setData(taskDictionary) { (error) in
if let error = error {
print("error = \(error)")
} else {
print("success")
self.taskTitle = ""
self.taskNote = ""
}
}
}, label: {
ZStack {
RoundedRectangle(cornerRadius: 25)
.foregroundColor(Color(#colorLiteral(red: 0.4274509804, green: 0.2196078431, blue: 1, alpha: 1)))
Text("Create task")
.foregroundColor(.white)
.font(.title)
.fontWeight(.bold)
}
.padding(.horizontal, 10)
.frame(width: UIScreen.main.bounds.width - 20, height: 90)
})
}
}
.navigationBarTitle("Create a task")
.font(Font.custom("SFCompactDisplay-Bold", size: 30))
}
}
And this is how I append selectedTask and call NewTaskView()
import SwiftUI
struct TaskFrameView: View {
@ObservedObject private var obser = observer()
var body: some View {
VStack(alignment: .leading){
Text("Today task")
.padding()
ScrollViewTask()
}
}
}
struct ScrollViewTask: View {
@EnvironmentObject var selectedTask : SelectedTask
@State var shown: Bool = false
@ObservedObject private var obser = observer()
var body: some View {
ScrollView(.vertical) {
VStack {
ForEach(self.obser.tasks) { task in
TaskElementView(task:task)
.onTapGesture {
self.shown.toggle()
self.selectedTask.appendNewTask(task: task)
}
}
}
}
.onAppear {
self.obser.fetchData()
}
.fullScreenCover(isPresented: $shown, content: {
NewTaskView()
.environmentObject(selectedTask)
})
}
}
How should I modify NewTaskView to be able to see selectedTask datas when an item is selected on Scrollview?
I have tried like below but I am getting index out of range error when I try to add new item. Plus there must be a better way than doing it with if else
VStack(alignment: .leading) {
Group {
Text("Task Title")
if task.item[0].title == "" {
TextField("Title", text:$taskTitle)
} else {
TextField(task.item[0].title, text: $taskTitle)
}
You have to add these code in
SceneDelegate.swift