ServiceEvent.swift 3.4 KB
Newer Older
1
/*
2
 *  Copyright (C) 2016-2019 Savoir-faire Linux Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 *  Author: Romain Bertozzi <romain.bertozzi@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */

/**
 Events types that can be configured as identifier for the ServiceEvent.

 - AccountChanged: the accounts have been changed daemon-side
25
 - AccountAdded: an account has been added
26 27
 */
enum ServiceEventType {
28 29 30
    case accountAdded
    case accountsChanged
    case registrationStateChanged
31
    case presenceUpdated
32
    case messageStateChanged
33
    case knownDevicesChanged
34
    case exportOnRingEnded
35 36
    case contactAdded
    case contactRequestReceived
37
    case contactRequestDiscarded
38
    case proxyEnabled
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
39
    case notificationEnabled
40
    case callEnded
41 42 43
    case dataTransferCreated
    case dataTransferChanged
    case dataTransferMessageUpdated
44
    case deviceRevocationEnded
45
    case newIncomingMessage
46
    case nameRegistrationEnded
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
47 48
    case callProviderAnswerCall
    case callProviderCancellCall
49
    case audioActivated
50 51 52 53 54 55
}

/**
 Keys that can be set as keys of the private input dictionary
 */
enum ServiceEventInput {
56 57 58
    case id
    case state
    case registrationState
59 60
    case uri
    case presenceStatus
61 62
    case messageStatus
    case messageId
63
    case pin
64 65
    case accountId
    case date
66 67
    case callType
    case callTime
68
    case transferId
69
    case localPhotolID
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
70
    case proxyAddress
71
    case deviceId
72 73 74 75
    case content
    case peerUri
    case accountUri
    case name
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
76
    case callUUID
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
}

/**
 A struct representing an output of the services.
 Is meant to be used as stream events for example.
 Its responsabilities:
 - contain the information returned from the service
 */
struct ServiceEvent {
    // MARK: - Public members
    /**
     Identifies the event type.
     */
    fileprivate(set) internal var eventType: ServiceEventType

    // MARK: - Private members
    /**
     Contains all the metadata of the event.
     */
96
    fileprivate var inputs = [ServiceEventInput: Any]()
97 98 99 100 101 102 103 104 105 106 107 108

    /**
     Initializer
     */
    init(withEventType eventType: ServiceEventType) {
        self.eventType = eventType
    }

    // MARK: Core
    /**
     Allows to add an entry in the metadata of the event.
     */
109
    mutating func addEventInput<T>(_ input: ServiceEventInput, value: T) {
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
        inputs.updateValue(value, forKey: input)
    }

    /**
     Allows to get an entry of the metadata of the event.
     - Parameter input: the key of the data to find
     - Parameter T: the expected class of the data to get
     - Returns: the data casted in the correct T class, nil otherwise
     */
    func getEventInput<T>(_ input: ServiceEventInput) -> T? {
        let object = inputs[input]
        if let result = object as? T {
            return result
        }
        return nil
    }
}