This repository has been archived by the owner on Feb 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FeedView.swift
134 lines (114 loc) · 3.67 KB
/
FeedView.swift
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//
// FeedView.swift
// Cut
//
// Created by Kyle McAlpine on 06/11/2017.
// Copyright © 2017 Kyle McAlpine. All rights reserved.
//
import UIKit
import EasyPeasy
import RxSwift
enum FeedViewState {
case loading
case loginSignUp
case followFriends
case showFeed
var body: String {
switch self {
case .loginSignUp:
return "Login or sign up to follow friends and see when they rate films or add them to their watch list."
case .followFriends:
return "Follow some friends to see the films their interested in"
default:
return ""
}
}
var cta: String {
switch self {
case .loginSignUp:
return "Login or Sign Up"
case .followFriends:
return "Follow Friends"
default:
return ""
}
}
}
class FeedView: UIView {
let state: Variable<FeedViewState>
let ctaButton = UIButton(type: .custom)
let tableView = UITableView(frame: .zero, style: .plain)
fileprivate let loadingContainer = UIView()
fileprivate let spinner = UIActivityIndicatorView(activityIndicatorStyle: .gray)
fileprivate let introContainer = UIView()
fileprivate let bodyLabel = UILabel()
let messageContainer = UIView()
init(state: FeedViewState) {
self.state = Variable(state)
super.init(frame: .zero)
backgroundColor = .white
bodyLabel.text = state.body
bodyLabel.numberOfLines = 0
bodyLabel.textAlignment = .center
ctaButton.setTitle(state.cta, for: .normal)
ctaButton.setTitleColor(.white, for: .normal)
ctaButton.backgroundColor = .red
ctaButton.layer.cornerRadius = 5
loadingContainer.addSubview(spinner)
spinner <- Center()
introContainer.addSubview(bodyLabel)
introContainer.addSubview(ctaButton)
bodyLabel <- [
Leading(20),
CenterX(),
CenterY()
]
ctaButton <- [
Top(20).to(bodyLabel),
CenterX(),
Leading(50),
Height(44)
]
addSubview(tableView)
addSubview(introContainer)
addSubview(loadingContainer)
let containerContstraints = [
Leading(),
Trailing(),
Top().to(safeAreaLayoutGuide, .top),
Bottom().to(safeAreaLayoutGuide, .bottom)
]
tableView <- containerContstraints
introContainer <- containerContstraints
loadingContainer <- containerContstraints
_ = self.state.asObservable().takeUntil(rx.deallocated).observeOn(MainScheduler.instance).subscribe(self)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension FeedView: ObserverType {
typealias E = FeedViewState
func on(_ event: Event<FeedViewState>) {
guard case .next(let state) = event else { return }
switch state {
case .followFriends, .loginSignUp:
bodyLabel.text = state.body
ctaButton.setTitle(state.cta, for: .normal)
introContainer.alpha = 1
loadingContainer.alpha = 0
tableView.alpha = 0
spinner.stopAnimating()
case .loading:
introContainer.alpha = 0
loadingContainer.alpha = 1
tableView.alpha = 0
spinner.startAnimating()
case .showFeed:
introContainer.alpha = 0
loadingContainer.alpha = 0
tableView.alpha = 1
spinner.stopAnimating()
}
}
}