ThemeActivityIndicatorViewStylePicker.swift 3.0 KB

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