summaryrefslogtreecommitdiff
path: root/JuickNext/View/FeedView.swift
blob: cd0569f6c002d7b6cd3c93181a11d41fdd465c6a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//
//  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 feed: Feed
    
    init(_ feed: Feed) {
        self.feed = feed
        messageFetcher = MessageFetcher(url: feed.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 {
        VStack {
            stateContent
        }.toolbar {
            ToolbarItem(placement: .principal) {
                Text(feed.title)
            }
        }
    }
}

struct FeedView_Previews: PreviewProvider {
    static var previews: some View {
        FeedView(Feed(title:"Discover", url: "https://api.juick.com/messages"))
    }
}