ThemeBarStylePicker.swift 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // ThemeBarStylePicker.swift
  3. // SwiftTheme
  4. //
  5. // Created by Gesen on 2017/1/28.
  6. // Copyright © 2017年 Gesen. All rights reserved.
  7. //
  8. import UIKit
  9. #if os(tvOS)
  10. final class ThemeBarStylePicker: ThemePicker {}
  11. #else
  12. @objc public final class ThemeBarStylePicker: ThemePicker {
  13. public convenience init(keyPath: String) {
  14. self.init(v: { ThemeBarStylePicker.getStyle(stringStyle: O2ThemeManager.string(for: keyPath) ?? "") })
  15. }
  16. public convenience init(keyPath: String, map: @escaping (Any?) -> UIBarStyle?) {
  17. self.init(v: { map(O2ThemeManager.value(for: keyPath)) })
  18. }
  19. public convenience init(styles: UIBarStyle...) {
  20. self.init(v: { O2ThemeManager.element(for: styles) })
  21. }
  22. public required convenience init(arrayLiteral elements: UIBarStyle...) {
  23. self.init(v: { O2ThemeManager.element(for: elements) })
  24. }
  25. public required convenience init(stringLiteral value: String) {
  26. self.init(keyPath: value)
  27. }
  28. public required convenience init(unicodeScalarLiteral value: String) {
  29. self.init(keyPath: value)
  30. }
  31. public required convenience init(extendedGraphemeClusterLiteral value: String) {
  32. self.init(keyPath: value)
  33. }
  34. class func getStyle(stringStyle: String) -> UIBarStyle {
  35. switch stringStyle.lowercased() {
  36. case "default" : return .default
  37. case "black" : return .black
  38. default: return .default
  39. }
  40. }
  41. }
  42. public extension ThemeBarStylePicker {
  43. class func pickerWithKeyPath(_ keyPath: String, map: @escaping (Any?) -> UIBarStyle?) -> ThemeBarStylePicker {
  44. return ThemeBarStylePicker(v: { map(O2ThemeManager.value(for: keyPath)) })
  45. }
  46. class func pickerWithStyles(_ styles: [UIBarStyle]) -> ThemeBarStylePicker {
  47. return ThemeBarStylePicker(v: { O2ThemeManager.element(for: styles) })
  48. }
  49. }
  50. @objc public extension ThemeBarStylePicker {
  51. class func pickerWithKeyPath(_ keyPath: String) -> ThemeBarStylePicker {
  52. return ThemeBarStylePicker(keyPath: keyPath)
  53. }
  54. class func pickerWithStringStyles(_ styles: [String]) -> ThemeBarStylePicker {
  55. return ThemeBarStylePicker(v: { O2ThemeManager.element(for: styles.map(getStyle)) })
  56. }
  57. }
  58. extension ThemeBarStylePicker: ExpressibleByArrayLiteral {}
  59. extension ThemeBarStylePicker: ExpressibleByStringLiteral {}
  60. #endif