session-ios/SessionMessagingKit/Database/Storage+Contacts.swift

34 lines
1.2 KiB
Swift
Raw Normal View History

2020-12-17 06:32:39 +01:00
extension Storage {
private static let contactCollection = "LokiContactCollection"
@objc(getContactWithSessionID:)
2020-12-17 06:32:39 +01:00
public func getContact(with sessionID: String) -> Contact? {
var result: Contact?
Storage.read { transaction in
result = transaction.object(forKey: sessionID, inCollection: Storage.contactCollection) as? Contact
}
return result
}
@objc(setContact:usingTransaction:)
2020-12-17 06:32:39 +01:00
public func setContact(_ contact: Contact, using transaction: Any) {
(transaction as! YapDatabaseReadWriteTransaction).setObject(contact, forKey: contact.sessionID, inCollection: Storage.contactCollection)
2021-03-01 03:15:54 +01:00
DispatchQueue.main.async {
NotificationCenter.default.post(name: .contactUpdated, object: contact.sessionID)
}
2020-12-17 06:32:39 +01:00
}
2020-12-17 23:47:18 +01:00
public func getAllContacts() -> Set<Contact> {
var result: Set<Contact> = []
Storage.read { transaction in
transaction.enumerateRows(inCollection: Storage.contactCollection) { _, object, _, _ in
guard let contact = object as? Contact else { return }
result.insert(contact)
}
}
return result
}
2020-12-17 06:32:39 +01:00
}