GlobalNotificationSettingsViewController.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 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 UIKit
  21. class GlobalNotificationSettingsViewController: ThemedTableViewController {
  22. @IBOutlet weak var inAppSoundsSwitch: UISwitch!
  23. @IBOutlet weak var inAppVibrateSwitch: UISwitch!
  24. @IBOutlet weak var inAppPreviewSwitch: UISwitch!
  25. @IBOutlet weak var inAppSoundsLabel: UILabel!
  26. @IBOutlet weak var inAppVibrateLabel: UILabel!
  27. @IBOutlet weak var inAppPreviewLabel: UILabel!
  28. @IBOutlet weak var pushSoundLabel: UILabel!
  29. @IBOutlet weak var pushGroupSoundLabel: UILabel!
  30. @IBOutlet weak var pushDecryptLabel: UILabel!
  31. @IBOutlet weak var pushShowNicknameLabel: UILabel!
  32. @IBOutlet weak var pushSoundValueLabel: UILabel!
  33. @IBOutlet weak var pushGroupSoundValueLabel: UILabel!
  34. @IBOutlet weak var pushGroupSoundCell: UITableViewCell!
  35. @IBOutlet weak var pushDecryptSwitch: UISwitch!
  36. @IBOutlet weak var pushPreviewCell: UITableViewCell!
  37. @IBOutlet weak var voIPSoundLabel: UILabel!
  38. @IBOutlet weak var voIPSoundValueLabel: UILabel!
  39. @IBOutlet weak var pushShowNicknameSwitch: UISwitch!
  40. @IBOutlet weak var masterDndLabel: UILabel!
  41. @IBOutlet weak var masterDndSwitch: UISwitch!
  42. @IBOutlet weak var masterDndDaysCell: UITableViewCell!
  43. @IBOutlet weak var masterDndStartTimeCell: UITableViewCell!
  44. @IBOutlet weak var masterDndEndTimeCell: UITableViewCell!
  45. @IBOutlet weak var masterDndDaysLabel: UILabel!
  46. @IBOutlet weak var masterDndDaysValueLabel: UILabel!
  47. @IBOutlet weak var masterDndStartTimeLabel: UILabel!
  48. @IBOutlet weak var masterDndStartTimeValueLabel: UILabel!
  49. @IBOutlet weak var masterDndEndTimeLabel: UILabel!
  50. @IBOutlet weak var masterDndEndTimeValueLabel: UILabel!
  51. let mdmSetup: MDMSetup
  52. var timePickerIndexPath: IndexPath?
  53. var selectedCellForPickerIndexPath: IndexPath?
  54. typealias TimeOfDay = (hour: Int, minute: Int)
  55. required init?(coder aDecoder: NSCoder) {
  56. self.mdmSetup = MDMSetup(setup: false)
  57. super.init(coder: aDecoder)
  58. }
  59. override func viewDidLoad() {
  60. super.viewDidLoad()
  61. tableView.register(UINib(nibName: TimePickerwCell.nibName(), bundle: nil), forCellReuseIdentifier: TimePickerwCell.reuseIdentifier())
  62. }
  63. override func viewWillAppear(_ animated: Bool) {
  64. super.viewWillAppear(animated)
  65. self.setupSwitches()
  66. self.setupLabels()
  67. self.tableView.reloadData()
  68. }
  69. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  70. if segue.identifier == "EditPushGroupSound" {
  71. if let psvc = segue.destination as? PushSoundViewController {
  72. psvc.group = true
  73. }
  74. }
  75. }
  76. // MARK Private functions
  77. private func setupSwitches() {
  78. self.inAppSoundsSwitch.isOn = UserSettings.shared().inAppSounds
  79. self.inAppVibrateSwitch.isOn = UserSettings.shared().inAppVibrate
  80. self.inAppPreviewSwitch.isOn = UserSettings.shared().inAppPreview
  81. self.pushShowNicknameSwitch.isOn = UserSettings.shared().pushShowNickname
  82. self.pushDecryptSwitch.isOn = UserSettings.shared().pushDecrypt
  83. self.masterDndSwitch.isOn = UserSettings.shared().enableMasterDnd
  84. self.pushDecryptSwitch.isEnabled = !mdmSetup.existsMdmKey(MDM_KEY_DISABLE_MESSAGE_PREVIEW)
  85. }
  86. private func setupLabels() {
  87. self.inAppSoundsLabel.text = BundleUtil.localizedString(forKey: "settings_notifications_inapp_sounds")
  88. self.inAppVibrateLabel.text = BundleUtil.localizedString(forKey: "settings_notifications_inapp_vibrate")
  89. self.inAppPreviewLabel.text = BundleUtil.localizedString(forKey: "settings_notifications_inapp_preview")
  90. self.pushSoundLabel.text = BundleUtil.localizedString(forKey: "settings_notifications_push_sound")
  91. self.pushGroupSoundLabel.text = BundleUtil.localizedString(forKey: "settings_notifications_push_groupsound")
  92. self.pushDecryptLabel.text = BundleUtil.localizedString(forKey: "settings_notifications_push_preview")
  93. self.pushShowNicknameLabel.text = BundleUtil.localizedString(forKey: "settings_notifications_push_showNickname")
  94. self.voIPSoundLabel.text = BundleUtil.localizedString(forKey: "settings_notifications_call_sound")
  95. let pushSoundName = "sound_\(UserSettings.shared().pushSound!)"
  96. self.pushSoundValueLabel.text = BundleUtil.localizedString(forKey: pushSoundName)
  97. let pushGroupSoundName = "sound_\(UserSettings.shared().pushGroupSound!)"
  98. self.pushGroupSoundValueLabel.text = BundleUtil.localizedString(forKey: pushGroupSoundName)
  99. let voIPSoundName = "sound_\(UserSettings.shared().voIPSound!)"
  100. self.voIPSoundValueLabel.text = BundleUtil.localizedString(forKey: voIPSoundName)
  101. self.masterDndLabel.text = BundleUtil.localizedString(forKey: "settings_notifications_masterDnd")
  102. self.masterDndDaysLabel.text = BundleUtil.localizedString(forKey: "settings_notifications_masterDnd_workingDays")
  103. var workingDayShortString = ""
  104. let workingDays = UserSettings.shared().masterDndWorkingDays!
  105. if workingDays.count > 0 {
  106. let sortedWorkingDays = workingDays.sortedArray { (a, b) -> ComparisonResult in
  107. var dayA = a as! Int
  108. var dayB = b as! Int
  109. if dayA < Calendar.current.firstWeekday {
  110. dayA += Calendar.current.weekdaySymbols.count
  111. }
  112. if dayB < Calendar.current.firstWeekday {
  113. dayB += Calendar.current.weekdaySymbols.count
  114. }
  115. if dayA < dayB {
  116. return .orderedAscending
  117. }
  118. if dayA > dayB {
  119. return .orderedDescending
  120. }
  121. return .orderedSame
  122. }
  123. for dayNumber in sortedWorkingDays {
  124. if workingDayShortString.count > 0 {
  125. workingDayShortString.append(", ")
  126. }
  127. workingDayShortString.append(Calendar.current.shortWeekdaySymbols[(dayNumber as! Int)-1])
  128. }
  129. self.masterDndDaysValueLabel.text = workingDayShortString
  130. } else {
  131. self.masterDndDaysValueLabel.text = ""
  132. }
  133. self.masterDndStartTimeLabel.text = BundleUtil.localizedString(forKey: "settings_notifications_masterDnd_startTime")
  134. self.masterDndStartTimeValueLabel.text = UserSettings.shared().masterDndStartTime
  135. self.masterDndEndTimeLabel.text = BundleUtil.localizedString(forKey: "settings_notifications_masterDnd_endTime")
  136. self.masterDndEndTimeValueLabel.text = UserSettings.shared().masterDndEndTime
  137. self.pushDecryptLabel.isEnabled = !mdmSetup.existsMdmKey(MDM_KEY_DISABLE_MESSAGE_PREVIEW)
  138. }
  139. // MARK IBActions
  140. @IBAction func inAppSoundsChanged(sender: UISwitch) {
  141. UserSettings.shared().inAppSounds = self.inAppSoundsSwitch.isOn
  142. }
  143. @IBAction func inAppVibrateChanged(sender: UISwitch) {
  144. UserSettings.shared().inAppVibrate = self.inAppVibrateSwitch.isOn
  145. }
  146. @IBAction func inAppPreviewChanged(sender: UISwitch) {
  147. UserSettings.shared().inAppPreview = self.inAppPreviewSwitch.isOn
  148. }
  149. @IBAction func pushDecryptChanged(sender: UISwitch) {
  150. UserSettings.shared().pushDecrypt = self.pushDecryptSwitch.isOn
  151. }
  152. @IBAction func pushShowNicknameChanged(sender: UISwitch) {
  153. UserSettings.shared()?.pushShowNickname = self.pushShowNicknameSwitch.isOn
  154. }
  155. @IBAction func masterDndChanged(sender: UISwitch) {
  156. UserSettings.shared().enableMasterDnd = sender.isOn
  157. self.tableView.beginUpdates()
  158. if let timePickerIndexPath = timePickerIndexPath {
  159. tableView.deleteRows(at: [timePickerIndexPath], with: .fade)
  160. self.timePickerIndexPath = nil
  161. self.selectedCellForPickerIndexPath = nil
  162. }
  163. self.tableView.reloadSections(IndexSet(integer: 3), with: .none)
  164. self.tableView.endUpdates()
  165. }
  166. // MARK: - Table view data source
  167. override func numberOfSections(in tableView: UITableView) -> Int {
  168. if LicenseStore.requiresLicenseKey() == true {
  169. return 4
  170. }
  171. return 3
  172. }
  173. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  174. if timePickerIndexPath != nil, section == timePickerIndexPath?.section {
  175. return super.tableView(tableView, numberOfRowsInSection: section) + 1
  176. }
  177. if section == 3, !masterDndSwitch.isOn {
  178. return 1
  179. }
  180. return super.tableView(tableView, numberOfRowsInSection: section)
  181. }
  182. override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
  183. if timePickerIndexPath == indexPath {
  184. return TimePickerwCell.cellHeight()
  185. } else {
  186. return super.tableView(tableView, estimatedHeightForRowAt: indexPath)
  187. }
  188. }
  189. override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  190. if timePickerIndexPath == indexPath {
  191. return TimePickerwCell.cellHeight()
  192. } else {
  193. return super.tableView(tableView, heightForRowAt: indexPath)
  194. }
  195. }
  196. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  197. if timePickerIndexPath == indexPath {
  198. let dateCell = tableView.cellForRow(at: selectedCellForPickerIndexPath!)
  199. let datePickerCell = tableView.dequeueReusableCell(withIdentifier: TimePickerwCell.reuseIdentifier()) as! TimePickerwCell
  200. var time = "00:00"
  201. if dateCell?.tag == 1 {
  202. time = UserSettings.shared().masterDndStartTime!
  203. }
  204. if dateCell?.tag == 2 {
  205. time = UserSettings.shared().masterDndEndTime!
  206. }
  207. let date = dateFromTimeString(timeString: time)
  208. datePickerCell.updateCell(date: date, indexPath: indexPath)
  209. datePickerCell.delegate = self
  210. return datePickerCell
  211. } else {
  212. if timePickerIndexPath != nil, indexPath.section == 3, indexPath.row >= timePickerIndexPath!.row {
  213. let newIndexPath = IndexPath(row: indexPath.row - 1, section: indexPath.section)
  214. return super.tableView(tableView, cellForRowAt: newIndexPath)
  215. } else {
  216. return super.tableView(tableView, cellForRowAt: indexPath)
  217. }
  218. }
  219. }
  220. override func tableView(_ tableView: UITableView, indentationLevelForRowAt indexPath: IndexPath) -> Int {
  221. return 0
  222. }
  223. override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  224. switch section {
  225. case 0:
  226. return BundleUtil.localizedString(forKey: "settings_notifications_inapp_section")
  227. case 1:
  228. return BundleUtil.localizedString(forKey: "settings_notifications_push_section")
  229. case 2:
  230. return BundleUtil.localizedString(forKey: "settings_notifications_call_section")
  231. case 3:
  232. return BundleUtil.localizedString(forKey: "settings_notifications_masterDnd_section_header")
  233. default:
  234. return nil
  235. }
  236. }
  237. override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
  238. switch section {
  239. case 1:
  240. if mdmSetup.disableMessagePreview() {
  241. return BundleUtil.localizedString(forKey: "disabled_by_device_policy")
  242. }
  243. case 3:
  244. return BundleUtil.localizedString(forKey: "settings_notifications_masterDnd_section_footer")
  245. default:
  246. return nil
  247. }
  248. return nil
  249. }
  250. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  251. let cell = tableView.cellForRow(at: indexPath)
  252. var scrollToPicker = false
  253. if cell?.tag == 1 || cell?.tag == 2 {
  254. tableView.beginUpdates()
  255. if let timePickerIndexPath = timePickerIndexPath, timePickerIndexPath.row - 1 == indexPath.row {
  256. tableView.deleteRows(at: [timePickerIndexPath], with: .fade)
  257. self.timePickerIndexPath = nil
  258. self.selectedCellForPickerIndexPath = nil
  259. } else {
  260. if let timePickerIndexPath = timePickerIndexPath {
  261. tableView.deleteRows(at: [timePickerIndexPath], with: .fade)
  262. }
  263. timePickerIndexPath = indexPathToInsertTimePicker(indexPath: indexPath)
  264. selectedCellForPickerIndexPath = indexPath
  265. tableView.insertRows(at: [timePickerIndexPath!], with: .fade)
  266. scrollToPicker = true
  267. }
  268. tableView.endUpdates()
  269. } else {
  270. if let timePickerIndexPath = timePickerIndexPath {
  271. tableView.beginUpdates()
  272. tableView.deleteRows(at: [timePickerIndexPath], with: .fade)
  273. self.timePickerIndexPath = nil
  274. self.selectedCellForPickerIndexPath = nil
  275. tableView.endUpdates()
  276. }
  277. }
  278. tableView.deselectRow(at: tableView.indexPathForSelectedRow!, animated: true)
  279. if scrollToPicker {
  280. tableView.scrollToRow(at: timePickerIndexPath!, at: .middle, animated: true)
  281. }
  282. }
  283. // MARK: private functions
  284. private func indexPathToInsertTimePicker(indexPath: IndexPath) -> IndexPath {
  285. if let timePickerIndexPath = timePickerIndexPath, timePickerIndexPath.row < indexPath.row {
  286. return indexPath
  287. } else {
  288. return IndexPath(row: indexPath.row + 1, section: indexPath.section)
  289. }
  290. }
  291. private func timeOfDayFromTimeString(timeString: String) -> TimeOfDay {
  292. let components: [String] = timeString.components(separatedBy: ":")
  293. return TimeOfDay(hour: Int(components[0])!, minute: Int(components[1])!)
  294. }
  295. private func dateFromTimeString(timeString: String) -> Date {
  296. let components: [String] = timeString.components(separatedBy: ":")
  297. return Calendar.current.date(bySettingHour: Int(components[0])!, minute: Int(components[1])!, second: 0, of: Date())!
  298. }
  299. }
  300. extension GlobalNotificationSettingsViewController: TimePickerDelegate {
  301. func didChangeTime(date: Date, indexPath: IndexPath) {
  302. let cell = tableView.cellForRow(at: indexPath)
  303. let calendar = Calendar.current
  304. let hour = calendar.component(.hour, from: date)
  305. let minute = calendar.component(.minute, from: date)
  306. let newTimeString = String(format: "%02d:%02d", hour, minute)
  307. if cell?.tag == 1 {
  308. let newStartTime = timeOfDayFromTimeString(timeString: newTimeString)
  309. let endTime = timeOfDayFromTimeString(timeString: UserSettings.shared().masterDndEndTime)
  310. if newStartTime < endTime {
  311. UserSettings.shared().masterDndStartTime = newTimeString
  312. self.masterDndStartTimeValueLabel.text = newTimeString
  313. } else {
  314. if timePickerIndexPath != nil {
  315. if let datePickerCell = self.tableView.cellForRow(at: timePickerIndexPath!) as? TimePickerwCell {
  316. let date = dateFromTimeString(timeString: UserSettings.shared().masterDndStartTime)
  317. datePickerCell.updateCell(date: date, indexPath: timePickerIndexPath!)
  318. }
  319. }
  320. }
  321. }
  322. if cell?.tag == 2 {
  323. let newEndTime = timeOfDayFromTimeString(timeString: newTimeString)
  324. let startTime = timeOfDayFromTimeString(timeString: UserSettings.shared().masterDndStartTime)
  325. if newEndTime > startTime {
  326. UserSettings.shared().masterDndEndTime = newTimeString
  327. self.masterDndEndTimeValueLabel.text = newTimeString
  328. } else {
  329. if timePickerIndexPath != nil {
  330. if let datePickerCell = self.tableView.cellForRow(at: timePickerIndexPath!) as? TimePickerwCell {
  331. let date = dateFromTimeString(timeString: UserSettings.shared().masterDndEndTime)
  332. datePickerCell.updateCell(date: date, indexPath: timePickerIndexPath!)
  333. }
  334. }
  335. }
  336. }
  337. }
  338. }