From a6855747b223efa726d593cf738e1525d495ebed Mon Sep 17 00:00:00 2001 From: Sandeep Pathak Date: Wed, 13 Jul 2022 13:35:01 +0530 Subject: [PATCH 1/2] Swift Package Support --- .build/workspace-state.json | 11 + .gitignore | 1 + .swiftformat | 2 + .swiftlint.yml | 102 +++++ Example/Tests/Tests.swift | 12 +- Example/mtpThemeManager/AppDelegate.swift | 9 +- Example/mtpThemeManager/Helpers.swift | 14 +- .../SettingsViewController.swift | 76 ++-- Example/mtpThemeManager/Themes.swift | 29 +- Example/mtpThemeManager/ViewController.swift | 14 +- Package.swift | 35 ++ .../mtpThemeManager}/Extensions.swift | 66 +-- .../mtpThemeManager}/NightModeStatus.swift | 4 +- .../mtpThemeManager}/Style+Extensions.swift | 115 ++---- Sources/mtpThemeManager/Style.swift | 358 +++++++++++++++++ Sources/mtpThemeManager/Theme.swift | 220 ++++++++++ .../ThemeManager+Appearance.swift | 299 ++++++-------- .../ThemeManager+Properties.swift | 251 ++++++++++++ .../mtpThemeManager}/ThemeManager.swift | 183 ++++----- .../mtpThemeManagerTests.swift | 12 + _Pods.xcodeproj | 1 - mtpThemeManager.podspec | 4 +- mtpThemeManager/Assets/.gitkeep | 0 mtpThemeManager/Classes/.gitkeep | 0 mtpThemeManager/Classes/Style.swift | 378 ------------------ mtpThemeManager/Classes/Theme.swift | 218 ---------- .../Classes/ThemeManager+Properties.swift | 323 --------------- 27 files changed, 1334 insertions(+), 1403 deletions(-) create mode 100644 .build/workspace-state.json create mode 100644 .swiftformat create mode 100644 .swiftlint.yml create mode 100644 Package.swift rename {mtpThemeManager/Classes => Sources/mtpThemeManager}/Extensions.swift (52%) rename {mtpThemeManager/Classes => Sources/mtpThemeManager}/NightModeStatus.swift (75%) rename {mtpThemeManager/Classes => Sources/mtpThemeManager}/Style+Extensions.swift (69%) create mode 100644 Sources/mtpThemeManager/Style.swift create mode 100644 Sources/mtpThemeManager/Theme.swift rename {mtpThemeManager/Classes => Sources/mtpThemeManager}/ThemeManager+Appearance.swift (62%) create mode 100644 Sources/mtpThemeManager/ThemeManager+Properties.swift rename {mtpThemeManager/Classes => Sources/mtpThemeManager}/ThemeManager.swift (55%) create mode 100644 Tests/mtpThemeManagerTests/mtpThemeManagerTests.swift delete mode 120000 _Pods.xcodeproj delete mode 100644 mtpThemeManager/Assets/.gitkeep delete mode 100644 mtpThemeManager/Classes/.gitkeep delete mode 100644 mtpThemeManager/Classes/Style.swift delete mode 100644 mtpThemeManager/Classes/Theme.swift delete mode 100644 mtpThemeManager/Classes/ThemeManager+Properties.swift diff --git a/.build/workspace-state.json b/.build/workspace-state.json new file mode 100644 index 0000000..b0528ff --- /dev/null +++ b/.build/workspace-state.json @@ -0,0 +1,11 @@ +{ + "object" : { + "artifacts" : [ + + ], + "dependencies" : [ + + ] + }, + "version" : 5 +} \ No newline at end of file diff --git a/.gitignore b/.gitignore index e7b722d..2cf5303 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,7 @@ profile DerivedData *.hmap *.ipa +.swiftpm/ # Bundler .bundle diff --git a/.swiftformat b/.swiftformat new file mode 100644 index 0000000..1d04c58 --- /dev/null +++ b/.swiftformat @@ -0,0 +1,2 @@ +--exclude Pods,Generated +--swiftversion 5.0 diff --git a/.swiftlint.yml b/.swiftlint.yml new file mode 100644 index 0000000..328754a --- /dev/null +++ b/.swiftlint.yml @@ -0,0 +1,102 @@ +excluded: + - DerivedData + - SourcePackages + +disabled_rules: + - discarded_notification_center_observer + - notification_center_detachment + - orphaned_doc_comment + - todo + - unused_capture_list + - trailing_comma + - opening_brace + +opt_in_rules: + - array_init + - attributes + - closure_end_indentation + - closure_spacing + - collection_alignment + - colon # promote to error + - convenience_type + - discouraged_object_literal + - empty_collection_literal + - empty_count + - empty_string + - enum_case_associated_values_count + - fatal_error_message + - first_where + - force_unwrapping + - last_where + - legacy_random + - literal_expression_end_indentation + - multiline_arguments + - multiline_function_chains + - multiline_literal_brackets + - multiline_parameters + - multiline_parameters_brackets + - operator_usage_whitespace + - overridden_super_call + - pattern_matching_keywords + - prefer_self_type_over_type_of_self + - redundant_nil_coalescing + - redundant_type_annotation + - toggle_bool + - trailing_closure + - unneeded_parentheses_in_closure_argument + - unused_import + - yoda_condition + + +custom_rules: + array_constructor: + name: "Array/Dictionary initializer" + regex: '[let,var] .+ = (\[.+\]\(\))' + capture_group: 1 + message: "Use explicit type annotation when initializing empty arrays and dictionaries" + severity: warning + + +attributes: + always_on_same_line: + - "@IBSegueAction" + - "@IBAction" + - "@NSManaged" + - "@objc" + +force_cast: warning +force_try: warning +function_body_length: + warning: 60 + +legacy_hashing: error + +identifier_name: + excluded: + - i + - id + - x + - y + - z + +indentation_width: + indentation_width: 4 + +line_length: + ignores_urls: true + ignores_function_declarations: true + ignores_comments: true + +multiline_arguments: + first_argument_location: next_line + only_enforce_after_first_closure_on_first_line: true + +private_over_fileprivate: + validate_extensions: true + +trailing_whitespace: + ignores_empty_lines: true + ignores_comments: true + +vertical_whitespace: + max_empty_lines: 2 diff --git a/Example/Tests/Tests.swift b/Example/Tests/Tests.swift index 4c69641..7bad9ef 100644 --- a/Example/Tests/Tests.swift +++ b/Example/Tests/Tests.swift @@ -1,29 +1,27 @@ +import mtpThemeManager import UIKit import XCTest -import mtpThemeManager class Tests: XCTestCase { - override func setUp() { super.setUp() // Put setup code here. This method is called before the invocation of each test method in the class. } - + override func tearDown() { // Put teardown code here. This method is called after the invocation of each test method in the class. super.tearDown() } - + func testExample() { // This is an example of a functional test case. XCTAssert(true, "Pass") } - + func testPerformanceExample() { // This is an example of a performance test case. - self.measure() { + measure { // Put the code you want to measure the time of here. } } - } diff --git a/Example/mtpThemeManager/AppDelegate.swift b/Example/mtpThemeManager/AppDelegate.swift index 15b300b..4977113 100644 --- a/Example/mtpThemeManager/AppDelegate.swift +++ b/Example/mtpThemeManager/AppDelegate.swift @@ -6,19 +6,16 @@ // Copyright (c) 2017 mostafa.taghipour@ymail.com. All rights reserved. // -import UIKit import mtpThemeManager +import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { - var window: UIWindow? - - func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { + + func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. ThemeManager.shared.setTheme(dayNight: RedTheme()) return true } - } - diff --git a/Example/mtpThemeManager/Helpers.swift b/Example/mtpThemeManager/Helpers.swift index b281b63..ffd02d9 100644 --- a/Example/mtpThemeManager/Helpers.swift +++ b/Example/mtpThemeManager/Helpers.swift @@ -8,13 +8,11 @@ import UIKit - extension UIPickerView { - private struct associationKey{ + private struct associationKey { static var selectorColorAssociationKey: UInt8 = 0 } - - + @IBInspectable dynamic var selectorColor: UIColor? { get { return objc_getAssociatedObject(self, &associationKey.selectorColorAssociationKey) as? UIColor @@ -24,16 +22,14 @@ extension UIPickerView { objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN) } } - - open override func didAddSubview(_ subview: UIView) { + + override open func didAddSubview(_ subview: UIView) { super.didAddSubview(subview) - + if let color = selectorColor { if subview.bounds.height < 1.0 { subview.backgroundColor = color } } } - - } diff --git a/Example/mtpThemeManager/SettingsViewController.swift b/Example/mtpThemeManager/SettingsViewController.swift index 39f7b90..da31da3 100644 --- a/Example/mtpThemeManager/SettingsViewController.swift +++ b/Example/mtpThemeManager/SettingsViewController.swift @@ -6,42 +6,38 @@ // Copyright © 2017 CocoaPods. All rights reserved. // -import UIKit import mtpThemeManager +import UIKit class SettingsViewController: UIViewController { - - @IBOutlet weak var themePicker: UIPickerView! - @IBOutlet weak var nightPicker: UIPickerView! - - let themeManager=ThemeManager.shared - var allThemes:[Theme]! - + @IBOutlet var themePicker: UIPickerView! + @IBOutlet var nightPicker: UIPickerView! + + let themeManager = ThemeManager.shared + var allThemes: [Theme]! + override func viewDidLoad() { super.viewDidLoad() - - themePicker.dataSource=self - themePicker.delegate=self + themePicker.dataSource = self + themePicker.delegate = self themePicker.selectorColor = secondaryTextColor - - allThemes=themeManager.availableThemes - if let currentTheme=themeManager.currentTheme , let selectedValue=allThemes.firstIndex(where: {$0.id==currentTheme.id}){ + + allThemes = themeManager.availableThemes + if let currentTheme = themeManager.currentTheme, let selectedValue = allThemes.firstIndex(where: { $0.id == currentTheme.id }) { themePicker.selectRow(selectedValue, inComponent: 0, animated: false) } - - nightPicker.dataSource=self - nightPicker.delegate=self + + nightPicker.dataSource = self + nightPicker.delegate = self nightPicker.selectorColor = secondaryTextColor - - - if let selectedValue=NightModeStatus.all.firstIndex(of: themeManager.nightModeStatus ){ + + if let selectedValue = NightModeStatus.all.firstIndex(of: themeManager.nightModeStatus) { nightPicker.selectRow(selectedValue, inComponent: 0, animated: false) } } - - - func reloadAllViewControllers(){ + + func reloadAllViewControllers() { let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let navigationController = mainStoryboard.instantiateViewController(withIdentifier: "mainNavigationController") as! UINavigationController let settingVC = mainStoryboard.instantiateViewController(withIdentifier: "settingVC") @@ -50,37 +46,31 @@ class SettingsViewController: UIViewController { } } - -extension SettingsViewController:UIPickerViewDelegate,UIPickerViewDataSource{ - func numberOfComponents(in pickerView: UIPickerView) -> Int { - return 1 +extension SettingsViewController: UIPickerViewDelegate, UIPickerViewDataSource { + func numberOfComponents(in _: UIPickerView) -> Int { + return 1 } - - func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { - + + func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent _: Int) -> Int { return pickerView === themePicker ? allThemes.count : NightModeStatus.all.count } - - func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { + + func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent _: Int) -> String? { return pickerView === themePicker ? allThemes[row].displayName : "\(NightModeStatus.all[row])" } - - func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { - if (pickerView === themePicker){ + + func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent _: Int) { + if pickerView === themePicker { themeManager.setTheme(dayNight: allThemes[row] as! AppTheme) - } - else{ + } else { themeManager.nightModeStatus = NightModeStatus.all[row] } reloadAllViewControllers() } - - func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? { + + func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent _: Int) -> NSAttributedString? { let titleData = pickerView === themePicker ? allThemes[row].displayName : "\(NightModeStatus.all[row])" - let myTitle = NSAttributedString(string: titleData, attributes: [NSAttributedString.Key.foregroundColor:themeManager.primaryTextColor!]) + let myTitle = NSAttributedString(string: titleData, attributes: [NSAttributedString.Key.foregroundColor: themeManager.primaryTextColor!]) return myTitle } } - - - diff --git a/Example/mtpThemeManager/Themes.swift b/Example/mtpThemeManager/Themes.swift index adc6bd5..8602cda 100644 --- a/Example/mtpThemeManager/Themes.swift +++ b/Example/mtpThemeManager/Themes.swift @@ -6,40 +6,39 @@ // Copyright © 2017 CocoaPods. All rights reserved. // -import UIKit import mtpThemeManager +import UIKit -protocol AppTheme:DayNightTheme {} +protocol AppTheme: DayNightTheme {} -class RedTheme:AppTheme { - required init() {} - var id: Int=1 - var displayName: String="Red" +class RedTheme: AppTheme { + required init() {} + var id: Int = 1 + var displayName: String = "Red" var tintColor: UIColor = .red - } -class BlueTheme:AppTheme { - required init() {} - var id: Int=2 - var displayName: String="Blue" +class BlueTheme: AppTheme { + required init() {} + var id: Int = 2 + var displayName: String = "Blue" var tintColor: UIColor = .blue } -extension AppTheme{ +extension AppTheme { var secondaryTextColor: UIColor { return UIColor.gray.withAlphaComponent(0.8) } - + var secondaryTextColorNight: UIColor { return UIColor.gray.withAlphaComponent(0.9) } } -var currentTheme : AppTheme { +var currentTheme: AppTheme { return ThemeManager.shared.currentTheme as! AppTheme } -var secondaryTextColor:UIColor{ +var secondaryTextColor: UIColor { return ThemeManager.shared.isItNight ? currentTheme.secondaryTextColorNight : currentTheme.secondaryTextColor } diff --git a/Example/mtpThemeManager/ViewController.swift b/Example/mtpThemeManager/ViewController.swift index 9d4cd7c..dc8b0ff 100644 --- a/Example/mtpThemeManager/ViewController.swift +++ b/Example/mtpThemeManager/ViewController.swift @@ -6,27 +6,21 @@ // Copyright (c) 2017 mostafa.taghipour@ymail.com. All rights reserved. // -import UIKit import mtpThemeManager +import UIKit class ViewController: UIViewController { - - @IBOutlet weak var subtitleLabel: UILabel! - + @IBOutlet var subtitleLabel: UILabel! + override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. - - subtitleLabel.textColor = secondaryTextColor + subtitleLabel.textColor = secondaryTextColor } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } - } - - - diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..b143f90 --- /dev/null +++ b/Package.swift @@ -0,0 +1,35 @@ +// swift-tools-version: 5.6 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "mtpThemeManager", + platforms: [ + .iOS(.v11), + .macOS(.v10_12), + ], + products: [ + // Products define the executables and libraries a package produces, and make them visible to other packages. + .library( + name: "mtpThemeManager", + targets: ["mtpThemeManager"] + ), + ], + dependencies: [ + // Dependencies declare other packages that this package depends on. + // .package(url: /* package url */, from: "1.0.0"), + ], + targets: [ + // Targets are the basic building blocks of a package. A target can define a module or a test suite. + // Targets can depend on other targets in this package, and on products in packages this package depends on. + .target( + name: "mtpThemeManager", + dependencies: [] + ), + .testTarget( + name: "mtpThemeManagerTests", + dependencies: ["mtpThemeManager"] + ), + ] +) diff --git a/mtpThemeManager/Classes/Extensions.swift b/Sources/mtpThemeManager/Extensions.swift similarity index 52% rename from mtpThemeManager/Classes/Extensions.swift rename to Sources/mtpThemeManager/Extensions.swift index b1733e8..aede5b8 100644 --- a/mtpThemeManager/Classes/Extensions.swift +++ b/Sources/mtpThemeManager/Extensions.swift @@ -8,45 +8,32 @@ import UIKit - -extension Notification.Name -{ - public static let ThemeDidChange = Notification.Name("themeChanged") +extension Notification.Name { + public static let ThemeDidChange = Notification.Name("themeChanged") } - - -extension UIViewController{ - - open override func awakeFromNib() { +extension UIViewController { + override open func awakeFromNib() { super.awakeFromNib() - if let bgColor=ThemeManager.shared.backgroundColor{ - - if view.backgroundColor != bgColor{ + if let bgColor = ThemeManager.shared.backgroundColor { + if view.backgroundColor != bgColor { view.backgroundColor = bgColor - } - } - else{ + } else { let when = DispatchTime.now() + 0.2 DispatchQueue.main.asyncAfter(deadline: when) { - if let bgColor=ThemeManager.shared.backgroundColor{ - - if self.view.backgroundColor != bgColor{ + if let bgColor = ThemeManager.shared.backgroundColor { + if self.view.backgroundColor != bgColor { self.view.backgroundColor = bgColor - } } } } } - } - extension UIView { - @IBInspectable dynamic var cornerRadius: CGFloat - { + @IBInspectable dynamic var cornerRadius: CGFloat { get { return layer.cornerRadius } @@ -55,8 +42,8 @@ extension UIView { layer.masksToBounds = newValue > 0 } } - @IBInspectable dynamic var borderWidth: CGFloat - { + + @IBInspectable dynamic var borderWidth: CGFloat { get { return layer.borderWidth } @@ -64,49 +51,40 @@ extension UIView { layer.borderWidth = newValue } } - @IBInspectable dynamic var borderColor: UIColor? - { - set{ self.layer.borderColor = newValue?.cgColor } - get{ return UIColor(cgColor: self.layer.borderColor!) } + + @IBInspectable dynamic var borderColor: UIColor? { + set { layer.borderColor = newValue?.cgColor } + get { return UIColor(cgColor: layer.borderColor!) } } } - -extension UITextField{ +extension UITextField { @IBInspectable dynamic var placeHolderColor: UIColor? { get { return self.placeHolderColor } set { - self.attributedPlaceholder = NSAttributedString(string:self.placeholder != nil ? self.placeholder! : "", attributes:[NSAttributedString.Key.foregroundColor: newValue!]) + attributedPlaceholder = NSAttributedString(string: placeholder != nil ? placeholder! : "", attributes: [NSAttributedString.Key.foregroundColor: newValue!]) } } } - -extension UITextView{ +extension UITextView { @IBInspectable dynamic var keyboardAppear: UIKeyboardAppearance { get { - return self.keyboardAppearance + return keyboardAppearance } set { - self.keyboardAppearance=newValue + keyboardAppearance = newValue } } } - // fixing Bug in XCode // http://openradar.appspot.com/18448072 extension UIImageView { override open func awakeFromNib() { super.awakeFromNib() - self.tintColorDidChange() + tintColorDidChange() } } - - - - - - diff --git a/mtpThemeManager/Classes/NightModeStatus.swift b/Sources/mtpThemeManager/NightModeStatus.swift similarity index 75% rename from mtpThemeManager/Classes/NightModeStatus.swift rename to Sources/mtpThemeManager/NightModeStatus.swift index 4003866..d8d8a7e 100644 --- a/mtpThemeManager/Classes/NightModeStatus.swift +++ b/Sources/mtpThemeManager/NightModeStatus.swift @@ -12,6 +12,6 @@ public enum NightModeStatus { case disable case enable case auto - - public static var all : [NightModeStatus] = [.disable , .enable , .auto] + + public static var all: [NightModeStatus] = [.disable, .enable, .auto] } diff --git a/mtpThemeManager/Classes/Style+Extensions.swift b/Sources/mtpThemeManager/Style+Extensions.swift similarity index 69% rename from mtpThemeManager/Classes/Style+Extensions.swift rename to Sources/mtpThemeManager/Style+Extensions.swift index ae96e7a..2c5fcd6 100644 --- a/mtpThemeManager/Classes/Style+Extensions.swift +++ b/Sources/mtpThemeManager/Style+Extensions.swift @@ -8,42 +8,37 @@ import UIKit -extension UINavigationBar{ - public func setStyle(style:NavigationBarStyle) { +extension UINavigationBar { + public func setStyle(style: NavigationBarStyle) { tintColor = style.tintColor titleTextAttributes = style.titleColor != nil ? [NSAttributedString.Key.foregroundColor: style.titleColor!] : UINavigationBar.appearance().titleTextAttributes barStyle = style.barStyle isTranslucent = style.isTranslucent || style.isTransparent shadowImage = style.isHairlineHidden || style.isTransparent ? UIImage() : ThemeManager.shared.default_bar_shadow - setBackgroundImage(style.isTransparent ? UIImage() : ThemeManager.shared.default_navigationBarBackground , for:.default) + setBackgroundImage(style.isTransparent ? UIImage() : ThemeManager.shared.default_navigationBarBackground, for: .default) barTintColor = style.backgroundColor isHidden = style.isHidden } } - - -extension UITabBar{ - public func setStyle(style:TabBarStyle) { - - if #available(iOS 10, *),let color=style.unselectedTintColor { +extension UITabBar { + public func setStyle(style: TabBarStyle) { + if #available(iOS 10, *), let color = style.unselectedTintColor { unselectedItemTintColor = color } - + tintColor = style.tintColor barTintColor = style.backgroundColor barStyle = style.barStyle isTranslucent = style.isTranslucent || style.isTransparent shadowImage = style.isHairlineHidden || style.isTransparent ? nil : ThemeManager.shared.default_bar_shadow clipsToBounds = style.isHairlineHidden || style.isTransparent - backgroundImage=style.isTransparent ? UIImage() : ThemeManager.shared.default_tabBarBackground - + backgroundImage = style.isTransparent ? UIImage() : ThemeManager.shared.default_tabBarBackground } } - -extension UIToolbar{ - public func setStyle(style:ToolbarStyle) { +extension UIToolbar { + public func setStyle(style: ToolbarStyle) { tintColor = style.tintColor barTintColor = style.backgroundColor barStyle = style.barStyle @@ -51,36 +46,32 @@ extension UIToolbar{ setShadowImage(style.isHairlineHidden || style.isTransparent ? UIImage() : ThemeManager.shared.default_bar_shadow, forToolbarPosition: .any) clipsToBounds = style.isHairlineHidden || style.isTransparent setBackgroundImage(style.isTransparent ? UIImage() : ThemeManager.shared.default_toolbarBackground, forToolbarPosition: .any, barMetrics: .default) - } } - -extension UISearchBar{ - public func setStyle(style:SearchBarStyle) { - +extension UISearchBar { + public func setStyle(style: SearchBarStyle) { barTintColor = style.backgroundColor tintColor = style.tintColor searchBarStyle = .minimal barStyle = style.barStyle isTranslucent = style.isTranslucent || style.isTransparent setBackgroundImage(style.isTransparent ? UIImage() : ThemeManager.shared.default_searchBarBackground, for: .any, barMetrics: .default) - - if let fieldStyle=style.textFieldStyle , - let textField=value(forKey: "searchField") as? UITextField{ + + if let fieldStyle = style.textFieldStyle, + let textField = value(forKey: "searchField") as? UITextField + { textField.backgroundColor = fieldStyle.backgroundColor ?? ThemeManager.shared.default_searchFieldColor - textField.borderColor=fieldStyle.borderColor - textField.borderWidth=fieldStyle.borderWidth - textField.cornerRadius=fieldStyle.cornerRadius + textField.borderColor = fieldStyle.borderColor + textField.borderWidth = fieldStyle.borderWidth + textField.cornerRadius = fieldStyle.cornerRadius textField.clearButtonMode = fieldStyle.clearButtonMode } - } } -extension UITextField{ - public func setStyle(style:TextFieldStyle) { - +extension UITextField { + public func setStyle(style: TextFieldStyle) { borderStyle = style.borderStyle textColor = style.textColor borderColor = style.borderColor @@ -89,105 +80,87 @@ extension UITextField{ backgroundColor = style.backgroundColor clearButtonMode = style.clearButtonMode placeHolderColor = style.placeholderColor - } } - -extension UITextView{ - public func setStyle(style:TextViewStyle) { - +extension UITextView { + public func setStyle(style: TextViewStyle) { textColor = style.textColor borderColor = style.borderColor cornerRadius = style.cornerRadius borderWidth = style.borderWidth backgroundColor = style.backgroundColor textContainerInset = style.textContainerInset ?? ThemeManager.shared.default_textViewContainrInsets - clipsToBounds=true - + clipsToBounds = true } - } - -extension UIActivityIndicatorView{ - public func setStyle(style:ActivityIndicatorViewStyle) { +extension UIActivityIndicatorView { + public func setStyle(style: ActivityIndicatorViewStyle) { self.style = style.style - self.color = style.color - + color = style.color } } - -extension UIScrollView{ - public func setStyle(style:ScrollViewStyle) { +extension UIScrollView { + public func setStyle(style: ScrollViewStyle) { UIScrollView.appearance().indicatorStyle = style.indicatorStyle } } - -extension UISegmentedControl{ - public func setStyle(style:SegmentedControlStyle) { +extension UISegmentedControl { + public func setStyle(style: SegmentedControlStyle) { tintColor = style.tintColor backgroundColor = style.backgroundColor } } - - -extension UIStepper{ - public func setStyle(style:StepperStyle) { +extension UIStepper { + public func setStyle(style: StepperStyle) { tintColor = style.tintColor backgroundColor = style.backgroundColor } } - -extension UIPageControl{ - public func setStyle(style:PageControlStyle) { +extension UIPageControl { + public func setStyle(style: PageControlStyle) { currentPageIndicatorTintColor = style.currentPageIndicatorTintColor pageIndicatorTintColor = style.pageIndicatorTintColor } } - - -extension UIProgressView{ - public func setStyle(style:ProgressViewStyle) { +extension UIProgressView { + public func setStyle(style: ProgressViewStyle) { progressTintColor = style.progressTintColor trackTintColor = style.trackTintColor progressViewStyle = style.style } } - -extension UISlider{ - public func setStyle(style:SliderStyle) { +extension UISlider { + public func setStyle(style: SliderStyle) { minimumTrackTintColor = style.minimumTrackTintColor thumbTintColor = style.thumbTintColor maximumTrackTintColor = style.maximumTrackTintColor } } - -extension UISwitch{ - public func setStyle(style:SwitchStyle) { +extension UISwitch { + public func setStyle(style: SwitchStyle) { onTintColor = style.onTintColor thumbTintColor = style.thumbTintColor tintColor = style.tintColor } } - -extension UIButton{ - public func setStyle(style:ButtonStyle) { +extension UIButton { + public func setStyle(style: ButtonStyle) { tintColor = style.tintColor borderColor = style.borderColor cornerRadius = style.cornerRadius borderWidth = style.borderWidth backgroundColor = style.backgroundColor contentEdgeInsets = style.contentEdgeInsets ?? ThemeManager.shared.default_buttonContentInsets - clipsToBounds=true + clipsToBounds = true } } - diff --git a/Sources/mtpThemeManager/Style.swift b/Sources/mtpThemeManager/Style.swift new file mode 100644 index 0000000..a7d1204 --- /dev/null +++ b/Sources/mtpThemeManager/Style.swift @@ -0,0 +1,358 @@ +// +// Style.swift +// ThemeManager +// +// Created by Mostafa Taghipour on 11/3/17. +// Copyright © 2017 RainyDay. All rights reserved. +// + +import UIKit + +public protocol Style {} + +// MARK: - StatusBarStyle + +public struct StatusBarStyle: Style { + public var backgroundColor: UIColor? + public var style: UIStatusBarStyle = .default + public var isHidden: Bool = false + + public init(backgroundColor: UIColor? = nil, + style: UIStatusBarStyle = .default, + isHidden: Bool = false) + { + self.backgroundColor = backgroundColor + self.style = style + self.isHidden = isHidden + } +} + +// MARK: - NavigationBarStyle + +public struct NavigationBarStyle: Style { + public var backgroundColor: UIColor? + public var titleColor: UIColor? + public var tintColor: UIColor? + public var barStyle: UIBarStyle = .default + public var isTranslucent: Bool = true + public var isHidden: Bool = false + public var isTransparent: Bool = false + public var isHairlineHidden: Bool = false + + public init(backgroundColor: UIColor? = nil, + titleColor: UIColor? = nil, + tintColor: UIColor? = nil, + barStyle: UIBarStyle = .default, + isTranslucent: Bool = true, + isHidden: Bool = false, + isTransparent: Bool = false, + isHairlineHidden: Bool = false) + { + self.backgroundColor = backgroundColor + self.titleColor = titleColor + self.tintColor = tintColor + self.barStyle = barStyle + self.isTransparent = isTransparent + self.isTranslucent = isTranslucent + self.isHidden = isHidden + self.isHairlineHidden = isHairlineHidden + } +} + +// MARK: - TabBarStyle + +public struct TabBarStyle: Style { + public var backgroundColor: UIColor? + public var badgeColor: UIColor? + public var tintColor: UIColor? + public var unselectedTintColor: UIColor? + public var barStyle: UIBarStyle = .default + public var isTranslucent: Bool = true + public var isTransparent: Bool = false + public var isHairlineHidden: Bool = false + + public init(backgroundColor: UIColor? = nil, + badgeColor: UIColor? = nil, + tintColor: UIColor? = nil, + unselectedTintColor: UIColor? = nil, + barStyle: UIBarStyle = .default, + isTranslucent: Bool = true, + isTransparent: Bool = false, + isHairlineHidden: Bool = false) + { + self.backgroundColor = backgroundColor + self.badgeColor = badgeColor + self.tintColor = tintColor + self.unselectedTintColor = unselectedTintColor + self.barStyle = barStyle + self.isTransparent = isTransparent + self.isTranslucent = isTranslucent + self.isHairlineHidden = isHairlineHidden + } +} + +// MARK: - ToolbarStyle + +public struct ToolbarStyle: Style { + public var backgroundColor: UIColor? + public var tintColor: UIColor? + public var barStyle: UIBarStyle = .default + public var isTranslucent: Bool = true + public var isTransparent: Bool = false + public var isHairlineHidden: Bool = false + + public init(backgroundColor: UIColor? = nil, + tintColor: UIColor? = nil, + barStyle: UIBarStyle = .default, + isTranslucent: Bool = true, + isTransparent: Bool = false, + isHairlineHidden: Bool = false) + { + self.backgroundColor = backgroundColor + self.tintColor = tintColor + self.barStyle = barStyle + self.isTransparent = isTransparent + self.isTranslucent = isTranslucent + self.isHairlineHidden = isHairlineHidden + } +} + +// MARK: - SearchBarStyle + +public struct SearchBarStyle: Style { + public var backgroundColor: UIColor? + public var tintColor: UIColor? + public var barStyle: UIBarStyle = .default + public var searchBarStyle: UISearchBar.Style = .default + public var isTranslucent: Bool = true + public var isTransparent: Bool = false + public var textFieldStyle: TextFieldStyle? + + public init(backgroundColor: UIColor? = nil, + tintColor: UIColor? = nil, + barStyle: UIBarStyle = .default, + searchStyle: UISearchBar.Style = .default, + isTranslucent: Bool = true, + isTransparent: Bool = false, + textFieldStyle: TextFieldStyle? = nil) + { + self.backgroundColor = backgroundColor + self.tintColor = tintColor + self.barStyle = barStyle + searchBarStyle = searchStyle + self.isTransparent = isTransparent + self.isTranslucent = isTranslucent + self.textFieldStyle = textFieldStyle + } +} + +// MARK: - TextFieldStyle + +public struct TextFieldStyle: Style { + public var borderColor: UIColor? + public var borderWidth: CGFloat = 0 + public var cornerRadius: CGFloat = 0 + public var backgroundColor: UIColor? + public var textColor: UIColor? + public var placeholderColor: UIColor? + public var borderStyle: UITextField.BorderStyle = .roundedRect + public var clearButtonMode: UITextField.ViewMode = .never + + public init(backgroundColor: UIColor? = nil, + textColor: UIColor? = nil, + borderColor: UIColor? = nil, + borderWidth: CGFloat = 0, + cornerRadius: CGFloat = 0, + placeholderColor: UIColor? = nil, + borderStyle: UITextField.BorderStyle = .roundedRect, + clearButton: UITextField.ViewMode = .never) + { + self.backgroundColor = backgroundColor + self.textColor = textColor + self.borderColor = borderColor + self.borderWidth = borderWidth + self.cornerRadius = cornerRadius + self.borderStyle = borderStyle + clearButtonMode = clearButton + self.placeholderColor = placeholderColor + } +} + +// MARK: - TextViewStyle + +public struct TextViewStyle: Style { + public var backgroundColor: UIColor? + public var textColor: UIColor? + public var borderColor: UIColor? + public var borderWidth: CGFloat = 0 + public var cornerRadius: CGFloat = 0 + public var textContainerInset: UIEdgeInsets? + + public init(backgroundColor: UIColor? = nil, + textColor: UIColor? = nil, + borderColor: UIColor? = nil, + borderWidth: CGFloat = 0, + cornerRadius: CGFloat = 0, + textContainerInset: UIEdgeInsets? = nil) + { + self.backgroundColor = backgroundColor + self.textColor = textColor + self.borderColor = borderColor + self.borderWidth = borderWidth + self.cornerRadius = cornerRadius + self.textContainerInset = textContainerInset + } +} + +// MARK: - ButtonStyle + +public struct ButtonStyle: Style { + public var backgroundColor: UIColor? + public var tintColor: UIColor? + public var borderColor: UIColor? + public var borderWidth: CGFloat = 0 + public var cornerRadius: CGFloat = 0 + public var contentEdgeInsets: UIEdgeInsets? + + public init(backgroundColor: UIColor? = nil, + tintColor: UIColor? = nil, + borderColor: UIColor? = nil, + borderWidth: CGFloat = 0, + cornerRadius: CGFloat = 0, + contentEdgeInsets: UIEdgeInsets? = nil) + { + self.backgroundColor = backgroundColor + self.tintColor = tintColor + self.borderColor = borderColor + self.borderWidth = borderWidth + self.cornerRadius = cornerRadius + self.contentEdgeInsets = contentEdgeInsets + } +} + +// MARK: - ActivityIndicatorViewStyle + +public struct ActivityIndicatorViewStyle: Style { + public var style: UIActivityIndicatorView.Style = .gray + public var color: UIColor? + + public init(style: UIActivityIndicatorView.Style = .gray, + color: UIColor? = nil) + { + self.style = style + self.color = color + } +} + +// MARK: - ScrollViewStyle + +public struct ScrollViewStyle: Style { + public var indicatorStyle: UIScrollView.IndicatorStyle = .default + + public init(indicatorStyle: UIScrollView.IndicatorStyle = .default) { + self.indicatorStyle = indicatorStyle + } +} + +// MARK: - SegmentedControlStyle + +public struct SegmentedControlStyle: Style { + public var backgroundColor: UIColor? + public var tintColor: UIColor? + + public init(backgroundColor: UIColor? = nil, + tintColor: UIColor? = nil) + { + self.backgroundColor = backgroundColor + self.tintColor = tintColor + } +} + +// MARK: - StepperStyle + +public struct StepperStyle: Style { + public var backgroundColor: UIColor? + public var tintColor: UIColor? + + public init(backgroundColor: UIColor? = nil, + tintColor: UIColor? = nil) + { + self.backgroundColor = backgroundColor + self.tintColor = tintColor + } +} + +// MARK: - PageControlStyle + +public struct PageControlStyle: Style { + public var pageIndicatorTintColor: UIColor? + public var currentPageIndicatorTintColor: UIColor? + + public init(pageIndicatorTintColor: UIColor? = nil, + currentPageIndicatorTintColor: UIColor? = nil) + { + self.pageIndicatorTintColor = pageIndicatorTintColor + self.currentPageIndicatorTintColor = currentPageIndicatorTintColor + } +} + +// MARK: - ProgressViewStyle + +public struct ProgressViewStyle: Style { + public var progressTintColor: UIColor? + public var trackTintColor: UIColor? + public var style: UIProgressView.Style = .default + + public init(progressTintColor: UIColor? = nil, + trackTintColor: UIColor? = nil, + style: UIProgressView.Style = .default) + { + self.progressTintColor = progressTintColor + self.trackTintColor = trackTintColor + self.style = style + } +} + +// MARK: - SliderStyle + +public struct SliderStyle: Style { + public var minimumTrackTintColor: UIColor? + public var maximumTrackTintColor: UIColor? + public var thumbTintColor: UIColor? + + public init(minimumTrackTintColor: UIColor? = nil, + maximumTrackTintColor: UIColor? = nil, + thumbTintColor: UIColor? = nil) + { + self.minimumTrackTintColor = minimumTrackTintColor + self.maximumTrackTintColor = maximumTrackTintColor + self.thumbTintColor = thumbTintColor + } +} + +// MARK: - SwitchStyle + +public struct SwitchStyle: Style { + public var thumbTintColor: UIColor? + public var onTintColor: UIColor? + public var tintColor: UIColor? + + public init(tintColor: UIColor? = nil, + onTintColor: UIColor? = nil, + thumbTintColor: UIColor? = nil) + { + self.tintColor = tintColor + self.onTintColor = onTintColor + self.thumbTintColor = thumbTintColor + } +} + +// MARK: - KeyboardStyle + +public struct KeyboardStyle: Style { + public var appearance: UIKeyboardAppearance = .default + + public init(appearance: UIKeyboardAppearance = .default) { + self.appearance = appearance + } +} diff --git a/Sources/mtpThemeManager/Theme.swift b/Sources/mtpThemeManager/Theme.swift new file mode 100644 index 0000000..4b354e3 --- /dev/null +++ b/Sources/mtpThemeManager/Theme.swift @@ -0,0 +1,220 @@ +// +// Theme.swift +// ThemeManager +// +// Created by Mostafa Taghipour on 11/3/17. +// Copyright © 2017 RainyDay. All rights reserved. +// + +import UIKit + +// MARK: - Theme + +public protocol Theme { + init() + + var id: Int { get } + var displayName: String { get } + var tintColor: UIColor { get } + var textColor: UIColor { get } + var backgroundColor: UIColor { get } + var navigationBarStyle: NavigationBarStyle { get } + var tabBarStyle: TabBarStyle { get } + var toolbarStyle: ToolbarStyle { get } + var searchBarStyle: SearchBarStyle { get } + var statusBarStyle: StatusBarStyle { get } + var keyboardStyle: KeyboardStyle { get } + var buttonStyle: ButtonStyle { get } + var textFieldStyle: TextFieldStyle { get } + var textViewStyle: TextViewStyle { get } + var activityIndicatorViewStyle: ActivityIndicatorViewStyle { get } + var scrollViewStyle: ScrollViewStyle { get } + var segmentedControlStyle: SegmentedControlStyle { get } + var stepperStyle: StepperStyle { get } + var pageControlStyle: PageControlStyle { get } + var progressViewStyle: ProgressViewStyle { get } + var sliderStyle: SliderStyle { get } + var switchStyle: SwitchStyle { get } +} + +public extension Theme { + var backgroundColor: UIColor { return .white } + + var textColor: UIColor { return UIColor.darkText } + + var navigationBarStyle: NavigationBarStyle { return NavigationBarStyle() } + + var tabBarStyle: TabBarStyle { return TabBarStyle() } + + var toolbarStyle: ToolbarStyle { return ToolbarStyle() } + + var searchBarStyle: SearchBarStyle { return SearchBarStyle(textFieldStyle: TextFieldStyle(backgroundColor: nil, + textColor: textColor, + borderColor: nil, + borderWidth: 0, + cornerRadius: 8.0, + placeholderColor: .lightGray)) } + + var statusBarStyle: StatusBarStyle { return StatusBarStyle() } + + var keyboardStyle: KeyboardStyle { return KeyboardStyle() } + + var buttonStyle: ButtonStyle { + return ButtonStyle() + } + + var textFieldStyle: TextFieldStyle { + return TextFieldStyle(backgroundColor: .clear, + textColor: textColor, + borderColor: .lightGray, + borderWidth: 0.5, + cornerRadius: 8.0, + placeholderColor: .lightGray) + } + + var textViewStyle: TextViewStyle { + return TextViewStyle(textColor: textColor) + } + + var activityIndicatorViewStyle: ActivityIndicatorViewStyle { + return ActivityIndicatorViewStyle() + } + + var scrollViewStyle: ScrollViewStyle { + return ScrollViewStyle() + } + + var segmentedControlStyle: SegmentedControlStyle { + return SegmentedControlStyle() + } + + var stepperStyle: StepperStyle { + return StepperStyle() + } + + var pageControlStyle: PageControlStyle { + return PageControlStyle(pageIndicatorTintColor: UIColor.lightGray.withAlphaComponent(0.5), currentPageIndicatorTintColor: tintColor) + } + + var progressViewStyle: ProgressViewStyle { + return ProgressViewStyle() + } + + var sliderStyle: SliderStyle { + return SliderStyle() + } + + var switchStyle: SwitchStyle { + return SwitchStyle() + } +} + +// MARK: - DayNight + +public protocol DayNightTheme: Theme { + var tintColorNight: UIColor { get } + var textColorNight: UIColor { get } + var backgroundColorNight: UIColor { get } + var navigationBarStyleNight: NavigationBarStyle { get } + var tabBarStyleNight: TabBarStyle { get } + var toolbarStyleNight: ToolbarStyle { get } + var searchBarStyleNight: SearchBarStyle { get } + var statusBarStyleNight: StatusBarStyle { get } + var keyboardStyleNight: KeyboardStyle { get } + var buttonStyleNight: ButtonStyle { get } + var textFieldStyleNight: TextFieldStyle { get } + var textViewStyleNight: TextViewStyle { get } + var activityIndicatorViewStyleNight: ActivityIndicatorViewStyle { get } + var scrollViewStyleNight: ScrollViewStyle { get } + var segmentedControlStyleNight: SegmentedControlStyle { get } + var stepperStyleNight: StepperStyle { get } + var pageControlStyleNight: PageControlStyle { get } + var progressViewStyleNight: ProgressViewStyle { get } + var sliderStyleNight: SliderStyle { get } + var switchStyleNight: SwitchStyle { get } +} + +public extension DayNightTheme { + var tintColorNight: UIColor { return .white } + + var textColorNight: UIColor { return UIColor.lightText } + + var backgroundColorNight: UIColor { return UIColor(red: 31 / 255, green: 33 / 255, blue: 36 / 255, alpha: 1) } + + var navigationBarStyleNight: NavigationBarStyle { return NavigationBarStyle(barStyle: .black) } + + var tabBarStyleNight: TabBarStyle { return TabBarStyle(barStyle: .black) } + + var toolbarStyleNight: ToolbarStyle { return ToolbarStyle(barStyle: .black) } + + var searchBarStyleNight: SearchBarStyle { return SearchBarStyle(barStyle: .black, + textFieldStyle: TextFieldStyle(backgroundColor: nil, + textColor: textColorNight, + borderColor: nil, + borderWidth: 0, + cornerRadius: 8.0, + placeholderColor: UIColor.white.withAlphaComponent(0.2))) } + + var statusBarStyleNight: StatusBarStyle { return StatusBarStyle(style: .lightContent) } + + var keyboardStyleNight: KeyboardStyle { return KeyboardStyle(appearance: .dark) } + + var textFieldStyleNight: TextFieldStyle { + return TextFieldStyle(backgroundColor: .clear, + textColor: textColorNight, + borderColor: UIColor.white.withAlphaComponent(0.3), + borderWidth: 0.5, + cornerRadius: 8.0, + placeholderColor: UIColor.white.withAlphaComponent(0.3)) + } + + var textViewStyleNight: TextViewStyle { + return TextViewStyle(textColor: textColorNight) + } + + var activityIndicatorViewStyleNight: ActivityIndicatorViewStyle { + return ActivityIndicatorViewStyle(color: .white) + } + + var scrollViewStyleNight: ScrollViewStyle { + return ScrollViewStyle(indicatorStyle: .white) + } + + var segmentedControlStyleNight: SegmentedControlStyle { + return SegmentedControlStyle() + } + + var stepperStyleNight: StepperStyle { + return StepperStyle() + } + + var pageControlStyleNight: PageControlStyle { + return PageControlStyle() + } + + var progressViewStyleNight: ProgressViewStyle { + return ProgressViewStyle() + } + + var sliderStyleNight: SliderStyle { + return SliderStyle() + } + + var switchStyleNight: SwitchStyle { + return SwitchStyle() + } + + var buttonStyleNight: ButtonStyle { + return ButtonStyle() + } +} + +// MARK: - Operators + +func == (lhs: Theme?, rhs: Theme?) -> Bool { + return lhs?.id == rhs?.id +} + +func != (lhs: Theme?, rhs: Theme?) -> Bool { + return lhs?.id != rhs?.id +} diff --git a/mtpThemeManager/Classes/ThemeManager+Appearance.swift b/Sources/mtpThemeManager/ThemeManager+Appearance.swift similarity index 62% rename from mtpThemeManager/Classes/ThemeManager+Appearance.swift rename to Sources/mtpThemeManager/ThemeManager+Appearance.swift index edd085b..b4a1f25 100644 --- a/mtpThemeManager/Classes/ThemeManager+Appearance.swift +++ b/Sources/mtpThemeManager/ThemeManager+Appearance.swift @@ -8,182 +8,161 @@ import UIKit - -extension ThemeManager{ - - func customizeTheme(tintColor:UIColor, - textColor:UIColor, - statusBarStyle:StatusBarStyle, - navigationBarStyle:NavigationBarStyle, - tabBarStyle:TabBarStyle, - toolbarStyle:ToolbarStyle, - searchBarStyle:SearchBarStyle, - keyboardStyle:KeyboardStyle, - textFieldStyle:TextFieldStyle, - textViewStyle:TextViewStyle, - activityIndicatorViewStyle:ActivityIndicatorViewStyle, - progressViewStyle:ProgressViewStyle, - stepperStyle:StepperStyle, - segmentedControlStyle:SegmentedControlStyle, - scrollViewStyle:ScrollViewStyle, - sliderStyle:SliderStyle, - switchStyle:SwitchStyle, - pageControlStyle:PageControlStyle, - buttonStyle:ButtonStyle){ - - self.setTintColor(color: tintColor) - self.setTextColor(color: textColor) - self.setButtonStyle(style: buttonStyle) - self.setNavigationBarStyle(style: navigationBarStyle) - self.setTabBarStyle(style: tabBarStyle) - self.setToolbarStyle(style: toolbarStyle) - self.setStatusBarStyle(style: statusBarStyle) - self.setKeyboardStyle(style: keyboardStyle) - self.setTextFieldStyle(style: textFieldStyle) - self.setSearchBarStyle(style: searchBarStyle) - self.setTextViewStyle(style: textViewStyle) - self.setActivityIndicatorViewStyle(style: activityIndicatorViewStyle) - self.setSliderStyle(style: sliderStyle) - self.setSwitchStyle(style: switchStyle) - self.setStepperStyle(style: stepperStyle) - self.setScrollViewStyle(style: scrollViewStyle) - self.setSegmentedControlStyle(style: segmentedControlStyle) - self.setPageControlStyle(style: pageControlStyle) - self.setProgressViewStyle(style: progressViewStyle) - +extension ThemeManager { + func customizeTheme(tintColor: UIColor, + textColor: UIColor, + statusBarStyle: StatusBarStyle, + navigationBarStyle: NavigationBarStyle, + tabBarStyle: TabBarStyle, + toolbarStyle: ToolbarStyle, + searchBarStyle: SearchBarStyle, + keyboardStyle: KeyboardStyle, + textFieldStyle: TextFieldStyle, + textViewStyle: TextViewStyle, + activityIndicatorViewStyle: ActivityIndicatorViewStyle, + progressViewStyle: ProgressViewStyle, + stepperStyle: StepperStyle, + segmentedControlStyle: SegmentedControlStyle, + scrollViewStyle: ScrollViewStyle, + sliderStyle: SliderStyle, + switchStyle: SwitchStyle, + pageControlStyle: PageControlStyle, + buttonStyle: ButtonStyle) + { + setTintColor(color: tintColor) + setTextColor(color: textColor) + setButtonStyle(style: buttonStyle) + setNavigationBarStyle(style: navigationBarStyle) + setTabBarStyle(style: tabBarStyle) + setToolbarStyle(style: toolbarStyle) + setStatusBarStyle(style: statusBarStyle) + setKeyboardStyle(style: keyboardStyle) + setTextFieldStyle(style: textFieldStyle) + setSearchBarStyle(style: searchBarStyle) + setTextViewStyle(style: textViewStyle) + setActivityIndicatorViewStyle(style: activityIndicatorViewStyle) + setSliderStyle(style: sliderStyle) + setSwitchStyle(style: switchStyle) + setStepperStyle(style: stepperStyle) + setScrollViewStyle(style: scrollViewStyle) + setSegmentedControlStyle(style: segmentedControlStyle) + setPageControlStyle(style: pageControlStyle) + setProgressViewStyle(style: progressViewStyle) } - + private func resetButtonAppearance(_ btnAppearance: UIButton) { - btnAppearance.backgroundColor=nil - btnAppearance.borderColor=nil - btnAppearance.borderWidth=0 - btnAppearance.tintColor=nil - btnAppearance.cornerRadius=0 + btnAppearance.backgroundColor = nil + btnAppearance.borderColor = nil + btnAppearance.borderWidth = 0 + btnAppearance.tintColor = nil + btnAppearance.cornerRadius = 0 btnAppearance.contentEdgeInsets = default_buttonContentInsets } - - private func setTintColor(color:UIColor){ - if let window = (UIApplication.shared.delegate)?.window{ - window?.tintColor=color + + private func setTintColor(color: UIColor) { + if let window = (UIApplication.shared.delegate)?.window { + window?.tintColor = color } } - - private func setTextColor(color:UIColor){ - UILabel.appearance().textColor=color + + private func setTextColor(color: UIColor) { + UILabel.appearance().textColor = color } - - + /// setNavigationBarStyle /// /// - Parameter style: NavigationBarStyle - private func setNavigationBarStyle(style:NavigationBarStyle) { - + private func setNavigationBarStyle(style: NavigationBarStyle) { let btnAppearance = UIButton.appearance(whenContainedInInstancesOf: [UINavigationBar.self]) resetButtonAppearance(btnAppearance) - - + let apperance = UINavigationBar.appearance() - + var titleAttr = apperance.titleTextAttributes ?? [:] - if let titleColor = style.titleColor{ + if let titleColor = style.titleColor { titleAttr.updateValue(titleColor, forKey: NSAttributedString.Key.foregroundColor) - } - else{ + } else { titleAttr.removeValue(forKey: NSAttributedString.Key.foregroundColor) } apperance.titleTextAttributes = titleAttr - - - let buttonBarAppearance=UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]) + + let buttonBarAppearance = UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]) var barButtonTitleAttr = buttonBarAppearance.titleTextAttributes(for: .normal) ?? [:] - if let buttonColor=style.tintColor{ + if let buttonColor = style.tintColor { barButtonTitleAttr.updateValue(buttonColor, forKey: NSAttributedString.Key.foregroundColor) - } - else{ + } else { barButtonTitleAttr.updateValue(tintColor!, forKey: NSAttributedString.Key.foregroundColor) } let convertedAttributes = Dictionary(uniqueKeysWithValues: barButtonTitleAttr.lazy.map { ($0.key, $0.value) } ) buttonBarAppearance.setTitleTextAttributes(convertedAttributes, for: .normal) - - + apperance.barStyle = style.barStyle apperance.isTranslucent = style.isTranslucent || style.isTransparent apperance.shadowImage = style.isHairlineHidden || style.isTransparent ? UIImage() : default_bar_shadow - apperance.setBackgroundImage(style.isTransparent ? UIImage() : default_navigationBarBackground , for:.default) + apperance.setBackgroundImage(style.isTransparent ? UIImage() : default_navigationBarBackground, for: .default) apperance.barTintColor = style.backgroundColor apperance.tintColor = style.tintColor apperance.isHidden = style.isHidden - } - - - ///You need to add below key in your Info.plist file: + + /// You need to add below key in your Info.plist file: /// View controller-based status bar appearance with boolean value set to NO /// /// - Parameter style: StatusBarStyle - private func setStatusBarStyle(style:StatusBarStyle) { - + private func setStatusBarStyle(style: StatusBarStyle) { UIApplication.shared.statusBarStyle = style.style UIApplication.shared.isStatusBarHidden = style.isHidden - + if let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView { statusBar.backgroundColor = style.backgroundColor } } - - + /// setKeyboardStyle /// /// - Parameter style: KeyboardStyle - private func setKeyboardStyle(style:KeyboardStyle) { + private func setKeyboardStyle(style: KeyboardStyle) { UITextField.appearance().keyboardAppearance = style.appearance UITextView.appearance().keyboardAppear = style.appearance } - - + /// setTabBarStyle /// /// - Parameter style: TabBarStyle - private func setTabBarStyle(style:TabBarStyle) { - + private func setTabBarStyle(style: TabBarStyle) { let btnAppearance = UIButton.appearance(whenContainedInInstancesOf: [UITabBar.self]) resetButtonAppearance(btnAppearance) - + let apperance = UITabBar.appearance() - - if #available(iOS 10, *),let color=style.badgeColor { + + if #available(iOS 10, *), let color = style.badgeColor { UITabBarItem.appearance().badgeColor = color } - - if #available(iOS 10, *),let color=style.unselectedTintColor { + + if #available(iOS 10, *), let color = style.unselectedTintColor { apperance.unselectedItemTintColor = color } - + apperance.tintColor = style.tintColor apperance.barTintColor = style.backgroundColor apperance.barStyle = style.barStyle apperance.isTranslucent = style.isTranslucent || style.isTransparent apperance.shadowImage = style.isHairlineHidden || style.isTransparent ? nil : default_bar_shadow apperance.clipsToBounds = style.isHairlineHidden || style.isTransparent - apperance.backgroundImage=style.isTransparent ? UIImage() : default_tabBarBackground - + apperance.backgroundImage = style.isTransparent ? UIImage() : default_tabBarBackground } - - - + /// setToolbarStyle /// /// - Parameter style: ToolbarStyle - private func setToolbarStyle(style:ToolbarStyle) { - + private func setToolbarStyle(style: ToolbarStyle) { let btnAppearance = UIButton.appearance(whenContainedInInstancesOf: [UIToolbar.self]) resetButtonAppearance(btnAppearance) - + let apperance = UIToolbar.appearance() - - + apperance.tintColor = style.tintColor apperance.barTintColor = style.backgroundColor apperance.barStyle = style.barStyle @@ -191,42 +170,37 @@ extension ThemeManager{ apperance.setShadowImage(style.isHairlineHidden || style.isTransparent ? UIImage() : default_bar_shadow, forToolbarPosition: .any) apperance.clipsToBounds = style.isHairlineHidden || style.isTransparent apperance.setBackgroundImage(style.isTransparent ? UIImage() : default_toolbarBackground, forToolbarPosition: .any, barMetrics: .default) - } - - + /// setSearchBarStyle /// /// - Parameter style: SearchBarStyle - private func setSearchBarStyle(style:SearchBarStyle) { - + private func setSearchBarStyle(style: SearchBarStyle) { let apperance = UISearchBar.appearance() - + apperance.barTintColor = style.backgroundColor apperance.tintColor = style.tintColor apperance.searchBarStyle = .minimal apperance.barStyle = style.barStyle apperance.isTranslucent = style.isTranslucent || style.isTransparent apperance.setBackgroundImage(style.isTransparent ? UIImage() : default_searchBarBackground, for: .any, barMetrics: .default) - - if let fieldStyle=style.textFieldStyle { - let fieldAppearence=UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]) + + if let fieldStyle = style.textFieldStyle { + let fieldAppearence = UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]) fieldAppearence.backgroundColor = fieldStyle.backgroundColor ?? default_searchFieldColor - fieldAppearence.borderColor=fieldStyle.borderColor - fieldAppearence.borderWidth=fieldStyle.borderWidth - fieldAppearence.cornerRadius=fieldStyle.cornerRadius + fieldAppearence.borderColor = fieldStyle.borderColor + fieldAppearence.borderWidth = fieldStyle.borderWidth + fieldAppearence.cornerRadius = fieldStyle.cornerRadius fieldAppearence.clearButtonMode = fieldStyle.clearButtonMode } - } - + /// setTextFieldStyle /// /// - Parameter style: TextFieldStyle - private func setTextFieldStyle(style:TextFieldStyle) { - + private func setTextFieldStyle(style: TextFieldStyle) { let apperance = UITextField.appearance() - + apperance.borderStyle = style.borderStyle apperance.textColor = style.textColor apperance.borderColor = style.borderColor @@ -235,138 +209,115 @@ extension ThemeManager{ apperance.backgroundColor = style.backgroundColor apperance.clearButtonMode = style.clearButtonMode UILabel.appearance(whenContainedInInstancesOf: [UITextField.self]).textColor = style.placeholderColor - } - - + /// setTextViewStyle /// /// - Parameter style: TextViewStyle - private func setTextViewStyle(style:TextViewStyle) { - + private func setTextViewStyle(style: TextViewStyle) { let apperance = UITextView.appearance() - + apperance.textColor = style.textColor apperance.borderColor = style.borderColor apperance.cornerRadius = style.cornerRadius apperance.borderWidth = style.borderWidth apperance.backgroundColor = style.backgroundColor apperance.textContainerInset = style.textContainerInset ?? default_textViewContainrInsets - apperance.clipsToBounds=true - + apperance.clipsToBounds = true } - - + /// setActivityIndicatorViewStyle /// /// - Parameter style: ActivityIndicatorViewStyle - private func setActivityIndicatorViewStyle(style:ActivityIndicatorViewStyle) { - + private func setActivityIndicatorViewStyle(style: ActivityIndicatorViewStyle) { let apperance = UIActivityIndicatorView.appearance() - + apperance.style = style.style apperance.color = style.color - } - - + /// setScrollViewStyle /// /// - Parameter style: ScrollViewStyle - private func setScrollViewStyle(style:ScrollViewStyle) { + private func setScrollViewStyle(style: ScrollViewStyle) { UIScrollView.appearance().indicatorStyle = style.indicatorStyle } - - - + /// setSegmentedControlStyle /// /// - Parameter style: SegmentedControlStyle - private func setSegmentedControlStyle(style:SegmentedControlStyle) { - + private func setSegmentedControlStyle(style: SegmentedControlStyle) { let appearance = UISegmentedControl.appearance() - + appearance.tintColor = style.tintColor appearance.backgroundColor = style.backgroundColor } - - - + /// setStepperStyle /// /// - Parameter style: StepperStyle - private func setStepperStyle(style:StepperStyle) { - + private func setStepperStyle(style: StepperStyle) { let btnAppearance = UIButton.appearance(whenContainedInInstancesOf: [UIStepper.self]) resetButtonAppearance(btnAppearance) - + let appearance = UIStepper.appearance() - + appearance.tintColor = style.tintColor appearance.backgroundColor = style.backgroundColor } - - + /// setPageControlStyle /// /// - Parameter style: PageControlStyle - private func setPageControlStyle(style:PageControlStyle) { + private func setPageControlStyle(style: PageControlStyle) { let appearance = UIPageControl.appearance() appearance.currentPageIndicatorTintColor = style.currentPageIndicatorTintColor appearance.pageIndicatorTintColor = style.pageIndicatorTintColor } - - - + /// setProgressViewStyle /// /// - Parameter style: ProgressViewStyle - private func setProgressViewStyle(style:ProgressViewStyle) { + private func setProgressViewStyle(style: ProgressViewStyle) { let appearance = UIProgressView.appearance() appearance.progressTintColor = style.progressTintColor appearance.trackTintColor = style.trackTintColor appearance.progressViewStyle = style.style } - - + /// setSliderStyle /// /// - Parameter style: SliderStyle - private func setSliderStyle(style:SliderStyle) { + private func setSliderStyle(style: SliderStyle) { let appearance = UISlider.appearance() appearance.minimumTrackTintColor = style.minimumTrackTintColor appearance.thumbTintColor = style.thumbTintColor appearance.maximumTrackTintColor = style.maximumTrackTintColor } - - + /// setSwitchStyle /// /// - Parameter style: SwitchStyle - private func setSwitchStyle(style:SwitchStyle) { + private func setSwitchStyle(style: SwitchStyle) { let appearance = UISwitch.appearance() - + appearance.onTintColor = style.onTintColor appearance.thumbTintColor = style.thumbTintColor appearance.tintColor = style.tintColor - } - - + /// setButtonStyle /// /// - Parameter style: ButtonStyle - private func setButtonStyle(style:ButtonStyle) { + private func setButtonStyle(style: ButtonStyle) { let appearance = UIButton.appearance() - + appearance.tintColor = style.tintColor appearance.borderColor = style.borderColor appearance.cornerRadius = style.cornerRadius appearance.borderWidth = style.borderWidth appearance.backgroundColor = style.backgroundColor appearance.contentEdgeInsets = style.contentEdgeInsets ?? default_buttonContentInsets - appearance.clipsToBounds=true - + appearance.clipsToBounds = true } - } diff --git a/Sources/mtpThemeManager/ThemeManager+Properties.swift b/Sources/mtpThemeManager/ThemeManager+Properties.swift new file mode 100644 index 0000000..e579c32 --- /dev/null +++ b/Sources/mtpThemeManager/ThemeManager+Properties.swift @@ -0,0 +1,251 @@ +// +// ThemeManager+Properties.swift +// ThemeManager +// +// Created by Mostafa Taghipour on 11/3/17. +// Copyright © 2017 RainyDay. All rights reserved. +// + +import UIKit + +public extension ThemeManager { + var tintColor: UIColor? { + if let unwrapedCurrentTheme = currentTheme { + if let theme = unwrapedCurrentTheme as? DayNightTheme { + return isItNight ? theme.tintColorNight : theme.tintColor + } else { + return unwrapedCurrentTheme.tintColor + } + } + + return nil + } + + var backgroundColor: UIColor? { + if let unwrapedCurrentTheme = currentTheme { + if let theme = unwrapedCurrentTheme as? DayNightTheme { + return isItNight ? theme.backgroundColorNight : theme.backgroundColor + } else { + return unwrapedCurrentTheme.backgroundColor + } + } + + return nil + } + + var primaryTextColor: UIColor? { + if let unwrapedCurrentTheme = currentTheme { + if let theme = unwrapedCurrentTheme as? DayNightTheme { + return isItNight ? theme.textColorNight : theme.textColor + } else { + return unwrapedCurrentTheme.textColor + } + } + + return nil + } + + var navigationBarStyle: NavigationBarStyle? { + if let unwrapedCurrentTheme = currentTheme { + if let theme = unwrapedCurrentTheme as? DayNightTheme { + return isItNight ? theme.navigationBarStyleNight : theme.navigationBarStyle + } else { + return unwrapedCurrentTheme.navigationBarStyle + } + } + + return nil + } + + var tabBarStyle: TabBarStyle? { + if let unwrapedCurrentTheme = currentTheme { + if let theme = unwrapedCurrentTheme as? DayNightTheme { + return isItNight ? theme.tabBarStyleNight : theme.tabBarStyle + } else { + return unwrapedCurrentTheme.tabBarStyle + } + } + + return nil + } + + var toolbarStyle: ToolbarStyle? { + if let unwrapedCurrentTheme = currentTheme { + if let theme = unwrapedCurrentTheme as? DayNightTheme { + return isItNight ? theme.toolbarStyleNight : theme.toolbarStyle + } else { + return unwrapedCurrentTheme.toolbarStyle + } + } + + return nil + } + + var searchBarStyle: SearchBarStyle? { + if let unwrapedCurrentTheme = currentTheme { + if let theme = unwrapedCurrentTheme as? DayNightTheme { + return isItNight ? theme.searchBarStyleNight : theme.searchBarStyle + } else { + return unwrapedCurrentTheme.searchBarStyle + } + } + + return nil + } + + var statusBarStyle: StatusBarStyle? { + if let unwrapedCurrentTheme = currentTheme { + if let theme = unwrapedCurrentTheme as? DayNightTheme { + return isItNight ? theme.statusBarStyleNight : theme.statusBarStyle + } else { + return unwrapedCurrentTheme.statusBarStyle + } + } + + return nil + } + + var buttonStyles: ButtonStyle? { + if let unwrapedCurrentTheme = currentTheme { + if let theme = unwrapedCurrentTheme as? DayNightTheme { + return isItNight ? theme.buttonStyleNight : theme.buttonStyle + } else { + return unwrapedCurrentTheme.buttonStyle + } + } + + return nil + } + + var textFieldStyle: TextFieldStyle? { + if let unwrapedCurrentTheme = currentTheme { + if let theme = unwrapedCurrentTheme as? DayNightTheme { + return isItNight ? theme.textFieldStyleNight : theme.textFieldStyle + } else { + return unwrapedCurrentTheme.textFieldStyle + } + } + + return nil + } + + var textViewStyle: TextViewStyle? { + if let unwrapedCurrentTheme = currentTheme { + if let theme = unwrapedCurrentTheme as? DayNightTheme { + return isItNight ? theme.textViewStyleNight : theme.textViewStyle + } else { + return unwrapedCurrentTheme.textViewStyle + } + } + + return nil + } + + var keyboardStyle: KeyboardStyle? { + if let unwrapedCurrentTheme = currentTheme { + if let theme = unwrapedCurrentTheme as? DayNightTheme { + return isItNight ? theme.keyboardStyleNight : theme.keyboardStyle + } else { + return unwrapedCurrentTheme.keyboardStyle + } + } + + return nil + } + + var activityIndicatorViewStyle: ActivityIndicatorViewStyle? { + if let unwrapedCurrentTheme = currentTheme { + if let theme = unwrapedCurrentTheme as? DayNightTheme { + return isItNight ? theme.activityIndicatorViewStyleNight : theme.activityIndicatorViewStyle + } else { + return unwrapedCurrentTheme.activityIndicatorViewStyle + } + } + + return nil + } + + var switchStyle: SwitchStyle? { + if let unwrapedCurrentTheme = currentTheme { + if let theme = unwrapedCurrentTheme as? DayNightTheme { + return isItNight ? theme.switchStyleNight : theme.switchStyle + } else { + return unwrapedCurrentTheme.switchStyle + } + } + + return nil + } + + var sliderStyle: SliderStyle? { + if let unwrapedCurrentTheme = currentTheme { + if let theme = unwrapedCurrentTheme as? DayNightTheme { + return isItNight ? theme.sliderStyleNight : theme.sliderStyle + } else { + return unwrapedCurrentTheme.sliderStyle + } + } + + return nil + } + + var progressViewStyle: ProgressViewStyle? { + if let unwrapedCurrentTheme = currentTheme { + if let theme = unwrapedCurrentTheme as? DayNightTheme { + return isItNight ? theme.progressViewStyleNight : theme.progressViewStyle + } else { + return unwrapedCurrentTheme.progressViewStyle + } + } + + return nil + } + + var pageControlStyle: PageControlStyle? { + if let unwrapedCurrentTheme = currentTheme { + if let theme = unwrapedCurrentTheme as? DayNightTheme { + return isItNight ? theme.pageControlStyleNight : theme.pageControlStyle + } else { + return unwrapedCurrentTheme.pageControlStyle + } + } + + return nil + } + + var stepperStyle: StepperStyle? { + if let unwrapedCurrentTheme = currentTheme { + if let theme = unwrapedCurrentTheme as? DayNightTheme { + return isItNight ? theme.stepperStyleNight : theme.stepperStyle + } else { + return unwrapedCurrentTheme.stepperStyle + } + } + + return nil + } + + var segmentedControlStyle: SegmentedControlStyle? { + if let unwrapedCurrentTheme = currentTheme { + if let theme = unwrapedCurrentTheme as? DayNightTheme { + return isItNight ? theme.segmentedControlStyleNight : theme.segmentedControlStyle + } else { + return unwrapedCurrentTheme.segmentedControlStyle + } + } + + return nil + } + + var scrollViewStyle: ScrollViewStyle? { + if let unwrapedCurrentTheme = currentTheme { + if let theme = unwrapedCurrentTheme as? DayNightTheme { + return isItNight ? theme.scrollViewStyleNight : theme.scrollViewStyle + } else { + return unwrapedCurrentTheme.scrollViewStyle + } + } + + return nil + } +} diff --git a/mtpThemeManager/Classes/ThemeManager.swift b/Sources/mtpThemeManager/ThemeManager.swift similarity index 55% rename from mtpThemeManager/Classes/ThemeManager.swift rename to Sources/mtpThemeManager/ThemeManager.swift index 83ee33c..c928160 100644 --- a/mtpThemeManager/Classes/ThemeManager.swift +++ b/Sources/mtpThemeManager/ThemeManager.swift @@ -9,67 +9,63 @@ import UIKit public class ThemeManager { - - private var _currentTheme:Theme? - public var currentTheme:Theme?{ + private var _currentTheme: Theme? + public var currentTheme: Theme? { return _currentTheme } - - public private(set) var isItNight:Bool = false - - private var _nightModeStatus:NightModeStatus = .disable - public var nightModeStatus:NightModeStatus { - get{ + + public private(set) var isItNight: Bool = false + + private var _nightModeStatus: NightModeStatus = .disable + public var nightModeStatus: NightModeStatus { + get { return _nightModeStatus } - set{ - if let theme = _currentTheme as? DayNightTheme{ + set { + if let theme = _currentTheme as? DayNightTheme { let needUpdate = _nightModeStatus != newValue - if needUpdate{ - self.setTheme(dayNight: theme, nightModeStatus: newValue) + if needUpdate { + setTheme(dayNight: theme, nightModeStatus: newValue) } } } } - - public lazy var availableThemes:[Theme]={ - var themes:[Theme]=[] - let classes=getClassList() - + + public lazy var availableThemes: [Theme] = { + var themes: [Theme] = [] + let classes = getClassList() + for cls in classes { - if let theme = cls as? Theme.Type{ + if let theme = cls as? Theme.Type { themes.append(theme.init()) } } - + return themes }() - - + // Can't init is singleton private init() { setupDefaults() } - + // Shared Instance public static let shared = ThemeManager() - - + // set theme - public func setTheme(theme:Theme){ + public func setTheme(theme: Theme) { guard _currentTheme != theme else { return } - - self._currentTheme=theme - + + _currentTheme = theme + NotificationCenter.default.post(name: NSNotification.Name.ThemeDidChange, object: theme) - - - customizeTheme(tintColor:theme.tintColor, + + customizeTheme(tintColor: theme.tintColor, textColor: theme.textColor, statusBarStyle: theme.statusBarStyle, - navigationBarStyle:theme.navigationBarStyle, + navigationBarStyle: theme.navigationBarStyle, tabBarStyle: theme.tabBarStyle, toolbarStyle: theme.toolbarStyle, searchBarStyle: theme.searchBarStyle, @@ -77,36 +73,34 @@ public class ThemeManager { textFieldStyle: theme.textFieldStyle, textViewStyle: theme.textViewStyle, activityIndicatorViewStyle: theme.activityIndicatorViewStyle, - progressViewStyle:theme.progressViewStyle, - stepperStyle:theme.stepperStyle, - segmentedControlStyle:theme.segmentedControlStyle, - scrollViewStyle:theme.scrollViewStyle, - sliderStyle:theme.sliderStyle, - switchStyle:theme.switchStyle, - pageControlStyle:theme.pageControlStyle, + progressViewStyle: theme.progressViewStyle, + stepperStyle: theme.stepperStyle, + segmentedControlStyle: theme.segmentedControlStyle, + scrollViewStyle: theme.scrollViewStyle, + sliderStyle: theme.sliderStyle, + switchStyle: theme.switchStyle, + pageControlStyle: theme.pageControlStyle, buttonStyle: theme.buttonStyle) } - - - //set theme night mode - public func setTheme(dayNight theme:DayNightTheme,nightModeStatus:NightModeStatus? = nil){ - + + // set theme night mode + public func setTheme(dayNight theme: DayNightTheme, nightModeStatus: NightModeStatus? = nil) { let nightStatus = nightModeStatus ?? self.nightModeStatus - - guard _currentTheme != theme || nightStatus != self._nightModeStatus else { + + guard _currentTheme != theme || nightStatus != _nightModeStatus else { return } - - self._currentTheme=theme - _nightModeStatus=nightStatus + + _currentTheme = theme + _nightModeStatus = nightStatus isItNight = checkisItNight(nightModeStatus: nightStatus) - + NotificationCenter.default.post(name: NSNotification.Name.ThemeDidChange, object: theme) - - customizeTheme(tintColor:isItNight ? theme.tintColorNight : theme.tintColor, + + customizeTheme(tintColor: isItNight ? theme.tintColorNight : theme.tintColor, textColor: isItNight ? theme.textColorNight : theme.textColor, statusBarStyle: isItNight ? theme.statusBarStyleNight : theme.statusBarStyle, - navigationBarStyle:isItNight ? theme.navigationBarStyleNight : theme.navigationBarStyle, + navigationBarStyle: isItNight ? theme.navigationBarStyleNight : theme.navigationBarStyle, tabBarStyle: isItNight ? theme.tabBarStyleNight : theme.tabBarStyle, toolbarStyle: isItNight ? theme.toolbarStyleNight : theme.toolbarStyle, searchBarStyle: isItNight ? theme.searchBarStyleNight : theme.searchBarStyle, @@ -114,62 +108,57 @@ public class ThemeManager { textFieldStyle: isItNight ? theme.textFieldStyleNight : theme.textFieldStyle, textViewStyle: isItNight ? theme.textViewStyleNight : theme.textViewStyle, activityIndicatorViewStyle: isItNight ? theme.activityIndicatorViewStyleNight : theme.activityIndicatorViewStyle, - progressViewStyle:isItNight ? theme.progressViewStyleNight : theme.progressViewStyle, - stepperStyle:isItNight ? theme.stepperStyleNight : theme.stepperStyle, - segmentedControlStyle:isItNight ? theme.segmentedControlStyleNight : theme.segmentedControlStyle, - scrollViewStyle:isItNight ? theme.scrollViewStyleNight : theme.scrollViewStyle, - sliderStyle:isItNight ? theme.sliderStyleNight : theme.sliderStyle, - switchStyle:isItNight ? theme.switchStyleNight : theme.switchStyle, - pageControlStyle:isItNight ? theme.pageControlStyleNight : theme.pageControlStyle, + progressViewStyle: isItNight ? theme.progressViewStyleNight : theme.progressViewStyle, + stepperStyle: isItNight ? theme.stepperStyleNight : theme.stepperStyle, + segmentedControlStyle: isItNight ? theme.segmentedControlStyleNight : theme.segmentedControlStyle, + scrollViewStyle: isItNight ? theme.scrollViewStyleNight : theme.scrollViewStyle, + sliderStyle: isItNight ? theme.sliderStyleNight : theme.sliderStyle, + switchStyle: isItNight ? theme.switchStyleNight : theme.switchStyle, + pageControlStyle: isItNight ? theme.pageControlStyleNight : theme.pageControlStyle, buttonStyle: isItNight ? theme.buttonStyleNight : theme.buttonStyle) } - - - private func checkisItNight(nightModeStatus:NightModeStatus) -> Bool { + + private func checkisItNight(nightModeStatus: NightModeStatus) -> Bool { switch nightModeStatus { case .disable: return false case .enable: return true case .auto: - + let hour = Calendar.current.component(.hour, from: Date()) - + switch hour { - case 6..<19 : return false - default: return true + case 6 ..< 19: return false + default: return true } } } - - + private func getClassList() -> [AnyClass] { let expectedClassCount = objc_getClassList(nil, 0) let allClasses = UnsafeMutablePointer.allocate(capacity: Int(expectedClassCount)) let autoreleasingAllClasses = AutoreleasingUnsafeMutablePointer(allClasses) - let actualClassCount:Int32 = objc_getClassList(autoreleasingAllClasses, expectedClassCount) - + let actualClassCount: Int32 = objc_getClassList(autoreleasingAllClasses, expectedClassCount) + var classes = [AnyClass]() for i in 0 ..< actualClassCount { if let currentClass: AnyClass = allClasses[Int(i)] { classes.append(currentClass) } } - + allClasses.deallocate() - - //.deallocate(capacity: Int(expectedClassCount)) - + + // .deallocate(capacity: Int(expectedClassCount)) + return classes } - - - - - - //MARK:- default values + + // MARK: - default values + private func setupDefaults() { - if let window = (UIApplication.shared.delegate)?.window{ + if let window = (UIApplication.shared.delegate)?.window { default_tintColor = window?.tintColor } default_navigationBarBackground = UINavigationBar.appearance().backgroundImage(for: UIBarMetrics.default) @@ -177,25 +166,21 @@ public class ThemeManager { default_tabBarBackground = UITabBar.appearance().backgroundImage default_searchBarBackground = UISearchBar.appearance().backgroundImage(for: .any, barMetrics: .default) default_toolbarBackground = UIToolbar.appearance().backgroundImage(forToolbarPosition: .any, barMetrics: .default) - default_searchFieldColor=UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor + default_searchFieldColor = UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor default_buttonContentInsets = UIButton.appearance().contentEdgeInsets default_textViewContainrInsets = UITextView.appearance().textContainerInset } - - var default_tintColor:UIColor? - var default_navigationBarBackground:UIImage? - var default_tabBarBackground:UIImage? - var default_toolbarBackground:UIImage? - var default_searchBarBackground:UIImage? - var default_bar_shadow:UIImage? - var default_searchFieldColor:UIColor? - var default_buttonContentInsets:UIEdgeInsets! - var default_textViewContainrInsets:UIEdgeInsets! -} - - - + var default_tintColor: UIColor? + var default_navigationBarBackground: UIImage? + var default_tabBarBackground: UIImage? + var default_toolbarBackground: UIImage? + var default_searchBarBackground: UIImage? + var default_bar_shadow: UIImage? + var default_searchFieldColor: UIColor? + var default_buttonContentInsets: UIEdgeInsets! + var default_textViewContainrInsets: UIEdgeInsets! +} /* . new style @@ -207,5 +192,3 @@ public class ThemeManager { . properties . style extension */ - - diff --git a/Tests/mtpThemeManagerTests/mtpThemeManagerTests.swift b/Tests/mtpThemeManagerTests/mtpThemeManagerTests.swift new file mode 100644 index 0000000..583b572 --- /dev/null +++ b/Tests/mtpThemeManagerTests/mtpThemeManagerTests.swift @@ -0,0 +1,12 @@ +@testable import mtpThemeManager +import XCTest + +final class mtpThemeManagerTests: XCTestCase { + func testExample() throws { + // This is an example of a functional test case. + // Use XCTAssert and related functions to verify your tests produce the correct + // results. + let x = "Hello, World!" + XCTAssertEqual(x, "Hello, World!") + } +} diff --git a/_Pods.xcodeproj b/_Pods.xcodeproj deleted file mode 120000 index 3c5a8e7..0000000 --- a/_Pods.xcodeproj +++ /dev/null @@ -1 +0,0 @@ -Example/Pods/Pods.xcodeproj \ No newline at end of file diff --git a/mtpThemeManager.podspec b/mtpThemeManager.podspec index a1b5381..8b95270 100644 --- a/mtpThemeManager.podspec +++ b/mtpThemeManager.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'mtpThemeManager' - s.version = '1.0.7' + s.version = '2.0.1' s.summary = 'An iOS theme manager with all the features.' # This description is used to generate tags and improve search results. @@ -32,7 +32,7 @@ contains apply theme to whole app, multiple themes, night mode, styles , ... s.ios.deployment_target = '9.0' - s.source_files = 'mtpThemeManager/Classes/**/*' + s.source_files = 'Sources/mtpThemeManager/**/*' # s.resource_bundles = { # 'mtpThemeManager' => ['mtpThemeManager/Assets/*.png'] diff --git a/mtpThemeManager/Assets/.gitkeep b/mtpThemeManager/Assets/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/mtpThemeManager/Classes/.gitkeep b/mtpThemeManager/Classes/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/mtpThemeManager/Classes/Style.swift b/mtpThemeManager/Classes/Style.swift deleted file mode 100644 index 2b6f025..0000000 --- a/mtpThemeManager/Classes/Style.swift +++ /dev/null @@ -1,378 +0,0 @@ -// -// Style.swift -// ThemeManager -// -// Created by Mostafa Taghipour on 11/3/17. -// Copyright © 2017 RainyDay. All rights reserved. -// - -import UIKit - - -public protocol Style { - -} - - -//MARK:- StatusBarStyle -public struct StatusBarStyle: Style { - - public var backgroundColor:UIColor? = nil - public var style:UIStatusBarStyle = .default - public var isHidden:Bool = false - - public init(backgroundColor:UIColor? = nil, - style:UIStatusBarStyle = .default, - isHidden:Bool = false) { - - self.backgroundColor=backgroundColor - self.style=style - self.isHidden=isHidden - } -} - -//MARK:- NavigationBarStyle -public struct NavigationBarStyle: Style { - - public var backgroundColor:UIColor? = nil - public var titleColor:UIColor? = nil - public var tintColor:UIColor? = nil - public var barStyle:UIBarStyle = .default - public var isTranslucent:Bool = true - public var isHidden:Bool = false - public var isTransparent:Bool = false - public var isHairlineHidden:Bool = false - - public init(backgroundColor:UIColor? = nil, - titleColor:UIColor? = nil, - tintColor:UIColor? = nil, - barStyle:UIBarStyle = .default, - isTranslucent:Bool = true, - isHidden:Bool = false, - isTransparent:Bool = false, - isHairlineHidden:Bool = false) { - - self.backgroundColor=backgroundColor - self.titleColor=titleColor - self.tintColor=tintColor - self.barStyle=barStyle - self.isTransparent=isTransparent - self.isTranslucent=isTranslucent - self.isHidden=isHidden - self.isHairlineHidden=isHairlineHidden - } -} - -//MARK:- TabBarStyle -public struct TabBarStyle: Style { - - public var backgroundColor:UIColor? = nil - public var badgeColor:UIColor? = nil - public var tintColor:UIColor? = nil - public var unselectedTintColor:UIColor? = nil - public var barStyle:UIBarStyle = .default - public var isTranslucent:Bool = true - public var isTransparent:Bool = false - public var isHairlineHidden:Bool = false - - public init(backgroundColor:UIColor? = nil, - badgeColor:UIColor? = nil, - tintColor:UIColor? = nil, - unselectedTintColor:UIColor? = nil, - barStyle:UIBarStyle = .default, - isTranslucent:Bool = true, - isTransparent:Bool = false, - isHairlineHidden:Bool = false) { - - self.backgroundColor=backgroundColor - self.badgeColor=badgeColor - self.tintColor=tintColor - self.unselectedTintColor=unselectedTintColor - self.barStyle=barStyle - self.isTransparent=isTransparent - self.isTranslucent=isTranslucent - self.isHairlineHidden=isHairlineHidden - } -} - - -//MARK:- ToolbarStyle -public struct ToolbarStyle: Style { - - public var backgroundColor:UIColor? = nil - public var tintColor:UIColor? = nil - public var barStyle:UIBarStyle = .default - public var isTranslucent:Bool = true - public var isTransparent:Bool = false - public var isHairlineHidden:Bool = false - - public init(backgroundColor:UIColor? = nil, - tintColor:UIColor? = nil, - barStyle:UIBarStyle = .default, - isTranslucent:Bool = true, - isTransparent:Bool = false, - isHairlineHidden:Bool = false) { - - self.backgroundColor=backgroundColor - self.tintColor=tintColor - self.barStyle=barStyle - self.isTransparent=isTransparent - self.isTranslucent=isTranslucent - self.isHairlineHidden=isHairlineHidden - } -} - - -//MARK:- SearchBarStyle -public struct SearchBarStyle: Style { - - public var backgroundColor:UIColor? = nil - public var tintColor:UIColor? = nil - public var barStyle:UIBarStyle = .default - public var searchBarStyle:UISearchBar.Style = .default - public var isTranslucent:Bool = true - public var isTransparent:Bool = false - public var textFieldStyle:TextFieldStyle? = nil - - public init(backgroundColor:UIColor? = nil, - tintColor:UIColor? = nil, - barStyle:UIBarStyle = .default, - searchStyle:UISearchBar.Style = .default, - isTranslucent:Bool = true, - isTransparent:Bool = false, - textFieldStyle:TextFieldStyle? = nil) { - - self.backgroundColor=backgroundColor - self.tintColor=tintColor - self.barStyle=barStyle - self.searchBarStyle=searchStyle - self.isTransparent=isTransparent - self.isTranslucent=isTranslucent - self.textFieldStyle=textFieldStyle - } -} - - - -//MARK:- TextFieldStyle -public struct TextFieldStyle: Style { - - public var borderColor: UIColor?=nil - public var borderWidth: CGFloat=0 - public var cornerRadius: CGFloat=0 - public var backgroundColor: UIColor?=nil - public var textColor:UIColor? = nil - public var placeholderColor:UIColor? = nil - public var borderStyle:UITextField.BorderStyle = .roundedRect - public var clearButtonMode:UITextField.ViewMode = .never - - public init(backgroundColor:UIColor? = nil, - textColor:UIColor? = nil, - borderColor:UIColor? = nil, - borderWidth:CGFloat = 0, - cornerRadius:CGFloat = 0, - placeholderColor:UIColor? = nil, - borderStyle:UITextField.BorderStyle = .roundedRect, - clearButton:UITextField.ViewMode = .never) { - - self.backgroundColor=backgroundColor - self.textColor=textColor - self.borderColor=borderColor - self.borderWidth=borderWidth - self.cornerRadius=cornerRadius - self.borderStyle=borderStyle - self.clearButtonMode=clearButton - self.placeholderColor=placeholderColor - } -} - - -//MARK:- TextViewStyle -public struct TextViewStyle: Style { - - public var backgroundColor:UIColor? = nil - public var textColor:UIColor? = nil - public var borderColor:UIColor? = nil - public var borderWidth:CGFloat = 0 - public var cornerRadius:CGFloat = 0 - public var textContainerInset:UIEdgeInsets? = nil - - public init(backgroundColor:UIColor? = nil, - textColor:UIColor? = nil, - borderColor:UIColor? = nil, - borderWidth:CGFloat = 0, - cornerRadius:CGFloat = 0, - textContainerInset:UIEdgeInsets? = nil) { - - self.backgroundColor=backgroundColor - self.textColor=textColor - self.borderColor=borderColor - self.borderWidth=borderWidth - self.cornerRadius=cornerRadius - self.textContainerInset=textContainerInset - } -} - - -//MARK:- ButtonStyle -public struct ButtonStyle: Style { - - public var backgroundColor:UIColor? = nil - public var tintColor:UIColor? = nil - public var borderColor:UIColor? = nil - public var borderWidth:CGFloat = 0 - public var cornerRadius:CGFloat = 0 - public var contentEdgeInsets:UIEdgeInsets? = nil - - public init(backgroundColor:UIColor? = nil, - tintColor:UIColor? = nil, - borderColor:UIColor? = nil, - borderWidth:CGFloat = 0, - cornerRadius:CGFloat = 0, - contentEdgeInsets:UIEdgeInsets? = nil) { - - self.backgroundColor=backgroundColor - self.tintColor=tintColor - self.borderColor=borderColor - self.borderWidth=borderWidth - self.cornerRadius=cornerRadius - self.contentEdgeInsets=contentEdgeInsets - } -} - - - -//MARK:- ActivityIndicatorViewStyle -public struct ActivityIndicatorViewStyle: Style { - - public var style: UIActivityIndicatorView.Style = .gray - public var color:UIColor? = nil - - public init(style: UIActivityIndicatorView.Style = .gray, - color:UIColor? = nil) { - - self.style=style - self.color=color - } -} - - -//MARK:- ScrollViewStyle -public struct ScrollViewStyle: Style { - - public var indicatorStyle: UIScrollView.IndicatorStyle = .default - - public init(indicatorStyle: UIScrollView.IndicatorStyle = .default) { - self.indicatorStyle=indicatorStyle - } -} - - -//MARK:- SegmentedControlStyle -public struct SegmentedControlStyle: Style { - - public var backgroundColor:UIColor? = nil - public var tintColor:UIColor? = nil - - public init(backgroundColor:UIColor? = nil, - tintColor:UIColor? = nil) { - - self.backgroundColor=backgroundColor - self.tintColor=tintColor - } -} - - -//MARK:- StepperStyle -public struct StepperStyle: Style { - - public var backgroundColor:UIColor? = nil - public var tintColor:UIColor? = nil - - public init(backgroundColor:UIColor? = nil, - tintColor:UIColor? = nil) { - - self.backgroundColor=backgroundColor - self.tintColor=tintColor - } -} - - -//MARK:- PageControlStyle -public struct PageControlStyle: Style { - - public var pageIndicatorTintColor:UIColor? = nil - public var currentPageIndicatorTintColor:UIColor? = nil - - public init(pageIndicatorTintColor:UIColor? = nil, - currentPageIndicatorTintColor:UIColor? = nil) { - - self.pageIndicatorTintColor=pageIndicatorTintColor - self.currentPageIndicatorTintColor=currentPageIndicatorTintColor - } -} - - -//MARK:- ProgressViewStyle -public struct ProgressViewStyle: Style { - - public var progressTintColor:UIColor? = nil - public var trackTintColor:UIColor? = nil - public var style:UIProgressView.Style = .default - - public init(progressTintColor:UIColor? = nil, - trackTintColor:UIColor? = nil, - style:UIProgressView.Style = .default) { - - self.progressTintColor=progressTintColor - self.trackTintColor=trackTintColor - self.style=style - } -} - - -//MARK:- SliderStyle -public struct SliderStyle: Style { - - public var minimumTrackTintColor:UIColor? = nil - public var maximumTrackTintColor:UIColor? = nil - public var thumbTintColor:UIColor? = nil - - public init(minimumTrackTintColor:UIColor? = nil, - maximumTrackTintColor:UIColor? = nil, - thumbTintColor:UIColor? = nil) { - - self.minimumTrackTintColor=minimumTrackTintColor - self.maximumTrackTintColor=maximumTrackTintColor - self.thumbTintColor=thumbTintColor - } -} - -//MARK:- SwitchStyle -public struct SwitchStyle: Style { - - public var thumbTintColor:UIColor? = nil - public var onTintColor:UIColor? = nil - public var tintColor:UIColor? = nil - - public init(tintColor:UIColor? = nil, - onTintColor:UIColor? = nil, - thumbTintColor:UIColor? = nil) { - - self.tintColor=tintColor - self.onTintColor=onTintColor - self.thumbTintColor=thumbTintColor - } -} - - -//MARK:- KeyboardStyle -public struct KeyboardStyle: Style { - - public var appearance:UIKeyboardAppearance = .default - - public init(appearance:UIKeyboardAppearance = .default) { - self.appearance=appearance - } -} - diff --git a/mtpThemeManager/Classes/Theme.swift b/mtpThemeManager/Classes/Theme.swift deleted file mode 100644 index 8eb3f98..0000000 --- a/mtpThemeManager/Classes/Theme.swift +++ /dev/null @@ -1,218 +0,0 @@ -// -// Theme.swift -// ThemeManager -// -// Created by Mostafa Taghipour on 11/3/17. -// Copyright © 2017 RainyDay. All rights reserved. -// - -import UIKit - -//Mark:- Theme -public protocol Theme{ - init() - - var id:Int{get} - var displayName:String{get} - var tintColor:UIColor{get} - var textColor:UIColor{get} - var backgroundColor:UIColor{get} - var navigationBarStyle:NavigationBarStyle{get} - var tabBarStyle:TabBarStyle{get} - var toolbarStyle:ToolbarStyle{get} - var searchBarStyle:SearchBarStyle{get} - var statusBarStyle:StatusBarStyle{get} - var keyboardStyle:KeyboardStyle{get} - var buttonStyle:ButtonStyle{get} - var textFieldStyle:TextFieldStyle{get} - var textViewStyle:TextViewStyle{get} - var activityIndicatorViewStyle : ActivityIndicatorViewStyle {get} - var scrollViewStyle:ScrollViewStyle{get} - var segmentedControlStyle:SegmentedControlStyle{get} - var stepperStyle:StepperStyle{get} - var pageControlStyle:PageControlStyle{get} - var progressViewStyle:ProgressViewStyle{get} - var sliderStyle:SliderStyle{get} - var switchStyle:SwitchStyle{get} -} - -public extension Theme { - var backgroundColor:UIColor { - get{ return .white } - } - var textColor:UIColor { - get{ return UIColor.darkText } - } - - var navigationBarStyle:NavigationBarStyle { - get{ return NavigationBarStyle() } - } - var tabBarStyle:TabBarStyle { - get{ return TabBarStyle() } - } - var toolbarStyle:ToolbarStyle { - get{ return ToolbarStyle() } - } - var searchBarStyle:SearchBarStyle { - get{ return SearchBarStyle(textFieldStyle:TextFieldStyle(backgroundColor: nil, - textColor: textColor, - borderColor: nil, - borderWidth:0, - cornerRadius:8.0, - placeholderColor: .lightGray)) } - } - var statusBarStyle:StatusBarStyle { - get{ return StatusBarStyle() } - } - var keyboardStyle:KeyboardStyle { - get{ return KeyboardStyle() } - } - var buttonStyle:ButtonStyle{ - return ButtonStyle() - } - var textFieldStyle:TextFieldStyle{ - return TextFieldStyle(backgroundColor: .clear, - textColor: textColor, - borderColor: .lightGray, - borderWidth:0.5, - cornerRadius:8.0, - placeholderColor: .lightGray) - } - - var textViewStyle:TextViewStyle{ - return TextViewStyle(textColor: textColor) - } - var activityIndicatorViewStyle : ActivityIndicatorViewStyle { - return ActivityIndicatorViewStyle.init() - } - var scrollViewStyle:ScrollViewStyle{ - return ScrollViewStyle() - } - var segmentedControlStyle:SegmentedControlStyle{ - return SegmentedControlStyle() - } - var stepperStyle:StepperStyle{ - return StepperStyle() - } - var pageControlStyle:PageControlStyle{ - return PageControlStyle(pageIndicatorTintColor: UIColor.lightGray.withAlphaComponent(0.5), currentPageIndicatorTintColor: tintColor) - } - var progressViewStyle:ProgressViewStyle{ - return ProgressViewStyle() - } - var sliderStyle:SliderStyle{ - return SliderStyle() - } - var switchStyle:SwitchStyle{ - return SwitchStyle() - } -} - - -//Mark:- DayNight -public protocol DayNightTheme:Theme{ - var tintColorNight:UIColor{get} - var textColorNight:UIColor{get} - var backgroundColorNight:UIColor{get} - var navigationBarStyleNight:NavigationBarStyle{get} - var tabBarStyleNight:TabBarStyle{get} - var toolbarStyleNight:ToolbarStyle{get} - var searchBarStyleNight:SearchBarStyle{get} - var statusBarStyleNight:StatusBarStyle{get} - var keyboardStyleNight:KeyboardStyle{get} - var buttonStyleNight:ButtonStyle{get} - var textFieldStyleNight:TextFieldStyle{get} - var textViewStyleNight:TextViewStyle{get} - var activityIndicatorViewStyleNight : ActivityIndicatorViewStyle {get} - var scrollViewStyleNight:ScrollViewStyle{get} - var segmentedControlStyleNight:SegmentedControlStyle{get} - var stepperStyleNight:StepperStyle{get} - var pageControlStyleNight:PageControlStyle{get} - var progressViewStyleNight:ProgressViewStyle{get} - var sliderStyleNight:SliderStyle{get} - var switchStyleNight:SwitchStyle{get} -} - -public extension DayNightTheme{ - var tintColorNight:UIColor { - get{ return .white } - } - var textColorNight:UIColor { - get{ return UIColor.lightText } - } - var backgroundColorNight:UIColor { - get{ return UIColor(red: 31/255, green: 33/255, blue: 36/255, alpha: 1) } - } - var navigationBarStyleNight:NavigationBarStyle { - get{ return NavigationBarStyle(barStyle: .black) } - } - var tabBarStyleNight:TabBarStyle { - get{ return TabBarStyle(barStyle: .black) } - } - var toolbarStyleNight:ToolbarStyle { - get{ return ToolbarStyle(barStyle: .black) } - } - var searchBarStyleNight:SearchBarStyle { - get{ return SearchBarStyle(barStyle: .black, - textFieldStyle: TextFieldStyle(backgroundColor: nil, - textColor: textColorNight, - borderColor: nil, - borderWidth:0, - cornerRadius:8.0, - placeholderColor: UIColor.white.withAlphaComponent(0.2))) } - } - var statusBarStyleNight:StatusBarStyle { - get{ return StatusBarStyle(style : .lightContent) } - } - var keyboardStyleNight:KeyboardStyle { - get{ return KeyboardStyle(appearance: .dark) } - } - var textFieldStyleNight:TextFieldStyle{ - return TextFieldStyle(backgroundColor: .clear, - textColor: textColorNight, - borderColor: UIColor.white.withAlphaComponent(0.3), - borderWidth:0.5, - cornerRadius:8.0, - placeholderColor: UIColor.white.withAlphaComponent(0.3)) - } - var textViewStyleNight:TextViewStyle{ - return TextViewStyle(textColor: textColorNight) - } - var activityIndicatorViewStyleNight : ActivityIndicatorViewStyle { - return ActivityIndicatorViewStyle(color: .white) - } - var scrollViewStyleNight:ScrollViewStyle{ - return ScrollViewStyle(indicatorStyle: .white) - } - var segmentedControlStyleNight:SegmentedControlStyle{ - return SegmentedControlStyle() - } - var stepperStyleNight:StepperStyle{ - return StepperStyle() - } - var pageControlStyleNight:PageControlStyle{ - return PageControlStyle() - } - var progressViewStyleNight:ProgressViewStyle{ - return ProgressViewStyle() - } - var sliderStyleNight:SliderStyle{ - return SliderStyle() - } - var switchStyleNight:SwitchStyle{ - return SwitchStyle() - } - var buttonStyleNight:ButtonStyle{ - return ButtonStyle() - } -} - - -//Mark:- Operators -func ==(lhs: Theme?, rhs: Theme?) -> Bool{ - return lhs?.id == rhs?.id -} - -func !=(lhs: Theme?, rhs: Theme?) -> Bool{ - return lhs?.id != rhs?.id -} diff --git a/mtpThemeManager/Classes/ThemeManager+Properties.swift b/mtpThemeManager/Classes/ThemeManager+Properties.swift deleted file mode 100644 index eec48e0..0000000 --- a/mtpThemeManager/Classes/ThemeManager+Properties.swift +++ /dev/null @@ -1,323 +0,0 @@ -// -// ThemeManager+Properties.swift -// ThemeManager -// -// Created by Mostafa Taghipour on 11/3/17. -// Copyright © 2017 RainyDay. All rights reserved. -// - -import UIKit - - - -public extension ThemeManager{ - var tintColor:UIColor?{ - get{ - if let unwrapedCurrentTheme=currentTheme{ - if let theme = unwrapedCurrentTheme as? DayNightTheme{ - return isItNight ? theme.tintColorNight : theme.tintColor - } - else{ - return unwrapedCurrentTheme.tintColor - } - } - - return nil - } - } - - - var backgroundColor:UIColor?{ - get{ - if let unwrapedCurrentTheme=currentTheme{ - if let theme = unwrapedCurrentTheme as? DayNightTheme{ - return isItNight ? theme.backgroundColorNight : theme.backgroundColor - } - else{ - return unwrapedCurrentTheme.backgroundColor - } - } - - return nil - } - } - - var primaryTextColor:UIColor?{ - get{ - if let unwrapedCurrentTheme=currentTheme{ - if let theme = unwrapedCurrentTheme as? DayNightTheme{ - return isItNight ? theme.textColorNight : theme.textColor - } - else{ - return unwrapedCurrentTheme.textColor - } - } - - return nil - } - } - - - var navigationBarStyle:NavigationBarStyle?{ - get{ - if let unwrapedCurrentTheme=currentTheme{ - if let theme = unwrapedCurrentTheme as? DayNightTheme{ - return isItNight ? theme.navigationBarStyleNight : theme.navigationBarStyle - } - else{ - return unwrapedCurrentTheme.navigationBarStyle - } - } - - return nil - } - } - - var tabBarStyle:TabBarStyle?{ - get{ - if let unwrapedCurrentTheme=currentTheme{ - if let theme = unwrapedCurrentTheme as? DayNightTheme{ - return isItNight ? theme.tabBarStyleNight : theme.tabBarStyle - } - else{ - return unwrapedCurrentTheme.tabBarStyle - } - } - - return nil - } - } - - var toolbarStyle:ToolbarStyle?{ - get{ - if let unwrapedCurrentTheme=currentTheme{ - if let theme = unwrapedCurrentTheme as? DayNightTheme{ - return isItNight ? theme.toolbarStyleNight : theme.toolbarStyle - } - else{ - return unwrapedCurrentTheme.toolbarStyle - } - } - - return nil - } - } - - var searchBarStyle:SearchBarStyle?{ - get{ - if let unwrapedCurrentTheme=currentTheme{ - if let theme = unwrapedCurrentTheme as? DayNightTheme{ - return isItNight ? theme.searchBarStyleNight : theme.searchBarStyle - } - else{ - return unwrapedCurrentTheme.searchBarStyle - } - } - - return nil - } - } - - var statusBarStyle:StatusBarStyle?{ - get{ - if let unwrapedCurrentTheme=currentTheme{ - if let theme = unwrapedCurrentTheme as? DayNightTheme{ - return isItNight ? theme.statusBarStyleNight : theme.statusBarStyle - } - else{ - return unwrapedCurrentTheme.statusBarStyle - } - } - - return nil - } - } - - var buttonStyles:ButtonStyle?{ - get{ - if let unwrapedCurrentTheme=currentTheme{ - if let theme = unwrapedCurrentTheme as? DayNightTheme{ - return isItNight ? theme.buttonStyleNight : theme.buttonStyle - } - else{ - return unwrapedCurrentTheme.buttonStyle - } - } - - return nil - } - } - - var textFieldStyle:TextFieldStyle?{ - get{ - if let unwrapedCurrentTheme=currentTheme{ - if let theme = unwrapedCurrentTheme as? DayNightTheme{ - return isItNight ? theme.textFieldStyleNight : theme.textFieldStyle - } - else{ - return unwrapedCurrentTheme.textFieldStyle - } - } - - return nil - } - } - - - var textViewStyle:TextViewStyle?{ - get{ - if let unwrapedCurrentTheme=currentTheme{ - if let theme = unwrapedCurrentTheme as? DayNightTheme{ - return isItNight ? theme.textViewStyleNight : theme.textViewStyle - } - else{ - return unwrapedCurrentTheme.textViewStyle - } - } - - return nil - } - } - - var keyboardStyle:KeyboardStyle?{ - get{ - if let unwrapedCurrentTheme=currentTheme{ - if let theme = unwrapedCurrentTheme as? DayNightTheme{ - return isItNight ? theme.keyboardStyleNight : theme.keyboardStyle - } - else{ - return unwrapedCurrentTheme.keyboardStyle - } - } - - return nil - } - } - - var activityIndicatorViewStyle:ActivityIndicatorViewStyle?{ - get{ - if let unwrapedCurrentTheme=currentTheme{ - if let theme = unwrapedCurrentTheme as? DayNightTheme{ - return isItNight ? theme.activityIndicatorViewStyleNight : theme.activityIndicatorViewStyle - } - else{ - return unwrapedCurrentTheme.activityIndicatorViewStyle - } - } - - return nil - } - } - - - - var switchStyle:SwitchStyle?{ - get{ - if let unwrapedCurrentTheme=currentTheme{ - if let theme = unwrapedCurrentTheme as? DayNightTheme{ - return isItNight ? theme.switchStyleNight : theme.switchStyle - } - else{ - return unwrapedCurrentTheme.switchStyle - } - } - - return nil - } - } - - - var sliderStyle:SliderStyle?{ - get{ - if let unwrapedCurrentTheme=currentTheme{ - if let theme = unwrapedCurrentTheme as? DayNightTheme{ - return isItNight ? theme.sliderStyleNight : theme.sliderStyle - } - else{ - return unwrapedCurrentTheme.sliderStyle - } - } - - return nil - } - } - - var progressViewStyle:ProgressViewStyle?{ - get{ - if let unwrapedCurrentTheme=currentTheme{ - if let theme = unwrapedCurrentTheme as? DayNightTheme{ - return isItNight ? theme.progressViewStyleNight : theme.progressViewStyle - } - else{ - return unwrapedCurrentTheme.progressViewStyle - } - } - - return nil - } - } - - var pageControlStyle:PageControlStyle?{ - get{ - if let unwrapedCurrentTheme=currentTheme{ - if let theme = unwrapedCurrentTheme as? DayNightTheme{ - return isItNight ? theme.pageControlStyleNight : theme.pageControlStyle - } - else{ - return unwrapedCurrentTheme.pageControlStyle - } - } - - return nil - } - } - - var stepperStyle:StepperStyle?{ - get{ - if let unwrapedCurrentTheme=currentTheme{ - if let theme = unwrapedCurrentTheme as? DayNightTheme{ - return isItNight ? theme.stepperStyleNight : theme.stepperStyle - } - else{ - return unwrapedCurrentTheme.stepperStyle - } - } - - return nil - } - } - - var segmentedControlStyle:SegmentedControlStyle?{ - get{ - if let unwrapedCurrentTheme=currentTheme{ - if let theme = unwrapedCurrentTheme as? DayNightTheme{ - return isItNight ? theme.segmentedControlStyleNight : theme.segmentedControlStyle - } - else{ - return unwrapedCurrentTheme.segmentedControlStyle - } - } - - return nil - } - } - - var scrollViewStyle:ScrollViewStyle?{ - get{ - if let unwrapedCurrentTheme=currentTheme{ - if let theme = unwrapedCurrentTheme as? DayNightTheme{ - return isItNight ? theme.scrollViewStyleNight : theme.scrollViewStyle - } - else{ - return unwrapedCurrentTheme.scrollViewStyle - } - } - - return nil - } - } - -} - - - From 310f5ce5d04d4f24c31d76ef9913e94df3714732 Mon Sep 17 00:00:00 2001 From: Sandeep Pathak Date: Wed, 13 Jul 2022 14:53:44 +0530 Subject: [PATCH 2/2] Fixed Statusbar crash in iOS 13+ --- .../ThemeManager+Appearance.swift | 4 +-- Sources/mtpThemeManager/UIApplication+.swift | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 Sources/mtpThemeManager/UIApplication+.swift diff --git a/Sources/mtpThemeManager/ThemeManager+Appearance.swift b/Sources/mtpThemeManager/ThemeManager+Appearance.swift index b4a1f25..91ee4b8 100644 --- a/Sources/mtpThemeManager/ThemeManager+Appearance.swift +++ b/Sources/mtpThemeManager/ThemeManager+Appearance.swift @@ -115,9 +115,7 @@ extension ThemeManager { UIApplication.shared.statusBarStyle = style.style UIApplication.shared.isStatusBarHidden = style.isHidden - if let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView { - statusBar.backgroundColor = style.backgroundColor - } + UIApplication.shared.statusBarView?.backgroundColor = style.backgroundColor } /// setKeyboardStyle diff --git a/Sources/mtpThemeManager/UIApplication+.swift b/Sources/mtpThemeManager/UIApplication+.swift new file mode 100644 index 0000000..8e454a2 --- /dev/null +++ b/Sources/mtpThemeManager/UIApplication+.swift @@ -0,0 +1,30 @@ +// +// UIApplication+.swift +// +// +// Created by Sandeep Pathak on 13/07/22. +// + +import UIKit + +extension UIApplication { + var statusBarView: UIView? { + if #available(iOS 13.0, *) { + let tag = 38482458385 + if let statusBar = self.keyWindow?.viewWithTag(tag) { + return statusBar + } else { + let statusBarView = UIView(frame: UIApplication.shared.statusBarFrame) + statusBarView.tag = tag + + self.keyWindow?.addSubview(statusBarView) + return statusBarView + } + } else { + if responds(to: Selector(("statusBar"))) { + return value(forKey: "statusBar") as? UIView + } + } + return nil + } +}