Release 2.0.0

This commit is contained in:
Frank Rotermund
2022-11-27 13:18:53 +00:00
parent bf57719009
commit 0dc8ec0982
570 changed files with 20366 additions and 4410 deletions
@@ -18,7 +18,6 @@ import Combine
import MatrixSDK
class UserSessionOverviewService: UserSessionOverviewServiceProtocol {
// MARK: - Members
private(set) var pusherEnabledSubject: CurrentValueSubject<Bool?, Never>
@@ -29,54 +28,97 @@ class UserSessionOverviewService: UserSessionOverviewServiceProtocol {
private let session: MXSession
private let sessionInfo: UserSessionInfo
private var pusher: MXPusher?
private var localNotificationSettings: [String: Any]?
// MARK: - Setup
init(session: MXSession, sessionInfo: UserSessionInfo) {
self.session = session
self.sessionInfo = sessionInfo
self.pusherEnabledSubject = CurrentValueSubject(nil)
self.remotelyTogglingPushersAvailableSubject = CurrentValueSubject(false)
checkServerVersions { [weak self] in
self?.checkPusher()
pusherEnabledSubject = CurrentValueSubject(nil)
remotelyTogglingPushersAvailableSubject = CurrentValueSubject(false)
localNotificationSettings = session.accountData.localNotificationSettingsForDevice(withId: sessionInfo.id)
if let localNotificationSettings = localNotificationSettings, let isSilenced = localNotificationSettings[kMXAccountDataIsSilencedKey] as? Bool {
remotelyTogglingPushersAvailableSubject.send(true)
pusherEnabledSubject.send(!isSilenced)
}
checkPusher { [weak self] in
guard self?.pusher != nil else {
return
}
self?.checkServerVersions()
}
}
// MARK: - UserSessionOverviewServiceProtocol
func togglePushNotifications() {
guard let pusher = pusher, let enabled = pusher.enabled?.boolValue, self.remotelyTogglingPushersAvailableSubject.value else {
guard let pusher = pusher, let enabled = pusher.enabled?.boolValue else {
updateLocalNotification()
return
}
let data = pusher.data.jsonDictionary() as? [String: Any] ?? [:]
self.session.matrixRestClient.setPusher(pushKey: pusher.pushkey,
kind: MXPusherKind(value: pusher.kind),
appId: pusher.appId,
appDisplayName:pusher.appDisplayName,
deviceDisplayName: pusher.deviceDisplayName,
profileTag: pusher.profileTag ?? "",
lang: pusher.lang,
data: data,
append: false,
enabled: !enabled) { [weak self] response in
guard let self = self else { return }
switch response {
case .success:
self.checkPusher()
case .failure(let error):
MXLog.warning("[UserSessionOverviewService] togglePushNotifications failed due to error: \(error)")
self.pusherEnabledSubject.send(enabled)
}
}
toggle(pusher, enabled: !enabled)
}
// MARK: - Private
private func checkServerVersions(_ completion: @escaping () -> Void) {
private func toggle(_ pusher: MXPusher, enabled: Bool) {
guard remotelyTogglingPushersAvailableSubject.value else {
MXLog.warning("[UserSessionOverviewService] toggle pusher canceled: remotely toggling pushers not available")
return
}
MXLog.debug("[UserSessionOverviewService] remotely toggling pusher")
let data = pusher.data.jsonDictionary() as? [String: Any] ?? [:]
session.matrixRestClient.setPusher(pushKey: pusher.pushkey,
kind: MXPusherKind(value: pusher.kind),
appId: pusher.appId,
appDisplayName: pusher.appDisplayName,
deviceDisplayName: pusher.deviceDisplayName,
profileTag: pusher.profileTag ?? "",
lang: pusher.lang,
data: data,
append: false,
enabled: enabled) { [weak self] response in
guard let self = self else { return }
switch response {
case .success:
if let account = MXKAccountManager.shared().activeAccounts.first, account.device?.deviceId == pusher.deviceId {
account.loadCurrentPusher(nil)
}
self.checkPusher()
case .failure(let error):
MXLog.warning("[UserSessionOverviewService] togglePusher failed due to error: \(error)")
self.pusherEnabledSubject.send(!enabled)
}
}
}
private func updateLocalNotification() {
guard var localNotificationSettings = localNotificationSettings, let isSilenced = localNotificationSettings[kMXAccountDataIsSilencedKey] as? Bool else {
MXLog.warning("[UserSessionOverviewService] updateLocalNotification canceled: \"\(kMXAccountDataIsSilencedKey)\" notification property not found")
return
}
localNotificationSettings[kMXAccountDataIsSilencedKey] = !isSilenced
session.setAccountData(localNotificationSettings, forType: MXAccountData.localNotificationSettingsKeyForDevice(withId: sessionInfo.id)) { [weak self] in
self?.localNotificationSettings = localNotificationSettings
self?.pusherEnabledSubject.send(isSilenced)
} failure: { [weak self] error in
MXLog.warning("[UserSessionOverviewService] updateLocalNotification failed due to error: \(String(describing: error))")
self?.pusherEnabledSubject.send(!isSilenced)
}
}
private func checkServerVersions() {
session.supportedMatrixVersions { [weak self] response in
switch response {
case .success(let versions):
@@ -84,11 +126,10 @@ class UserSessionOverviewService: UserSessionOverviewServiceProtocol {
case .failure(let error):
MXLog.warning("[UserSessionOverviewService] checkServerVersions failed due to error: \(error)")
}
completion()
}
}
private func checkPusher() {
private func checkPusher(_ completion: (() -> Void)? = nil) {
session.matrixRestClient.pushers { [weak self] response in
switch response {
case .success(let pushers):
@@ -96,6 +137,7 @@ class UserSessionOverviewService: UserSessionOverviewServiceProtocol {
case .failure(let error):
MXLog.warning("[UserSessionOverviewService] checkPusher failed due to error: \(error)")
}
completion?()
}
}
@@ -18,18 +18,16 @@ import Combine
import Foundation
class MockUserSessionOverviewService: UserSessionOverviewServiceProtocol {
var pusherEnabledSubject: CurrentValueSubject<Bool?, Never>
var remotelyTogglingPushersAvailableSubject: CurrentValueSubject<Bool, Never>
init(pusherEnabled: Bool? = nil, remotelyTogglingPushersAvailable: Bool = true) {
self.pusherEnabledSubject = CurrentValueSubject(pusherEnabled)
self.remotelyTogglingPushersAvailableSubject = CurrentValueSubject(remotelyTogglingPushersAvailable)
pusherEnabledSubject = CurrentValueSubject(pusherEnabled)
remotelyTogglingPushersAvailableSubject = CurrentValueSubject(remotelyTogglingPushersAvailable)
}
func togglePushNotifications() {
guard let enabled = pusherEnabledSubject.value, self.remotelyTogglingPushersAvailableSubject.value else {
guard let enabled = pusherEnabledSubject.value, remotelyTogglingPushersAvailableSubject.value else {
return
}