DoNotDisturbViewController.swift 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. protocol DoNotDisturbDelegate {
  23. func pushSettingChanged(pushSetting: PushSetting)
  24. }
  25. class DoNotDisturbViewController: ThemedTableViewController {
  26. var delegate:DoNotDisturbDelegate?
  27. var pushSetting:PushSetting!
  28. override func viewDidLoad() {
  29. super.viewDidLoad()
  30. tableView.rowHeight = UITableView.automaticDimension
  31. tableView.estimatedRowHeight = UITableView.automaticDimension
  32. }
  33. override func viewWillAppear(_ animated: Bool) {
  34. super.viewWillAppear(animated)
  35. self.title = NSLocalizedString("doNotDisturb_title", comment: "")
  36. tableView.reloadData()
  37. }
  38. override func viewWillDisappear(_ animated: Bool) {
  39. super.viewWillDisappear(animated)
  40. if pushSetting.type == .on {
  41. pushSetting.periodOffTillDate = nil
  42. }
  43. else if pushSetting.type == .off {
  44. pushSetting.periodOffTillDate = nil
  45. }
  46. delegate?.pushSettingChanged(pushSetting: pushSetting)
  47. }
  48. // MARK: - Table view data source
  49. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  50. if pushSetting.type == .offPeriod {
  51. return 4
  52. }
  53. return 3
  54. }
  55. override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  56. if indexPath.row == 3 {
  57. return 162
  58. } else {
  59. return super.tableView(tableView, heightForRowAt: indexPath)
  60. }
  61. }
  62. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  63. if indexPath.row <= 2 {
  64. let cell = tableView.dequeueReusableCell(withIdentifier: "SelectionCell", for: indexPath)
  65. switch indexPath.row {
  66. case 0:
  67. cell.textLabel?.text = NSLocalizedString("doNotDisturb_off", comment: "")
  68. cell.accessoryType = pushSetting.type == .on ? .checkmark : .none
  69. break
  70. case 1:
  71. cell.textLabel?.text = NSLocalizedString("doNotDisturb_on", comment: "")
  72. cell.accessoryType = pushSetting.type == .off ? .checkmark : .none
  73. break
  74. case 2:
  75. cell.textLabel?.text = NSLocalizedString("doNotDisturb_onPeriod", comment: "")
  76. cell.accessoryType = pushSetting.type == .offPeriod ? .checkmark : .none
  77. break
  78. default: break
  79. }
  80. return cell
  81. } else {
  82. let cell = tableView.dequeueReusableCell(withIdentifier: "PickerCell", for: indexPath)
  83. return cell
  84. }
  85. }
  86. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  87. if indexPath.row == 0 {
  88. pushSetting.type = .on
  89. }
  90. else if indexPath.row == 1 {
  91. pushSetting.type = .off
  92. }
  93. if indexPath.row == 2 {
  94. pushSetting.type = .offPeriod
  95. self.pickerView(UIPickerView(), didSelectRow: 0, inComponent: 0)
  96. }
  97. tableView.reloadSections(IndexSet.init(integer: 0), with: .automatic)
  98. delegate?.pushSettingChanged(pushSetting: pushSetting)
  99. }
  100. }
  101. extension DoNotDisturbViewController: UIPickerViewDataSource {
  102. func numberOfComponents(in pickerView: UIPickerView) -> Int {
  103. return 1
  104. }
  105. func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
  106. return 7
  107. }
  108. }
  109. extension DoNotDisturbViewController: UIPickerViewDelegate {
  110. func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
  111. var componentText: String? = nil
  112. switch row {
  113. case PeriodOffTime.time1Hour.rawValue:
  114. componentText = NSLocalizedString("doNotDisturb_onPeriod_1Hour", comment: "")
  115. case PeriodOffTime.time2Hours.rawValue:
  116. componentText = NSLocalizedString("doNotDisturb_onPeriod_2Hours", comment: "")
  117. case PeriodOffTime.time3Hours.rawValue:
  118. componentText = NSLocalizedString("doNotDisturb_onPeriod_3Hours", comment: "")
  119. case PeriodOffTime.time4Hours.rawValue:
  120. componentText = NSLocalizedString("doNotDisturb_onPeriod_4Hours", comment: "")
  121. case PeriodOffTime.time8Hours.rawValue:
  122. componentText = NSLocalizedString("doNotDisturb_onPeriod_8Hours", comment: "")
  123. case PeriodOffTime.time1Day.rawValue:
  124. componentText = NSLocalizedString("doNotDisturb_onPeriod_1day", comment: "")
  125. case PeriodOffTime.time1Week.rawValue:
  126. componentText = NSLocalizedString("doNotDisturb_onPeriod_1week", comment: "")
  127. default:
  128. return nil
  129. }
  130. return NSAttributedString.init(string: componentText!, attributes: [.foregroundColor : Colors.fontNormal()!])
  131. }
  132. func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
  133. pushSetting.periodOffTime = PeriodOffTime(rawValue: row)!
  134. switch pushSetting.periodOffTime.rawValue {
  135. case PeriodOffTime.time1Hour.rawValue:
  136. pushSetting.periodOffTillDate = Calendar.current.date(byAdding: .hour, value: 1, to: Date.init())
  137. break
  138. case PeriodOffTime.time2Hours.rawValue:
  139. pushSetting.periodOffTillDate = Calendar.current.date(byAdding: .hour, value: 2, to: Date.init())
  140. break
  141. case PeriodOffTime.time3Hours.rawValue:
  142. pushSetting.periodOffTillDate = Calendar.current.date(byAdding: .hour, value: 3, to: Date.init())
  143. break
  144. case PeriodOffTime.time4Hours.rawValue:
  145. pushSetting.periodOffTillDate = Calendar.current.date(byAdding: .hour, value: 4, to: Date.init())
  146. break
  147. case PeriodOffTime.time8Hours.rawValue:
  148. pushSetting.periodOffTillDate = Calendar.current.date(byAdding: .hour, value: 8, to: Date.init())
  149. break
  150. case PeriodOffTime.time1Day.rawValue:
  151. pushSetting.periodOffTillDate = Calendar.current.date(byAdding: .day, value: 1, to: Date.init())
  152. break
  153. case PeriodOffTime.time1Week.rawValue:
  154. pushSetting.periodOffTillDate = Calendar.current.date(byAdding: .weekOfMonth, value: 1, to: Date.init())
  155. break
  156. default:
  157. break
  158. }
  159. delegate?.pushSettingChanged(pushSetting: pushSetting)
  160. }
  161. }