Placeholders added in push notifications (#1176)

* placeholders added in push notifications

* this code is a bit easier to read

* code improvement
This commit is contained in:
Mauro
2023-06-27 13:44:19 +02:00
committed by GitHub
parent 87cac534f5
commit 544d928287
6 changed files with 66 additions and 19 deletions
@@ -16,15 +16,21 @@
import Foundation
import Intents
import SwiftUI
import UserNotifications
struct NotificationIcon {
struct GroupInfo {
let name: String
let id: String
}
let mediaSource: MediaSourceProxy?
// Required as the key to set images for groups
let groupName: String?
let groupInfo: GroupInfo?
var shouldDisplayAsGroup: Bool {
groupName != nil
groupInfo != nil
}
}
@@ -99,11 +105,12 @@ extension UNMutableNotificationContent {
senderID: String,
senderName: String,
icon: NotificationIcon) async throws -> UNMutableNotificationContent {
var image = INImage(named: "")
var fetchedImage: INImage?
let image: INImage
if let mediaSource = icon.mediaSource {
switch await mediaProvider?.loadImageDataFromSource(mediaSource) {
case .success(let data):
image = INImage(imageData: data)
fetchedImage = INImage(imageData: data)
case .failure(let error):
MXLog.error("Couldn't add sender icon: \(error)")
case .none:
@@ -111,6 +118,15 @@ extension UNMutableNotificationContent {
}
}
if let fetchedImage {
image = fetchedImage
} else if let data = await getPlaceholderAvatarImageData(name: icon.groupInfo?.name ?? senderName,
id: icon.groupInfo?.name ?? senderName) {
image = INImage(imageData: data)
} else {
image = INImage(named: "")
}
let senderHandle = INPersonHandle(value: senderID, type: .unknown)
let sender = INPerson(personHandle: senderHandle,
nameComponents: nil,
@@ -122,10 +138,10 @@ extension UNMutableNotificationContent {
// These are required to show the group name as subtitle
var speakableGroupName: INSpeakableString?
var recipients: [INPerson]?
if let groupName = icon.groupName {
if let groupInfo = icon.groupInfo {
let meHandle = INPersonHandle(value: receiverID, type: .unknown)
let me = INPerson(personHandle: meHandle, nameComponents: nil, displayName: nil, image: nil, contactIdentifier: nil, customIdentifier: nil, isMe: true)
speakableGroupName = INSpeakableString(spokenPhrase: groupName)
speakableGroupName = INSpeakableString(spokenPhrase: groupInfo.name)
recipients = [sender, me]
}
@@ -157,4 +173,13 @@ extension UNMutableNotificationContent {
// swiftlint:disable:next force_cast
return updatedContent.mutableCopy() as! UNMutableNotificationContent
}
private func getPlaceholderAvatarImageData(name: String, id: String) async -> Data? {
let image = PlaceholderAvatarImage(name: name,
contentID: id)
.clipShape(Circle())
.frame(width: 100, height: 100)
let renderer = await ImageRenderer(content: image)
return await renderer.uiImage?.jpegData(compressionQuality: 0.8)
}
}