//
//  BrightnessToggle.swift
//  NSInternational
//
//  Created by Lammert Westerhoff on 04/02/16.
//  Copyright © 2016 Lammert Westerhoff. All rights reserved.
//

import Foundation

private var brightness: CGFloat?
public var setToMax: Bool { brightness != nil }

public var enabled = true

private func internalMaxBrightness() {
    UIScreen.main.brightness = 1
}

public func maxBrightness() {
    if enabled && !setToMax {
        brightness = UIScreen.main.brightness
        internalMaxBrightness()
    }
}

private func internalRestoreBrightness() {
    if let brightness = brightness {
        UIScreen.main.brightness = brightness
    }
}

public func restoreBrightness() {
    if enabled {
        internalRestoreBrightness()
        brightness = nil
    }
}

public func applicationWillResignActive() {
    if enabled {
        internalRestoreBrightness()
    }
}

public func applicationWillEnterForeground() {
    if enabled && setToMax {
        internalMaxBrightness()
    }
}

//
//  BrightnessToggle.swift
//  NSInternational
//
//  Created by Lammert Westerhoff on 04/02/16.
//  Copyright © 2016 Lammert Westerhoff. All rights reserved.
//

import Foundation

private var brightness: CGFloat?
public var setToMax: Bool { brightness != nil }

public var enabled = true

public func maxBrightness() {
    if enabled {
        brightness = UIScreen.main.brightness
        UIScreen.main.brightness = 1
    }
}

private func internalRestoreBrightness() {
    if enabled {
        if let brightness = brightness {
            UIScreen.main.brightness = brightness
        }
    }
}

public func restoreBrightness() {
    if enabled {
        internalRestoreBrightness()
        brightness = nil
    }
}

public func applicationWillResignActive() {
    internalRestoreBrightness()
}

public func applicationWillEnterForeground() {
    if setToMax {
        maxBrightness()
    }
}

--- 
+++ 
@@ -13,18 +13,20 @@
 
 public var enabled = true
 
+private func internalMaxBrightness() {
+    UIScreen.main.brightness = 1
+}
+
 public func maxBrightness() {
-    if enabled {
+    if enabled && !setToMax {
         brightness = UIScreen.main.brightness
-        UIScreen.main.brightness = 1
+        internalMaxBrightness()
     }
 }
 
 private func internalRestoreBrightness() {
-    if enabled {
-        if let brightness = brightness {
-            UIScreen.main.brightness = brightness
-        }
+    if let brightness = brightness {
+        UIScreen.main.brightness = brightness
     }
 }
 
@@ -36,11 +38,13 @@
 }
 
 public func applicationWillResignActive() {
-    internalRestoreBrightness()
+    if enabled {
+        internalRestoreBrightness()
+    }
 }
 
 public func applicationWillEnterForeground() {
-    if setToMax {
-        maxBrightness()
+    if enabled && setToMax {
+        internalMaxBrightness()
     }
 }
Fix bug when calling maxBrightness() multiple times.

Prevent the saved screen brightness from getting overwritten with 1.0 on the second call.

Also, do all checks for enabled at the public interface. Private functions can just assume enabled.
