-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFeedPageView.swift
110 lines (92 loc) · 2.81 KB
/
FeedPageView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// FeedPageView.swift
// MobileAcebook
//
// Created by Santosh Dasari on 02/09/2024.
//
import Foundation
// Mock up a simple posting
// Make a call to the Acebook API to retreive a posting
// Loop through and render each posting
import SwiftUI
// View Model to manage the list of posts
class PostViewModel: ObservableObject {
@Published var posts: [Post] = [
Post(title: "This is my first post", comment: "This is a comment on post 1", likes: 0),
Post(title: "This is my second post", comment: "This is a comment on post 2", likes: 0),
Post(title: "This is my third post", comment: "This is a comment on post 3", likes: 0)
]
func likePost(_ post: Post) {
if let index = posts.firstIndex(where: { $0.id == post.id }) {
posts[index].likes += 1
}
}
func addComment(_ post: Post, newComment: String) {
if let index = posts.firstIndex(where: { $0.id == post.id }) {
posts[index].comment = newComment
}
}
}
// Main View
struct ContentView: View {
@StateObject private var viewModel = PostViewModel()
var body: some View {
NavigationView {
List {
ForEach(viewModel.posts) { post in
PostView(post: post)
.environmentObject(viewModel)
}
}
.navigationTitle("Postings")
}
}
}
// View for a single post
struct PostView: View {
@EnvironmentObject var viewModel: PostViewModel
@State private var newComment: String = ""
var post: Post
var body: some View {
VStack(alignment: .leading) {
Text(post.title)
.font(.headline)
Text(post.comment)
.font(.subheadline)
.foregroundColor(.gray)
HStack {
Button(action: {
viewModel.likePost(post)
}) {
HStack {
Image(systemName: "hand.thumbsup")
Text("\(post.likes) Likes")
}
}
.buttonStyle(BorderlessButtonStyle())
Spacer()
}
.padding(.top, 5)
TextField("Add a comment...", text: $newComment, onCommit: {
viewModel.addComment(post, newComment: newComment)
newComment = ""
})
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding(.top, 5)
}
.padding()
}
}
/*@main
struct PostingsApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}*/
struct PostView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}