NotificationSound.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // NotificationSound.swift
  3. // NexilisLite
  4. //
  5. // Created by Akhmad Al Qindi Irsyam on 28/11/22.
  6. //
  7. import UIKit
  8. import AVFoundation
  9. public class NotificationSound: UIViewController, UITableViewDelegate, UITableViewDataSource {
  10. public var isPersonal = true
  11. let tableView = UITableView()
  12. var data: [NotifSound] = []
  13. var isSelectedSound = 0
  14. public override func viewDidLoad() {
  15. super.viewDidLoad()
  16. let attributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 16.0), NSAttributedString.Key.foregroundColor: UIColor.white]
  17. let navBarAppearance = UINavigationBarAppearance()
  18. navBarAppearance.configureWithOpaqueBackground()
  19. navBarAppearance.backgroundColor = self.traitCollection.userInterfaceStyle == .dark ? .blackDarkMode : UIColor.mainColor
  20. navBarAppearance.titleTextAttributes = attributes
  21. navigationController?.navigationBar.standardAppearance = navBarAppearance
  22. navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
  23. view.addSubview(tableView)
  24. tableView.translatesAutoresizingMaskIntoConstraints = false
  25. tableView.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor).isActive = true
  26. tableView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
  27. tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
  28. tableView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
  29. tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellNotifSound")
  30. tableView.delegate = self
  31. tableView.dataSource = self
  32. self.title = "Notification Message(s)".localized()
  33. if !isPersonal {
  34. self.title = "Notification Message(s) Group".localized()
  35. }
  36. navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel".localized(), style: .plain, target: self, action: #selector(cancel(sender:)))
  37. navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Save".localized(), style: .plain, target: self, action: #selector(save(sender:)))
  38. data.append(NotifSound(id: 1007, name: "sms-received1 (Default)", isSelected: false))
  39. data.append(NotifSound(id: 1008, name: "sms-received2", isSelected: false))
  40. data.append(NotifSound(id: 1009, name: "sms-received3", isSelected: false))
  41. data.append(NotifSound(id: 1010, name: "sms-received4", isSelected: false))
  42. data.append(NotifSound(id: 1013, name: "sms-received5", isSelected: false))
  43. data.append(NotifSound(id: 1014, name: "sms-received6", isSelected: false))
  44. var selectedSound: String = SecureUserDefaults.shared.value(forKey: "notifSoundPersonal") ?? ""
  45. if !isPersonal {
  46. selectedSound = SecureUserDefaults.shared.value(forKey: "notifSoundGroup") ?? ""
  47. }
  48. if !selectedSound.isEmpty {
  49. let selectedSoundId = Int(selectedSound.components(separatedBy: ":")[0])
  50. let idx = data.firstIndex(where: {$0.id == selectedSoundId})
  51. if idx != nil {
  52. data[idx!].isSelected = true
  53. isSelectedSound = selectedSoundId!
  54. } else {
  55. data[0].isSelected = true
  56. isSelectedSound = 1007
  57. }
  58. } else {
  59. data[0].isSelected = true
  60. isSelectedSound = 1007
  61. }
  62. }
  63. @objc func cancel(sender: Any) {
  64. navigationController?.dismiss(animated: true, completion: nil)
  65. }
  66. @objc func save(sender: Any) {
  67. let idx = data.firstIndex(where: {$0.id == isSelectedSound})
  68. if isPersonal {
  69. SecureUserDefaults.shared.set("\(data[idx!].id):\(data[idx!].name)", forKey: "notifSoundPersonal")
  70. } else {
  71. SecureUserDefaults.shared.set("\(data[idx!].id):\(data[idx!].name)", forKey: "notifSoundGroup")
  72. }
  73. navigationController?.dismiss(animated: true, completion: nil)
  74. }
  75. // MARK: - Table view data source
  76. public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  77. let idx = data.firstIndex(where: {$0.id == isSelectedSound})
  78. data[idx!].isSelected = false
  79. let idxNew = data.firstIndex(where: {$0.id == data[indexPath.row].id})
  80. data[idxNew!].isSelected = true
  81. isSelectedSound = data[indexPath.row].id
  82. let systemSoundID: SystemSoundID = SystemSoundID(isSelectedSound)
  83. AudioServicesPlaySystemSound(systemSoundID)
  84. tableView.reloadData()
  85. }
  86. public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  87. return data.count
  88. }
  89. public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  90. let cell = tableView.dequeueReusableCell(withIdentifier: "cellNotifSound", for: indexPath)
  91. var content = cell.defaultContentConfiguration()
  92. content.text = data[indexPath.row].name
  93. cell.contentConfiguration = content
  94. cell.accessoryType = data[indexPath.row].isSelected ? .checkmark : .none
  95. cell.selectionStyle = .none
  96. return cell
  97. }
  98. }