This repository has been archived by the owner on Oct 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
AppDependencies.swift
129 lines (116 loc) · 6.69 KB
/
AppDependencies.swift
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//
// AppDependencies.swift
// Baby Monitor
//
import Foundation
import RealmSwift
import RxSwift
import PocketSocket
import AudioKit
import FirebaseMessaging
final class AppDependencies {
private let bag = DisposeBag()
/// Service for cleaning too many crying events
private(set) lazy var memoryCleaner: MemoryCleanerProtocol = MemoryCleaner()
/// Service for capturing/recording microphone audio
private(set) lazy var audioMicrophoneService: AudioMicrophoneServiceProtocol? = try? AudioMicrophoneService(microphoneFactory: AudioKitMicrophoneFactory.makeMicrophoneFactory)
/// Service for detecting baby's cry
private(set) lazy var cryingDetectionService: CryingDetectionServiceProtocol = CryingDetectionService(microphoneCapture: audioMicrophoneService)
/// Service that takes care of appropriate controling: crying detection, audio recording and saving these events to realm database
private(set) lazy var cryingEventService: CryingEventsServiceProtocol = CryingEventService(
cryingDetectionService: cryingDetectionService,
microphoneRecord: audioMicrophoneService,
activityLogEventsRepository: databaseRepository,
storageService: storageServerService)
private(set) lazy var netServiceClient: NetServiceClientProtocol = NetServiceClient()
private(set) lazy var netServiceServer: NetServiceServerProtocol = NetServiceServer(appStateProvider: NotificationCenter.default)
private(set) lazy var peerConnectionFactory: PeerConnectionFactoryProtocol = RTCPeerConnectionFactory()
private(set) lazy var webRtcServer: () -> WebRtcServerManagerProtocol = {
WebRtcServerManager(peerConnectionFactory: self.peerConnectionFactory)
}
private(set) lazy var webRtcClient: () -> WebRtcClientManagerProtocol = {
WebRtcClientManager(
peerConnectionFactory: self.peerConnectionFactory,
connectionChecker: self.connectionChecker,
appStateProvider: NotificationCenter.default
)
}
private lazy var eventMessageConductorFactory: (Observable<String>, AnyObserver<EventMessage>) -> WebSocketConductor<EventMessage> = { emitter, handler in
WebSocketConductor(
webSocket: self.webSocket,
messageEmitter: emitter,
messageHandler: handler,
messageDecoders: [self.babyMonitorEventMessagesDecoder]
)
}
private lazy var webRtcConductorFactory: (Observable<String>, AnyObserver<WebRtcMessage>) -> WebSocketConductor<WebRtcMessage> = { emitter, handler in
WebSocketConductor(
webSocket: self.webSocket,
messageEmitter: emitter,
messageHandler: handler,
messageDecoders: self.webRtcMessageDecoders
)
}
lazy var webSocketEventMessageService = ClearableLazyItem<WebSocketEventMessageServiceProtocol> { [unowned self] in
return WebSocketEventMessageService(
cryingEventsRepository: self.databaseRepository,
eventMessageConductorFactory: self.eventMessageConductorFactory)
}
lazy var webSocketWebRtcService = ClearableLazyItem<WebSocketWebRtcServiceProtocol> { [unowned self] in
return WebSocketWebRtcService(
webRtcClientManager: self.webRtcClient(),
webRtcConductorFactory: self.webRtcConductorFactory)
}
private func makeWebSocketEventMessageService() -> WebSocketEventMessageServiceProtocol {
return WebSocketEventMessageService(cryingEventsRepository: databaseRepository, eventMessageConductorFactory: eventMessageConductorFactory)
}
private(set) lazy var networkDispatcher: NetworkDispatcherProtocol = NetworkDispatcher(
urlSession: URLSession(configuration: .default),
dispatchQueue: DispatchQueue(label: "NetworkDispatcherQueue")
)
private(set) lazy var localNotificationService: NotificationServiceProtocol = NotificationService(
networkDispatcher: networkDispatcher,
cacheService: cacheService,
serverKeyObtainable: serverKeyObtainable)
private let serverKeyObtainable: ServerKeyObtainableProtocol = ServerKeyObtainable()
private(set) var webRtcMessageDecoders: [AnyMessageDecoder<WebRtcMessage>] = [AnyMessageDecoder<WebRtcMessage>(SdpOfferDecoder()), AnyMessageDecoder<WebRtcMessage>(SdpAnswerDecoder()), AnyMessageDecoder<WebRtcMessage>(IceCandidateDecoder())]
private(set) var babyMonitorEventMessagesDecoder = AnyMessageDecoder<EventMessage>(EventMessageDecoder())
private(set) var cacheService: CacheServiceProtocol = CacheService()
private(set) lazy var connectionChecker: ConnectionChecker = NetServiceConnectionChecker(netServiceClient: netServiceClient, urlConfiguration: urlConfiguration)
private(set) lazy var serverService: ServerServiceProtocol = ServerService(
webRtcServerManager: webRtcServer(),
messageServer: messageServer,
netServiceServer: netServiceServer,
webRtcDecoders: webRtcMessageDecoders,
cryingService: cryingEventService,
babyModelController: databaseRepository,
cacheService: cacheService,
notificationsService: localNotificationService,
babyMonitorEventMessagesDecoder: babyMonitorEventMessagesDecoder
)
private(set) lazy var storageServerService = FirebaseStorageService(memoryCleaner: memoryCleaner)
private(set) var urlConfiguration: URLConfiguration = UserDefaultsURLConfiguration()
private(set) lazy var messageServer = MessageServer(server: webSocketServer)
private(set) lazy var webSocketServer: WebSocketServerProtocol = {
let webSocketServer = PSWebSocketServer(host: nil, port: UInt(Constants.iosWebsocketPort))!
return PSWebSocketServerWrapper(server: webSocketServer)
}()
private lazy var webSocket = ClearableLazyItem<WebSocketProtocol?> { [unowned self] in
guard let url = self.urlConfiguration.url else { return nil }
guard let rawSocket = PSWebSocket.clientSocket(with: URLRequest(url: url)) else { return nil }
let webSocket = PSWebSocketWrapper(socket: rawSocket)
webSocket.disconnectionObservable
.subscribe(onNext: { [unowned self] in self.resetForNoneState() })
.disposed(by: self.bag)
return webSocket
}
func resetForNoneState() {
webSocketWebRtcService.clear()
webSocketEventMessageService.clear()
webSocket.clear()
}
/// Baby service for getting and adding babies throughout the app
private(set) lazy var databaseRepository: BabyModelControllerProtocol & ActivityLogEventsRepositoryProtocol = RealmBabiesRepository(realm: try! Realm())
/// Service for handling errors and showing error alerts
private(set) var errorHandler: ErrorHandlerProtocol = ErrorHandler()
}