MasterDndDaysViewController.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 MasterDndDaysViewController: ThemedTableViewController {
  22. var days:[String] = Calendar.current.weekdaySymbols
  23. var selectedIndexPaths:NSMutableOrderedSet = NSMutableOrderedSet()
  24. override func viewDidLoad() {
  25. super.viewDidLoad()
  26. self.title = BundleUtil.localizedString(forKey: "settings_notifications_masterDnd_workingDays")
  27. }
  28. override func viewWillAppear(_ animated: Bool) {
  29. super.viewWillAppear(animated)
  30. selectedIndexPaths = NSMutableOrderedSet.init(orderedSet: UserSettings.shared().masterDndWorkingDays)
  31. tableView.reloadData()
  32. }
  33. override func viewWillDisappear(_ animated: Bool) {
  34. super.viewWillDisappear(animated)
  35. UserSettings.shared().masterDndWorkingDays = selectedIndexPaths
  36. }
  37. // MARK: - Table view data source
  38. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  39. return days.count
  40. }
  41. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  42. let cell = tableView.dequeueReusableCell(withIdentifier: "MasterDndDaysCell", for: indexPath)
  43. var row = indexPath.row + Calendar.current.firstWeekday
  44. if row > days.count {
  45. row = row - days.count
  46. }
  47. cell.textLabel?.text = days[row - 1]
  48. if selectedIndexPaths.contains(row) {
  49. cell.accessoryType = .checkmark
  50. } else {
  51. cell.accessoryType = .none
  52. }
  53. return cell
  54. }
  55. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  56. let cell = tableView.cellForRow(at: indexPath)
  57. var row = indexPath.row + Calendar.current.firstWeekday
  58. if row > days.count {
  59. row = row - days.count
  60. }
  61. if selectedIndexPaths.contains(row) {
  62. selectedIndexPaths.remove(row)
  63. cell?.accessoryType = .none
  64. } else {
  65. selectedIndexPaths.add(row)
  66. cell?.accessoryType = .checkmark
  67. }
  68. tableView.deselectRow(at: indexPath, animated: true)
  69. }
  70. }