@@ -0,0 +1,195 @@
1+//
2+// ChannelViewController.swift
3+// MessageApp
4+//
5+// Created by cstore on 02/05/2019.
6+// Copyright © 2019 drHSE. All rights reserved.
7+//
8+
9+import UIKit
10+
11+protocol UpdatableName: class{
12+ func updateName(with name: String)
13+}
14+
15+protocol UpdatableChannel: class{
16+ func updateChannel(with channel: Model.Channels)
17+}
18+
19+class ChannelViewController: UITableViewController, UpdatableName, UpdatableChannel {
20+
21+ func updateName(with name: String){
22+ channel?.name = name
23+ }
24+
25+ func updateChannel(with channel: Model.Channels) {
26+ self.channel = channel
27+ }
28+
29+ var channel: Model.Channels?
30+
31+ // func to show preview of the current channel
32+ var onShowingPreview: ((Model.Channels)->())?
33+
34+ override func viewDidLoad()
35+ {
36+ super.viewDidLoad()
37+
38+ navigationItem.rightBarButtonItems = [ UIBarButtonItem(barButtonSystemItem: .play, target: self, action: #selector(showPreview)), self.editButtonItem]
39+ navigationItem.largeTitleDisplayMode = .never
40+ tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
41+ tableView.register(UITableViewCell.self, forCellReuseIdentifier: "searchCell")
42+ }
43+
44+ override func viewWillAppear(_ animated: Bool) {
45+ super.viewWillAppear(animated)
46+ tableView.reloadData()
47+ }
48+ // TODO: update channel
49+ override func viewWillDisappear(_ animated: Bool) {
50+
51+ defer {
52+ super.viewWillDisappear(animated)
53+ }
54+
55+ guard let _ = self.navigationController?.viewControllers.lastIndex(of: self) else {
56+ if let _ = channel?.id
57+ {
58+ Model.updateChannel(with: channel!)
59+ } else {
60+ Model.createChannel(with: channel!)
61+ }
62+ return
63+ }
64+ // nou
65+ }
66+
67+ @objc func showPreview(){
68+ if let channel = channel {
69+ onShowingPreview?(channel)
70+ }
71+ }
72+
73+ // MARK: - Table view data source
74+
75+ override func numberOfSections(in tableView: UITableView) -> Int {
76+ return 3
77+ }
78+
79+ override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
80+ switch section {
81+ case 1:
82+ return (channel?.people.count ?? 0) + 1
83+ case 2:
84+ return (channel?.tags.count ?? 0) + 1
85+ default:
86+ return 1
87+ }
88+ }
89+
90+ override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
91+ switch section {
92+ case 1:
93+ return "People"
94+ case 2:
95+ return "Tags"
96+ default:
97+ return "Title"
98+ }
99+ }
100+
101+ override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
102+
103+ switch indexPath.section{
104+ case 1 , 2:
105+ return whichCell(tableView, cellForRowAt: indexPath)
106+ default:
107+ let cell = tableView.dequeueReusableCell(withIdentifier: "titleCell") as! TitleCell
108+
109+ cell.fill(title: channel?.name)
110+ cell.update = self
111+
112+ return cell
113+ }
114+ }
115+
116+ private func whichCell(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
117+ {
118+ let row = indexPath.row
119+ let section = indexPath.section
120+
121+ if row == 0
122+ {
123+ let cell = tableView.dequeueReusableCell(withIdentifier: "searchCell")!
124+ cell.textLabel?.text = "Add more"
125+ cell.accessoryType = .disclosureIndicator
126+ return cell
127+ }
128+
129+ let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
130+
131+ if section == 1 {
132+ cell.textLabel?.text = Model.Channels.fullPeopleDict[channel?.people[row-1] ?? 0]?.fullName()
133+ } else {
134+ cell.textLabel?.text = "# \(channel!.tags[row-1])"
135+ }
136+
137+ return cell
138+ }
139+
140+ override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath)
141+ {
142+ if editingStyle == UITableViewCell.EditingStyle.delete {
143+ if indexPath.section == 1 {
144+ channel?.people.remove(at: indexPath.row-1)
145+ } else {
146+ channel?.tags.remove(at: indexPath.row-1)
147+ }
148+ }
149+
150+ tableView.reloadData()
151+ }
152+
153+ override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
154+ if indexPath.row == 0 {
155+ return false
156+ }
157+
158+ return true
159+ }
160+
161+ override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
162+ {
163+ if indexPath.row == 0 && indexPath.section != 0
164+ {
165+ let vc = storyboard?.instantiateViewController(withIdentifier: addToChannelVCId) as! AddToChannelVC
166+
167+ vc.dataSourse = indexPath.section == 1 ? .people : .tags
168+ vc.channel = channel
169+ vc.update = self
170+
171+ navigationController?.pushViewController(vc, animated: true)
172+ }
173+ }
174+}
175+
176+
177+class TitleCell: UITableViewCell, UITextFieldDelegate {
178+
179+ @IBOutlet weak var titleLabel: UITextField!
180+
181+ weak var update: UpdatableName?
182+
183+ override func awakeFromNib() {
184+ super.awakeFromNib()
185+ }
186+
187+ func fill(title: String?){
188+ titleLabel.text = title
189+ titleLabel.addTarget(self, action: #selector(changedText(_:)), for: .editingChanged)
190+ }
191+
192+ @objc func changedText(_ textField: UITextField){
193+ update?.updateName(with: textField.text!)
194+ }
195+}