-
Notifications
You must be signed in to change notification settings - Fork 25
/
ServiceStatusManager.swift
78 lines (65 loc) · 2.54 KB
/
ServiceStatusManager.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
//
// AppStatusManager.swift
// safesafe Live
//
// Created by Rafał Małczyński on 22/04/2020.
// Copyright © 2020 Lukasz szyszkowski. All rights reserved.
//
import Foundation
import PromiseKit
protocol ServiceStatusManagerProtocol {
func currentServiceStatus(delay: TimeInterval) -> Promise<ServicesResponse>
func serviceStatusJson(delay: TimeInterval) -> Promise<String>
}
final class ServiceStatusManager: ServiceStatusManagerProtocol {
// MARK: - Properties
private let notificationManager: NotificationManagerProtocol
private let exposureNotificationStatus: ExposureNotificationStatusProtocol
func currentServiceStatus(delay: TimeInterval) -> Promise<ServicesResponse> {
Promise { seal in
firstly {
when(fulfilled:
Permissions.instance.state(for: .notifications),
exposureNotificationStatus.status,
exposureNotificationStatus.isBluetoothOn(delay: delay)
)
}.done { notificationStatus, exposureStatus, bluetoothStatus in
let status = ServicesResponse.Status(
exposureNotificationStatus: exposureStatus,
isBluetoothOn: bluetoothStatus,
isNotificationEnabled: notificationStatus == .authorized)
seal.fulfill(ServicesResponse(status: status))
}.catch { error in
console(error, type: .error)
seal.reject(error)
}
}
}
func serviceStatusJson(delay: TimeInterval) -> Promise<String> {
Promise<String> { [weak self] seal in
guard let self = self else {
seal.reject(InternalError.deinitialized)
return
}
self.currentServiceStatus(delay: delay)
.done { status in
guard let json = status.jsonString else {
seal.reject(InternalError.serializationFailed)
return
}
console(json)
seal.fulfill(json)
}.catch { error in
seal.reject(error)
}
}
}
// MARK: - Life Cycle
init(
notificationManager: NotificationManagerProtocol,
exposureNotificationStatus: ExposureNotificationStatusProtocol
) {
self.notificationManager = notificationManager
self.exposureNotificationStatus = exposureNotificationStatus
}
}