Decode title, content, and post type from a post
This commit is contained in:
parent
1492df1545
commit
633365b055
2 changed files with 20 additions and 2 deletions
|
@ -1,5 +1,14 @@
|
|||
import Foundation
|
||||
|
||||
enum PostType: String, Decodable {
|
||||
case picture
|
||||
}
|
||||
|
||||
struct Post: Decodable, Identifiable {
|
||||
let id: Int
|
||||
|
||||
let title: String?
|
||||
let content: String
|
||||
|
||||
let postType: PostType
|
||||
}
|
||||
|
|
|
@ -8,7 +8,13 @@ struct ProfileView: View {
|
|||
var body: some View {
|
||||
VStack {
|
||||
List(posts) { post in
|
||||
VStack {
|
||||
Text(String(post.id))
|
||||
|
||||
if post.title != nil {
|
||||
Text(post.title!)
|
||||
}
|
||||
}
|
||||
}
|
||||
}.navigationBarTitle(username + "'s Feed").onAppear {
|
||||
let url = URL(string: "https://www.pillowfort.social/" + self.username + "/json")!
|
||||
|
@ -20,7 +26,10 @@ struct ProfileView: View {
|
|||
let posts: [Post]
|
||||
}
|
||||
|
||||
let decodedPosts = try JSONDecoder().decode(Posts.self, from: jsonData)
|
||||
let decoder = JSONDecoder()
|
||||
decoder.keyDecodingStrategy = .convertFromSnakeCase
|
||||
|
||||
let decodedPosts = try decoder.decode(Posts.self, from: jsonData)
|
||||
|
||||
DispatchQueue.main.sync {
|
||||
self.posts = decodedPosts.posts
|
||||
|
|
Reference in a new issue