From 4e532014a042a109ef1eb716aec0ce89cfaa6127 Mon Sep 17 00:00:00 2001 From: Austin Lim Date: Tue, 12 Nov 2024 18:24:49 -0800 Subject: [PATCH 1/3] Added delete post button --- .../ViewLayer/ContentView.swift | 277 ++++++++++-------- .../Preview Content/ViewLayer/LoginView.swift | 3 + .../Preview Content/ViewLayer/adminView.swift | 2 +- .../ViewLayer/deletePostView.swift | 57 ++++ 4 files changed, 211 insertions(+), 128 deletions(-) create mode 100644 mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/deletePostView.swift diff --git a/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/ContentView.swift b/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/ContentView.swift index 17392ad..13b5767 100644 --- a/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/ContentView.swift +++ b/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/ContentView.swift @@ -48,142 +48,165 @@ struct ContentView: View { // Assign posts to the posts array self.posts = [post1, post2, post3] } - - var body: some View { - NavigationView{ - VStack { - //gets the list of posts as well as puts it in the order - List(posts, id: \.createdAt) { post in - VStack(alignment: .leading){ - Text(post.contents) - .font(.headline) - Text("Posted by: \(post.user.name)") - .font(.subheadline) - Text("Posted at: \(post.createdAt.formatted())") - .font(.subheadline) - - ForEach(post.comments, id: \.createdAt){ comment in - Text(comment.contents) - .font(.body) - .padding(.leading) - } + + var body: some View { + NavigationView{ + VStack { + //gets the list of posts as well as puts it in the order + List(posts, id: \.createdAt) { post in + VStack(alignment: .leading){ + HStack{ + Text(post.contents) + .font(.headline) + + Spacer() + } + .padding() + + if isAdmin{ + NavigationLink(destination: deletePostView()){ + Image(systemName: "trash") + .foregroundColor(.red) + .padding() + } + } + + - if isLoggedIn{ - //when the user is logged in they will see the comment button and can make a comment - NavigationLink(destination: newCommentView()) - { - Text("Add a comment").font(.caption).foregroundColor(.blue) + + Text(post.contents) + .font(.headline) + Text("Posted by: \(post.user.name)") + .font(.subheadline) + Text("Posted at: \(post.createdAt.formatted())") + .font(.subheadline) + + + ForEach(post.comments, id: \.createdAt){ comment in + Text(comment.contents) + .font(.body) + .padding(.leading) + } -// THIS IS ALL OLD CODE, I DID not want to delete it bc it helps see //thought process -// Button(action: { -// selectedPost = post -// showNewCommentForm.toggle() -// }){ -// Text("Add a comment") -// .font(.caption) -// .foregroundColor(.blue) -// } -// .sheet(isPresented: $showNewCommentForm) -// { -// VStack -// { -// Text("Add a comment").font(.title2) -// TextField("Enter comment", text: $newCommentString) -// .textFieldStyle(RoundedBorderTextFieldStyle()) -// .padding() -// Button("Submit") -// { -// -// } -// -// } -// } + + if isLoggedIn{ + //when the user is logged in they will see the comment button and can make a comment + NavigationLink(destination: newCommentView()) + { + Text("Add a comment").font(.caption).foregroundColor(.blue) + } + // THIS IS ALL OLD CODE, I DID not want to delete it bc it helps see //thought process + // Button(action: { + // selectedPost = post + // showNewCommentForm.toggle() + // }){ + // Text("Add a comment") + // .font(.caption) + // .foregroundColor(.blue) + // } + // .sheet(isPresented: $showNewCommentForm) + // { + // VStack + // { + // Text("Add a comment").font(.title2) + // TextField("Enter comment", text: $newCommentString) + // .textFieldStyle(RoundedBorderTextFieldStyle()) + // .padding() + // Button("Submit") + // { + // + // } + // + // } + // } + } + } } - } - if isLoggedIn{ - NavigationLink(destination: NewPostView()) - { - Text("Create New Post") + if isLoggedIn{ + NavigationLink(destination: NewPostView()) + { + Text("Create New Post") + } + .bold() + .padding(.top) + //OLD LOG OUT OR LOG IN CODE. Feel free to delete but is simpler, so can help //see thought process + // Button(action: { + // logout() + // }) { + // Text("Logout") + // .font(.headline) + // .foregroundColor(.blue) + // .padding() + // .frame(maxWidth: .infinity) + // .cornerRadius(10) + // } + // + // } - .bold() - .padding(.top) - //OLD LOG OUT OR LOG IN CODE. Feel free to delete but is simpler, so can help //see thought process -// Button(action: { -// logout() -// }) { -// Text("Logout") -// .font(.headline) -// .foregroundColor(.blue) -// .padding() -// .frame(maxWidth: .infinity) -// .cornerRadius(10) -// } -// -// + // if !isLoggedIn{ + // NavigationLink(destination: LoginView()) + // { + // Text("Login") + // } + // .bold() + // .padding(.top) + // } + } -// if !isLoggedIn{ -// NavigationLink(destination: LoginView()) -// { -// Text("Login") -// } -// .bold() -// .padding(.top) -// } - - } - //This is whatt allows us to have to login and logout in the top corner - .navigationTitle("Posts") - .navigationBarItems(trailing: HStack { - if !isLoggedIn { - NavigationLink(destination: LoginView()) { - Text("Login") - .font(.headline) - .foregroundColor(.blue) - } - } else { - Button(action: { - logout() - }) { - Text("Logout") - .font(.headline) - .foregroundColor(.blue) + //This is whatt allows us to have to login and logout in the top corner + .navigationTitle("Posts") + .navigationBarItems(trailing: HStack { + if !isLoggedIn { + NavigationLink(destination: LoginView()) { + Text("Login") + .font(.headline) + .foregroundColor(.blue) + } + } else { + Button(action: { + logout() + }) { + Text("Logout") + .font(.headline) + .foregroundColor(.blue) + } } - } - }) + }) + } + .onAppear{ + setupMockData() + } } - .onAppear{ - setupMockData() + func logout(){ + isLoggedIn = false } } - func logout(){ - isLoggedIn = false + + + #Preview { + ContentView() } -} - - -#Preview { - ContentView() -} - -// init(loginState: LoginState) { -// self._loginState = State(initialValue: loginState) -// } - -//#Preview { -// ContentView(loginState: .notLoggedIn) -//} + + // init(loginState: LoginState) { + // self._loginState = State(initialValue: loginState) + // } + + //#Preview { + // ContentView(loginState: .notLoggedIn) + //} + + // switch loginState { + // case .notLoggedIn: + // //LoginView(loginState: $loginState) + // ForumView(loginState: $loginState) + // case .userLoggedIn: + // UserView(loginState: $loginState) + // case .adminLoggedIn: + // AdminView(loginState: $loginState) + // } + // } + // .padding() + // } + //} -// switch loginState { -// case .notLoggedIn: -// //LoginView(loginState: $loginState) -// ForumView(loginState: $loginState) -// case .userLoggedIn: -// UserView(loginState: $loginState) -// case .adminLoggedIn: -// AdminView(loginState: $loginState) -// } -// } -// .padding() -// } -//} diff --git a/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/LoginView.swift b/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/LoginView.swift index be5595e..5cf22be 100644 --- a/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/LoginView.swift +++ b/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/LoginView.swift @@ -62,6 +62,7 @@ struct LoginView: View { .frame(maxWidth: .infinity) .cornerRadius(10) } + } .padding() } @@ -70,6 +71,8 @@ struct LoginView: View { //Actions for the button added here print("Username: \(username), Password: \(password)" ) } + + } #Preview { ContentView() diff --git a/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/adminView.swift b/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/adminView.swift index 7a4e4b5..052bcb4 100644 --- a/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/adminView.swift +++ b/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/adminView.swift @@ -5,7 +5,7 @@ // Created by Darien Aranda on 11/9/24. // -import SwiftUI +//import SwiftUI //struct AdminView: View { // @Binding var loginState: LoginState diff --git a/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/deletePostView.swift b/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/deletePostView.swift new file mode 100644 index 0000000..bb55308 --- /dev/null +++ b/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/deletePostView.swift @@ -0,0 +1,57 @@ +// +// deletePostView.swift +// miniProject-seniorDesign +// +// Created by Austin Lim on 11/11/24. +// + +import SwiftUI + +struct deletePostView: View{ + @Environment(\.presentationMode) var presentationMode + var body: some View{ + ZStack{ + Color.black.opacity(0.4) // Semi-transparent background to simulate a modal overlay + .edgesIgnoringSafeArea(.all) // Extends the overlay to cover the whole screen + + VStack(spacing: 20) { + Text("Are you sure you want to delete this post?") + .font(.title2) + .multilineTextAlignment(.center) + .padding() + HStack(spacing: 40){ + Button(action:{ + + }){ + Text("No") + .font(.headline) + .foregroundColor(.blue) + .padding() + .frame(width: 100) + .background(Color.white) + .cornerRadius(10) + .shadow(radius: 5) + } + Button(action:{ + + }){ + Text("Yes") + .font(.headline) + .foregroundColor(.blue) + .padding() + .frame(width: 100) + .background(Color.white) + .cornerRadius(10) + .shadow(radius: 5) + } + } + } + + + + } + } +} +#Preview { + ContentView() +} From 842db68b858b08399720e340dce60439d4d0756f Mon Sep 17 00:00:00 2001 From: Jacob Fernandez Date: Tue, 12 Nov 2024 19:33:40 -0800 Subject: [PATCH 2/3] added the delete button --- .../ViewLayer/ContentView.swift | 182 ++++++------------ 1 file changed, 64 insertions(+), 118 deletions(-) diff --git a/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/ContentView.swift b/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/ContentView.swift index 13b5767..9c693e2 100644 --- a/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/ContentView.swift +++ b/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/ContentView.swift @@ -49,140 +49,86 @@ struct ContentView: View { self.posts = [post1, post2, post3] } - var body: some View { - NavigationView{ - VStack { - //gets the list of posts as well as puts it in the order - List(posts, id: \.createdAt) { post in - VStack(alignment: .leading){ - HStack{ - Text(post.contents) - .font(.headline) - - Spacer() - } - .padding() - - if isAdmin{ - NavigationLink(destination: deletePostView()){ - Image(systemName: "trash") - .foregroundColor(.red) - .padding() - } - } + + var body: some View { + NavigationView { + VStack { + List(posts, id: \.createdAt) { post in + VStack(alignment: .leading) { + HStack { + VStack(alignment: .leading, spacing: 4) { + Text(post.contents) + .font(.headline) - - - - Text(post.contents) - .font(.headline) - Text("Posted by: \(post.user.name)") - .font(.subheadline) - Text("Posted at: \(post.createdAt.formatted())") - .font(.subheadline) - - - ForEach(post.comments, id: \.createdAt){ comment in - Text(comment.contents) - .font(.body) - .padding(.leading) + Text("Posted by: \(post.user.name)") + .font(.subheadline) + Text("Posted at: \(post.createdAt.formatted())") + .font(.subheadline) } - if isLoggedIn{ - //when the user is logged in they will see the comment button and can make a comment - NavigationLink(destination: newCommentView()) - { - Text("Add a comment").font(.caption).foregroundColor(.blue) + Spacer() + + if isAdmin { + NavigationLink(destination: deletePostView()) { + Image(systemName: "trash") + .foregroundColor(.red) } - // THIS IS ALL OLD CODE, I DID not want to delete it bc it helps see //thought process - // Button(action: { - // selectedPost = post - // showNewCommentForm.toggle() - // }){ - // Text("Add a comment") - // .font(.caption) - // .foregroundColor(.blue) - // } - // .sheet(isPresented: $showNewCommentForm) - // { - // VStack - // { - // Text("Add a comment").font(.title2) - // TextField("Enter comment", text: $newCommentString) - // .textFieldStyle(RoundedBorderTextFieldStyle()) - // .padding() - // Button("Submit") - // { - // - // } - // - // } - // } + .buttonStyle(BorderlessButtonStyle()) // Remove button background padding + .frame(width: 24, height: 24) // Adjust to fit icon size exactly } + } + ForEach(post.comments, id: \.createdAt) { comment in + Text(comment.contents) + .font(.body) + .padding(.leading) } - } - if isLoggedIn{ - NavigationLink(destination: NewPostView()) - { - Text("Create New Post") + + if isLoggedIn { + NavigationLink(destination: newCommentView()) { + Text("Add a comment") + .font(.caption) + .foregroundColor(.blue) + } } - .bold() - .padding(.top) - //OLD LOG OUT OR LOG IN CODE. Feel free to delete but is simpler, so can help //see thought process - // Button(action: { - // logout() - // }) { - // Text("Logout") - // .font(.headline) - // .foregroundColor(.blue) - // .padding() - // .frame(maxWidth: .infinity) - // .cornerRadius(10) - // } - // - // } - // if !isLoggedIn{ - // NavigationLink(destination: LoginView()) - // { - // Text("Login") - // } - // .bold() - // .padding(.top) - // } - + .padding() } - //This is whatt allows us to have to login and logout in the top corner - .navigationTitle("Posts") - .navigationBarItems(trailing: HStack { - if !isLoggedIn { - NavigationLink(destination: LoginView()) { - Text("Login") - .font(.headline) - .foregroundColor(.blue) - } - } else { - Button(action: { - logout() - }) { - Text("Logout") - .font(.headline) - .foregroundColor(.blue) - } + + if isLoggedIn { + NavigationLink(destination: NewPostView()) { + Text("Create New Post") } - }) - } - .onAppear{ - setupMockData() + .bold() + .padding(.top) + } } + .navigationTitle("Posts") + .navigationBarItems(trailing: HStack { + if !isLoggedIn { + NavigationLink(destination: LoginView()) { + Text("Login") + .font(.headline) + .foregroundColor(.blue) + } + } else { + Button(action: { logout() }) { + Text("Logout") + .font(.headline) + .foregroundColor(.blue) + } + } + }) + .onAppear { setupMockData() } } - func logout(){ - isLoggedIn = false - } } + func logout() { + isLoggedIn = false + } +} + #Preview { ContentView() From 1102b9c7d22fb32322d15382d61f10ca96a6facf Mon Sep 17 00:00:00 2001 From: Austin Lim Date: Tue, 12 Nov 2024 19:33:40 -0800 Subject: [PATCH 3/3] added view comment button --- .../ViewLayer/ContentView.swift | 30 ++++--------------- .../ViewLayer/commentView.swift | 30 +++++++++++++++++++ 2 files changed, 35 insertions(+), 25 deletions(-) create mode 100644 mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/commentView.swift diff --git a/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/ContentView.swift b/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/ContentView.swift index 9c693e2..b577f18 100644 --- a/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/ContentView.swift +++ b/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/ContentView.swift @@ -79,10 +79,11 @@ struct ContentView: View { } } - ForEach(post.comments, id: \.createdAt) { comment in - Text(comment.contents) - .font(.body) - .padding(.leading) + NavigationLink(destination: commentView(comments: post.comments)) { + Text("View all comments") + .font(.caption) + .foregroundColor(.blue) + .padding(.top, 5) } if isLoggedIn { @@ -134,25 +135,4 @@ struct ContentView: View { ContentView() } - // init(loginState: LoginState) { - // self._loginState = State(initialValue: loginState) - // } - - //#Preview { - // ContentView(loginState: .notLoggedIn) - //} - - // switch loginState { - // case .notLoggedIn: - // //LoginView(loginState: $loginState) - // ForumView(loginState: $loginState) - // case .userLoggedIn: - // UserView(loginState: $loginState) - // case .adminLoggedIn: - // AdminView(loginState: $loginState) - // } - // } - // .padding() - // } - //} diff --git a/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/commentView.swift b/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/commentView.swift new file mode 100644 index 0000000..503e5ef --- /dev/null +++ b/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/commentView.swift @@ -0,0 +1,30 @@ +// +// commentView.swift +// miniProject-seniorDesign +// +// Created by Austin Lim on 11/12/24. +// + +import SwiftUI + +struct commentView: View{ + let comments: [Comment] + + var body : some View{ + List(comments, id: \.createdAt) { comment in + VStack(alignment: .leading) { + HStack{ + VStack{ + Text(comment.contents) + .font(.body) + } + } + + } + .padding() + } + .navigationTitle("Comments") + + } + +}