AppearanceSettingsViewController.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2019-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import Foundation
  21. import CocoaLumberjackSwift
  22. class AppearanceSettingsViewController: ThemedTableViewController {
  23. @IBOutlet weak var showProfilePicturesSwitch: UISwitch!
  24. @IBOutlet weak var showGalleryPreviewSwitch: UISwitch!
  25. @IBOutlet weak var hideStaleContactsSwitch: UISwitch!
  26. @IBOutlet weak var systemThemeButton: UIButton!
  27. @IBOutlet weak var lightThemeButton: UIButton!
  28. @IBOutlet weak var darkThemeButton: UIButton!
  29. @IBOutlet weak var displayOrderValue: UILabel!
  30. @IBOutlet weak var previewLimitLabel: UILabel!
  31. @IBOutlet weak var previewLimitSlider: UISlider!
  32. @IBOutlet weak var systemStackView: UIStackView!
  33. @IBOutlet weak var systemThemeLabel: UILabel!
  34. @IBOutlet weak var lightThemeLabel: UILabel!
  35. @IBOutlet weak var darkThemeLabel: UILabel!
  36. @IBOutlet weak var hideStaleContactsLabel: UILabel!
  37. @IBOutlet weak var showProfilePicturesLabel: UILabel!
  38. @IBOutlet weak var displayOrderLabel: UILabel!
  39. @IBOutlet weak var showGalleryPreviewLabel: UILabel!
  40. @IBOutlet weak var themeCell: UITableViewCell!
  41. override func viewDidLoad() {
  42. super.viewDidLoad()
  43. NotificationCenter.default.addObserver(forName: Notification.Name(kNotificationColorThemeChanged), object: nil, queue: nil) { (notification) in
  44. switch Colors.getTheme() {
  45. case ColorThemeDark, ColorThemeDarkWork:
  46. self.systemThemeButton.layer.shadowColor = Colors.white()?.cgColor
  47. break
  48. case ColorThemeLight, ColorThemeLightWork, ColorThemeUndefined:
  49. self.systemThemeButton.layer.shadowColor = Colors.black()?.cgColor
  50. break
  51. default:
  52. self.systemThemeButton.layer.shadowColor = Colors.black()?.cgColor
  53. }
  54. }
  55. }
  56. override func viewWillAppear(_ animated: Bool) {
  57. super.viewWillAppear(animated)
  58. self.updateView()
  59. }
  60. override func viewDidAppear(_ animated: Bool) {
  61. super.viewDidAppear(animated)
  62. previewLimitSlider.isEnabled = showGalleryPreviewSwitch.isOn
  63. }
  64. func updateView() {
  65. darkThemeLabel.text = BundleUtil.localizedString(forKey: "settings_appearance_dark_theme")
  66. lightThemeLabel.text = BundleUtil.localizedString(forKey: "settings_appearance_light_theme")
  67. systemThemeLabel.text = BundleUtil.localizedString(forKey: "settings_appearance_system_theme")
  68. darkThemeButton.accessibilityLabel = BundleUtil.localizedString(forKey: "settings_appearance_dark_theme")
  69. lightThemeButton.accessibilityLabel = BundleUtil.localizedString(forKey: "settings_appearance_light_theme")
  70. systemThemeButton.accessibilityLabel = BundleUtil.localizedString(forKey: "settings_appearance_system_theme")
  71. hideStaleContactsLabel.text = BundleUtil.localizedString(forKey: "settings_appearance_hide_stale_contacts")
  72. showProfilePicturesLabel.text = BundleUtil.localizedString(forKey: "settings_appearance_show_profile_pictures")
  73. displayOrderLabel.text = BundleUtil.localizedString(forKey: "settings_appearance_display_order")
  74. showGalleryPreviewLabel.text = BundleUtil.localizedString(forKey: "settings_appearance_show_gallery_preview")
  75. showProfilePicturesSwitch.isOn = UserSettings.shared().showProfilePictures
  76. showGalleryPreviewSwitch.isOn = UserSettings.shared().showGalleryPreview
  77. darkThemeButton.layer.shadowColor = Colors.white()?.cgColor
  78. darkThemeButton.layer.shadowOffset = CGSize.init(width: 0, height: 0)
  79. darkThemeButton.layer.shadowRadius = 9.0
  80. lightThemeButton.layer.shadowColor = Colors.black()?.cgColor
  81. lightThemeButton.layer.shadowOffset = CGSize.init(width: 0, height: 0)
  82. lightThemeButton.layer.shadowRadius = 9.0
  83. switch Colors.getTheme() {
  84. case ColorThemeDark, ColorThemeDarkWork:
  85. systemThemeButton.layer.shadowColor = Colors.white()?.cgColor
  86. break
  87. case ColorThemeLight, ColorThemeLightWork, ColorThemeUndefined:
  88. systemThemeButton.layer.shadowColor = Colors.black()?.cgColor
  89. break
  90. default:
  91. systemThemeButton.layer.shadowColor = Colors.black()?.cgColor
  92. }
  93. systemThemeButton.layer.shadowOffset = CGSize.init(width: 0, height: 0)
  94. systemThemeButton.layer.shadowRadius = 9.0
  95. if LicenseStore.requiresLicenseKey() == false {
  96. lightThemeButton.setImage(BundleUtil.imageNamed("Light-Theme"), for: .normal)
  97. darkThemeButton.setImage(BundleUtil.imageNamed("Dark-Theme"), for: .normal)
  98. systemThemeButton.setImage(BundleUtil.imageNamed("System-Theme"), for: .normal)
  99. } else {
  100. lightThemeButton.setImage(BundleUtil.imageNamed("Light-Theme-Work"), for: .normal)
  101. darkThemeButton.setImage(BundleUtil.imageNamed("Dark-Theme-Work"), for: .normal)
  102. systemThemeButton.setImage(BundleUtil.imageNamed("System-Theme-Work"), for: .normal)
  103. }
  104. displayOrderValue.text = UserSettings.shared().displayOrderFirstName ? BundleUtil.localizedString(forKey: "SortOrder_Firstname") : BundleUtil.localizedString(forKey: "SortOrder_Lastname")
  105. hideStaleContactsSwitch.isOn = UserSettings.shared().hideStaleContacts
  106. previewLimitSlider.value = UserSettings.shared().previewLimit
  107. previewLimitLabel.text = String.init(format: BundleUtil.localizedString(forKey: "preview_limit"), previewLimitSlider.value)
  108. previewLimitLabel.isEnabled = showGalleryPreviewSwitch.isOn
  109. if #available(iOS 13.0, *) {
  110. systemStackView.isHidden = false
  111. if UserSettings.shared().useSystemTheme {
  112. lightThemeButton.layer.shadowOpacity = 0.0
  113. darkThemeButton.layer.shadowOpacity = 0.0
  114. systemThemeButton.layer.shadowOpacity = 1.0
  115. themeCell.accessibilityLabel = String.localizedStringWithFormat(BundleUtil.localizedString(forKey: "settings_appearance_theme_selected"), BundleUtil.localizedString(forKey: "settings_appearance_system_theme"))
  116. systemThemeButton.accessibilityLabel = BundleUtil.localizedString(forKey: "settings_appearance_system_theme") + ", " + BundleUtil.localizedString(forKey: "settings_appearance_theme_active")
  117. } else {
  118. updateButtonShadowForCurrentTheme()
  119. }
  120. } else {
  121. systemStackView.isHidden = true
  122. updateButtonShadowForCurrentTheme()
  123. }
  124. }
  125. private func updateButtonShadowForCurrentTheme() {
  126. switch Colors.getTheme() {
  127. case ColorThemeDark, ColorThemeDarkWork:
  128. lightThemeButton.layer.shadowOpacity = 0.0
  129. darkThemeButton.layer.shadowOpacity = 1.0
  130. systemThemeButton.layer.shadowOpacity = 0.0
  131. themeCell.accessibilityLabel = String.localizedStringWithFormat(BundleUtil.localizedString(forKey: "settings_appearance_theme_selected"), BundleUtil.localizedString(forKey: "settings_appearance_dark_theme"))
  132. darkThemeButton.accessibilityLabel = BundleUtil.localizedString(forKey: "settings_appearance_dark_theme") + ", " + BundleUtil.localizedString(forKey: "settings_appearance_theme_active")
  133. break
  134. case ColorThemeLight, ColorThemeLightWork, ColorThemeUndefined:
  135. lightThemeButton.layer.shadowOpacity = 1.0
  136. darkThemeButton.layer.shadowOpacity = 0.0
  137. systemThemeButton.layer.shadowOpacity = 0.0
  138. themeCell.accessibilityLabel = String.localizedStringWithFormat(BundleUtil.localizedString(forKey: "settings_appearance_theme_selected"), BundleUtil.localizedString(forKey: "settings_appearance_light_theme"))
  139. lightThemeButton.accessibilityLabel = BundleUtil.localizedString(forKey: "settings_appearance_light_theme") + ", " + BundleUtil.localizedString(forKey: "settings_appearance_theme_active")
  140. break
  141. default:
  142. lightThemeButton.layer.shadowOpacity = 1.0
  143. darkThemeButton.layer.shadowOpacity = 0.0
  144. systemThemeButton.layer.shadowOpacity = 0.0
  145. themeCell.accessibilityLabel = String.localizedStringWithFormat(BundleUtil.localizedString(forKey: "settings_appearance_theme_selected"), BundleUtil.localizedString(forKey: "settings_appearance_light_theme"))
  146. lightThemeButton.accessibilityLabel = BundleUtil.localizedString(forKey: "settings_appearance_light_theme") + ", " + BundleUtil.localizedString(forKey: "settings_appearance_theme_active")
  147. }
  148. }
  149. }
  150. extension AppearanceSettingsViewController {
  151. override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  152. if indexPath.section == 0 && indexPath.row == 0 {
  153. return 230.0
  154. }
  155. if #available(iOS 11.0, *) {
  156. // do nothing
  157. } else {
  158. if indexPath.row == 3 {
  159. return 0
  160. }
  161. }
  162. return UITableView.automaticDimension
  163. }
  164. override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
  165. if indexPath.section == 0 && indexPath.row == 0 {
  166. return 230
  167. }
  168. return UITableView.automaticDimension
  169. }
  170. override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  171. return UITableView.automaticDimension
  172. }
  173. override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  174. if section == 0 {
  175. return BundleUtil.localizedString(forKey: "settings_appearance_theme_section")
  176. }
  177. return nil
  178. }
  179. override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
  180. if section == 1 {
  181. return UserSettings.shared().hideStaleContacts ? BundleUtil.localizedString(forKey: "show_stale_contacts_on") : BundleUtil.localizedString(forKey: "show_stale_contacts_off")
  182. }
  183. return nil
  184. }
  185. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  186. tableView.deselectRow(at: indexPath, animated: true)
  187. }
  188. }
  189. extension AppearanceSettingsViewController {
  190. @IBAction func systemThemeSelected(sender: UIButton) {
  191. UserSettings.shared()?.useSystemTheme = true
  192. darkThemeButton.layer.shadowOpacity = 0.0
  193. lightThemeButton.layer.shadowOpacity = 0.0
  194. systemThemeButton.layer.shadowOpacity = 1.0
  195. if #available(iOS 13.0, *) {
  196. AppDelegate.shared().window.overrideUserInterfaceStyle = .unspecified
  197. if self.traitCollection.userInterfaceStyle == .dark {
  198. if Colors.getTheme() != ColorThemeDark && Colors.getTheme() != ColorThemeDarkWork {
  199. Colors.setTheme(LicenseStore.requiresLicenseKey() ? ColorThemeDarkWork : ColorThemeDark)
  200. }
  201. } else {
  202. if Colors.getTheme() != ColorThemeLight && Colors.getTheme() != ColorThemeLightWork {
  203. Colors.setTheme(LicenseStore.requiresLicenseKey() ? ColorThemeLightWork : ColorThemeLight)
  204. }
  205. }
  206. }
  207. }
  208. @IBAction func lightThemeSelected(sender: UIButton) {
  209. UserSettings.shared().useSystemTheme = false
  210. switch Colors.getTheme() {
  211. case ColorThemeLight, ColorThemeLightWork:
  212. break
  213. case ColorThemeDark, ColorThemeDarkWork, ColorThemeUndefined:
  214. Colors.setTheme(LicenseStore.requiresLicenseKey() ? ColorThemeLightWork : ColorThemeLight)
  215. break
  216. default:
  217. Colors.setTheme(LicenseStore.requiresLicenseKey() ? ColorThemeLightWork : ColorThemeLight)
  218. }
  219. updateButtonShadowForCurrentTheme()
  220. if #available(iOS 13.0, *) {
  221. AppDelegate.shared()?.window.overrideUserInterfaceStyle = .light
  222. }
  223. }
  224. @IBAction func darkThemeSelected(sender: UIButton) {
  225. UserSettings.shared().useSystemTheme = false
  226. switch Colors.getTheme() {
  227. case ColorThemeDark, ColorThemeDarkWork:
  228. break
  229. case ColorThemeLight, ColorThemeLightWork, ColorThemeUndefined:
  230. Colors.setTheme(LicenseStore.requiresLicenseKey() ? ColorThemeDarkWork : ColorThemeDark)
  231. break
  232. default:
  233. Colors.setTheme(LicenseStore.requiresLicenseKey() ? ColorThemeDarkWork : ColorThemeDark)
  234. }
  235. updateButtonShadowForCurrentTheme()
  236. if #available(iOS 13.0, *) {
  237. AppDelegate.shared()?.window.overrideUserInterfaceStyle = .dark
  238. }
  239. }
  240. @IBAction func showProfilePicturesChanged(sender: UISwitch) {
  241. UserSettings.shared()?.showProfilePictures = showProfilePicturesSwitch.isOn
  242. }
  243. @IBAction func showGalleryPreviewChanged(sender: UISwitch) {
  244. UserSettings.shared()?.showGalleryPreview = showGalleryPreviewSwitch.isOn
  245. previewLimitLabel.isEnabled = showGalleryPreviewSwitch.isOn
  246. previewLimitSlider.isEnabled = showGalleryPreviewSwitch.isOn
  247. }
  248. @IBAction func hideStaleContactsChanged(sender: UISwitch) {
  249. UserSettings.shared()?.hideStaleContacts = hideStaleContactsSwitch.isOn
  250. tableView.reloadData()
  251. }
  252. @IBAction func previewLimitChanged(sender: UISlider) {
  253. let roundedValue = round(sender.value / 5) * 5
  254. sender.value = roundedValue
  255. previewLimitLabel.text = String.init(format: BundleUtil.localizedString(forKey: "preview_limit"), sender.value)
  256. UserSettings.shared()?.previewLimit = sender.value
  257. }
  258. }