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)
}
}
@@ -16,8 +16,11 @@
import SwiftUI
import Compound
import DesignKit
struct PlaceholderAvatarImage: View {
@Environment(\.redactionReasons) var redactionReasons
@Environment(\.redactionReasons) private var redactionReasons
private let textForImage: String
private let contentID: String?
@@ -185,6 +185,15 @@ extension NotificationItemProxyProtocol {
}
}
var icon: NotificationIcon {
if isDirect {
return NotificationIcon(mediaSource: senderAvatarMediaSource, groupInfo: nil)
} else {
return NotificationIcon(mediaSource: roomAvatarMediaSource,
groupInfo: .init(name: roomDisplayName, id: roomID))
}
}
/// Process the receiver item proxy
/// - Parameters:
/// - receiverId: identifier of the user that has received the notification
@@ -236,13 +245,10 @@ extension NotificationItemProxyProtocol {
notification.categoryIdentifier = NotificationConstants.Category.invite
let icon: NotificationIcon
let body: String
if !isDirect {
icon = NotificationIcon(mediaSource: roomAvatarMediaSource, groupName: roomDisplayName)
body = L10n.notificationRoomInviteBody
} else {
icon = NotificationIcon(mediaSource: senderAvatarMediaSource, groupName: nil)
body = L10n.notificationInviteBody
}
@@ -292,17 +298,9 @@ extension NotificationItemProxyProtocol {
}
notification.categoryIdentifier = NotificationConstants.Category.message
let senderName = senderDisplayName ?? roomDisplayName
let icon: NotificationIcon
if !isDirect {
icon = NotificationIcon(mediaSource: roomAvatarMediaSource, groupName: roomDisplayName)
} else {
icon = NotificationIcon(mediaSource: senderAvatarMediaSource, groupName: nil)
}
notification = try await notification.addSenderIcon(using: mediaProvider,
senderID: event.senderID,
senderName: senderName,
senderName: senderDisplayName ?? roomDisplayName,
icon: icon)
return notification
}