-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathSkillListingViewController.swift
163 lines (140 loc) · 5.59 KB
/
SkillListingViewController.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//
// SkillListingViewController.swift
// Susi
//
// Created by Chashmeet Singh on 2017-08-24.
// Copyright © 2017 FOSSAsia. All rights reserved.
//
import UIKit
import Material
import Reachability
class SkillListingViewController: UITableViewController {
// for opening settings view controller
let reachability = try! Reachability()
var dismissChecker: Bool?
lazy var settingsButton: IconButton = {
let ib = IconButton()
ib.image = Icon.moreVertical
ib.tintColor = .white
ib.layer.cornerRadius = 18.0
ib.addTarget(self, action: #selector(presentSettingsController), for: .touchUpInside)
return ib
}()
lazy var languageButton: UIButton = {
let button = UIButton()
button.setTitleColor(UIColor.white, for: .normal)
button.addTarget(self, action: #selector(getToAllLanguageVC), for: .touchUpInside)
let spacing = 0.5
let line = UIView()
line.translatesAutoresizingMaskIntoConstraints = false
line.backgroundColor = UIColor.white
button.addSubview(line)
button.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[line]|", metrics: nil, views: ["line":line]))
button.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[line(2)]-(\(-spacing))-|", metrics: nil, views: ["line":line]))
button.setTitle("Select A Language", for: .normal)
return button
}()
// for opening settings view controller
lazy var backButton: IconButton = {
let ib = IconButton()
ib.image = Icon.cm.arrowBack
ib.tintColor = .white
ib.addTarget(self, action: #selector(dismissController), for: .touchUpInside)
return ib
}()
var groups: [String]?
weak var chatViewControllerDelegate: ChatViewControllerProtocol?
var shouldShowShimmerLoading: Bool = true
// stores how many group's data fetched
var count = 0 {
didSet {
if count == groups?.count {
shouldAnimateIndicators(false)
shouldShowShimmerLoading = false
tableView.reloadData()
}
}
}
var skills: Dictionary<String, [Skill]> = [:]
var presentLangugage: LanguageModel = LanguageModel.getDefaultLanguageModel()
let activityIndicator: UIActivityIndicatorView = {
let indicator = UIActivityIndicatorView()
indicator.hidesWhenStopped = true
indicator.style = .whiteLarge
indicator.color = UIColor.defaultColor()
return indicator
}()
var selectedSkill: Skill?
override func viewDidLoad() {
super.viewDidLoad()
prepareActivityIndicator()
getAllGroups()
self.tableView.reloadData()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.setupView()
checkReachability()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.view.hideAllToasts()
}
// MARK: - Table view data source
func dismissingTheController() {
if dismissChecker ?? false {
self.dismiss(animated: true, completion: nil)
}
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// return the number of rows
return groups?.count ?? (shouldShowShimmerLoading ? 4 : 0)
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "skillListCell", for: indexPath) as? SkillListingTableCell {
cell.viewModel = SkillListingCellViewModel(skill: skills[groups?[indexPath.row] ?? ""], isLoading: shouldShowShimmerLoading, groupName: groups?[indexPath.row], skillListController: self)
return cell
}
return UITableViewCell()
}
override func viewDidAppear(_ animated: Bool) {
NotificationCenter.default.addObserver(self, selector: #selector(internetConnection), name: Notification.Name.reachabilityChanged, object: reachability)
do {
try reachability.startNotifier()
} catch {
print(error)
}
}
@objc func internetConnection(notification: NSNotification) {
guard let reachability = notification.object as? Reachability else {return}
if reachability.connection != .none {
print("internet is available")
} else {
print("internet is not available")
}
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if let group = groups?[indexPath.row], let skills = self.skills[group] {
if skills.count > 0 {
return 224.0
}
}
return shouldShowShimmerLoading ? 224.0 : 0.0
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let skillDetailVC = segue.destination as? SkillDetailViewController, segue.identifier == ControllerConstants.skillDetailControllerIdentifier {
skillDetailVC.chatViewControllerDelegate = chatViewControllerDelegate
skillDetailVC.skill = selectedSkill
}
}
}
extension SkillListingViewController: SkillSelectionProtocol {
//MARK: - SkillSelectionProtocol method
func didSelectSkill(skill: Skill?) {
guard let skill = skill else {
return
}
selectedSkill = skill
performSegue(withIdentifier: ControllerConstants.skillDetailControllerIdentifier, sender: self)
}
}