forked from CatchChat/Yep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChooseChannelViewController.swift
125 lines (91 loc) · 3.06 KB
/
ChooseChannelViewController.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
//
// ChooseChannelViewController.swift
// Yep
//
// Created by NIX on 16/5/24.
// Copyright © 2016年 Catch Inc. All rights reserved.
//
import UIKit
import YepKit
import RealmSwift
class ChooseChannelViewController: UITableViewController {
var currentPickedSkill: Skill?
var pickedSkillAction: ((skill: Skill?) -> Void)?
@IBOutlet weak var doneButton: UIBarButtonItem! {
didSet {
doneButton.enabled = false
}
}
private lazy var selectedBackgroundView: UIView = {
let view = UIView()
view.backgroundColor = UIColor(red: 50/255.0, green: 167/255.0, blue: 255/255.0, alpha: 1.0)
return view
}()
private let skills: [Skill] = {
guard let me = me() else {
return []
}
let skills = skillsFromUserSkillList(me.masterSkills) + skillsFromUserSkillList(me.learningSkills)
return skills
}()
override func viewDidLoad() {
super.viewDidLoad()
title = NSLocalizedString("Choose Channel", comment: "")
tableView.tableFooterView = UIView()
}
// MARK: - Actions
@IBAction func cancel(sender: UIBarButtonItem) {
dismissViewControllerAnimated(true, completion: nil)
}
@IBAction func done(sender: UIBarButtonItem) {
pickedSkillAction?(skill: currentPickedSkill)
dismissViewControllerAnimated(true, completion: nil)
}
// MARK: - Table view data source
enum Section: Int {
case DefaultSkill
case Skills
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let section = Section(rawValue: section) else {
fatalError()
}
switch section {
case .DefaultSkill:
return 1
case .Skills:
return skills.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("ChannelCell", forIndexPath: indexPath)
guard let section = Section(rawValue: indexPath.section) else {
fatalError()
}
switch section {
case .DefaultSkill:
currentPickedSkill = nil
cell.textLabel?.text = NSLocalizedString("Default", comment: "")
case .Skills:
let skill = skills[indexPath.row]
cell.textLabel?.text = skill.localName
}
cell.selectedBackgroundView = selectedBackgroundView
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
guard let section = Section(rawValue: indexPath.section) else {
fatalError()
}
switch section {
case .DefaultSkill:
currentPickedSkill = nil
case .Skills:
currentPickedSkill = skills[indexPath.row]
}
doneButton.enabled = true
}
}