-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathFindFriendsViewController.swift
134 lines (109 loc) · 4.03 KB
/
FindFriendsViewController.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
import Foundation
import UIKit
import ReactiveExtensions
import ReactiveSwift
import Library
import Prelude
import KsApi
internal final class FindFriendsViewController: UITableViewController {
fileprivate let viewModel: FindFriendsViewModelType = FindFriendsViewModel()
fileprivate let dataSource = FindFriendsDataSource()
internal static func configuredWith(source: FriendsSource) -> FindFriendsViewController {
let vc = Storyboard.Friends.instantiate(FindFriendsViewController.self)
vc.viewModel.inputs.configureWith(source: source)
return vc
}
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.estimatedRowHeight = 100.0
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.dataSource = dataSource
self.viewModel.inputs.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.setNavigationBarHidden(false, animated: animated)
}
override func bindViewModel() {
super.bindViewModel()
self.viewModel.outputs.friends
.observeForUI()
.observeValues { [weak self] (friends, source) in
self?.dataSource.friends(friends, source: source)
self?.tableView.reloadData()
}
self.viewModel.outputs.stats
.observeForUI()
.observeValues { [weak self] (stats, source) in
self?.dataSource.stats(stats: stats, source: source)
self?.tableView.reloadData()
}
self.viewModel.outputs.showFacebookConnect
.observeForUI()
.observeValues { [weak self] (source, visible) in
self?.dataSource.facebookConnect(source: source, visible: visible)
self?.tableView.reloadData()
}
self.viewModel.outputs.showFollowAllFriendsAlert
.observeForUI()
.observeValues { [weak self] count in
self?.showFollowAllConfirmationAlert(count: count)
}
self.viewModel.outputs.showErrorAlert
.observeForUI()
.observeValues { [weak self] error in
self?.present(
UIAlertController.alertController(forError: error),
animated: true,
completion: nil
)
}
}
override func bindStyles() {
super.bindStyles()
_ = self
|> baseTableControllerStyle()
|> UIViewController.lens.title %~ { _ in Strings.Follow_friends() }
_ = self.navigationController?.navigationBar
?|> baseNavigationBarStyle
}
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell,
forRowAt indexPath: IndexPath) {
if let statsCell = cell as? FindFriendsStatsCell, statsCell.delegate == nil {
statsCell.delegate = self
} else if let fbConnectCell = cell as? FindFriendsFacebookConnectCell, fbConnectCell.delegate == nil {
fbConnectCell.delegate = self
}
self.viewModel.inputs.willDisplayRow(self.dataSource.itemIndexAt(indexPath),
outOf: self.dataSource.numberOfItems())
}
fileprivate func showFollowAllConfirmationAlert(count: Int) {
self.present(
UIAlertController.confirmFollowAllFriends(
friendsCount: count,
yesHandler: { _ in
self.viewModel.inputs.confirmFollowAllFriends()
},
noHandler: { _ in
self.viewModel.inputs.declineFollowAllFriends()
}
),
animated: true,
completion: nil
)
}
}
extension FindFriendsViewController: FindFriendsStatsCellDelegate {
func findFriendsStatsCellShowFollowAllFriendsAlert(friendCount: Int) {
self.viewModel.inputs.findFriendsStatsCellShowFollowAllFriendsAlert(friendCount: friendCount)
}
}
extension FindFriendsViewController: FindFriendsFacebookConnectCellDelegate {
func findFriendsFacebookConnectCellDidFacebookConnectUser() {
self.viewModel.inputs.findFriendsFacebookConnectCellDidFacebookConnectUser()
}
func findFriendsFacebookConnectCellDidDismissHeader() {}
func findFriendsFacebookConnectCellShowErrorAlert(_ alert: AlertError) {
self.viewModel.inputs.findFriendsFacebookConnectCellShowErrorAlert(alert)
}
}