From 1492df1545421b19ecb05cc5961ff0cda4e92335 Mon Sep 17 00:00:00 2001 From: redstrate <54911369+redstrate@users.noreply.github.com> Date: Wed, 3 Jun 2020 21:45:44 -0400 Subject: [PATCH] Add list for post id's in profile view --- .../MobileFort.xcodeproj/project.pbxproj | 4 +++ MobileFort/MobileFort/Common.swift | 5 +++ MobileFort/MobileFort/ContentView.swift | 34 +++++++++++++++++-- 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 MobileFort/MobileFort/Common.swift diff --git a/MobileFort/MobileFort.xcodeproj/project.pbxproj b/MobileFort/MobileFort.xcodeproj/project.pbxproj index 3c31733..5306bc0 100644 --- a/MobileFort/MobileFort.xcodeproj/project.pbxproj +++ b/MobileFort/MobileFort.xcodeproj/project.pbxproj @@ -12,6 +12,7 @@ 03427F5D2488856C00A0073D /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03427F5C2488856C00A0073D /* ContentView.swift */; }; 03427F5F2488856D00A0073D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 03427F5E2488856D00A0073D /* Assets.xcassets */; }; 03427F652488856D00A0073D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 03427F632488856D00A0073D /* LaunchScreen.storyboard */; }; + 03427F6D248887D200A0073D /* Common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03427F6C248887D200A0073D /* Common.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -22,6 +23,7 @@ 03427F5E2488856D00A0073D /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 03427F642488856D00A0073D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 03427F662488856D00A0073D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 03427F6C248887D200A0073D /* Common.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Common.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -60,6 +62,7 @@ 03427F5E2488856D00A0073D /* Assets.xcassets */, 03427F632488856D00A0073D /* LaunchScreen.storyboard */, 03427F662488856D00A0073D /* Info.plist */, + 03427F6C248887D200A0073D /* Common.swift */, ); path = MobileFort; sourceTree = ""; @@ -133,6 +136,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 03427F6D248887D200A0073D /* Common.swift in Sources */, 03427F592488856C00A0073D /* AppDelegate.swift in Sources */, 03427F5B2488856C00A0073D /* SceneDelegate.swift in Sources */, 03427F5D2488856C00A0073D /* ContentView.swift in Sources */, diff --git a/MobileFort/MobileFort/Common.swift b/MobileFort/MobileFort/Common.swift new file mode 100644 index 0000000..678ae59 --- /dev/null +++ b/MobileFort/MobileFort/Common.swift @@ -0,0 +1,5 @@ +import Foundation + +struct Post: Decodable, Identifiable { + let id: Int +} diff --git a/MobileFort/MobileFort/ContentView.swift b/MobileFort/MobileFort/ContentView.swift index 9f7705a..cfd742c 100644 --- a/MobileFort/MobileFort/ContentView.swift +++ b/MobileFort/MobileFort/ContentView.swift @@ -3,10 +3,34 @@ import SwiftUI struct ProfileView: View { let username: String + @State var posts: [Post] = [] + var body: some View { VStack { - Text("Hello, world!") - }.navigationBarTitle(username + "'s Feed") + List(posts) { post in + Text(String(post.id)) + } + }.navigationBarTitle(username + "'s Feed").onAppear { + let url = URL(string: "https://www.pillowfort.social/" + self.username + "/json")! + + URLSession.shared.dataTask(with: url) { (data, response, error) in + do { + if let jsonData = data { + struct Posts : Decodable { + let posts: [Post] + } + + let decodedPosts = try JSONDecoder().decode(Posts.self, from: jsonData) + + DispatchQueue.main.sync { + self.posts = decodedPosts.posts + } + } + } catch { + print("\(error)") + } + }.resume() + } } } @@ -24,3 +48,9 @@ struct MainView: View { } } } + +struct MainView_Previews: PreviewProvider { + static var previews: some View { + MainView() + } +}