123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- //
- // NotificationSound.swift
- // NexilisLite
- //
- // Created by Akhmad Al Qindi Irsyam on 28/11/22.
- //
- import UIKit
- import AVFoundation
- public class NotificationSound: UIViewController, UITableViewDelegate, UITableViewDataSource {
-
- public var isPersonal = true
- let tableView = UITableView()
- var data: [NotifSound] = []
- var isSelectedSound = 0
- public override func viewDidLoad() {
- super.viewDidLoad()
- self.view.backgroundColor = .white
-
- view.addSubview(tableView)
- tableView.translatesAutoresizingMaskIntoConstraints = false
- tableView.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor).isActive = true
- tableView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
- tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
- tableView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
-
- tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellNotifSound")
-
- tableView.delegate = self
- tableView.dataSource = self
- self.title = "Notification Message(s)".localized()
- if !isPersonal {
- self.title = "Notification Message(s) Group".localized()
- }
- navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel".localized(), style: .plain, target: self, action: #selector(cancel(sender:)))
-
- navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Save".localized(), style: .plain, target: self, action: #selector(save(sender:)))
- data.append(NotifSound(id: 1007, name: "sms-received1 (Default)", isSelected: false))
- data.append(NotifSound(id: 1008, name: "sms-received2", isSelected: false))
- data.append(NotifSound(id: 1009, name: "sms-received3", isSelected: false))
- data.append(NotifSound(id: 1010, name: "sms-received4", isSelected: false))
- data.append(NotifSound(id: 1013, name: "sms-received5", isSelected: false))
- data.append(NotifSound(id: 1014, name: "sms-received6", isSelected: false))
- var selectedSound = UserDefaults.standard.string(forKey: "notifSoundPersonal") ?? ""
- if !isPersonal {
- selectedSound = UserDefaults.standard.string(forKey: "notifSoundGroup") ?? ""
- }
- if !selectedSound.isEmpty {
- let selectedSoundId = Int(selectedSound.components(separatedBy: ":")[0])
- let idx = data.firstIndex(where: {$0.id == selectedSoundId})
- if idx != nil {
- data[idx!].isSelected = true
- isSelectedSound = selectedSoundId!
- } else {
- data[0].isSelected = true
- isSelectedSound = 1007
- }
- } else {
- data[0].isSelected = true
- isSelectedSound = 1007
- }
- }
-
- @objc func cancel(sender: Any) {
- navigationController?.dismiss(animated: true, completion: nil)
- }
-
- @objc func save(sender: Any) {
- let idx = data.firstIndex(where: {$0.id == isSelectedSound})
- if isPersonal {
- UserDefaults.standard.set("\(data[idx!].id):\(data[idx!].name)", forKey: "notifSoundPersonal")
- } else {
- UserDefaults.standard.set("\(data[idx!].id):\(data[idx!].name)", forKey: "notifSoundGroup")
- }
- navigationController?.dismiss(animated: true, completion: nil)
- }
-
- // MARK: - Table view data source
-
- public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- let idx = data.firstIndex(where: {$0.id == isSelectedSound})
- data[idx!].isSelected = false
-
- let idxNew = data.firstIndex(where: {$0.id == data[indexPath.row].id})
- data[idxNew!].isSelected = true
- isSelectedSound = data[indexPath.row].id
-
- let systemSoundID: SystemSoundID = SystemSoundID(isSelectedSound)
- AudioServicesPlaySystemSound(systemSoundID)
- tableView.reloadData()
- }
- public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return data.count
- }
- public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withIdentifier: "cellNotifSound", for: indexPath)
- var content = cell.defaultContentConfiguration()
- content.text = data[indexPath.row].name
- cell.contentConfiguration = content
- cell.accessoryType = data[indexPath.row].isSelected ? .checkmark : .none
- cell.selectionStyle = .none
- return cell
- }
- }
|