diff --git a/MobileFort/MobileFort.xcodeproj/project.pbxproj b/MobileFort/MobileFort.xcodeproj/project.pbxproj index d4ee057..4db0c69 100644 --- a/MobileFort/MobileFort.xcodeproj/project.pbxproj +++ b/MobileFort/MobileFort.xcodeproj/project.pbxproj @@ -9,22 +9,26 @@ /* Begin PBXBuildFile section */ 03427F592488856C00A0073D /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03427F582488856C00A0073D /* AppDelegate.swift */; }; 03427F5B2488856C00A0073D /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03427F5A2488856C00A0073D /* SceneDelegate.swift */; }; - 03427F5D2488856C00A0073D /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03427F5C2488856C00A0073D /* ContentView.swift */; }; + 03427F5D2488856C00A0073D /* MainView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03427F5C2488856C00A0073D /* MainView.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 */; }; 03427F7024888C6E00A0073D /* URLImage in Frameworks */ = {isa = PBXBuildFile; productRef = 03427F6F24888C6E00A0073D /* URLImage */; }; + 03BCD7432488947200DA1F27 /* ProfileView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03BCD7422488947200DA1F27 /* ProfileView.swift */; }; + 03BCD7452488948200DA1F27 /* PostView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03BCD7442488948200DA1F27 /* PostView.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ 03427F552488856C00A0073D /* MobileFort.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MobileFort.app; sourceTree = BUILT_PRODUCTS_DIR; }; 03427F582488856C00A0073D /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 03427F5A2488856C00A0073D /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; - 03427F5C2488856C00A0073D /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; + 03427F5C2488856C00A0073D /* MainView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainView.swift; sourceTree = ""; }; 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 = ""; }; + 03BCD7422488947200DA1F27 /* ProfileView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfileView.swift; sourceTree = ""; }; + 03BCD7442488948200DA1F27 /* PostView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostView.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -60,11 +64,13 @@ children = ( 03427F582488856C00A0073D /* AppDelegate.swift */, 03427F5A2488856C00A0073D /* SceneDelegate.swift */, - 03427F5C2488856C00A0073D /* ContentView.swift */, + 03427F5C2488856C00A0073D /* MainView.swift */, 03427F5E2488856D00A0073D /* Assets.xcassets */, 03427F632488856D00A0073D /* LaunchScreen.storyboard */, 03427F662488856D00A0073D /* Info.plist */, 03427F6C248887D200A0073D /* Common.swift */, + 03BCD7422488947200DA1F27 /* ProfileView.swift */, + 03BCD7442488948200DA1F27 /* PostView.swift */, ); path = MobileFort; sourceTree = ""; @@ -146,8 +152,10 @@ files = ( 03427F6D248887D200A0073D /* Common.swift in Sources */, 03427F592488856C00A0073D /* AppDelegate.swift in Sources */, + 03BCD7432488947200DA1F27 /* ProfileView.swift in Sources */, 03427F5B2488856C00A0073D /* SceneDelegate.swift in Sources */, - 03427F5D2488856C00A0073D /* ContentView.swift in Sources */, + 03BCD7452488948200DA1F27 /* PostView.swift in Sources */, + 03427F5D2488856C00A0073D /* MainView.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/MobileFort/MobileFort/ContentView.swift b/MobileFort/MobileFort/ContentView.swift deleted file mode 100644 index 1aca524..0000000 --- a/MobileFort/MobileFort/ContentView.swift +++ /dev/null @@ -1,97 +0,0 @@ -import SwiftUI -import URLImage - -extension String { - func encodeUrl() -> String? { - return self.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) - } -} - -struct PostView: View { - let post: Post - - var body: some View { - VStack { - if post.isReblogged() { - Text("Reblogged from " + post.originalUsername!) - } - - if post.getTitle() != nil { - Text(post.getTitle()!) - } - - VStack { - ForEach(post.media) { media in - VStack { - URLImage(URL(string: media.url.encodeUrl()!)!, - delay: 0.25) { proxy in - proxy.image - .resizable() - .aspectRatio(contentMode: .fit) - } - } - } - } - } - } -} - -struct ProfileView: View { - let username: String - - @State var posts: [Post] = [] - - var body: some View { - VStack { - List(posts) { post in - PostView(post: post) - } - }.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 decoder = JSONDecoder() - decoder.keyDecodingStrategy = .convertFromSnakeCase - - let decodedPosts = try decoder.decode(Posts.self, from: jsonData) - - DispatchQueue.main.sync { - self.posts = decodedPosts.posts - } - } - } catch { - print("\(error)") - } - }.resume() - } - } -} - -struct MainView: View { - @State private var username: String = "" - - var body: some View { - NavigationView { - VStack { - TextField("Username", text: $username) - .disableAutocorrection(true) - .autocapitalization(.none) - NavigationLink(destination: ProfileView(username: username)) { - Text("Show Feed") - } - } - } - } -} - -struct MainView_Previews: PreviewProvider { - static var previews: some View { - MainView() - } -} diff --git a/MobileFort/MobileFort/MainView.swift b/MobileFort/MobileFort/MainView.swift new file mode 100644 index 0000000..a233882 --- /dev/null +++ b/MobileFort/MobileFort/MainView.swift @@ -0,0 +1,24 @@ +import SwiftUI + +struct MainView: View { + @State private var username: String = "" + + var body: some View { + NavigationView { + VStack { + TextField("Username", text: $username) + .disableAutocorrection(true) + .autocapitalization(.none) + NavigationLink(destination: ProfileView(username: username)) { + Text("Show Feed") + } + } + } + } +} + +struct MainView_Previews: PreviewProvider { + static var previews: some View { + MainView() + } +} diff --git a/MobileFort/MobileFort/PostView.swift b/MobileFort/MobileFort/PostView.swift new file mode 100644 index 0000000..86f7200 --- /dev/null +++ b/MobileFort/MobileFort/PostView.swift @@ -0,0 +1,44 @@ +import SwiftUI +import URLImage + +extension String { + func encodeUrl() -> String? { + return self.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) + } +} + +struct PostView: View { + let post: Post + + var body: some View { + VStack { + if post.isReblogged() { + Text("Reblogged from " + post.originalUsername!) + } + + if post.getTitle() != nil { + Text(post.getTitle()!) + } + + VStack { + ForEach(post.media) { media in + VStack { + URLImage(URL(string: media.url.encodeUrl()!)!, + delay: 0.25) { proxy in + proxy.image + .resizable() + .aspectRatio(contentMode: .fit) + } + } + } + } + } + } +} + +struct PostView_Previews: PreviewProvider { + static var previews: some View { + //PostView() + Text("Hello, world!") + } +} diff --git a/MobileFort/MobileFort/ProfileView.swift b/MobileFort/MobileFort/ProfileView.swift new file mode 100644 index 0000000..0d48ce8 --- /dev/null +++ b/MobileFort/MobileFort/ProfileView.swift @@ -0,0 +1,45 @@ +import SwiftUI + +struct ProfileView: View { + let username: String + + @State var posts: [Post] = [] + + var body: some View { + VStack { + List(posts) { post in + PostView(post: post) + } + }.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 decoder = JSONDecoder() + decoder.keyDecodingStrategy = .convertFromSnakeCase + + let decodedPosts = try decoder.decode(Posts.self, from: jsonData) + + DispatchQueue.main.sync { + self.posts = decodedPosts.posts + } + } + } catch { + print("\(error)") + } + }.resume() + } + } +} + +struct ProfileView_Previews: PreviewProvider { + static var previews: some View { + //ProfileView() + Text("Hello, world!") + } +}