From 145f635768771aed14b812af2e1e4b7a3fb68fa1 Mon Sep 17 00:00:00 2001 From: redstrate <54911369+redstrate@users.noreply.github.com> Date: Wed, 18 Nov 2020 08:32:37 -0500 Subject: [PATCH] Add description view to macOS --- MobileFort/Common/ProfileView.swift | 102 +++++++------------ MobileFort/MobileFort (iOS)/MainView.swift | 2 +- MobileFort/MobileFort (macOS)/MainView.swift | 10 +- 3 files changed, 48 insertions(+), 66 deletions(-) diff --git a/MobileFort/Common/ProfileView.swift b/MobileFort/Common/ProfileView.swift index 547f116..47d4c72 100644 --- a/MobileFort/Common/ProfileView.swift +++ b/MobileFort/Common/ProfileView.swift @@ -75,76 +75,52 @@ struct ProfileView: View { .navigationBarItems(trailing: NavigationLink(destination: DescriptionView(username: username)) { Text("Sidebar") }) - .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) - - var postArray = [ParsedPostContainer]() - - for post in decodedPosts.posts { - let container = ParsedPostContainer(post: post, contentAttributed: (try? NSMutableAttributedString(data: post.getContent().data(using: .utf8)!, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil))!) - postArray.append(container) - } - - DispatchQueue.main.sync { - self.posts = postArray - } - } - } catch { - print("\(error)") - } - }.resume() - } + .onAppear(perform: load) } #else var body: some View { - List(posts, id: \.post.id) { post in - PostView(post: post) - } - .onAppear { - let url = URL(string: "https://www.pillowfort.social/" + self.username + "/json")! + HStack { + List(posts, id: \.post.id) { post in + PostView(post: post) + } + .onAppear(perform: load) - 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) - - var postArray = [ParsedPostContainer]() - - for post in decodedPosts.posts { - let container = ParsedPostContainer(post: post, contentAttributed: (try? NSMutableAttributedString(data: post.getContent().data(using: .utf8)!, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil))!) - postArray.append(container) - } - - DispatchQueue.main.sync { - self.posts = postArray - } - } - } catch { - print("\(error)") - } - }.resume() + DescriptionView(username: username) } } #endif + + private func load() { + 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) + + var postArray = [ParsedPostContainer]() + + for post in decodedPosts.posts { + let container = ParsedPostContainer(post: post, contentAttributed: (try? NSMutableAttributedString(data: post.getContent().data(using: .utf8)!, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil))!) + postArray.append(container) + } + + DispatchQueue.main.sync { + self.posts = postArray + } + } + } catch { + print("\(error)") + } + }.resume() + } } struct ProfileView_Previews: PreviewProvider { diff --git a/MobileFort/MobileFort (iOS)/MainView.swift b/MobileFort/MobileFort (iOS)/MainView.swift index 707dfc1..a233882 100644 --- a/MobileFort/MobileFort (iOS)/MainView.swift +++ b/MobileFort/MobileFort (iOS)/MainView.swift @@ -8,7 +8,7 @@ struct MainView: View { VStack { TextField("Username", text: $username) .disableAutocorrection(true) - //.autocapitalization(.none) + .autocapitalization(.none) NavigationLink(destination: ProfileView(username: username)) { Text("Show Feed") } diff --git a/MobileFort/MobileFort (macOS)/MainView.swift b/MobileFort/MobileFort (macOS)/MainView.swift index dc9c1b8..c2f9e44 100644 --- a/MobileFort/MobileFort (macOS)/MainView.swift +++ b/MobileFort/MobileFort (macOS)/MainView.swift @@ -4,8 +4,14 @@ struct MainView: View { @State private var username: String = "" var body: some View { - VStack { - ProfileView(username: "redstrate") + NavigationView { + VStack { + TextField("Username", text: $username) + .disableAutocorrection(true) + NavigationLink(destination: ProfileView(username: username)) { + Text("Show Feed") + } + } } } }