forked from Awful/Awful.app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostsPageSettingsViewController.swift
More file actions
107 lines (87 loc) · 3.98 KB
/
PostsPageSettingsViewController.swift
File metadata and controls
107 lines (87 loc) · 3.98 KB
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
// PostsPageSettingsViewController.swift
//
// Copyright 2014 Awful Contributors. CC BY-NC-SA 3.0 US https://github.com/Awful/Awful.app
import UIKit
/**
A PostsPageSettingsViewController is a modal view controller for changing settings specific to a posts page. By default it presents in a popover on all devices.
*/
class PostsPageSettingsViewController: AwfulViewController, UIPopoverPresentationControllerDelegate {
let forum: AwfulForum
var themes: [AwfulTheme] {
return AwfulThemeLoader.sharedLoader().themesForForumWithID(forum.forumID) as [AwfulTheme]
}
var selectedTheme: AwfulTheme! {
get {
return _selectedTheme
}
set {
_selectedTheme = selectedTheme
if isViewLoaded() {
updateSelectedThemeInPicker()
}
}
}
private var _selectedTheme: AwfulTheme?
init(forum: AwfulForum) {
self.forum = forum
super.init(nibName: "PostsPageSettings", bundle: nil)
modalPresentationStyle = .Popover
popoverPresentationController!.delegate = self
}
@IBOutlet weak var headerLabel: UILabel!
@IBOutlet weak var headerBackground: UIView!
@IBOutlet var labels: [UILabel]!
@IBOutlet var switches: [UISwitch]!
@IBOutlet weak var themePicker: AwfulThemePicker!
@IBAction func changeSelectedTheme(sender: AwfulThemePicker) {
_selectedTheme = themes[sender.selectedThemeIndex]
AwfulSettings.sharedSettings().setThemeName(selectedTheme.name, forForumID: forum.forumID)
if !selectedTheme.forumSpecific {
AwfulSettings.sharedSettings().darkTheme = selectedTheme != AwfulThemeLoader.sharedLoader().defaultTheme
}
}
override func viewDidLoad() {
super.viewDidLoad()
for (i, theme) in enumerate(themes) {
let color = theme.descriptiveColor
color.accessibilityLabel = theme.descriptiveName
themePicker.insertThemeWithColor(color, atIndex: i)
}
updateSelectedThemeInPicker()
let preferredHeight = view.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
preferredContentSize = CGSize(width: 320, height: preferredHeight)
}
private func updateSelectedThemeInPicker() {
if let i = find(themes, theme) {
themePicker.selectedThemeIndex = i
}
}
override func themeDidChange() {
super.themeDidChange()
view.tintColor = theme["tintColor"] as? UIColor
let backgroundColor = theme["sheetBackgroundColor"] as? UIColor
view.backgroundColor = backgroundColor
popoverPresentationController?.backgroundColor = backgroundColor
headerLabel.textColor = theme["sheetTitleColor"] as? UIColor ?? UIColor.blackColor() //BUG Beta 7: UILabel doesn't accept optionals for textColor, but probably should. Bug filed.
headerBackground.backgroundColor = theme["sheetTitleBackgroundColor"] as? UIColor
for label in labels {
label.textColor = theme["sheetTextColor"] as? UIColor ?? UIColor.blackColor()
}
for uiswitch in switches {
uiswitch.onTintColor = theme["settingsSwitchColor"] as? UIColor
}
// Theme picker's background is a light grey so I can see it (until I figure out how live views work in Xcode 6), but it should be transparent for real.
themePicker.backgroundColor = nil
}
// MARK: UIAdaptivePresentationControllerDelegate
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController!) -> UIModalPresentationStyle {
return .None
}
// MARK: Initializers not intended to be called
private override init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
fatalError("Selectotron needs a posts view controller")
}
required init(coder: NSCoder) {
fatalError("NSCoding is not supported")
}
}