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

Commit

Permalink
Ambience is now able to switch states and have a forced state
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiago Mergulhão committed Jan 3, 2018
1 parent 218c696 commit 26c4990
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 5 deletions.
15 changes: 15 additions & 0 deletions Ambience/Classes/Ambience.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ public class Ambience {

public static var shared : Ambience = Ambience()

public static var forcedState : AmbienceState? = nil {
didSet {
if let forcedState = self.forcedState {
self.currentState = forcedState
} else {
self.shared.checkBrightnessValue()
let currentState = self.currentState
self.currentState = currentState
}
}
}

public static var previousState : AmbienceState = .regular
public static var currentState : AmbienceState = .regular {
willSet {
Expand Down Expand Up @@ -59,6 +71,9 @@ public class Ambience {
processConstraints(forBrightness : UIScreen.main.brightness)
}
@objc public func brightnessDidChange (notification : NSNotification) {

if Ambience.forcedState != nil { return }

checkBrightnessValue()
}

Expand Down
29 changes: 29 additions & 0 deletions Ambience/Classes/AmbienceLabel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// AmbienceLabel.swift
// Ambience
//
// Created by Tiago Mergulhão on 03/01/2018.
//

import UIKit

public class AmbienceLabel : UILabel {

override public func ambience(_ notification: Notification) {

super.ambience(notification)

guard let currentState = notification.userInfo?["currentState"] as? AmbienceState else { return }

text = {

let aditionalText = Ambience.forcedState == nil ? "" : " (Forced)"

switch currentState {
case .contrast: return "Contrast" + aditionalText
case .invert: return "Invert" + aditionalText
case .regular: return "Regular" + aditionalText
}
}()
}
}
45 changes: 45 additions & 0 deletions Ambience/Classes/AmbienceObject.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// AmbienceObject.swift
// Ambience
//
// Created by Tiago Mergulhão on 03/01/2018.
//

import UIKit

class AmbienceObject : NSObject {

@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
}

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

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

actionSheet.addAction(switchToInvert)
actionSheet.addAction(switchToContrast)
actionSheet.addAction(switchToRegular)
actionSheet.addAction(switchToAuto)
actionSheet.addAction(cancel)

viewController?.present(actionSheet, animated: true)
}
}
31 changes: 30 additions & 1 deletion Example/Ambience/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,31 @@ In that dream I choose my own colors.</string>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="y66-RZ-EmC" customClass="AmbienceLabel" customModule="Ambience">
<rect key="frame" x="166" y="490" width="42" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" white="0.33333333333333331" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="boolean" keyPath="ambience" value="YES"/>
<userDefinedRuntimeAttribute type="color" keyPath="textColorInvert">
<color key="value" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="color" keyPath="textColorContrast">
<color key="value" red="0.1215686275" green="0.12941176469999999" blue="0.14117647059999999" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</label>
</subviews>
<color key="backgroundColor" red="0.96999612630499998" green="0.96999999999999997" blue="0.96999999999999997" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="sWS-iR-HcO" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="I19-dd-8Ll"/>
<constraint firstItem="y66-RZ-EmC" firstAttribute="top" secondItem="hii-uO-ezh" secondAttribute="bottom" constant="93" id="LpX-ej-G5S"/>
<constraint firstItem="oQH-rm-jV0" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="PzZ-dO-zbe"/>
<constraint firstItem="oQH-rm-jV0" firstAttribute="top" secondItem="sWS-iR-HcO" secondAttribute="bottom" constant="52" id="QlN-Um-8Kt"/>
<constraint firstItem="hii-uO-ezh" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="kkG-fd-Voo"/>
<constraint firstItem="hii-uO-ezh" firstAttribute="centerY" secondItem="kh9-bI-dsS" secondAttribute="centerY" id="nLd-Lk-xRI"/>
<constraint firstItem="y66-RZ-EmC" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="uTN-qf-zZU"/>
<constraint firstItem="oQH-rm-jV0" firstAttribute="centerY" secondItem="kh9-bI-dsS" secondAttribute="centerY" id="xYr-1G-E8N"/>
</constraints>
<userDefinedRuntimeAttributes>
Expand All @@ -80,9 +97,20 @@ In that dream I choose my own colors.</string>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</view>
<navigationItem key="navigationItem" id="G3D-I4-xOd"/>
<navigationItem key="navigationItem" id="G3D-I4-xOd">
<barButtonItem key="rightBarButtonItem" title="Ambience" id="15f-dt-mEx">
<connections>
<action selector="switchAmbience:" destination="tff-oA-QJz" id="OG5-Wm-xEI"/>
</connections>
</barButtonItem>
</navigationItem>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
<customObject id="tff-oA-QJz" customClass="AmbienceObject" customModule="Ambience">
<connections>
<outlet property="viewController" destination="vXZ-lx-hvc" id="SWh-vA-uT9"/>
</connections>
</customObject>
</objects>
<point key="canvasLocation" x="1250" y="31"/>
</scene>
Expand All @@ -92,6 +120,7 @@ In that dream I choose my own colors.</string>
<tabBarController automaticallyAdjustsScrollViewInsets="NO" id="d2u-4m-JWz" sceneMemberID="viewController">
<toolbarItems/>
<tabBar key="tabBar" contentMode="scaleToFill" insetsLayoutMarginsFromSafeArea="NO" id="R7S-xQ-DXk">
<rect key="frame" x="0.0" y="0.0" width="1000" height="1000"/>
<autoresizingMask key="autoresizingMask"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<userDefinedRuntimeAttributes>
Expand Down
16 changes: 12 additions & 4 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 26c4990

Please sign in to comment.