summaryrefslogtreecommitdiff
path: root/JuickNext/View
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2024-05-15 09:50:08 +0300
committerGravatar Vitaly Takmazov2024-05-15 09:50:08 +0300
commit891418e90fe29515364a236a2bb9c81e2a37ff65 (patch)
tree35422be50e4b38534ba5a0d442c90668d7d07500 /JuickNext/View
parent517bc874906e5b81cade91f5c7903736779fbeb0 (diff)
Refactor & reorganize project
* LoadableState -> Result
Diffstat (limited to 'JuickNext/View')
-rw-r--r--JuickNext/View/FeedView.swift57
-rw-r--r--JuickNext/View/LoadableImageView.swift29
-rw-r--r--JuickNext/View/MessageView.swift43
3 files changed, 129 insertions, 0 deletions
diff --git a/JuickNext/View/FeedView.swift b/JuickNext/View/FeedView.swift
new file mode 100644
index 0000000..3a612e8
--- /dev/null
+++ b/JuickNext/View/FeedView.swift
@@ -0,0 +1,57 @@
+//
+// FeedView.swift
+// tst
+//
+// Created by Vitaly Takmazov on 10.12.2019.
+// Copyright © 2019 com.juick. All rights reserved.
+//
+
+import SwiftUI
+
+struct FeedView: View {
+ @ObservedObject var messageFetcher : MessageFetcher
+
+ let title: String
+
+ init(_ title: String, url: String) {
+ self.title = title
+ messageFetcher = MessageFetcher(url: url)
+ }
+
+ private var stateContent: AnyView {
+ switch messageFetcher.state {
+ case nil:
+ return AnyView(
+ ProgressView()
+ )
+ case .failure(let error):
+ return AnyView(
+ Text(error.localizedDescription)
+ )
+ case .success(let root):
+ return AnyView(
+ List(root) { (message: Message) in
+ MessageView(message: message)
+ }
+ )
+ }
+ }
+
+ var body: some View {
+ NavigationView {
+ VStack {
+ stateContent
+ }.toolbar {
+ ToolbarItem(placement: .principal) {
+ Text(title)
+ }
+ }
+ }
+ }
+}
+
+struct FeedView_Previews: PreviewProvider {
+ static var previews: some View {
+ FeedView("Discover", url: "https://api.juick.com/messages")
+ }
+}
diff --git a/JuickNext/View/LoadableImageView.swift b/JuickNext/View/LoadableImageView.swift
new file mode 100644
index 0000000..6a41dc2
--- /dev/null
+++ b/JuickNext/View/LoadableImageView.swift
@@ -0,0 +1,29 @@
+//
+// LoadableImageView.swift
+// tst
+//
+// Created by Vitaly Takmazov on 10.12.2019.
+// Copyright © 2019 com.juick. All rights reserved.
+//
+
+import SwiftUI
+
+struct LoadableImageView: View {
+ @ObservedObject var imageFetcher: ImageFetcher
+
+ init(with urlString: String) {
+ imageFetcher = ImageFetcher(url: urlString)
+ }
+
+ var body: some View {
+ if let image = Image(data: imageFetcher.data) {
+ return AnyView(
+ image.resizable()
+ )
+ } else {
+ return AnyView(
+ ProgressView()
+ )
+ }
+ }
+}
diff --git a/JuickNext/View/MessageView.swift b/JuickNext/View/MessageView.swift
new file mode 100644
index 0000000..398978b
--- /dev/null
+++ b/JuickNext/View/MessageView.swift
@@ -0,0 +1,43 @@
+//
+// MessageView.swift
+// tst
+//
+// Created by Vitaly Takmazov on 10.12.2019.
+// Copyright © 2019 com.juick. All rights reserved.
+//
+
+import SwiftUI
+
+struct MessageView: View {
+ var message: Message
+ var body: some View {
+ VStack(alignment: .leading) {
+ HStack {
+ LoadableImageView(with: message.user.avatar ?? "")
+ .frame(width: 48, height: 48, alignment: .center)
+ Text(message.user.name)
+ .font(.headline)
+ .foregroundColor(.accentColor)
+ }
+ message.tags.map {
+ Text($0.joined(separator: ", "))
+ .font(.subheadline)
+ .italic()
+ .foregroundColor(.secondary)
+ }
+ Text(message.text ?? "")
+ .font(.body)
+ .padding()
+ message.attachment.map {
+ LoadableImageView(with: $0.url).scaledToFit()
+ }
+ }
+ }
+}
+
+struct MessageView_Previews: PreviewProvider {
+ static let msg = Message(id: 0, user: User(id: 0, name: "ugnich"), text: "Lorem ipsum")
+ static var previews: some View {
+ MessageView(message: msg)
+ }
+}