2019-11-20 09:25:56 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 New Vector Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
import MatrixSDK
|
|
|
|
|
|
|
|
@objc enum WidgetPermission: Int {
|
|
|
|
case undefined
|
|
|
|
case granted
|
|
|
|
case declined
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Shared user settings across all Riot clients.
|
|
|
|
/// It implements https://github.com/vector-im/riot-meta/blob/master/spec/settings.md
|
|
|
|
@objcMembers
|
|
|
|
class RiotSharedSettings: NSObject {
|
|
|
|
|
|
|
|
// MARK: - Constants
|
|
|
|
private enum Settings {
|
|
|
|
static let breadcrumbs = "im.vector.setting.breadcrumbs"
|
|
|
|
static let integrationProvisioning = "im.vector.setting.integration_provisioning"
|
|
|
|
static let allowedWidgets = "im.vector.setting.allowed_widgets"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// MARK: - Properties
|
|
|
|
// MARK: Private
|
|
|
|
private let session: MXSession
|
|
|
|
private lazy var serializationService: SerializationServiceType = SerializationService()
|
|
|
|
|
|
|
|
|
|
|
|
// MARK: - Setup
|
|
|
|
|
|
|
|
init(session: MXSession) {
|
|
|
|
self.session = session
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// MARK: - Public
|
|
|
|
|
2019-11-21 14:43:04 +00:00
|
|
|
// MARK: Integration provisioning
|
|
|
|
|
2019-11-21 16:19:39 +00:00
|
|
|
var hasIntegrationProvisioningEnabled: Bool {
|
2019-11-21 14:43:04 +00:00
|
|
|
return getIntegrationProvisioning()?.enabled ?? true
|
|
|
|
}
|
|
|
|
|
|
|
|
func getIntegrationProvisioning() -> RiotSettingIntegrationProvisioning? {
|
|
|
|
guard let integrationProvisioningDict = getAccountData(forEventType: Settings.integrationProvisioning) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-21 16:19:39 +00:00
|
|
|
return try? serializationService.deserialize(integrationProvisioningDict)
|
2019-11-21 14:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@discardableResult
|
|
|
|
func setIntegrationProvisioning(enabled: Bool,
|
|
|
|
success: @escaping () -> Void,
|
|
|
|
failure: @escaping (Error?) -> Void)
|
|
|
|
-> MXHTTPOperation? {
|
|
|
|
|
|
|
|
// Update only the "widgets" field in the account data
|
|
|
|
var integrationProvisioningDict = getAccountData(forEventType: Settings.integrationProvisioning) ?? [:]
|
|
|
|
integrationProvisioningDict[RiotSettingIntegrationProvisioning.CodingKeys.enabled.rawValue] = enabled
|
|
|
|
|
|
|
|
return session.setAccountData(integrationProvisioningDict, forType: Settings.integrationProvisioning, success: success, failure: failure)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-20 09:25:56 +00:00
|
|
|
// MARK: Allowed widgets
|
2019-11-20 17:41:18 +00:00
|
|
|
func permission(for widget: Widget) -> WidgetPermission {
|
2019-11-20 09:25:56 +00:00
|
|
|
guard let allowedWidgets = getAllowedWidgets() else {
|
|
|
|
return .undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
return allowedWidgets.widgets[widget.widgetEvent.eventId] == true ? .granted : .declined
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAllowedWidgets() -> RiotSettingAllowedWidgets? {
|
|
|
|
guard let allowedWidgetsDict = getAccountData(forEventType: Settings.allowedWidgets) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-21 16:19:39 +00:00
|
|
|
return try? serializationService.deserialize(allowedWidgetsDict)
|
2019-11-20 09:25:56 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 14:43:04 +00:00
|
|
|
@discardableResult
|
|
|
|
func setPermission(_ permission: WidgetPermission,
|
2019-11-20 17:41:18 +00:00
|
|
|
for widget: Widget,
|
|
|
|
success: @escaping () -> Void,
|
|
|
|
failure: @escaping (Error?) -> Void)
|
2019-11-20 09:25:56 +00:00
|
|
|
-> MXHTTPOperation? {
|
|
|
|
|
|
|
|
guard let widgetEventId = widget.widgetEvent.eventId else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var widgets = getAllowedWidgets()?.widgets ?? [:]
|
|
|
|
|
|
|
|
switch permission {
|
|
|
|
case .undefined:
|
|
|
|
widgets.removeValue(forKey: widgetEventId)
|
|
|
|
case .granted:
|
|
|
|
widgets[widgetEventId] = true
|
|
|
|
case .declined:
|
|
|
|
widgets[widgetEventId] = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update only the "widgets" field in the account data
|
|
|
|
var allowedWidgetsDict = getAccountData(forEventType: Settings.allowedWidgets) ?? [:]
|
|
|
|
allowedWidgetsDict[RiotSettingAllowedWidgets.CodingKeys.widgets.rawValue] = widgets
|
|
|
|
|
|
|
|
return session.setAccountData(allowedWidgetsDict, forType: Settings.allowedWidgets, success: success, failure: failure)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// MARK: - Private
|
|
|
|
private func getAccountData(forEventType eventType: String) -> [String: Any]? {
|
|
|
|
return session.accountData.accountData(forEventType: eventType) as? [String: Any]
|
|
|
|
}
|
|
|
|
}
|