Commit 2202e6d1 authored by Romain Bertozzi's avatar Romain Bertozzi

services: create serviceEvent structure

This patch creates the serviceEvent structure.

This structure will be sent in the services' response streams. They
will contain all the needed data gathered from the deamon by the
services.

Tuleap: #1377
Change-Id: Ia1e76be8550b9155c80a22f78df71f8e60898f10
parent 72a1b8d8
......@@ -27,6 +27,7 @@
02B22E011DF755E5000358C9 /* MainTabBarViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02B22E001DF755E5000358C9 /* MainTabBarViewController.swift */; };
02B22E031DF755F7000358C9 /* WalkthroughStoryboard.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 02B22E021DF755F7000358C9 /* WalkthroughStoryboard.storyboard */; };
02B22E091DF7585F000358C9 /* DaemonService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02B22E081DF7585F000358C9 /* DaemonService.swift */; };
02C9B63F1E1D4E8C00F82F0C /* ServiceEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02C9B63E1E1D4E8C00F82F0C /* ServiceEvent.swift */; };
02E1A0251DDE4ABA00D75B59 /* BoolStringExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 043866371D2304A700E06CE2 /* BoolStringExtension.swift */; };
043866211D218B1100E06CE2 /* AudioToolbox.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 043866201D218B1100E06CE2 /* AudioToolbox.framework */; };
043866331D22CE8C00E06CE2 /* MeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 043866321D22CE8C00E06CE2 /* MeViewController.swift */; };
......@@ -139,6 +140,7 @@
02B22E001DF755E5000358C9 /* MainTabBarViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MainTabBarViewController.swift; path = MainTabBar/MainTabBarViewController.swift; sourceTree = "<group>"; };
02B22E021DF755F7000358C9 /* WalkthroughStoryboard.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = WalkthroughStoryboard.storyboard; path = Walkthrough/WalkthroughStoryboard.storyboard; sourceTree = "<group>"; };
02B22E081DF7585F000358C9 /* DaemonService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = DaemonService.swift; path = Services/DaemonService.swift; sourceTree = "<group>"; };
02C9B63E1E1D4E8C00F82F0C /* ServiceEvent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ServiceEvent.swift; path = Services/ServiceEvent.swift; sourceTree = "<group>"; };
043866201D218B1100E06CE2 /* AudioToolbox.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AudioToolbox.framework; path = System/Library/Frameworks/AudioToolbox.framework; sourceTree = SDKROOT; };
043866321D22CE8C00E06CE2 /* MeViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MeViewController.swift; sourceTree = "<group>"; };
043866351D22D06500E06CE2 /* AccountTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AccountTableViewCell.swift; sourceTree = "<group>"; };
......@@ -357,6 +359,7 @@
02B22E081DF7585F000358C9 /* DaemonService.swift */,
02B22DFE1DF755DB000358C9 /* AccountsService.swift */,
0273C2FE1E0C438F00CF00BA /* AccountAdapterDelegate.swift */,
02C9B63E1E1D4E8C00F82F0C /* ServiceEvent.swift */,
);
name = Services;
sourceTree = "<group>";
......@@ -714,6 +717,7 @@
04399AAC1D1C304300E99CD9 /* AccountAdapter.mm in Sources */,
02B22E091DF7585F000358C9 /* DaemonService.swift in Sources */,
0273C3061E0C68B100CF00BA /* CreateRingAccountViewController.swift in Sources */,
02C9B63F1E1D4E8C00F82F0C /* ServiceEvent.swift in Sources */,
02B22E011DF755E5000358C9 /* MainTabBarViewController.swift in Sources */,
043999F71D1C2D9D00E99CD9 /* AppDelegate.swift in Sources */,
02B22DFC1DF755BB000358C9 /* AccountModel.swift in Sources */,
......
......@@ -65,7 +65,7 @@ class CreateRingAccountViewModel {
self?.addAccountDisposable = AccountsService.sharedInstance
.sharedResponseStream
.subscribe(onNext:{ (event) in
if event == AccountRxEvent.AccountChanged {
if event.eventType == ServiceEventType.AccountsChanged {
onSuccessCallback?()
}
}, onError: { error in
......
......@@ -22,15 +22,6 @@
import RxCocoa
import RxSwift
/**
Events that can be sent to the response stream
- AccountChanged: the accounts have been changed daemon-side
*/
enum AccountRxEvent {
case AccountChanged
}
class AccountsService: AccountAdapterDelegate {
// MARK: Private members
/**
......@@ -54,10 +45,10 @@ class AccountsService: AccountAdapterDelegate {
This stream is used strictly inside this service.
External observers should use the public shared responseStream.
- SeeAlso: `AccountRxEvent`
- SeeAlso: `ServiceEvent`
- SeeAlso: `sharedResponseStream`
*/
fileprivate let responseStream = PublishSubject<AccountRxEvent>()
fileprivate let responseStream = PublishSubject<ServiceEvent>()
// MARK: - Public members
/**
......@@ -79,9 +70,9 @@ class AccountsService: AccountAdapterDelegate {
External observers must subscribe to this stream to get results.
- SeeAlso: `responseStream`
- SeeAlso: `AccountRxEvent`
- SeeAlso: `ServiceEvent`
*/
var sharedResponseStream: Observable<AccountRxEvent>
var sharedResponseStream: Observable<ServiceEvent>
// MARK: - Singleton
static let sharedInstance = AccountsService()
......@@ -135,6 +126,9 @@ class AccountsService: AccountAdapterDelegate {
// MARK: - AccountAdapterDelegate
func accountsChanged() {
print("Accounts changed.")
self.responseStream.onNext(.AccountChanged)
reload()
let event = ServiceEvent.init(withEventType: .AccountsChanged)
self.responseStream.onNext(event)
}
}
/*
* Copyright (C) 2016 Savoir-faire Linux Inc.
*
* 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
*/
enum ServiceEventType {
case AccountsChanged
}
/**
Keys that can be set as keys of the private input dictionary
*/
enum ServiceEventInput {
case Id
}
/**
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.
*/
fileprivate var inputs = Dictionary<ServiceEventInput, Any>()
/**
Initializer
*/
init(withEventType eventType: ServiceEventType) {
self.eventType = eventType
}
// MARK: Core
/**
Allows to add an entry in the metadata of the event.
*/
mutating func addEventInput(_ input: ServiceEventInput, value: Any) {
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
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment