summaryrefslogtreecommitdiff
path: root/JuickNext/View/FeedView.swift
blob: 783d8c02917e7eb7d1195cbc2b488972acdd6f2f (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
56
57
58
59
//
//  FeedView.swift
//  tst
//
//  Created by Vitaly Takmazov on 10.12.2019.
//  Copyright © 2019 com.juick. All rights reserved.
//

import SwiftUI

struct FeedView: View {
    @Environment(\.horizontalSizeClass) private var size
    @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 {
        let content = VStack {
            stateContent
        }.screenTitle(title: feed.title)
        
        let view = (size == .compact) ?
        AnyView(
            NavigationView {
                content
            }) : AnyView(content)
        view
    }
}

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