NotificationSettingViewController.swift 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2018-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 UIKit
  22. class NotificationSettingViewController: ThemedTableViewController {
  23. @objc var isGroup: Bool = false
  24. @objc var identity: String?
  25. @objc var conversation: Conversation?
  26. private var _pushSetting:PushSetting?
  27. private var _pushSettingList: NSMutableOrderedSet?
  28. @IBOutlet weak var doNotDisturbCell: DoNotDisturbCell!
  29. @IBOutlet weak var mentionsCell: UITableViewCell!
  30. @IBOutlet weak var soundCell: UITableViewCell!
  31. @IBOutlet weak var soundSwitch: UISwitch!
  32. @IBOutlet weak var mentionsSwitch: UISwitch!
  33. @IBOutlet weak var masterDndView: UIView!
  34. @IBOutlet weak var masterDndInfoLabel: UILabel!
  35. @IBOutlet weak var masterDndInfoImageView: UIImageView!
  36. override func viewDidLoad() {
  37. super.viewDidLoad()
  38. NotificationCenter.default.addObserver(self, selector: #selector(colorThemeChanged(notification:)), name: NSNotification.Name(rawValue: kNotificationColorThemeChanged), object: nil)
  39. _pushSettingList = NSMutableOrderedSet.init(orderedSet: UserSettings.shared().pushSettingsList)
  40. _pushSetting = PushSetting.find(forIdentity: identity)
  41. if _pushSetting == nil {
  42. _pushSetting = PushSetting()
  43. _pushSetting!.identity = identity
  44. _pushSetting!.type = .on
  45. _pushSetting!.silent = false
  46. _pushSetting!.mentions = false
  47. } else {
  48. if _pushSetting!.type == .offPeriod {
  49. if _pushSetting!.periodOffTillDate != nil {
  50. if _pushSetting!.periodOffTillDate < Date() {
  51. _pushSetting!.type = .on
  52. }
  53. } else {
  54. _pushSetting!.type = .on
  55. }
  56. }
  57. }
  58. }
  59. override func viewWillAppear(_ animated: Bool) {
  60. super.viewWillAppear(animated)
  61. self.title = NSLocalizedString("pushSetting_title", comment: "")
  62. self.setup()
  63. tableView.reloadData()
  64. }
  65. override func viewWillDisappear(_ animated: Bool) {
  66. super.viewWillDisappear(animated)
  67. if self.isMovingFromParent {
  68. save()
  69. }
  70. }
  71. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  72. if segue.identifier == "ShowDoNotDisturb" {
  73. let controller = segue.destination as! DoNotDisturbViewController
  74. controller.delegate = self
  75. controller.pushSetting = _pushSetting
  76. }
  77. }
  78. private func setup() {
  79. soundSwitch.setOn(!_pushSetting!.silent, animated: false)
  80. mentionsSwitch.setOn(_pushSetting!.mentions, animated: false)
  81. doNotDisturbCell.titleLabel?.text = BundleUtil.localizedString(forKey: "doNotDisturb_title")
  82. if _pushSetting!.type == .on {
  83. doNotDisturbCell.detailLabel?.text = BundleUtil.localizedString(forKey: "doNotDisturb_off")
  84. }
  85. else if _pushSetting!.type == .off {
  86. doNotDisturbCell.detailLabel?.text = BundleUtil.localizedString(forKey: "doNotDisturb_on")
  87. }
  88. else if _pushSetting!.type == .offPeriod {
  89. if _pushSetting!.periodOffTillDate != nil {
  90. doNotDisturbCell.detailLabel?.text = String.init(format: "%@ %@", BundleUtil.localizedString(forKey: "doNotDisturb_onPeriod_time"), DateFormatter.getFullDate(for: _pushSetting!.periodOffTillDate))
  91. } else {
  92. doNotDisturbCell.detailLabel?.text = NSLocalizedString("doNotDisturb_onPeriod", comment: "")
  93. }
  94. }
  95. mentionsCell.textLabel?.text = BundleUtil.localizedString(forKey: "doNotDisturb_mention")
  96. soundCell.textLabel?.text = BundleUtil.localizedString(forKey: "notification_sound_title")
  97. masterDndInfoImageView.image = BundleUtil.imageNamed("Info")?.withTint(Colors.fontNormal())
  98. masterDndInfoLabel.text = BundleUtil.localizedString(forKey: "settings_notifications_masterDnd_info")
  99. if UserSettings.shared().enableMasterDnd {
  100. self.tableView.tableHeaderView = masterDndView
  101. } else {
  102. self.tableView.tableHeaderView = nil
  103. }
  104. setupSoundCell()
  105. }
  106. private func setupSoundCell() {
  107. if (_pushSetting!.type == .off && !_pushSetting!.mentions && isGroup) ||
  108. _pushSetting!.type == .off && !isGroup {
  109. soundSwitch.isEnabled = false
  110. soundCell.textLabel?.isEnabled = false
  111. } else {
  112. soundSwitch.isEnabled = true
  113. soundCell.textLabel?.isEnabled = true
  114. }
  115. }
  116. private func save() {
  117. let setting = PushSetting.findDict(forIdentity: identity)
  118. if setting == nil {
  119. _pushSettingList!.add(_pushSetting!.buildDict()!)
  120. UserSettings.shared().pushSettingsList = _pushSettingList
  121. } else {
  122. _pushSettingList!.remove(setting as Any)
  123. _pushSettingList!.add(_pushSetting!.buildDict()!)
  124. UserSettings.shared().pushSettingsList = _pushSettingList
  125. }
  126. if conversation != nil {
  127. WCSessionManager.shared.updateConversationPushSetting(conversation: conversation!)
  128. }
  129. }
  130. // MARK: - Table view data source
  131. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  132. if section == 0 {
  133. if _pushSetting!.type == .on {
  134. return 1
  135. }
  136. else if _pushSetting!.type == .off {
  137. if isGroup {
  138. return 2
  139. } else {
  140. return 1
  141. }
  142. }
  143. else if _pushSetting!.type == .offPeriod {
  144. if isGroup {
  145. return 2
  146. } else {
  147. return 1
  148. }
  149. }
  150. }
  151. return super.tableView(tableView, numberOfRowsInSection: section)
  152. }
  153. override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  154. if section == 0 {
  155. return NSLocalizedString("doNotDisturb_header", comment: "")
  156. } else {
  157. return NSLocalizedString("notification_sound_header", comment: "")
  158. }
  159. }
  160. override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
  161. if isGroup {
  162. if section == 0 && _pushSetting!.mentions && ( _pushSetting!.type == .off || _pushSetting!.type == .offPeriod) {
  163. return NSLocalizedString("doNotDisturb_mention_footer_on", comment: "")
  164. }
  165. else if section == 0 && !_pushSetting!.mentions && ( _pushSetting!.type == .off || _pushSetting!.type == .offPeriod) {
  166. return NSLocalizedString("doNotDisturb_mention_footer_off", comment: "")
  167. }
  168. else {
  169. return nil
  170. }
  171. }
  172. return nil
  173. }
  174. // MARK: - Actions
  175. @IBAction func soundSwitchChanged(sender: UISwitch) {
  176. _pushSetting!.silent = !sender.isOn
  177. save()
  178. }
  179. @IBAction func mentionsSwitchChanged(sender: UISwitch) {
  180. _pushSetting!.mentions = sender.isOn
  181. tableView.reloadData()
  182. save()
  183. setupSoundCell()
  184. }
  185. // MARK: - Notification
  186. @objc func colorThemeChanged(notification:NSNotification) {
  187. setup()
  188. }
  189. }
  190. extension NotificationSettingViewController: DoNotDisturbDelegate {
  191. func pushSettingChanged(pushSetting: PushSetting) {
  192. _pushSetting = pushSetting
  193. save()
  194. setupSoundCell()
  195. }
  196. }