Last active
November 15, 2024 18:55
-
-
Save Shubham0812/5954b5bde235fd341d60efc1515babf8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// TaskAddingView.swift | |
// ToDo_UI | |
// | |
// Created by Shubham on 12/11/24. | |
// | |
import SwiftUI | |
struct TaskAddingView: View { | |
// MARK: - Variables | |
@Environment(\.managedObjectContext) private var viewContext | |
@Environment(MainViewModel.self) var mainViewModel | |
@Environment(TaskManager.self) var taskManager | |
@State var taskName = "" | |
@State var isPriority = false | |
@State var viewAppeared: Bool = false | |
let animationDuration: TimeInterval = 0.35 | |
// MARK: - Views | |
var body: some View { | |
ZStack { | |
Color.background | |
.ignoresSafeArea() | |
VStack(alignment: .leading) { | |
HStack { | |
Spacer() | |
.frame(width: 44, height: 44) | |
Spacer() | |
Text(mainViewModel.addTask ? "New Task" : "Add Task") | |
.font(Nocturne.bold.font(size: 28)) | |
.opacity(mainViewModel.addTask ? 1 : 0.7) | |
Spacer() | |
Button { | |
withAnimation(.smooth(duration: animationDuration)) { | |
mainViewModel.addTask.toggle() | |
} | |
} label: { | |
Image(systemName: mainViewModel.addTask ? "multiply" : "plus") | |
.font(.system(size: 25, weight: .semibold)) | |
} | |
.opacity(mainViewModel.addTask ? 1 : 0.7) | |
.buttonStyle(.plain) | |
.frame(width: 44, height: 44) | |
} | |
Text("Task Name") | |
.font(Nocturne.semibold.font(size: 19)) | |
.padding(.top, 32) | |
CustomTextFieldView(toBindValue: $taskName, viewAppeared: $viewAppeared, placeHolder: "Enter Task Here *") | |
.padding(.top, 2) | |
Spacer() | |
HStack { | |
Spacer() | |
Button { | |
withAnimation(.smooth) { | |
taskManager.addTask(name: taskName, context: viewContext) | |
mainViewModel.addTask.toggle() | |
} | |
UIApplication.shared.endEditing() | |
} label: { | |
Text("Add Task") | |
.font(Montserrat.semibold.font(size: 22)) | |
} | |
.padding(.horizontal, 16) | |
.padding(.vertical, 12) | |
.disabled(taskName.isEmpty ? true : false) | |
.overlay { | |
RoundedRectangle(cornerRadius: 12) | |
.stroke(lineWidth: 2) | |
.opacity(taskName.isEmpty ? 0.4 : 1) | |
.animation(.easeInOut, value: taskName) | |
} | |
.buttonStyle(.plain) | |
.frame(height: 52) | |
Spacer() | |
} | |
} | |
.padding(18) | |
.padding(.horizontal, 8) | |
.onAppear() { | |
viewAppeared = true | |
} | |
} | |
} | |
} | |
#Preview { | |
VStack { | |
TaskAddingView() | |
.environment(MainViewModel()) | |
.environment(TaskManager()) | |
.colorInvert() | |
.frame(height: 350) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment