Guest User

Untitled

a guest
Jan 20th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.90 KB | None | 0 0
  1. //
  2. // Settings.swift
  3. // Stockpapers
  4. //
  5. // Created by Federico Vitale on 12/01/2019.
  6. // Copyright © 2019 Federico Vitale. All rights reserved.
  7. //
  8.  
  9. import Foundation
  10. import UIKit
  11.  
  12. enum AccessoryType: Int {
  13. case selectable = 0,
  14. switchable,
  15. bool,
  16. button,
  17. view,
  18. none
  19. }
  20.  
  21. enum Setting {
  22. case accentColor
  23. case imageSize
  24. case closeGesture
  25. case pictureQuality
  26. case hideGesture
  27. case history
  28. case favourites
  29. case about
  30. case removeWatermarks
  31. case restorePurchases
  32. case restoreSettings
  33. case unsplashLogin
  34.  
  35.  
  36. case toggleHighQualityPreview
  37. case toggleDarkTheme
  38. case toggleStatusBarOnPreview
  39. case toggleParallax
  40. }
  41.  
  42. struct SettingItem {
  43. let type: AccessoryType
  44. let title: String
  45. let section: SectionID
  46. let style: SettingTableViewCell.Style
  47. let viewColor: UIColor?
  48. let trigger : Selector?
  49. let id : Setting?
  50.  
  51. init(
  52. _ title: String,
  53. _ type: AccessoryType = .selectable,
  54. _ section: SectionID = .generals,
  55. id: Setting? = nil,
  56. style: SettingTableViewCell.Style = .default,
  57. action trigger: Selector? = nil,
  58. viewColor: UIColor? = nil
  59. ) {
  60. self.title = title
  61. self.type = type
  62. self.section = section
  63.  
  64. self.id = id
  65. self.style = style
  66. self.viewColor = viewColor
  67. self.trigger = trigger
  68. }
  69.  
  70. init(
  71. _ title: String,
  72. _ section: SectionID = .generals,
  73. type: AccessoryType = .selectable,
  74. id: Setting? = nil,
  75. style: SettingTableViewCell.Style = .default,
  76. action trigger: Selector? = nil,
  77. viewColor: UIColor? = nil
  78. ) {
  79. self.title = title
  80. self.type = type
  81. self.section = section
  82.  
  83. self.id = id
  84. self.style = style
  85. self.viewColor = viewColor
  86. self.trigger = trigger
  87. }
  88.  
  89.  
  90.  
  91. enum SectionID: Int {
  92. case generals = 0,
  93. theming,
  94. imageSettings,
  95. data,
  96. purchases,
  97. restore,
  98. info
  99. }
  100. }
  101.  
  102. class SettingTableViewCell: DarkTableViewCell {
  103. enum Style {
  104. case accent
  105. case danger
  106. case boldDanger
  107. case bold
  108. case `default`
  109. }
  110.  
  111. // Once you set this, you're ready to rock!
  112. var setting: SettingItem? {
  113. didSet {
  114. textLabel?.text = setting?.title
  115. setupStyle()
  116. }
  117. }
  118.  
  119. var isEnabled: Bool = true {
  120. didSet {
  121. print("Cell status changed: \(setting?.title) -> ", isEnabled)
  122.  
  123. if isEnabled == false {
  124. textLabel?.alpha = 0.1
  125. selectionStyle = .none
  126. }
  127. }
  128. }
  129.  
  130. override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  131. super.init(style: .value1, reuseIdentifier: reuseIdentifier)
  132. setupStyle()
  133. }
  134.  
  135. required init?(coder aDecoder: NSCoder) {
  136. fatalError("init(coder:) has not been implemented")
  137. }
  138.  
  139. override func setupStyle() {
  140. super.setupStyle()
  141.  
  142. guard let setting = setting else { return }
  143.  
  144. switch setting.type {
  145. case .selectable:
  146. accessoryType = .disclosureIndicator
  147. accessoryView = nil
  148. break
  149. case .switchable:
  150. accessoryType = .none
  151. accessoryView = {
  152. let toggle = UISwitch()
  153.  
  154. if let action = setting.trigger {
  155. toggle.addTarget(self, action: action, for: .valueChanged)
  156. }
  157.  
  158. toggle.onTintColor = theme.accentColor
  159. toggle.setOn(false, animated: false)
  160.  
  161. return toggle
  162. }()
  163. break
  164. case .button:
  165. accessoryType = .none
  166. accessoryView = {
  167. let button = UIButton(frame: CGRect(x: 0, y: 0, width: 75, height: 25));
  168.  
  169. button.setTitle("Tap Me", for: .normal)
  170. button.setTitleColor(theme.textColor, for: .normal)
  171. button.backgroundColor = theme.accentColor
  172. button.layer.cornerRadius = 2.5
  173. button.titleLabel?.font = .systemFont(ofSize: 12, weight: .bold)
  174.  
  175. if let action = setting.trigger {
  176. button.addTarget(self, action: action, for: .touchUpInside)
  177. }
  178.  
  179. return button
  180. }()
  181.  
  182. selectionStyle = .none
  183. break
  184. case .view:
  185. accessoryType = .none
  186. accessoryView = {
  187. let square = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
  188. square.layer.cornerRadius = 5
  189. square.backgroundColor = setting.viewColor ?? theme.accentColor
  190. return square
  191. }()
  192. break
  193. default:
  194. accessoryView = nil
  195. accessoryType = .none
  196. break
  197. }
  198.  
  199. backgroundColor = theme.color
  200. selectedBackgroundView = {
  201. let selectedView = UIView()
  202. selectedView.backgroundColor = theme.darkerColor
  203. return selectedView
  204. }()
  205.  
  206. textLabel?.textColor = {
  207. switch setting.style {
  208. case .accent:
  209. return theme.accentColor
  210. case .default:
  211. return isEnabled ? theme.textColor : theme.textColor.withAlpha(0.3)
  212. case .danger:
  213. return Colors.alizarin
  214. default:
  215. return theme.textColor
  216. }
  217. }()
  218.  
  219. detailTextLabel?.textColor = theme.textColor.withAlpha(0.2)
  220. tintColor = theme.textColor.withAlpha(0.2)
  221.  
  222. setupSeparator()
  223. }
  224.  
  225. override func prepareForReuse() {
  226. super.prepareForReuse()
  227.  
  228. self.isEnabled = true
  229. self.setting = nil
  230. self.alpha = 1
  231. }
  232. }
Add Comment
Please, Sign In to add comment