summaryrefslogtreecommitdiff
path: root/Juick/Views/FeedView.swift
blob: fe3aaed18cd792e2a1b7978e87780b388eb1105e (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
60
61
62
63
64
65
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)
    }
}