@@ -0,0 +1,195 @@
2+// ChannelViewController.swift
5+// Created by cstore on 02/05/2019.
6+// Copyright © 2019 drHSE. All rights reserved.
11+protocol UpdatableName: class{
12+ func updateName(with name: String)
15+protocol UpdatableChannel: class{
16+ func updateChannel(with channel: Model.Channels)
19+class ChannelViewController: UITableViewController, UpdatableName, UpdatableChannel {
21+ func updateName(with name: String){
25+ func updateChannel(with channel: Model.Channels) {
26+ self.channel = channel
29+ var channel: Model.Channels?
31+ // func to show preview of the current channel
32+ var onShowingPreview: ((Model.Channels)->())?
34+ override func viewDidLoad()
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")
44+ override func viewWillAppear(_ animated: Bool) {
45+ super.viewWillAppear(animated)
46+ tableView.reloadData()
48+ // TODO: update channel
49+ override func viewWillDisappear(_ animated: Bool) {
52+ super.viewWillDisappear(animated)
55+ guard let _ = self.navigationController?.viewControllers.lastIndex(of: self) else {
56+ if let _ = channel?.id
58+ Model.updateChannel(with: channel!)
60+ Model.createChannel(with: channel!)
67+ @objc func showPreview(){
68+ if let channel = channel {
69+ onShowingPreview?(channel)
73+ // MARK: - Table view data source
75+ override func numberOfSections(in tableView: UITableView) -> Int {
79+ override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
82+ return (channel?.people.count ?? 0) + 1
84+ return (channel?.tags.count ?? 0) + 1
90+ override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
101+ override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
103+ switch indexPath.section{
105+ return whichCell(tableView, cellForRowAt: indexPath)
107+ let cell = tableView.dequeueReusableCell(withIdentifier: "titleCell") as! TitleCell
109+ cell.fill(title: channel?.name)
116+ private func whichCell(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
118+ let row = indexPath.row
119+ let section = indexPath.section
123+ let cell = tableView.dequeueReusableCell(withIdentifier: "searchCell")!
124+ cell.textLabel?.text = "Add more"
125+ cell.accessoryType = .disclosureIndicator
129+ let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
132+ cell.textLabel?.text = Model.Channels.fullPeopleDict[channel?.people[row-1] ?? 0]?.fullName()
134+ cell.textLabel?.text = "# \(channel!.tags[row-1])"
140+ override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath)
142+ if editingStyle == UITableViewCell.EditingStyle.delete {
143+ if indexPath.section == 1 {
144+ channel?.people.remove(at: indexPath.row-1)
146+ channel?.tags.remove(at: indexPath.row-1)
150+ tableView.reloadData()
153+ override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
154+ if indexPath.row == 0 {
161+ override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
163+ if indexPath.row == 0 && indexPath.section != 0
165+ let vc = storyboard?.instantiateViewController(withIdentifier: addToChannelVCId) as! AddToChannelVC
167+ vc.dataSourse = indexPath.section == 1 ? .people : .tags
168+ vc.channel = channel
171+ navigationController?.pushViewController(vc, animated: true)
177+class TitleCell: UITableViewCell, UITextFieldDelegate {
179+ @IBOutlet weak var titleLabel: UITextField!
181+ weak var update: UpdatableName?
183+ override func awakeFromNib() {
184+ super.awakeFromNib()
187+ func fill(title: String?){
188+ titleLabel.text = title
189+ titleLabel.addTarget(self, action: #selector(changedText(_:)), for: .editingChanged)
192+ @objc func changedText(_ textField: UITextField){
193+ update?.updateName(with: textField.text!)