-
Notifications
You must be signed in to change notification settings - Fork 216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Location sharing: Persist beacon info summaries to disk (PSF-1036) #1474
Merged
Merged
Changes from 19 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
c7b873a
Podfile: Update for Swift compatibility and update Realm version.
SBiOSoftWhare 40c4ed0
Podspec: Update Realm version to 10.26.0
SBiOSoftWhare d723c4b
Realm: Add Swift support file.
SBiOSoftWhare d7eaf7d
Realm: Add convenient Swift extension for transactions.
SBiOSoftWhare 529b95b
MXBeaconAggregationsTests: Fix multiple fulfill issue.
SBiOSoftWhare 91865cb
Update podfile.lock
SBiOSoftWhare e2420e9
MXBeaconInfo: Expose full init.
SBiOSoftWhare 95f51da
MXBeaconInfoSummaryStoreProtocol: Add a method to get all summaries f…
SBiOSoftWhare 92489e4
MXBeaconInfoSummaryMemoryStore: Update with new MXBeaconInfoSummarySt…
SBiOSoftWhare db0933f
Add equivalent Realm objects to represent MXBeaconInfoSummary in data…
SBiOSoftWhare 3b004fd
MXBeaconAggregations: Improve beacon info summary updates in the stor…
SBiOSoftWhare bd1f56e
Add MXRealmBeaconMapper to convert convert `MXBeaconInfoSummary` to `…
SBiOSoftWhare a214418
Implement MXBeaconInfoSummaryRealmStore
SBiOSoftWhare 9e29006
MXLocationService: Add method to get all beacon info summaries from a…
SBiOSoftWhare 4994851
MXAggregations: Use MXBeaconInfoSummaryRealmStore.
SBiOSoftWhare 4abbb2f
Update pbxproj
SBiOSoftWhare b0b402e
Update changes
SBiOSoftWhare 7d25cb5
Realm+MatrixSDK: Remove wrong import.
SBiOSoftWhare 51403ab
Move extensions to the same folder.
SBiOSoftWhare a296c4b
MXBeaconInfoSummaryRealmStore: Use shared container if possible to st…
SBiOSoftWhare File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
270 changes: 270 additions & 0 deletions
270
MatrixSDK/Aggregations/LocationSharing/Store/Realm/MXBeaconInfoSummaryRealmStore.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,270 @@ | ||
// | ||
// 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 | ||
import Realm | ||
|
||
@objcMembers | ||
public class MXBeaconInfoSummaryRealmStore: NSObject { | ||
|
||
// MARK: - Constants | ||
|
||
private enum Database { | ||
static let folderName = "BeaconInfoSummaries" | ||
static let filename = "BeaconInfoSummaries" | ||
|
||
// TODO: Use a private modulemap to import private Objective-C files in Swift. To be able to use `MXRealmHelper` here. | ||
static let fileExtension = "realm" | ||
} | ||
|
||
private enum BeaconInfoSummaryPredicate { | ||
|
||
static func identifier(_ identifier: String) -> NSPredicate { | ||
return NSPredicate(format: "%K = %@", | ||
#keyPath(MXRealmBeaconInfoSummary.identifier), identifier) | ||
} | ||
|
||
static func room(_ roomId: String) -> NSPredicate { | ||
return NSPredicate(format: "%K = %@", | ||
#keyPath(MXRealmBeaconInfoSummary.roomId), roomId) | ||
} | ||
|
||
static func user(_ userId: String) -> NSPredicate { | ||
return NSPredicate(format: "%K = %@", | ||
#keyPath(MXRealmBeaconInfoSummary.userId), userId) | ||
} | ||
} | ||
|
||
// MARK: - Properties | ||
|
||
private unowned let session: MXSession | ||
private let mapper: MXRealmBeaconMapper | ||
|
||
private var realm: RLMRealm? { | ||
guard let userId = self.session.myUserId else { | ||
return nil | ||
} | ||
return self.realmStore(for: userId) | ||
} | ||
|
||
// MARK: - Setup | ||
|
||
public init(session: MXSession) { | ||
self.session = session | ||
self.mapper = MXRealmBeaconMapper(session: session) | ||
|
||
super.init() | ||
} | ||
|
||
// MARK: - Private | ||
|
||
private func realmStore(for userId: String) -> RLMRealm? { | ||
do { | ||
let configuration = try self.realmConfiguration(for: userId) | ||
|
||
let realm = try RLMRealm(configuration: configuration) | ||
|
||
return realm | ||
} catch { | ||
|
||
MXLog.error("[MXBeaconInfoSummaryRealmStore] failed to create Realm store with error: \(error)") | ||
return nil | ||
} | ||
} | ||
|
||
private func realmConfiguration(for userId: String) throws -> RLMRealmConfiguration { | ||
let realmConfiguration = RLMRealmConfiguration.default() | ||
|
||
let rootDirectoryURL: URL | ||
|
||
do { | ||
rootDirectoryURL = try FileManager.default.url(for: .cachesDirectory, in: .userDomainMask, appropriateFor: nil, create: true).appendingPathComponent(userId) | ||
} catch { | ||
MXLog.error("[MXBeaconInfoSummaryRealmStore] Fail to get user directory") | ||
|
||
throw error | ||
} | ||
|
||
let realmFileFolderURL = rootDirectoryURL.appendingPathComponent(Database.folderName, isDirectory: true) | ||
let realmFileURL = realmFileFolderURL.appendingPathComponent(Database.filename, isDirectory: false).appendingPathExtension(Database.fileExtension) | ||
|
||
do { | ||
try FileManager.default.createDirectory(at: realmFileFolderURL, withIntermediateDirectories: true, attributes: nil) | ||
} catch { | ||
MXLog.error("[MXBeaconInfoSummaryRealmStore] Fail to create Realm folder \(realmFileFolderURL) with error: \(error)") | ||
throw error | ||
} | ||
|
||
realmConfiguration.fileURL = realmFileURL | ||
realmConfiguration.deleteRealmIfMigrationNeeded = true | ||
|
||
// Manage only our objects in this realm | ||
realmConfiguration.objectClasses = [ | ||
MXRealmBeaconInfoSummary.self, | ||
MXRealmBeaconInfo.self, | ||
MXRealmBeacon.self | ||
] | ||
|
||
return realmConfiguration | ||
} | ||
|
||
private func realmBeaconInfoSummaryResults(in realm: RLMRealm, with roomId: String) -> RLMResults<MXRealmBeaconInfoSummary> { | ||
let roomPredicate = BeaconInfoSummaryPredicate.room(roomId) | ||
return self.realmBeaconInfoSummaryResults(in: realm, with: roomPredicate) | ||
} | ||
|
||
private func realmBeaconInfoSummaryResults(in realm: RLMRealm, with predicate: NSPredicate) -> RLMResults<MXRealmBeaconInfoSummary> { | ||
|
||
guard let realmSummaries = MXRealmBeaconInfoSummary.objects(in: realm, with: predicate) as? RLMResults<MXRealmBeaconInfoSummary> else { | ||
fatalError() | ||
} | ||
return realmSummaries | ||
} | ||
|
||
private func beaconInfoSummaries(from realmBeaconInfoSummaryResults: RLMResults<MXRealmBeaconInfoSummary>) -> [MXBeaconInfoSummary] { | ||
|
||
var summaries: [MXBeaconInfoSummary] = [] | ||
|
||
for realmSummary in realmBeaconInfoSummaryResults { | ||
|
||
if let realmBeaconInfoSummary = realmSummary as? MXRealmBeaconInfoSummary, let summary = self.mapper.beaconInfoSummary(from: realmBeaconInfoSummary) { | ||
summaries.append(summary) | ||
} | ||
} | ||
|
||
return summaries | ||
} | ||
} | ||
|
||
// MARK: - MXBeaconInfoSummaryStoreProtocol | ||
extension MXBeaconInfoSummaryRealmStore: MXBeaconInfoSummaryStoreProtocol { | ||
|
||
public func addOrUpdateBeaconInfoSummary(_ beaconInfoSummary: MXBeaconInfoSummary, inRoomWithId roomId: String) { | ||
|
||
guard let realm = self.realm else { | ||
return | ||
} | ||
|
||
do { | ||
try realm.mx_transaction(name: "[MXBeaconInfoSummaryRealmStore] addOrUpdateBeaconInfoSummary") { | ||
let realmBeaconInfoSummary = self.mapper.realmBeaconInfoSummary(from: beaconInfoSummary) | ||
realm.addOrUpdate(realmBeaconInfoSummary) | ||
} | ||
} catch { | ||
MXLog.error("[MXBeaconInfoSummaryRealmStore] addOrUpdateBeaconInfoSummary failed with error: \(error)") | ||
} | ||
} | ||
|
||
public func getBeaconInfoSummary(withIdentifier identifier: String, inRoomWithId roomId: String) -> MXBeaconInfoSummary? { | ||
|
||
guard let realm = self.realm else { | ||
return nil | ||
} | ||
|
||
let identifierPredicate = BeaconInfoSummaryPredicate.identifier(identifier) | ||
let roomPredicate = BeaconInfoSummaryPredicate.room(roomId) | ||
|
||
let predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [identifierPredicate, roomPredicate]) | ||
|
||
guard let realmBeaconInfoSummary = self.realmBeaconInfoSummaryResults(in: realm, with: predicate).firstObject() else { | ||
return nil | ||
} | ||
|
||
return self.mapper.beaconInfoSummary(from: realmBeaconInfoSummary) | ||
} | ||
|
||
public func getBeaconInfoSummary(withUserId userId: String, description: String?, timeout: UInt64, timestamp: UInt64, inRoomWithId roomId: String) -> MXBeaconInfoSummary? { | ||
|
||
guard let realm = self.realm else { | ||
return nil | ||
} | ||
|
||
let roomPredicate = BeaconInfoSummaryPredicate.room(roomId) | ||
|
||
let userPredicate = BeaconInfoSummaryPredicate.user(userId) | ||
|
||
let timeoutPredicate = NSPredicate(format: "%K = %ld", | ||
#keyPath(MXRealmBeaconInfoSummary.beaconInfo.timeout), Int(timeout)) | ||
|
||
let timestampPredicate = NSPredicate(format: "%K = %ld", #keyPath(MXRealmBeaconInfoSummary.beaconInfo.timestamp), Int(timestamp)) | ||
|
||
let predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [roomPredicate, userPredicate, timeoutPredicate, timestampPredicate]) | ||
|
||
let summaries = self.realmBeaconInfoSummaryResults(in: realm, with: predicate) | ||
|
||
|
||
guard let realmBeaconInfoSummary = summaries.firstObject() else { | ||
return nil | ||
} | ||
|
||
return self.mapper.beaconInfoSummary(from: realmBeaconInfoSummary) | ||
} | ||
|
||
public func getAllBeaconInfoSummaries(forUserId userId: String) -> [MXBeaconInfoSummary] { | ||
|
||
guard let realm = self.realm else { | ||
return [] | ||
} | ||
|
||
let userPredicate = BeaconInfoSummaryPredicate.user(userId) | ||
|
||
let realmSummaries = self.realmBeaconInfoSummaryResults(in: realm, with: userPredicate) | ||
|
||
return self.beaconInfoSummaries(from: realmSummaries) | ||
} | ||
|
||
public func getAllBeaconInfoSummaries(inRoomWithId roomId: String) -> [MXBeaconInfoSummary] { | ||
|
||
guard let realm = self.realm else { | ||
return [] | ||
} | ||
|
||
let realmSummaries = self.realmBeaconInfoSummaryResults(in: realm, with: roomId) | ||
|
||
return self.beaconInfoSummaries(from: realmSummaries) | ||
} | ||
|
||
public func deleteAllBeaconInfoSummaries(inRoomWithId roomId: String) { | ||
|
||
guard let realm = self.realm else { | ||
return | ||
} | ||
|
||
do { | ||
try realm.mx_transaction(name: "[MXBeaconInfoSummaryRealmStore] deleteAllBeaconInfoSummaries inRoomWithId") { | ||
let realmBeaconInfoSummaries = self.realmBeaconInfoSummaryResults(in: realm, with: roomId) | ||
realm.deleteObjects(realmBeaconInfoSummaries) | ||
} | ||
} catch { | ||
MXLog.error("[MXBeaconInfoSummaryRealmStore] deleteAllBeaconInfoSummaries failed with error: \(error)") | ||
} | ||
} | ||
|
||
public func deleteAllBeaconInfoSummaries() { | ||
|
||
guard let realm = self.realm else { | ||
return | ||
} | ||
|
||
do { | ||
try realm.mx_transaction(name: "[MXBeaconInfoSummaryRealmStore] deleteAllBeaconInfoSummaries") { | ||
realm.deleteAllObjects() | ||
} | ||
} catch { | ||
MXLog.error("[MXBeaconInfoSummaryRealmStore] Failed to delete all objects: \(error)") | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need to use shared container here?