diff options
Diffstat (limited to 'Juick/Views/FeedView.swift')
-rw-r--r-- | Juick/Views/FeedView.swift | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/Juick/Views/FeedView.swift b/Juick/Views/FeedView.swift new file mode 100644 index 0000000..fe3aaed --- /dev/null +++ b/Juick/Views/FeedView.swift @@ -0,0 +1,66 @@ +// +// FeedView.swift +// tst +// +// Created by Vitaly Takmazov on 10.12.2019. +// Copyright © 2019 com.juick. All rights reserved. +// + +import SwiftUI + +enum FeedType { + case messages + case thread +} + +struct FeedView: View { + @ObservedObject var messageFetcher : MessageFetcher + + let feedType : FeedType + + init(url: String, type: FeedType) { + feedType = type + messageFetcher = MessageFetcher(url: url) + } + + private var stateContent: AnyView { + switch messageFetcher.state { + case .loading: + return AnyView( + ActivityIndicator(style: .medium) + ) + case .fetched(let result): + switch result { + case .failure(let error): + return AnyView( + Text(error.localizedDescription) + ) + case .success(let root): + return AnyView( + List(root) { (message: MessageFetcher.MessageData) in + let destination = feedType == .thread ? AnyView(Text("YO")) : AnyView(FeedView(url: "https://api.juick.com/thread?mid=\(message.message.mid.stringValue)", type: .thread)) + ZStack { + MessageView(message: message.message).listRowInsets(.none) + NavigationLink( + destination: destination) { + EmptyView() + } + } + } + .listRowInsets(.none) + ) + } + } + } + + var body: some View { + stateContent + .navigationBarTitle(Text("Messages"), displayMode: .inline) + } +} + +struct FeedView_Previews: PreviewProvider { + static var previews: some View { + FeedView(url: "https://api.juick.com/messages", type: .messages) + } +} |