Skip to content
This repository was archived by the owner on Jun 26, 2019. It is now read-only.

Commit

Permalink
Ambience customization
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiago Mergulhão committed Jan 3, 2018
1 parent 26c4990 commit 781e645
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 33 deletions.
23 changes: 20 additions & 3 deletions Ambience/Classes/AmbienceLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import UIKit

public class AmbienceLabel : UILabel {

@IBInspectable var contrastLabel : String?
@IBInspectable var invertLabel : String?
@IBInspectable var regularLabel : String?
@IBInspectable var autoLabel : String?

override public func ambience(_ notification: Notification) {

super.ambience(notification)
Expand All @@ -20,9 +25,21 @@ public class AmbienceLabel : UILabel {
let aditionalText = Ambience.forcedState == nil ? "" : " (Forced)"

switch currentState {
case .contrast: return "Contrast" + aditionalText
case .invert: return "Invert" + aditionalText
case .regular: return "Regular" + aditionalText
case .contrast:
if let label = contrastLabel {
return label
}
return "Contrast" + aditionalText
case .invert:
if let label = invertLabel {
return label
}
return "Invert" + aditionalText
case .regular:
if let label = regularLabel {
return label
}
return "Regular" + aditionalText
}
}()
}
Expand Down
68 changes: 48 additions & 20 deletions Ambience/Classes/AmbienceObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,64 @@ import UIKit

class AmbienceObject : NSObject {

@IBInspectable var invertAvailable : Bool = true
@IBInspectable var invertLabel : String = "Invert"

@IBInspectable var regularAvailable : Bool = true
@IBInspectable var regularLabel : String = "Regular"

@IBInspectable var contrastAvailable : Bool = true
@IBInspectable var contrastLabel : String = "Contrast"

@IBInspectable var autoAvailable : Bool = true
@IBInspectable var autoLabel : String = "Auto"

@IBOutlet weak var viewController : UIViewController?

@IBAction func switchAmbience (_ sender : AnyObject) {
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

let switchToInvert = UIAlertAction(title: "Invert", style: .default) {
_ in
Ambience.forcedState = .invert
}
let switchToContrast = UIAlertAction(title: "Contrast", style: .default) {
_ in
Ambience.forcedState = .contrast
}
let switchToRegular = UIAlertAction(title: "Regular", style: .default) {
_ in
Ambience.forcedState = .regular
if invertAvailable {

let action = UIAlertAction(title: invertLabel, style: .default) {
_ in
Ambience.forcedState = .invert
}

actionSheet.addAction(action)
}

let switchToAuto = UIAlertAction(title: "Auto", style: .default) {
_ in
Ambience.forcedState = nil
if contrastAvailable {

let action = UIAlertAction(title: contrastLabel, style: .default) {
_ in
Ambience.forcedState = .contrast
}

actionSheet.addAction(action)
}

let cancel = UIAlertAction(title: "Cancel", style: .cancel)
if regularAvailable {

let action = UIAlertAction(title: regularLabel, style: .default) {
_ in
Ambience.forcedState = .regular
}

actionSheet.addAction(action)
}

actionSheet.addAction(switchToInvert)
actionSheet.addAction(switchToContrast)
actionSheet.addAction(switchToRegular)
actionSheet.addAction(switchToAuto)
actionSheet.addAction(cancel)
if autoAvailable {

let action = UIAlertAction(title: autoLabel, style: .default) {
_ in
Ambience.forcedState = nil
}

actionSheet.addAction(action)
}

actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel))

viewController?.present(actionSheet, animated: true)
}
Expand Down
7 changes: 3 additions & 4 deletions Ambience/Classes/AmbienceViews/UIButton+Ambience.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ extension UIButton {
return value
}

self.textColorRegular = self.tintColor ?? .black
self.textColorRegular = self.titleColor(for: .normal) ?? .black

return self.textColorRegular
}
set { objc_setAssociatedObject(self, &KeyValues.regular.textColor, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) }
}

public override func ambience(_ notification : Notification) {
open override func ambience(_ notification : Notification) {

super.ambience(notification)

Expand All @@ -66,7 +66,6 @@ extension UIButton {
}() ?? self.tintColor

self.setTitleColor(color, for: .normal)
self.tintColor = color

} else {

Expand All @@ -80,8 +79,8 @@ extension UIButton {
}() ?? self.tintColor

self.setTitleColor(color, for: .normal)
self.tintColor = color
}
}
}
}

2 changes: 1 addition & 1 deletion Ambience/Classes/AmbienceViews/UILabel+Ambience.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ extension UILabel {
set { objc_setAssociatedObject(self, &KeyValues.regular.textColor, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) }
}

public override func ambience(_ notification : Notification) {
open override func ambience(_ notification : Notification) {

super.ambience(notification)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import UIKit

extension UINavigationBar {

public override func ambience(_ notification : Notification) {
open override func ambience(_ notification : Notification) {

super.ambience(notification)

Expand Down
2 changes: 1 addition & 1 deletion Ambience/Classes/AmbienceViews/UISearchBar+Ambience.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import UIKit

extension UITabBar {

public override func ambience(_ notification : Notification) {
open override func ambience(_ notification : Notification) {

super.ambience(notification)

Expand Down
2 changes: 1 addition & 1 deletion Ambience/Classes/AmbienceViews/UITabBar+Ambience.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import UIKit

extension UISearchBar {

public override func ambience(_ notification : Notification) {
open override func ambience(_ notification : Notification) {

super.ambience(notification)

Expand Down
2 changes: 1 addition & 1 deletion Ambience/Classes/AmbienceViews/UITextView+Ambience.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ extension UITextView {
set { objc_setAssociatedObject(self, &KeyValues.regular.textColor, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) }
}

public override func ambience(_ notification : Notification) {
open override func ambience(_ notification : Notification) {

super.ambience(notification)

Expand Down
2 changes: 1 addition & 1 deletion Ambience/Classes/AmbienceViews/UIView+Ambience.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ extension UIView {
set { objc_setAssociatedObject(self, &KeyValues.regular.backgroundColor, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) }
}

@objc override public func ambience(_ notification: Notification) {
@objc override open func ambience(_ notification: Notification) {

super.ambience(notification)

Expand Down

0 comments on commit 781e645

Please sign in to comment.