-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'release/0.24.0/master'
- Loading branch information
Showing
87 changed files
with
3,024 additions
and
1,158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
MatrixSDK/ClientInformation/MXClientInformationService.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// | ||
// Copyright 2022 The Matrix.org Foundation C.I.C | ||
// | ||
// 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 | ||
|
||
@objcMembers | ||
public class MXClientInformationService: NSObject { | ||
|
||
private weak var session: MXSession? | ||
private let bundle: Bundle | ||
|
||
public init(withSession session: MXSession, | ||
bundle: Bundle) { | ||
self.session = session | ||
self.bundle = bundle | ||
} | ||
|
||
public func updateData() { | ||
guard let session = session else { | ||
return | ||
} | ||
|
||
guard MXSDKOptions.sharedInstance().enableNewClientInformationFeature else { | ||
return removeDataIfNeeded(on: session) | ||
} | ||
|
||
guard let updatedInfo = createClientInformation() else { | ||
return | ||
} | ||
|
||
let type = accountDataType(for: session) | ||
let currentInfo = session.accountData.accountData(forEventType: type) | ||
|
||
guard !NSDictionary(dictionary: updatedInfo).isEqual(to: currentInfo ?? [:]) else { | ||
MXLog.debug("[MXClientInformationService] refresh: no need to update") | ||
return | ||
} | ||
|
||
session.setAccountData(updatedInfo, forType: type) { | ||
MXLog.debug("[MXClientInformationService] refresh: updated successfully") | ||
} failure: { error in | ||
MXLog.debug("[MXClientInformationService] refresh: update failed: \(String(describing: error))") | ||
} | ||
} | ||
|
||
private func removeDataIfNeeded(on session: MXSession) { | ||
let type = accountDataType(for: session) | ||
|
||
guard let currentInfo = session.accountData.accountData(forEventType: type), | ||
!currentInfo.isEmpty else { | ||
// not exists, no need to do anything | ||
MXLog.debug("[MXClientInformationService] removeDataIfNeeded: no need to remove") | ||
return | ||
} | ||
|
||
session.setAccountData([:], forType: type) { | ||
MXLog.debug("[MXClientInformationService] removeDataIfNeeded: removed successfully") | ||
} failure: { error in | ||
MXLog.debug("[MXClientInformationService] removeDataIfNeeded: remove failed: \(String(describing: error))") | ||
} | ||
} | ||
|
||
private func createClientInformation() -> [AnyHashable: String]? { | ||
guard let name = bundle.object(forInfoDictionaryKey: "CFBundleDisplayName") | ||
?? bundle.object(forInfoDictionaryKey: "CFBundleName") as? String, | ||
let version = bundle.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String else { | ||
return nil | ||
} | ||
|
||
return [ | ||
"name": "\(name) iOS", | ||
"version": version | ||
] | ||
} | ||
|
||
private func accountDataType(for session: MXSession) -> String { | ||
guard let deviceId = session.myDeviceId else { | ||
fatalError("[MXClientInformationService] No device id") | ||
} | ||
return "\(kMXAccountDataTypeClientInformation).\(deviceId)" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.