summaryrefslogtreecommitdiff
path: root/Example Apps/Example Swift/AlertManager.swift
blob: 71769c51deecd09d3257549470599334f2192e86 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//
//  AlertManager.swift
//  SmartDeviceLink-Example-Swift
//
//  Created by Nicole on 4/12/18.
//  Copyright © 2018 smartdevicelink. All rights reserved.
//

import Foundation
import SmartDeviceLink
import SmartDeviceLinkSwift

class AlertManager {
    ///  Sends an alert with up to two lines of text, an image, and a close button that will dismiss the alert when tapped.
    /// - Parameters:
    ///   - imageName: The name of the image to upload
    ///   - textField1: The first line of text in the alert
    ///   - textField2: The second line of text in the alert
    ///   - sdlManager: The SDLManager
    class func sendAlert(imageName: String? = nil, textField1: String, textField2: String? = nil, sdlManager: SDLManager) {
        let okSoftButton = SDLSoftButtonObject(name: AlertOKButtonText, text: AlertOKButtonText, artwork: nil, handler: nil)
        let alert = SDLAlertView(text: textField1, buttons: [okSoftButton])
        alert.secondaryText = textField2

        let alertAudioData = SDLAlertAudioData(speechSynthesizerString: "alert")
        alertAudioData.playTone = true
        alert.audio = alertAudioData

        if let imageName = imageName {
            alert.icon = SDLArtwork(image: UIImage(named: imageName)!.withRenderingMode(.alwaysTemplate), persistent: false, as: .PNG)
        }

        sdlManager.screenManager.presentAlert(alert) { error in
            if let error = error {
                SDLLog.e("There was an error presenting the alert: \(error.localizedDescription)")
            }
        }
    }

    ///  Sends a subtle alert with up to two lines of text, and an image.
    /// - Parameters:
    ///   - imageName: The name of the image to upload
    ///   - textField1: The first line of text in the alert
    ///   - textField2: The second line of text in the alert
    ///   - sdlManager: The SDLManager
    class func sendSubtleAlert(imageName: String? = nil, textField1: String, textField2: String? = nil, sdlManager: SDLManager) {
        let subtleAlert = SDLSubtleAlert(alertText1: textField1, alertText2: textField2, alertIcon: nil, ttsChunks: nil, duration: nil, softButtons: nil, cancelID: NSNumber(0))

        if let imageName = imageName {
            sendImage(imageName, sdlManager: sdlManager) { (success, artworkName) in
                if success {
                    subtleAlert.alertIcon = SDLImage(name: artworkName, isTemplate: true)
                }
                sdlManager.send(subtleAlert)
            }
        } else {
            sdlManager.send(subtleAlert)
        }
    }

    /// Helper method for uploading an image before it is shown in an alert
    /// - Parameters:
    ///   - imageName: The name of the image to upload
    ///   - sdlManager: The SDLManager
    ///   - completionHandler: Handler called when the artwork has finished uploading with the success of the upload and the name of the uploaded image.
    private class func sendImage(_ imageName: String, sdlManager: SDLManager, completionHandler: @escaping ((_ success: Bool, _ artwork: String) -> ())) {
        let artwork = SDLArtwork(image: UIImage(named: imageName)!.withRenderingMode(.alwaysTemplate), persistent: false, as: .PNG)
        sdlManager.fileManager.upload(artwork: artwork) { (success, artworkName, bytesAvailable, error) in
            return completionHandler(success, artworkName)
        }
    }
}