Commit 0fde5355 authored by Chaitanya Pandit's avatar Chaitanya Pandit
Browse files

Importing avatar images from contacts

parent c1b03a58
......@@ -121,6 +121,43 @@ struct AddressBookController {
if let email = contact.emailAddresses.first?.value as String? {
try? person.setPropertyValue(name: "email", value: .string(email), db: db)
}
if contact.imageDataAvailable ,
let avatar = contact.imageData {
saveAvatar(avatar, forPerson: person, db: db)
}
}
}
static func saveAvatar(_ data: Data, forPerson person: ItemRecord, db: Database) {
if let personRowId = person.rowId {
do {
let newImageItem = ItemRecord(type: "Photo")
try newImageItem.save(db: db)
guard let newImageId = newImageItem.rowId else {
return
}
_ = try EdgeRecord.createFor(source: personRowId, type: "profilePicture", target: newImageId, db: db)
let newFileItem = ItemRecord(type: "File")
try newFileItem.save(db: db)
guard let newFileItemId = newFileItem.rowId else {
return
}
let fileName = "\(UUID().uuidString).jpg"
try FileStorageController.writeData(data, toFileForUUID: fileName)
let sha256 = FileStorageController.getHashForFile(withUUID: fileName)
try newFileItem.setPropertyValue(name: "sha256", value: .string(sha256), db: db)
try newFileItem.setPropertyValue(name: "filename", value: .string(fileName), db: db)
_ = try EdgeRecord.createFor(source: newImageId, type: "file", target: newFileItemId, db: db)
} catch {
print("Error saving avatar : \(error.localizedDescription)")
}
}
}
......@@ -158,6 +195,8 @@ struct AddressBookController {
CNContactPhoneNumbersKey,
CNContactEmailAddressesKey,
CNContactThumbnailImageDataKey,
CNContactImageDataAvailableKey,
CNContactImageDataKey,
] as [Any]
var allContainers: [CNContainer] = []
......
......@@ -61,7 +61,7 @@ class EdgeRecord: BaseRecord {
}
let edge = EdgeRecord(item: rowId, source: source, type: type, target: target, syncState: syncState, syncHasPriority: syncHasPriority)
try edge.save()
try edge.save(db: db)
return edge
}
......@@ -70,9 +70,9 @@ class EdgeRecord: BaseRecord {
return existingEdge
}
static func createFor(item: Int64? = nil, source: Int64, type: String, target: Int64, syncState: SyncState = .create, syncHasPriority: Bool = false, db: Database? = nil) throws -> EdgeRecord {
try AppController.shared.databaseController.writeSync { db in
return try createFor(source: source, type: type, target: target, db: db)
static func createFor(item: Int64? = nil, source: Int64, type: String, target: Int64, syncState: SyncState = .create, syncHasPriority: Bool = false, dbController: DatabaseController = AppController.shared.databaseController) throws -> EdgeRecord {
try dbController.writeSync { db in
return try createFor(item: item, source: source, type: type, target: target, syncState: syncState, syncHasPriority: syncHasPriority, db: db)
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment