-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGroupEditorVC.swift
272 lines (220 loc) · 9.49 KB
/
GroupEditorVC.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//
// GroupEditorVC.swift
// FeedReader
//
// Created by Mitchell Cooper on 2/20/15.
// Copyright (c) 2015 Mitchell Cooper. All rights reserved.
//
import UIKit
class GroupEditorVC: UITableViewController, UITextFieldDelegate {
weak var group: FeedGroup!
convenience init(group: FeedGroup) {
self.init(style: .grouped)
self.group = group
}
// MARK:- View controller
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(nibName: "GroupCell", bundle: nil), forCellReuseIdentifier: "group")
tableView.backgroundColor = Colors.tableColor
tableView.separatorColor = Colors.separatorColor
navigationItem.title = "\(group.title.capitalized) settings"
}
// MARK:- Table view data source
// number of sections
override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
// numbers of rows in each section
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return section == 0 ? 1 : 4
}
// all rows 60 except for preview
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return indexPath.section == 0 ? 100 : 60
}
// only able to select change icon cell
override func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
return indexPath == IndexPath(row: 3, section: 1)
}
// selected change icon cell
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
// only allow selection of change icon
if indexPath != IndexPath(row: 3, section: 1) {
return
}
let iconVC = GroupIconChooserVC(group: group)
navigationController?.pushViewController(iconVC, animated: true)
}
// section titles
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section == 0 { return "Preview" }
return "Settings"
}
// white header text
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
(view as! UITableViewHeaderFooterView).textLabel?.textColor = UIColor.white
}
// return a cell for a row
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
return previewCell()
}
switch indexPath.row {
case 0: return groupNameCell()
case 1: return stepperCell()
case 2: return automaticallyFetchCell()
case 3: return changeIconCell()
default: return UITableViewCell()
}
}
// MARK: Preview cell
fileprivate func previewCell() -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "group") as! GroupCell
cell.setGroup(group)
return cell
}
// MARK: Group name cell
fileprivate func groupNameCell() -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
cell.backgroundColor = Colors.cellColor
// label
cell.textLabel?.textColor = UIColor.white
cell.textLabel?.text = "Group name"
// text field
let textField = UITextField(frame: CGRect(x: 0, y: 0, width: 175, height: 30))
textField.keyboardAppearance = .dark
textField.backgroundColor = UIColor.clear
textField.returnKeyType = .done
textField.textAlignment = .right
textField.textColor = UIColor.white
textField.text = group.userSetTitle
textField.delegate = self
textField.attributedPlaceholder = NSAttributedString(string: "Unnamed", attributes: [ NSForegroundColorAttributeName: UIColor.lightGray ])
textField.addTarget(self, action: #selector(GroupEditorVC.updateTitle(_:)), for: .editingChanged)
cell.accessoryView = textField
return cell
}
// title changed
func updateTitle(_ textField: UITextField) {
group.userSetTitle = textField.text!
navigationItem.title = "\(group.title.capitalized) settings"
}
// Text field delegate
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
updateTitle(textField)
textField.resignFirstResponder()
return false
}
// MARK: Stepper cell
fileprivate weak var _daysLabel: UILabel?
fileprivate func stepperCell() -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
cell.backgroundColor = Colors.cellColor
// label
cell.textLabel?.textColor = UIColor.white
_daysLabel = cell.textLabel
// stepper
let stepper = UIStepper(frame: CGRect.zero) // frame is predefined
stepper.backgroundColor = Colors.tableColor
stepper.tintColor = Colors.accentColor
stepper.layer.cornerRadius = 5
stepper.addTarget(self, action: #selector(GroupEditorVC.updateStepper(_:)), for: .valueChanged)
stepper.maximumValue = 1000
stepper.minimumValue = 2
stepper.value = Double(group.daysToKeepArticles)
updateStepper(stepper)
cell.accessoryView = stepper
return cell
}
// stepper value changed
func updateStepper(_ stepper: UIStepper) {
group.daysToKeepArticles = Int(stepper.value)
_daysLabel?.text = "Keep articles for \(group.daysToKeepArticles) days"
}
// MARK: Automatically fetch cell
fileprivate func automaticallyFetchCell() -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
cell.backgroundColor = Colors.cellColor
// label
cell.textLabel?.textColor = UIColor.white
cell.textLabel?.text = "Refresh automatically"
// switch
let switchCtrl = UISwitch()
switchCtrl.tintColor = Colors.accentColor
switchCtrl.onTintColor = Colors.accentColor
switchCtrl.thumbTintColor = Colors.tableColor
switchCtrl.addTarget(self, action: #selector(GroupEditorVC.updateSwitch(_:)), for: .valueChanged)
switchCtrl.isOn = group.automaticRefresh
cell.accessoryView = switchCtrl
return cell
}
// switch value changed
func updateSwitch(_ switchCtrl: UISwitch) {
group.automaticRefresh = switchCtrl.isOn
}
// MARK: Change icon cell
fileprivate func changeIconCell() -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
cell.backgroundColor = Colors.cellColor
cell.textLabel?.textColor = UIColor.white
cell.textLabel?.text = "Change group icon"
cell.accessoryType = .disclosureIndicator
cell.selectedBackgroundView = Colors.cellSelectedBackgroundView
return cell
}
}
// MARK:- Icon chooser
class GroupIconChooserVC: UITableViewController {
weak var group: FeedGroup!
// find the icons
let files = FileManager.default.contentsOfDirectoryAtPath(Bundle.mainBundle().resourcePath! + "/icons/group", error: nil) as [String]!
convenience init(group: FeedGroup) {
self.init(style: .plain)
self.group = group
}
// MARK: View controller
override func viewDidLoad() {
super.viewDidLoad()
tableView.backgroundColor = Colors.tableColor
tableView.separatorColor = Colors.separatorColor
tableView.rowHeight = 60
navigationItem.title = "Choose icon"
navigationItem.hidesBackButton = true
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(GroupIconChooserVC.cancelButtonTapped(_:)))
}
// MARK: Table view data source
// just one section
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
// number of rows
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return files.count
}
// return a cell for a row
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let fileName = files[indexPath.row]
cell.textLabel?.textColor = UIColor.white
cell.textLabel?.text = fileName.stringByDeletingPathExtension
cell.textLabel?.font = UIFont.systemFont(ofSize: 20)
cell.imageView?.image = UIImage(named: "icons/group/\(fileName)")
cell.backgroundColor = Colors.cellColor
cell.selectedBackgroundView = Colors.cellSelectedBackgroundView
return cell
}
// selected an icon
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let fileName = files[indexPath.row]
group.icon = UIImage(named: "icons/group/\(fileName)")
group.iconResource = fileName
rss.center.post(name: Notification.Name(rawValue: FeedGroup.Notifications.AppearanceChanged), object: group)
navigationController?.popViewController(animated: true)
}
// cancel button tapped
func cancelButtonTapped(_: AnyObject) {
navigationController?.popViewController(animated: true)
}
}