summaryrefslogtreecommitdiff
path: root/Juick/MessageFetcher.swift
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2020-09-17 00:46:27 +0300
committerGravatar Vitaly Takmazov2020-12-10 18:59:55 +0300
commitcf97b1cd43a91725e0419a953815287fde0bf70f (patch)
tree82961197cf7fa1bfa40550748ddd309e5e4d38c3 /Juick/MessageFetcher.swift
parent4851a21c41488a6f0c01e60f1bd1472846f816bf (diff)
SwiftUI WIP
Diffstat (limited to 'Juick/MessageFetcher.swift')
-rw-r--r--Juick/MessageFetcher.swift46
1 files changed, 46 insertions, 0 deletions
diff --git a/Juick/MessageFetcher.swift b/Juick/MessageFetcher.swift
new file mode 100644
index 0000000..b478e9e
--- /dev/null
+++ b/Juick/MessageFetcher.swift
@@ -0,0 +1,46 @@
+//
+// MessageFetcher.swift
+// tst
+//
+// Created by Vitaly Takmazov on 10.12.2019.
+// Copyright © 2019 com.juick. All rights reserved.
+//
+
+import Foundation
+import Combine
+
+class MessageFetcher: ObservableObject {
+
+ typealias Feed = [MessageData]
+
+ struct MessageData : Identifiable {
+ var id : String {
+ get {
+ return "\(message.mid.stringValue)-\(message.rid?.intValue ?? 0)"
+ }
+ }
+ var message : Message
+ }
+
+ @Published var state: LoadableState<Feed> = .loading
+
+ init(url: String) {
+ guard let apiUrl = URL(string: url) else {
+ state = .fetched(.failure(.error("Malformed API URL.")))
+ return
+ }
+
+ AppDelegate.shared.api.pullNext(fromPath: url, params: nil) { (messages, error) in
+ if let error = error {
+ self.state = .fetched(.failure(.error(error.localizedDescription)))
+ return
+ }
+
+ guard let messages = messages else {
+ self.state = .fetched(.failure(.error("Malformed response data")))
+ return
+ }
+ self.state = .fetched(.success(messages.map { return MessageData(message: $0) } ))
+ }
+ }
+}