summaryrefslogtreecommitdiff
path: root/TalerWallet1/Helper/Controller+playSound.swift
blob: bf6a78451d674bcdd72fb8b1534b678ffba70227 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
 * This file is part of GNU Taler, ©2022-23 Taler Systems S.A.
 * See LICENSE.md
 */
import Foundation
import AVFoundation
import UIKit

extension Controller {

    /// 0 = failure, 1 = received, 2 = sent
    @MainActor func playSound(_ number: Int) {
        var soundID: SystemSoundID = 0
        let notificationGenerator = useHaptics ? UINotificationFeedbackGenerator() : nil
        if number > 9 {
            soundID = UInt32(number)
        } else {
            let sound = (number == 0) ? "payment_failure" :
                        (number == 1) ? "payment_success" : "PaymentReceived"
            let fileURL = URL(fileURLWithPath: "/System/Library/Audio/UISounds/"
                              + sound + ".caf")
            AudioServicesCreateSystemSoundID(fileURL as CFURL, &soundID)
            logger.log("\(sound, privacy: .public) \(soundID)")
        }
        if number == 0 || number > 9 || playSounds < 0 {
            AudioServicesPlaySystemSound(soundID);
        } else if playSounds > 0 {
            if let url = Bundle.main.url(forResource: (number == 1) ? "payment_received"
                                                                    : "payment_sent",
                                         withExtension: "m4a") {
                player.removeAllItems()
                player.insert(AVPlayerItem(url: url), after: nil)
                logger.log("\(url, privacy: .public) \(soundID)")
                player.play()
            } else {
                AudioServicesPlaySystemSound(soundID);
            }
        }
        if let notificationGenerator {
            notificationGenerator.notificationOccurred(number == 0 ? .error : .success)
        }
    }
}