NotificationSound.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. self.view.backgroundColor = .white
  17. view.addSubview(tableView)
  18. tableView.translatesAutoresizingMaskIntoConstraints = false
  19. tableView.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor).isActive = true
  20. tableView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
  21. tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
  22. tableView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
  23. tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellNotifSound")
  24. tableView.delegate = self
  25. tableView.dataSource = self
  26. self.title = "Notification Message(s)".localized()
  27. if !isPersonal {
  28. self.title = "Notification Message(s) Group".localized()
  29. }
  30. navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel".localized(), style: .plain, target: self, action: #selector(cancel(sender:)))
  31. navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Save".localized(), style: .plain, target: self, action: #selector(save(sender:)))
  32. data.append(NotifSound(id: 1007, name: "sms-received1 (Default)", isSelected: false))
  33. data.append(NotifSound(id: 1008, name: "sms-received2", isSelected: false))
  34. data.append(NotifSound(id: 1009, name: "sms-received3", isSelected: false))
  35. data.append(NotifSound(id: 1010, name: "sms-received4", isSelected: false))
  36. data.append(NotifSound(id: 1013, name: "sms-received5", isSelected: false))
  37. data.append(NotifSound(id: 1014, name: "sms-received6", isSelected: false))
  38. var selectedSound = UserDefaults.standard.string(forKey: "notifSoundPersonal") ?? ""
  39. if !isPersonal {
  40. selectedSound = UserDefaults.standard.string(forKey: "notifSoundGroup") ?? ""
  41. }
  42. if !selectedSound.isEmpty {
  43. let selectedSoundId = Int(selectedSound.components(separatedBy: ":")[0])
  44. let idx = data.firstIndex(where: {$0.id == selectedSoundId})
  45. if idx != nil {
  46. data[idx!].isSelected = true
  47. isSelectedSound = selectedSoundId!
  48. } else {
  49. data[0].isSelected = true
  50. isSelectedSound = 1007
  51. }
  52. } else {
  53. data[0].isSelected = true
  54. isSelectedSound = 1007
  55. }
  56. }
  57. @objc func cancel(sender: Any) {
  58. navigationController?.dismiss(animated: true, completion: nil)
  59. }
  60. @objc func save(sender: Any) {
  61. let idx = data.firstIndex(where: {$0.id == isSelectedSound})
  62. if isPersonal {
  63. UserDefaults.standard.set("\(data[idx!].id):\(data[idx!].name)", forKey: "notifSoundPersonal")
  64. } else {
  65. UserDefaults.standard.set("\(data[idx!].id):\(data[idx!].name)", forKey: "notifSoundGroup")
  66. }
  67. navigationController?.dismiss(animated: true, completion: nil)
  68. }
  69. // MARK: - Table view data source
  70. public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  71. let idx = data.firstIndex(where: {$0.id == isSelectedSound})
  72. data[idx!].isSelected = false
  73. let idxNew = data.firstIndex(where: {$0.id == data[indexPath.row].id})
  74. data[idxNew!].isSelected = true
  75. isSelectedSound = data[indexPath.row].id
  76. let systemSoundID: SystemSoundID = SystemSoundID(isSelectedSound)
  77. AudioServicesPlaySystemSound(systemSoundID)
  78. tableView.reloadData()
  79. }
  80. public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  81. return data.count
  82. }
  83. public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  84. let cell = tableView.dequeueReusableCell(withIdentifier: "cellNotifSound", for: indexPath)
  85. var content = cell.defaultContentConfiguration()
  86. content.text = data[indexPath.row].name
  87. cell.contentConfiguration = content
  88. cell.accessoryType = data[indexPath.row].isSelected ? .checkmark : .none
  89. cell.selectionStyle = .none
  90. return cell
  91. }
  92. }