<commit_before>/*
 * Copyright (C) 2015 - 2016, <NAME> and CosmicMind, Inc. <http://cosmicmind.com>.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *	*	Redistributions of source code must retain the above copyright notice, this
 *		list of conditions and the following disclaimer.
 *
 *	*	Redistributions in binary form must reproduce the above copyright notice,
 *		this list of conditions and the following disclaimer in the documentation
 *		and/or other materials provided with the distribution.
 *
 *	*	Neither the name of CosmicMind nor the names of its
 *		contributors may be used to endorse or promote products derived from
 *		this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import UIKit

@objc(SwitchStyle)
public enum SwitchStyle: Int {
	case light
	case dark
}

@objc(SwitchState)
public enum SwitchState: Int {
	case on
	case off
}

@objc(SwitchSize)
public enum SwitchSize: Int {
	case small
	case medium
	case large
}

@objc(SwitchDelegate)
public protocol SwitchDelegate {
	/**
     A Switch delegate method for state changes.
     - Parameter control: Switch control.
     - Parameter state: SwitchState value.
     */
    func switchDidChangeState(control: Switch, state: SwitchState)
}

open class Switch: UIControl {
    /// Will layout the view.
    open var willLayout: Bool {
        return 0 < width && 0 < height && nil != superview
    }
    
    /// An internal reference to the switchState public property.
	fileprivate var internalSwitchState = SwitchState.off
	
	/// Track thickness.
	fileprivate var trackThickness: CGFloat = 0
	
	/// Button diameter.
	fileprivate var buttonDiameter: CGFloat = 0
	
	/// Position when in the .on state.
	fileprivate var onPosition: CGFloat = 0
	
	/// Position when in the .off state.
	fileprivate var offPosition: CGFloat = 0
	
	/// The bounce offset when animating.
	fileprivate var bounceOffset: CGFloat = 3
		
	/// An Optional delegation method.
	open weak var delegate: SwitchDelegate?
	
	/// Indicates if the animation should bounce.
	@IBInspectable
    open var isBounceable = true {
		didSet {
			bounceOffset = isBounceable ? 3 : 0
		}
	}
	
	/// Button on color.
	@IBInspectable
    open var buttonOnColor = Color.clear {
		didSet {
			styleForState(state: switchState)
		}
	}
	
	/// Button off color.
	@IBInspectable
    open var buttonOffColor = Color.clear {
		didSet {
			styleForState(state: switchState)
		}
	}
	
	/// Track on color.
	@IBInspectable
    open var trackOnColor = Color.clear {
		didSet {
			styleForState(state: switchState)
		}
	}
	
	/// Track off color.
	@IBInspectable
    open var trackOffColor = Color.clear {
		didSet {
			styleForState(state: switchState)
		}
	}
	
	/// Button on disabled color.
	@IBInspectable
    open var buttonOnDisabledColor = Color.clear {
		didSet {
			styleForState(state: switchState)
		}
	}
	
	/// Track on disabled color.
	@IBInspectable
    open var trackOnDisabledColor = Color.clear {
		didSet {
			styleForState(state: switchState)
		}
	}
	
	/// Button off disabled color.
	@IBInspectable
    open var buttonOffDisabledColor = Color.clear {
		didSet {
			styleForState(state: switchState)
		}
	}
	
	/// Track off disabled color.
	@IBInspectable
    open var trackOffDisabledColor = Color.clear {
		didSet {
			styleForState(state: switchState)
		}
	}
	
	/// Track view reference.
	open fileprivate(set) var track: UIView {
		didSet {
			prepareTrack()
		}
	}
	
	/// Button view reference.
	open fileprivate(set) var button: FABButton {
		didSet {
			prepareButton()
		}
	}
	
	@IBInspectable
    open override var isEnabled: Bool {
		didSet {
			styleForState(state: internalSwitchState)
		}
	}
	
	/// A boolean indicating if the switch is on or not.
	@IBInspectable
    public var isOn: Bool {
		get {
			return .on == internalSwitchState
		}
		set(value) {
			updateSwitchState(state: .on, animated: true, isTriggeredByUserInteraction: false)
		}
	}

	/// Switch state.
	open var switchState: SwitchState {
		get {
			return internalSwitchState
		}
		set(value) {
			updateSwitchState(state: .on, animated: true, isTriggeredByUserInteraction: false)
		}
	}
	
	/// Switch style.
	open var switchStyle = SwitchStyle.dark {
		didSet {
			switch switchStyle {
			case .light:
				buttonOnColor = Color.blue.darken2
				trackOnColor = Color.blue.lighten3
				buttonOffColor = Color.blueGrey.lighten4
				trackOffColor = Color.grey.lighten3
				buttonOnDisabledColor = Color.grey.lighten2
				trackOnDisabledColor = Color.grey.lighten3
				buttonOffDisabledColor = Color.grey.lighten2
				trackOffDisabledColor = Color.grey.lighten3
			case .dark:
				buttonOnColor = Color.blue.lighten1
				trackOnColor = Color.blue.lighten2.withAlphaComponent(0.5)
				buttonOffColor = Color.grey.lighten3
				trackOffColor = Color.blueGrey.lighten4.withAlphaComponent(0.5)
				buttonOnDisabledColor = Color.grey.darken3
				trackOnDisabledColor = Color.grey.lighten1.withAlphaComponent(0.2)<commit_msg>development: issue-644: Fived issue where where Switch.isOn and Switch.switchState were not updating correctly.<commit_after>/*
 * Copyright (C) 2015 - 2016, <NAME> and CosmicMind, Inc. <http://cosmicmind.com>.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *	*	Redistributions of source code must retain the above copyright notice, this
 *		list of conditions and the following disclaimer.
 *
 *	*	Redistributions in binary form must reproduce the above copyright notice,
 *		this list of conditions and the following disclaimer in the documentation
 *		and/or other materials provided with the distribution.
 *
 *	*	Neither the name of CosmicMind nor the names of its
 *		contributors may be used to endorse or promote products derived from
 *		this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import UIKit

@objc(SwitchStyle)
public enum SwitchStyle: Int {
	case light
	case dark
}

@objc(SwitchState)
public enum SwitchState: Int {
	case on
	case off
}

@objc(SwitchSize)
public enum SwitchSize: Int {
	case small
	case medium
	case large
}

@objc(SwitchDelegate)
public protocol SwitchDelegate {
	/**
     A Switch delegate method for state changes.
     - Parameter control: Switch control.
     - Parameter state: SwitchState value.
     */
    func switchDidChangeState(control: Switch, state: SwitchState)
}

open class Switch: UIControl {
    /// Will layout the view.
    open var willLayout: Bool {
        return 0 < width && 0 < height && nil != superview
    }
    
    /// An internal reference to the switchState public property.
	fileprivate var internalSwitchState = SwitchState.off
	
	/// Track thickness.
	fileprivate var trackThickness: CGFloat = 0
	
	/// Button diameter.
	fileprivate var buttonDiameter: CGFloat = 0
	
	/// Position when in the .on state.
	fileprivate var onPosition: CGFloat = 0
	
	/// Position when in the .off state.
	fileprivate var offPosition: CGFloat = 0
	
	/// The bounce offset when animating.
	fileprivate var bounceOffset: CGFloat = 3
		
	/// An Optional delegation method.
	open weak var delegate: SwitchDelegate?
	
	/// Indicates if the animation should bounce.
	@IBInspectable
    open var isBounceable = true {
		didSet {
			bounceOffset = isBounceable ? 3 : 0
		}
	}
	
	/// Button on color.
	@IBInspectable
    open var buttonOnColor = Color.clear {
		didSet {
			styleForState(state: switchState)
		}
	}
	
	/// Button off color.
	@IBInspectable
    open var buttonOffColor = Color.clear {
		didSet {
			styleForState(state: switchState)
		}
	}
	
	/// Track on color.
	@IBInspectable
    open var trackOnColor = Color.clear {
		didSet {
			styleForState(state: switchState)
		}
	}
	
	/// Track off color.
	@IBInspectable
    open var trackOffColor = Color.clear {
		didSet {
			styleForState(state: switchState)
		}
	}
	
	/// Button on disabled color.
	@IBInspectable
    open var buttonOnDisabledColor = Color.clear {
		didSet {
			styleForState(state: switchState)
		}
	}
	
	/// Track on disabled color.
	@IBInspectable
    open var trackOnDisabledColor = Color.clear {
		didSet {
			styleForState(state: switchState)
		}
	}
	
	/// Button off disabled color.
	@IBInspectable
    open var buttonOffDisabledColor = Color.clear {
		didSet {
			styleForState(state: switchState)
		}
	}
	
	/// Track off disabled color.
	@IBInspectable
    open var trackOffDisabledColor = Color.clear {
		didSet {
			styleForState(state: switchState)
		}
	}
	
	/// Track view reference.
	open fileprivate(set) var track: UIView {
		didSet {
			prepareTrack()
		}
	}
	
	/// Button view reference.
	open fileprivate(set) var button: FABButton {
		didSet {
			prepareButton()
		}
	}
	
	@IBInspectable
    open override var isEnabled: Bool {
		didSet {
			styleForState(state: internalSwitchState)
		}
	}
	
	/// A boolean indicating if the switch is on or not.
	@IBInspectable
    public var isOn: Bool {
		get {
			return .on == internalSwitchState
		}
		set(value) {
            updateSwitchState(state: value ? .on : .off, animated: true, isTriggeredByUserInteraction: false)
		}
	}

	/// Switch state.
	open var switchState: SwitchState {
		get {
			return internalSwitchState
		}
		set(value) {
			updateSwitchState(state: value, animated: true, isTriggeredByUserInteraction: false)
		}
	}
	
	/// Switch style.
	open var switchStyle = SwitchStyle.dark {
		didSet {
			switch switchStyle {
			case .light:
				buttonOnColor = Color.blue.darken2
				trackOnColor = Color.blue.lighten3
				buttonOffColor = Color.blueGrey.lighten4
				trackOffColor = Color.grey.lighten3
				buttonOnDisabledColor = Color.grey.lighten2
				trackOnDisabledColor = Color.grey.lighten3
				buttonOffDisabledColor = Color.grey.lighten2
				trackOffDisabledColor = Color.grey.lighten3
			case .dark:
				buttonOnColor = Color.blue.lighten1
				trackOnColor = Color.blue.lighten2.withAlphaComponent(0.5)
				buttonOffColor = Color.grey.lighten3
				trackOffColor = Color.blueGrey.lighten4.withAlphaComponent(0.5)
				buttonOnDisabledColor = Color.grey.darken3
				trackOnDisabledColor = Color.grey.lighten1.withAlphaComponent(0.2)
{'fraction_non_alphanumeric': 0.07878275570583262, 'fraction_numerical': 0.00650887573964497, 'mean_word_length': 3.375369822485207, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6925213', 'n_tokens_mistral': 3935, 'n_tokens_neox': 3466, 'n_words': 1183}